Skip to content

Fetch targets by HASH.FILENAME.EXT #233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,16 @@ where
target: &'a TargetPath,
) -> Result<Box<dyn AsyncRead + Send + Unpin>> {
let target_description = self.fetch_target_description(target).await?;
self.remote.fetch_target(target, &target_description).await

// According to TUF section 5.5.2, when consistent snapshot is enabled, target files should
// be found at `$HASH.FILENAME.EXT`. Otherwise it is stored at `FILENAME.EXT`.
if self.tuf.root().consistent_snapshot() {
let (_, value) = crypto::hash_preference(target_description.hashes())?;
let target = target.with_hash_prefix(value)?;
self.remote.fetch_target(&target, &target_description).await
} else {
self.remote.fetch_target(target, &target_description).await
}
}

async fn lookup_target_description<'a>(
Expand Down
12 changes: 12 additions & 0 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,18 @@ impl TargetPath {
pub fn value(&self) -> &str {
&self.0
}

/// Prefix the target path with a hash value to support TUF spec 5.5.2.
pub fn with_hash_prefix(&self, hash: &HashValue) -> Result<TargetPath> {
let mut components = self.components();

// The unwrap here is safe because we checked in `safe_path` that the path is not empty.
let file_name = components.pop().unwrap();

components.push(format!("{}.{}", hash, file_name));

TargetPath::new(components.join("/"))
}
}

/// Description of a target, used in verification.
Expand Down
25 changes: 17 additions & 8 deletions tests/simple_example.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use futures_executor::block_on;
use tuf::client::{Client, Config, PathTranslator};
use tuf::crypto::{HashAlgorithm, KeyId, PrivateKey, SignatureScheme};
use tuf::crypto::{self, HashAlgorithm, KeyId, PrivateKey, SignatureScheme};
use tuf::interchange::Json;
use tuf::metadata::{
MetadataPath, MetadataVersion, RootMetadataBuilder, SnapshotMetadataBuilder, TargetPath,
TargetsMetadataBuilder, TimestampMetadataBuilder, VirtualTargetPath,
MetadataPath, MetadataVersion, RootMetadataBuilder, SnapshotMetadataBuilder, TargetDescription,
TargetPath, TargetsMetadataBuilder, TimestampMetadataBuilder, VirtualTargetPath,
};
use tuf::repository::{EphemeralRepository, Repository};
use tuf::Result;
Expand Down Expand Up @@ -131,16 +131,25 @@ where
//// build the targets ////

let target_file: &[u8] = b"things fade, alternatives exclude";
let target_description = TargetDescription::from_reader(target_file, &[HashAlgorithm::Sha256])?;

let target_path = TargetPath::new("foo-bar".into())?;
let _ = remote.store_target(target_file, &target_path).await;

// According to TUF section 5.5.2, when consistent snapshot is enabled, target files should be
// stored at `$HASH.FILENAME.EXT`. Otherwise it is stored at `FILENAME.EXT`.
if consistent_snapshot {
let (_, value) = crypto::hash_preference(target_description.hashes())?;
let hash_prefixed_path = target_path.with_hash_prefix(&value)?;
let _ = remote.store_target(target_file, &hash_prefixed_path).await;
} else {
let _ = remote.store_target(target_file, &target_path).await;
};

let targets = TargetsMetadataBuilder::new()
.insert_target_from_reader(
.insert_target_description(
config.path_translator().real_to_virtual(&target_path)?,
target_file,
&[HashAlgorithm::Sha256],
)?
target_description,
)
.signed::<Json>(&targets_key)?;

let targets_path = &MetadataPath::new("targets")?;
Expand Down