@@ -95,7 +95,8 @@ def _generate_and_write_metadata(rolename, metadata_filename,
95
95
targets_directory , metadata_directory , consistent_snapshot = False ,
96
96
filenames = None , allow_partially_signed = False , increment_version_number = True ,
97
97
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 ):
99
100
"""
100
101
Non-public function that can generate and write the metadata for the
101
102
specified 'rolename'. It also increments the version number of 'rolename' if
@@ -125,7 +126,8 @@ def _generate_and_write_metadata(rolename, metadata_filename,
125
126
targets_filename = TARGETS_FILENAME [:- len (METADATA_EXTENSION )]
126
127
metadata = generate_snapshot_metadata (metadata_directory ,
127
128
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 )
129
131
130
132
131
133
_log_warning_if_expires_soon (SNAPSHOT_FILENAME , roleinfo ['expires' ],
@@ -1415,7 +1417,8 @@ def _generate_targets_fileinfo(target_files, targets_directory,
1415
1417
1416
1418
1417
1419
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 ):
1419
1422
"""
1420
1423
<Purpose>
1421
1424
Create the snapshot metadata. The minimum metadata must exist (i.e.,
@@ -1450,6 +1453,16 @@ def generate_snapshot_metadata(metadata_directory, version, expiration_date,
1450
1453
The name of the repository. If not supplied, 'rolename' is added to the
1451
1454
'default' repository.
1452
1455
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
+
1453
1466
<Exceptions>
1454
1467
securesystemslib.exceptions.FormatError, if the arguments are improperly
1455
1468
formatted.
@@ -1474,6 +1487,8 @@ def generate_snapshot_metadata(metadata_directory, version, expiration_date,
1474
1487
securesystemslib .formats .PATH_SCHEMA .check_match (targets_filename )
1475
1488
securesystemslib .formats .BOOLEAN_SCHEMA .check_match (consistent_snapshot )
1476
1489
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 )
1477
1492
1478
1493
metadata_directory = _check_directory (metadata_directory )
1479
1494
@@ -1508,9 +1523,27 @@ def generate_snapshot_metadata(metadata_directory, version, expiration_date,
1508
1523
# list these roles found in the metadata directory.
1509
1524
if tuf .roledb .role_exists (rolename , repository_name ) and \
1510
1525
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 ,
1512
1542
repository_name )
1513
1543
1544
+ fileinfodict [metadata_name ] = tuf .formats .make_fileinfo (
1545
+ length , hashes , version = file_version ['version' ])
1546
+
1514
1547
else :
1515
1548
logger .debug ('Metadata file has an unsupported file'
1516
1549
' extension: ' + metadata_filename )
0 commit comments