Skip to content
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

Fetch targets by HASH.FILENAME.EXT #233

Merged
merged 1 commit into from
Dec 2, 2019

Conversation

erickt
Copy link
Collaborator

@erickt erickt commented Nov 21, 2019

In section 5.5.2 of the TUF Spec, it states that if consistent snapshots is enabled, targets are downloaded by fetching HASH.FILENAME.EXT.

Note that this is layered on top of #231, so I recommend reviewing that first, then reviewing the second commit.

Closes #225

@erickt erickt requested a review from heartsucker November 21, 2019 04:47
@heartsucker
Copy link
Contributor

Rebase to make review easier?

@erickt
Copy link
Collaborator Author

erickt commented Nov 21, 2019

Rebased! Thanks for merging the other CLs.

Comment on lines 184 to 215
// FIXME(232): This is a helper function that, if `consistent_snapshot` is true, it prefixes target
// paths with their file hash. This is unfortunately necessary because we don't have a high level
// interface for creating TUF metadata.
async fn store_target(
remote: &EphemeralRepository<Json>,
target_file: &[u8],
target_path: &TargetPath,
consistent_snapshot: bool,
) -> Result<()> {
if consistent_snapshot {
let target_path = hash_prefix_target_path(target_file, target_path)?;
remote.store_target(target_file, &target_path).await
} else {
remote.store_target(target_file, target_path).await
}
}

fn hash_prefix_target_path(target_file: &[u8], target: &TargetPath) -> Result<TargetPath> {
let target_description = TargetDescription::from_reader(target_file, &[HashAlgorithm::Sha256])?;
let (_, value) = crypto::hash_preference(target_description.hashes())?;

let mut components = target.components();
let file_name = match components.pop() {
Some(file_name) => file_name,
None => {
return Err(Error::IllegalArgument("Path cannot be empty".into()));
}
};
components.push(format!("{}.{}", value, file_name));

TargetPath::new(components.join("/"))
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than do all this, could you just hardcode the path, since it's only used once? That'd probably make the test easier to understand, too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cleaned this up, like it better?

src/client.rs Outdated
};
components.push(format!("{}.{}", value, file_name));

let target = TargetPath::new(components.join("/"))?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't really feel like this is the right place to be mangling the TargetPath. It seems like this translation is supposed to happen when you translate a VirtualTargetPath into a TargetPath? Does that not work for some reason?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, I factored this out a wee bit to make it a little more readable. Does it read better?

Regarding VirtualTargetPath, this is an area where rust-tuf is being a little inconsistent. The metadata is constructed with VirtualTargetPath, but Repository uses TargetPath. I actually think this is the wrong way to go about, the notion of mapping a target path to a real path should really only be done in FileSystemRepository, but clients really should just be using / separators everywhere (see #236 and theupdateframework/specification#63). I'd like to clean this up if / when we implement #236.

In section 5.5.2 of the [TUF Spec], it states that if consistent snapshots
is enabled, targets are downloaded by fetching HASH.FILENAME.EXT.

Closes theupdateframework#225

[TUF Spec]: https://github.com/theupdateframework/specification/blob/master/tuf-spec.md#5-detailed-workflows
@alpearce
Copy link
Contributor

I took a review pass in ComputerDruid's absence; LGTM.

@erickt erickt merged commit 4cc2a6b into theupdateframework:develop Dec 2, 2019
@erickt erickt deleted the hash-targets branch December 2, 2019 23:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fetch targets by HASH.FILENAME.EXT
4 participants