Skip to content

Commit c9f220f

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 f0a3188 commit c9f220f

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

tests/test_repository_lib.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ def _setup_generate_timestamp_metadata_test(self):
606606
repo_lib.METADATA_STAGED_DIRECTORY_NAME)
607607
targets_directory = os.path.join(repository_directory, repo_lib.TARGETS_DIRECTORY_NAME)
608608

609-
snapshot_filename = os.path.join(metadata_directory,
609+
snapshot_file_path = os.path.join(metadata_directory,
610610
repo_lib.SNAPSHOT_FILENAME)
611611

612612
# Set valid generate_timestamp_metadata() arguments.
@@ -622,15 +622,15 @@ def _setup_generate_timestamp_metadata_test(self):
622622
repository_junk = repo_tool.load_repository(repository_directory,
623623
repository_name)
624624

625-
return snapshot_filename, version, expiration_date, storage_backend, \
625+
return snapshot_file_path, version, expiration_date, storage_backend, \
626626
repository_name
627627

628628

629629
def test_generate_timestamp_metadata(self):
630-
snapshot_filename, version, expiration_date, storage_backend, \
630+
snapshot_file_path, version, expiration_date, storage_backend, \
631631
repository_name = self._setup_generate_timestamp_metadata_test()
632632

633-
timestamp_metadata = repo_lib.generate_timestamp_metadata(snapshot_filename,
633+
timestamp_metadata = repo_lib.generate_timestamp_metadata(snapshot_file_path,
634634
version, expiration_date, storage_backend, repository_name)
635635
self.assertTrue(tuf.formats.TIMESTAMP_SCHEMA.matches(timestamp_metadata))
636636

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

649649

650650

651651
def test_generate_timestamp_metadata_without_length(self):
652-
snapshot_filename, version, expiration_date, storage_backend, \
652+
snapshot_file_path, version, expiration_date, storage_backend, \
653653
repository_name = self._setup_generate_timestamp_metadata_test()
654654

655-
timestamp_metadata = repo_lib.generate_timestamp_metadata(snapshot_filename,
655+
timestamp_metadata = repo_lib.generate_timestamp_metadata(snapshot_file_path,
656656
version, expiration_date, storage_backend, repository_name,
657657
use_length=False)
658658
self.assertTrue(tuf.formats.TIMESTAMP_SCHEMA.matches(timestamp_metadata))
@@ -666,10 +666,10 @@ def test_generate_timestamp_metadata_without_length(self):
666666

667667

668668
def test_generate_timestamp_metadata_without_hashes(self):
669-
snapshot_filename, version, expiration_date, storage_backend, \
669+
snapshot_file_path, version, expiration_date, storage_backend, \
670670
repository_name = self._setup_generate_timestamp_metadata_test()
671671

672-
timestamp_metadata = repo_lib.generate_timestamp_metadata(snapshot_filename,
672+
timestamp_metadata = repo_lib.generate_timestamp_metadata(snapshot_file_path,
673673
version, expiration_date, storage_backend, repository_name,
674674
use_hashes=False)
675675
self.assertTrue(tuf.formats.TIMESTAMP_SCHEMA.matches(timestamp_metadata))
@@ -683,10 +683,10 @@ def test_generate_timestamp_metadata_without_hashes(self):
683683

684684

685685
def test_generate_timestamp_metadata_without_length_and_hashes(self):
686-
snapshot_filename, version, expiration_date, storage_backend, \
686+
snapshot_file_path, version, expiration_date, storage_backend, \
687687
repository_name = self._setup_generate_timestamp_metadata_test()
688688

689-
timestamp_metadata = repo_lib.generate_timestamp_metadata(snapshot_filename,
689+
timestamp_metadata = repo_lib.generate_timestamp_metadata(snapshot_file_path,
690690
version, expiration_date, storage_backend, repository_name,
691691
use_hashes=False, use_length=False)
692692
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
@@ -1523,16 +1527,17 @@ def generate_snapshot_metadata(metadata_directory, version, expiration_date,
15231527

15241528

15251529

1526-
def generate_timestamp_metadata(snapshot_filename, version, expiration_date,
1530+
1531+
def generate_timestamp_metadata(snapshot_file_path, version, expiration_date,
15271532
storage_backend, repository_name, use_length=True, use_hashes=True):
15281533
"""
15291534
<Purpose>
15301535
Generate the timestamp metadata object. The 'snapshot.json' file must
15311536
exist.
15321537
15331538
<Arguments>
1534-
snapshot_filename:
1535-
The required filename of the snapshot metadata file. The timestamp role
1539+
snapshot_file_path:
1540+
Path to the required snapshot metadata file. The timestamp role
15361541
needs to the calculate the file size and hash of this file.
15371542
15381543
version:
@@ -1578,7 +1583,7 @@ def generate_timestamp_metadata(snapshot_filename, version, expiration_date,
15781583
# This check ensures arguments have the appropriate number of objects and
15791584
# object types, and that all dict keys are properly named.
15801585
# Raise 'securesystemslib.exceptions.FormatError' if the check fails.
1581-
securesystemslib.formats.PATH_SCHEMA.check_match(snapshot_filename)
1586+
securesystemslib.formats.PATH_SCHEMA.check_match(snapshot_file_path)
15821587
tuf.formats.METADATAVERSION_SCHEMA.check_match(version)
15831588
securesystemslib.formats.ISO8601_DATETIME_SCHEMA.check_match(expiration_date)
15841589
securesystemslib.formats.NAME_SCHEMA.check_match(repository_name)
@@ -1587,15 +1592,16 @@ def generate_timestamp_metadata(snapshot_filename, version, expiration_date,
15871592

15881593
snapshot_fileinfo = {}
15891594

1590-
length, hashes = securesystemslib.util.get_file_details(snapshot_filename,
1595+
length, hashes = securesystemslib.util.get_file_details(snapshot_file_path,
15911596
tuf.settings.FILE_HASH_ALGORITHMS, storage_backend)
15921597

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

1601+
_, snapshot_filename = os.path.split(snapshot_file_path)
15961602
# Retrieve the versioninfo of the Snapshot metadata file.
15971603
snapshot_version = get_metadata_versioninfo('snapshot', repository_name)
1598-
snapshot_fileinfo[SNAPSHOT_FILENAME] = \
1604+
snapshot_fileinfo[snapshot_filename] = \
15991605
tuf.formats.make_metadata_fileinfo(snapshot_version['version'],
16001606
length, hashes)
16011607

0 commit comments

Comments
 (0)