Skip to content

Commit

Permalink
Way to disable hash prefix for consistent_snapshot
Browse files Browse the repository at this point in the history
Currently, if the repository is consistent_snapshot,
Updater will prefix the target filename with the hash
when constructing the download URL.
For some adopters of TUF (like Warehouse) this is not wanted
(warehouse target file paths are "consistent",
even if the filenames are not).

For example, Warehouse doesn't follow what tuf
(the reference implementation and specification) advice for naming
consistent filenames, which is to prefix the filename with the hash
of the files contents.
However, the target filenames it does use are consistent,
only the hash is part of the target's file path
not the target's file name.

Signed-off-by: Martin Vrachev <[email protected]>
  • Loading branch information
MVrachev authored and mnm678 committed Sep 8, 2020
1 parent f54bbf5 commit 2673d31
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions tuf/client/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -1314,7 +1314,8 @@ def _soft_check_file_length(self, file_object, trusted_file_length):



def _get_target_file(self, target_filepath, file_length, file_hashes):
def _get_target_file(self, target_filepath, file_length, file_hashes,
prefix_filename_with_hash):
"""
<Purpose>
Non-public method that safely (i.e., the file length and hash are
Expand All @@ -1333,6 +1334,13 @@ def _get_target_file(self, target_filepath, file_length, file_hashes):
file_hashes:
The expected hashes of the target file.
prefix_filename_with_hash:
Whether to prefix the targets file names with their hash when using
consistent snapshot.
This should be set to False when the served target filenames are not
prefixed with hashes (in this case the server uses other means
to ensure snapshot consistency).
<Exceptions>
tuf.exceptions.NoWorkingMirrorError:
The target could not be fetched. This is raised only when all known
Expand All @@ -1356,7 +1364,7 @@ def verify_target_file(target_file_object):
self._hard_check_file_length(target_file_object, file_length)
self._check_hashes(target_file_object, file_hashes)

if self.consistent_snapshot:
if self.consistent_snapshot and prefix_filename_with_hash:
# Note: values() does not return a list in Python 3. Use list()
# on values() for Python 2+3 compatibility.
target_digest = list(file_hashes.values()).pop()
Expand Down Expand Up @@ -3216,7 +3224,8 @@ def updated_targets(self, targets, destination_directory):



def download_target(self, target, destination_directory):
def download_target(self, target, destination_directory,
prefix_filename_with_hash=True):
"""
<Purpose>
Download 'target' and verify it is trusted.
Expand All @@ -3233,6 +3242,14 @@ def download_target(self, target, destination_directory):
destination_directory:
The directory to save the downloaded target file.
prefix_filename_with_hash:
Whether to prefix the targets file names with their hash when using
consistent snapshot.
This should be set to False when the served target filenames are not
prefixed with hashes (in this case the server uses other means
to ensure snapshot consistency).
Default is True.
<Exceptions>
securesystemslib.exceptions.FormatError:
If 'target' is not properly formatted.
Expand Down Expand Up @@ -3267,7 +3284,7 @@ def download_target(self, target, destination_directory):
# '_get_target_file()' checks every mirror and returns the first target
# that passes verification.
target_file_object = self._get_target_file(target_filepath, trusted_length,
trusted_hashes)
trusted_hashes, prefix_filename_with_hash)

# We acquired a target file object from a mirror. Move the file into place
# (i.e., locally to 'destination_directory'). Note: join() discards
Expand Down

0 comments on commit 2673d31

Please sign in to comment.