Skip to content

Commit dbd8242

Browse files
committed
Fix snapshot_filename inconsistency usage
First in the generate_timestamp_metadata both "snapshot_filename" and the constant SNAPSHOT_FILENAME are used which is redundant and possibly confusing. There should be only one input for the snapshot file name. Second, when calling the generate_timestamp_metadata there are cases when "snapshot_filename" is in reality "snapshot_file_path". That's what led to the need for the addition of SNAPSHOT_FILENAME when populating the "meta" field from the TIMESTAMP_SCHEMA. For the same reason, it seems logical to me to rename snapshot_filename to snapshot_file_path and explicitly take the snapshot file name from it. Third, in the _generate_and_write_metadata function the argument "filenames" is by default None, but at the same time without check it's considered that filenames is a dictionary which has a key "snapshot". This is could be okay if the default "filenames" value was not None, but in the current situation it's easy to call "_generate_and_write_metadata" with rolename = timestamp and forget to populate the filenames dictionary. Signed-off-by: Martin Vrachev <[email protected]>
1 parent 1b80390 commit dbd8242

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

tests/test_repository_lib.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -603,8 +603,8 @@ def _setup_generate_timestamp_metadata_test(self):
603603
repo_lib.METADATA_STAGED_DIRECTORY_NAME)
604604
targets_directory = os.path.join(repository_directory, repo_lib.TARGETS_DIRECTORY_NAME)
605605

606-
snapshot_filename = os.path.join(metadata_directory,
607-
repo_lib.SNAPSHOT_FILENAME)
606+
snapshot_file_path = os.path.join(metadata_directory,
607+
repo_lib.SNAPSHOT_FILENAME)
608608

609609
# Set valid generate_timestamp_metadata() arguments.
610610
version = 1
@@ -619,15 +619,15 @@ def _setup_generate_timestamp_metadata_test(self):
619619
repository_junk = repo_tool.load_repository(repository_directory,
620620
repository_name)
621621

622-
return snapshot_filename, version, expiration_date, storage_backend, \
622+
return snapshot_file_path, version, expiration_date, storage_backend, \
623623
repository_name
624624

625625

626626
def test_generate_timestamp_metadata(self):
627-
snapshot_filename, version, expiration_date, storage_backend, \
627+
snapshot_file_path, version, expiration_date, storage_backend, \
628628
repository_name = self._setup_generate_timestamp_metadata_test()
629629

630-
timestamp_metadata = repo_lib.generate_timestamp_metadata(snapshot_filename,
630+
timestamp_metadata = repo_lib.generate_timestamp_metadata(snapshot_file_path,
631631
version, expiration_date, storage_backend, repository_name)
632632
self.assertTrue(tuf.formats.TIMESTAMP_SCHEMA.matches(timestamp_metadata))
633633

@@ -637,19 +637,19 @@ def test_generate_timestamp_metadata(self):
637637
repo_lib.generate_timestamp_metadata, 3, version, expiration_date,
638638
storage_backend, repository_name)
639639
self.assertRaises(securesystemslib.exceptions.FormatError,
640-
repo_lib.generate_timestamp_metadata, snapshot_filename, '3',
640+
repo_lib.generate_timestamp_metadata, snapshot_file_path, '3',
641641
expiration_date, storage_backend, repository_name)
642642
self.assertRaises(securesystemslib.exceptions.FormatError,
643-
repo_lib.generate_timestamp_metadata, snapshot_filename, version, '3',
643+
repo_lib.generate_timestamp_metadata, snapshot_file_path, version, '3',
644644
storage_backend, repository_name)
645645

646646

647647

648648
def test_generate_timestamp_metadata_without_length(self):
649-
snapshot_filename, version, expiration_date, storage_backend, \
649+
snapshot_file_path, version, expiration_date, storage_backend, \
650650
repository_name = self._setup_generate_timestamp_metadata_test()
651651

