Skip to content

Commit ba76b27

Browse files
committed
Make length and hashes optional for snapshot
As per the specification (v1.0.1) length and hashes fields in snapshot metadata are optional. The reference implementation should reflect this. Signed-off-by: Martin Vrachev <[email protected]>
1 parent 513a746 commit ba76b27

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

tuf/repository_lib.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ def _generate_and_write_metadata(rolename, metadata_filename,
9595
targets_directory, metadata_directory, consistent_snapshot=False,
9696
filenames=None, allow_partially_signed=False, increment_version_number=True,
9797
repository_name='default', use_existing_fileinfo=False,
98-
use_timestamp_length=True, use_timestamp_hashes=True):
98+
use_timestamp_length=True, use_timestamp_hashes=True,
99+
use_snapshot_length=True, use_snapshot_hashes=True):
99100
"""
100101
Non-public function that can generate and write the metadata for the
101102
specified 'rolename'. It also increments the version number of 'rolename' if
@@ -125,7 +126,8 @@ def _generate_and_write_metadata(rolename, metadata_filename,
125126
targets_filename = TARGETS_FILENAME[:-len(METADATA_EXTENSION)]
126127
metadata = generate_snapshot_metadata(metadata_directory,
127128
roleinfo['version'], roleinfo['expires'], targets_filename,
128-
consistent_snapshot, repository_name)
129+
consistent_snapshot, repository_name,
130+
use_length=use_snapshot_length, use_hashes=use_snapshot_hashes)
129131

130132

131133
_log_warning_if_expires_soon(SNAPSHOT_FILENAME, roleinfo['expires'],
@@ -1415,7 +1417,8 @@ def _generate_targets_fileinfo(target_files, targets_directory,
14151417

14161418

14171419
def generate_snapshot_metadata(metadata_directory, version, expiration_date,
1418-
targets_filename, consistent_snapshot=False, repository_name='default'):
1420+
targets_filename, consistent_snapshot=False, repository_name='default',
1421+
use_length=True, use_hashes=True):
14191422
"""
14201423
<Purpose>
14211424
Create the snapshot metadata. The minimum metadata must exist (i.e.,
@@ -1450,6 +1453,16 @@ def generate_snapshot_metadata(metadata_directory, version, expiration_date,
14501453
The name of the repository. If not supplied, 'rolename' is added to the
14511454
'default' repository.
14521455
1456+
use_length:
1457+
Used to decide if the length attribute should be used. From version 1.0.1
1458+
of the tuf spec, length is an optional attribute in snapshot metadata.
1459+
Default is true.
1460+
1461+
use_hashes:
1462+
Used to decide if the hashes attribute should be used. From version 1.0.1
1463+
of the tuf spec, hashes is an optional attribute in snapshot metadata.
1464+
Default is true.
1465+
14531466
<Exceptions>
14541467
securesystemslib.exceptions.FormatError, if the arguments are improperly
14551468
formatted.
@@ -1474,6 +1487,8 @@ def generate_snapshot_metadata(metadata_directory, version, expiration_date,
14741487
securesystemslib.formats.PATH_SCHEMA.check_match(targets_filename)
14751488
securesystemslib.formats.BOOLEAN_SCHEMA.check_match(consistent_snapshot)
14761489
securesystemslib.formats.NAME_SCHEMA.check_match(repository_name)
1490+
securesystemslib.formats.BOOLEAN_SCHEMA.check_match(use_length)
1491+
securesystemslib.formats.BOOLEAN_SCHEMA.check_match(use_hashes)
14771492

14781493
metadata_directory = _check_directory(metadata_directory)
14791494

@@ -1508,9 +1523,27 @@ def generate_snapshot_metadata(metadata_directory, version, expiration_date,
15081523
# list these roles found in the metadata directory.
15091524
if tuf.roledb.role_exists(rolename, repository_name) and \
15101525
rolename not in ['root', 'snapshot', 'timestamp', 'targets']:
1511-
fileinfodict[metadata_name] = get_metadata_versioninfo(rolename,
1526+
1527+
length = None
1528+
hashes = None
1529+
if use_length or use_hashes:
1530+
1531+
length, hashes = securesystemslib.util.get_file_details(
1532+
os.path.join(metadata_directory, metadata_filename),
1533+
tuf.settings.FILE_HASH_ALGORITHMS)
1534+
1535+
if use_length == False:
1536+
length = None
1537+
1538+
if use_hashes == False:
1539+
hashes = None
1540+
1541+
file_version = get_metadata_versioninfo(rolename,
15121542
repository_name)
15131543

1544+
fileinfodict[metadata_name] = tuf.formats.make_fileinfo(
1545+
length, hashes, version=file_version['version'])
1546+
15141547
else:
15151548
logger.debug('Metadata file has an unsupported file'
15161549
' extension: ' + metadata_filename)

tuf/repository_tool.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,16 @@ class Repository(object):
184184
From version 1.0.1, of the tuf spec, hashes is an optional attribute in
185185
timestamp metadata file. Default is true.
186186
187+
use_snapshot_length:
188+
Used to decide if the length attribute should be used in the snapshot file.
189+
From version 1.0.1 of the tuf spec, length is an optional attribute in
190+
snapshot metadata file. Default is true.
191+
192+
use_snapshot_hashes:
193+
Used to decide if the hashes attribute should be used in the snapshot file.
194+
From version 1.0.1, of the tuf spec, hashes is an optional attribute in
195+
snapshot metadata file. Default is true.
196+
187197
<Exceptions>
188198
securesystemslib.exceptions.FormatError, if the arguments are improperly
189199
formatted.
@@ -198,7 +208,8 @@ class Repository(object):
198208

199209
def __init__(self, repository_directory, metadata_directory,
200210
targets_directory, repository_name='default',
201-
use_timestamp_length=True, use_timestamp_hashes=True):
211+
use_timestamp_length=True, use_timestamp_hashes=True,
212+
use_snapshot_length=True, use_snapshot_hashes=True):
202213

203214
# Do the arguments have the correct format?
204215
# Ensure the arguments have the appropriate number of objects and object
@@ -210,13 +221,17 @@ def __init__(self, repository_directory, metadata_directory,
210221
securesystemslib.formats.NAME_SCHEMA.check_match(repository_name)
211222
securesystemslib.formats.BOOLEAN_SCHEMA.check_match(use_timestamp_length)
212223
securesystemslib.formats.BOOLEAN_SCHEMA.check_match(use_timestamp_hashes)
224+
securesystemslib.formats.BOOLEAN_SCHEMA.check_match(use_snapshot_length)
225+
securesystemslib.formats.BOOLEAN_SCHEMA.check_match(use_snapshot_hashes)
213226

214227
self._repository_directory = repository_directory
215228
self._metadata_directory = metadata_directory
216229
self._targets_directory = targets_directory
217230
self._repository_name = repository_name
218231
self._use_timestamp_length = use_timestamp_length
219232
self._use_timestamp_hashes = use_timestamp_hashes
233+
self._use_snapshot_length = use_snapshot_length
234+
self._use_snapshot_hashes = use_snapshot_hashes
220235

221236
try:
222237
tuf.roledb.create_roledb(repository_name)
@@ -339,7 +354,9 @@ def writeall(self, consistent_snapshot=False, use_existing_fileinfo=False):
339354
snapshot_signable, junk = repo_lib._generate_and_write_metadata('snapshot',
340355
filenames['snapshot'], self._targets_directory,
341356
self._metadata_directory, consistent_snapshot, filenames,
342-
repository_name=self._repository_name)
357+
repository_name=self._repository_name,
358+
use_snapshot_length=self._use_snapshot_length,
359+
use_snapshot_hashes=self._use_snapshot_hashes)
343360

344361
# Generate the 'timestamp.json' metadata file.
345362
if 'timestamp' in dirty_rolenames:

0 commit comments

Comments
 (0)