Skip to content

Commit c4617ff

Browse files
authored
Merge pull request #1102 from MVrachev/1080
Add a way to disable hash prefix when using consistent_snapshot
2 parents 11a743c + 060d41e commit c4617ff

2 files changed

Lines changed: 54 additions & 4 deletions

File tree

tests/test_updater.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,39 @@ def test_6_download_target(self):
12911291
self.repository_updater.download_target(targetinfo2,
12921292
destination_directory)
12931293

1294+
# Checks if the file has been successfully downloaded
1295+
download_filepath = os.path.join(destination_directory, target_filepath2)
1296+
self.assertTrue(os.path.exists(download_filepath))
1297+
1298+
# Removes the file so that it can be downloaded again in the next test
1299+
os.remove(download_filepath)
1300+
1301+
# Test downloading with consistent snapshot enabled, but without adding
1302+
# the hash of the file as a prefix to its name.
1303+
1304+
file1_path = targetinfo2['filepath']
1305+
file1_hashes = securesystemslib.util.get_file_hashes(
1306+
os.path.join(self.repository_directory, 'targets', file1_path),
1307+
hash_algorithms=['sha256', 'sha512'])
1308+
1309+
# Currently in the repository directory, those three files exists:
1310+
# "file1.txt", "<sha256_hash>.file1.txt" and "<sha512_hash>.file1.txt"
1311+
# where both sha256 and sha512 hashes are for file file1.txt.
1312+
# Remove the files with the hash digest prefix to ensure that
1313+
# the served target file is not prefixed.
1314+
os.remove(os.path.join(self.repository_directory, 'targets',
1315+
file1_hashes['sha256'] + '.' + file1_path))
1316+
os.remove(os.path.join(self.repository_directory, 'targets',
1317+
file1_hashes['sha512'] + '.' + file1_path))
1318+
1319+
1320+
self.repository_updater.download_target(targetinfo2,
1321+
destination_directory,
1322+
prefix_filename_with_hash=False)
1323+
1324+
# Checks if the file has been successfully downloaded
1325+
self.assertTrue(os.path.exists(download_filepath))
1326+
12941327
# Test for a destination that cannot be written to (apart from a target
12951328
# file that already exists at the destination) and which raises an
12961329
# exception.

tuf/client/updater.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,8 @@ def _soft_check_file_length(self, file_object, trusted_file_length):
13151315

13161316

13171317

1318-
def _get_target_file(self, target_filepath, file_length, file_hashes):
1318+
def _get_target_file(self, target_filepath, file_length, file_hashes,
1319+
prefix_filename_with_hash):
13191320
"""
13201321
<Purpose>
13211322
Non-public method that safely (i.e., the file length and hash are
@@ -1334,6 +1335,13 @@ def _get_target_file(self, target_filepath, file_length, file_hashes):
13341335
file_hashes:
13351336
The expected hashes of the target file.
13361337
1338+
prefix_filename_with_hash:
1339+
Whether to prefix the targets file names with their hash when using
1340+
consistent snapshot.
1341+
This should be set to False when the served target filenames are not
1342+
prefixed with hashes (in this case the server uses other means
1343+
to ensure snapshot consistency).
1344+
13371345
<Exceptions>
13381346
tuf.exceptions.NoWorkingMirrorError:
13391347
The target could not be fetched. This is raised only when all known
@@ -1357,7 +1365,7 @@ def verify_target_file(target_file_object):
13571365
self._hard_check_file_length(target_file_object, file_length)
13581366
self._check_hashes(target_file_object, file_hashes)
13591367

1360-
if self.consistent_snapshot:
1368+
if self.consistent_snapshot and prefix_filename_with_hash:
13611369
# Note: values() does not return a list in Python 3. Use list()
13621370
# on values() for Python 2+3 compatibility.
13631371
target_digest = list(file_hashes.values()).pop()
@@ -3217,7 +3225,8 @@ def updated_targets(self, targets, destination_directory):
32173225

32183226

32193227

3220-
def download_target(self, target, destination_directory):
3228+
def download_target(self, target, destination_directory,
3229+
prefix_filename_with_hash=True):
32213230
"""
32223231
<Purpose>
32233232
Download 'target' and verify it is trusted.
@@ -3234,6 +3243,14 @@ def download_target(self, target, destination_directory):
32343243
destination_directory:
32353244
The directory to save the downloaded target file.
32363245
3246+
prefix_filename_with_hash:
3247+
Whether to prefix the targets file names with their hash when using
3248+
consistent snapshot.
3249+
This should be set to False when the served target filenames are not
3250+
prefixed with hashes (in this case the server uses other means
3251+
to ensure snapshot consistency).
3252+
Default is True.
3253+
32373254
<Exceptions>
32383255
securesystemslib.exceptions.FormatError:
32393256
If 'target' is not properly formatted.
@@ -3268,7 +3285,7 @@ def download_target(self, target, destination_directory):
32683285
# '_get_target_file()' checks every mirror and returns the first target
32693286
# that passes verification.
32703287
target_file_object = self._get_target_file(target_filepath, trusted_length,
3271-
trusted_hashes)
3288+
trusted_hashes, prefix_filename_with_hash)
32723289

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

0 commit comments

Comments
 (0)