652-
timestamp_metadata = repo_lib.generate_timestamp_metadata(snapshot_filename,
652+
timestamp_metadata = repo_lib.generate_timestamp_metadata(snapshot_file_path,
653653
version, expiration_date, storage_backend, repository_name,
654654
use_length=False)
655655
self.assertTrue(tuf.formats.TIMESTAMP_SCHEMA.matches(timestamp_metadata))
@@ -663,10 +663,10 @@ def test_generate_timestamp_metadata_without_length(self):
663663

664664

665665
def test_generate_timestamp_metadata_without_hashes(self):
666-
snapshot_filename, version, expiration_date, storage_backend, \
666+
snapshot_file_path, version, expiration_date, storage_backend, \
667667
repository_name = self._setup_generate_timestamp_metadata_test()
668668

669-
timestamp_metadata = repo_lib.generate_timestamp_metadata(snapshot_filename,
669+
timestamp_metadata = repo_lib.generate_timestamp_metadata(snapshot_file_path,
670670
version, expiration_date, storage_backend, repository_name,
671671
use_hashes=False)
672672
self.assertTrue(tuf.formats.TIMESTAMP_SCHEMA.matches(timestamp_metadata))
@@ -680,10 +680,10 @@ def test_generate_timestamp_metadata_without_hashes(self):
680680

681681

682682
def test_generate_timestamp_metadata_without_length_and_hashes(self):
683-
snapshot_filename, version, expiration_date, storage_backend, \
683+
snapshot_file_path, version, expiration_date, storage_backend, \
684684
repository_name = self._setup_generate_timestamp_metadata_test()
685685

686-
timestamp_metadata = repo_lib.generate_timestamp_metadata(snapshot_filename,
686+
timestamp_metadata = repo_lib.generate_timestamp_metadata(snapshot_file_path,
687687
version, expiration_date, storage_backend, repository_name,
688688
use_hashes=False, use_length=False)
689689
self.assertTrue(tuf.formats.TIMESTAMP_SCHEMA.matches(timestamp_metadata))

tuf/repository_lib.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,11 @@ def _generate_and_write_metadata(rolename, metadata_filename,
137137
SNAPSHOT_EXPIRES_WARN_SECONDS)
138138

139139
elif rolename == 'timestamp':
140-
snapshot_filename = filenames['snapshot']
141-
metadata = generate_timestamp_metadata(snapshot_filename, roleinfo['version'],
140+
# If filenames don't have "snapshot_filename" key, defaults to "snapshot.json"
141+
snapshot_file_path = (filenames and filenames['snapshot']) \
142+
or SNAPSHOT_FILENAME
143+
144+
metadata = generate_timestamp_metadata(snapshot_file_path, roleinfo['version'],
142145
roleinfo['expires'], storage_backend, repository_name,
143146
use_length=use_timestamp_length, use_hashes=use_timestamp_hashes)
144147

@@ -156,9 +159,10 @@ def _generate_and_write_metadata(rolename, metadata_filename,
156159
# Don't hash-prefix consistent target files if they are handled out of band
157160
consistent_targets = consistent_snapshot and not use_existing_fileinfo
158161

159-
metadata = generate_targets_metadata(targets_directory, roleinfo['paths'],
160-
roleinfo['version'], roleinfo['expires'], roleinfo['delegations'],
161-
consistent_targets, use_existing_fileinfo, storage_backend)
162+
metadata = generate_targets_metadata(targets_directory,
163+
roleinfo['paths'], roleinfo['version'], roleinfo['expires'],
164+
roleinfo['delegations'], consistent_targets, use_existing_fileinfo,
165+
storage_backend)
162166

163167
# Before writing 'rolename' to disk, automatically increment its version
164168
# number (if 'increment_version_number' is True) so that the caller does not
@@ -1522,16 +1526,17 @@ def generate_snapshot_metadata(metadata_directory, version, expiration_date,
15221526

15231527

15241528

1525-
def generate_timestamp_metadata(snapshot_filename, version, expiration_date,
1529+
1530+
def generate_timestamp_metadata(snapshot_file_path, version, expiration_date,
15261531
storage_backend, repository_name, use_length=True, use_hashes=True):
15271532
"""
15281533
<Purpose>
15291534
Generate the timestamp metadata object. The 'snapshot.json' file must
15301535
exist.
15311536
15321537
<Arguments>
1533-
snapshot_filename:
1534-
The required filename of the snapshot metadata file. The timestamp role
1538+
snapshot_file_path:
1539+
Path to the required snapshot metadata file. The timestamp role
15351540
needs to the calculate the file size and hash of this file.
15361541
15371542
version:
@@ -1577,7 +1582,7 @@ def generate_timestamp_metadata(snapshot_filename, version, expiration_date,
15771582
# This check ensures arguments have the appropriate number of objects and
15781583
# object types, and that all dict keys are properly named.
15791584
# Raise 'securesystemslib.exceptions.FormatError' if the check fails.
1580-
securesystemslib.formats.PATH_SCHEMA.check_match(snapshot_filename)
1585+
securesystemslib.formats.PATH_SCHEMA.check_match(snapshot_file_path)
15811586
tuf.formats.METADATAVERSION_SCHEMA.check_match(version)
15821587
securesystemslib.formats.ISO8601_DATETIME_SCHEMA.check_match(expiration_date)
15831588
securesystemslib.formats.NAME_SCHEMA.check_match(repository_name)
@@ -1586,15 +1591,16 @@ def generate_timestamp_metadata(snapshot_filename, version, expiration_date,
15861591

15871592
snapshot_fileinfo = {}
15881593

1589-
length, hashes = securesystemslib.util.get_file_details(snapshot_filename,
1594+
length, hashes = securesystemslib.util.get_file_details(snapshot_file_path,
15901595
tuf.settings.FILE_HASH_ALGORITHMS, storage_backend)
15911596

15921597
length = (use_length and length) or None
15931598
hashes = (use_hashes and hashes) or None
15941599

1600+
snapshot_filename = os.path.basename(snapshot_file_path)
15951601
# Retrieve the versioninfo of the Snapshot metadata file.
15961602
snapshot_version = get_metadata_versioninfo('snapshot', repository_name)
1597-
snapshot_fileinfo[SNAPSHOT_FILENAME] = \
1603+
snapshot_fileinfo[snapshot_filename] = \
15981604
tuf.formats.make_metadata_fileinfo(snapshot_version['version'],
15991605
length, hashes)
16001606

0 commit comments

Comments
 (0)