Skip to content

Commit 24cd3cc

Browse files
committed
Use repo_lib helper functions for bin names
Update the repo_lib helper functions to use a prefix and create a name using a zero-padded count instead of the prefix range. So the name will be of the format prefix-count (for example bin.hbd-02). In addition, this updates the repository tool to use the updated repo_lib functions to avoid duplicate code. Signed-off-by: marinamoore <[email protected]>
1 parent a72d98b commit 24cd3cc

File tree

2 files changed

+33
-50
lines changed

2 files changed

+33
-50
lines changed

tuf/repository_lib.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,31 +1035,28 @@ def get_metadata_versioninfo(rolename, repository_name):
10351035

10361036

10371037

1038-
def create_bin_name(low, high, prefix_len):
1038+
def create_bin_name(bin_num, bin_num_length, prefix=''):
10391039
"""
10401040
<Purpose>
1041-
Create a string name of a delegated hash bin, where name will be a range of
1042-
zero-padded (up to prefix_len) strings i.e. for low=00, high=07,
1043-
prefix_len=3 the returned name would be '000-007'.
1041+
Create a string name of a delegated hash bin, where name will be
1042+
prefix-NUM where NUM will be a
1043+
zero-padded (up to prefix_len) string i.e. for bin_num=0, bin_num_length=2
1044+
prefix="pre", the name will be pre-00
10441045
10451046
<Arguments>
1046-
low:
1047-
The low end of the prefix range to be binned
1047+
bin_num:
1048+
The bin number
10481049
1049-
high:
1050-
The high end of the prefix range to be binned
1050+
bin_num_length:
1051+
The length of the bin number, used to determine zero padding
10511052
1052-
prefix_len:
1053-
The length of the prefix range components
1053+
prefix:
1054+
The prefix for the bin name
10541055
10551056
<Returns>
1056-
A string bin name, with each end of the range zero-padded up to prefix_len
1057+
A string bin name
10571058
"""
1058-
if low == high:
1059-
return "{low:0{len}x}".format(low=low, len=prefix_len)
1060-
1061-
return "{low:0{len}x}-{high:0{len}x}".format(low=low, high=high,
1062-
len=prefix_len)
1059+
return '{pre}{num:0{len}x}'.format(pre=prefix, num=bin_num, len=bin_num_length)
10631060

10641061

10651062

@@ -1112,7 +1109,7 @@ def get_bin_numbers(number_of_bins):
11121109

11131110

11141111

1115-
def find_bin_for_target_hash(target_hash, number_of_bins):
1112+
def find_bin_for_target_hash(target_hash, number_of_bins, prefix):
11161113
"""
11171114
<Purpose>
11181115
For a given hashed filename, target_hash, calculate the name of a hashed bin
@@ -1126,6 +1123,9 @@ def find_bin_for_target_hash(target_hash, number_of_bins):
11261123
number_of_bins:
11271124
The number of hashed_bins in use
11281125
1126+
prefix:
1127+
The bin name prefix
1128+
11291129
<Returns>
11301130
The name of the hashed bin target_hash would be binned into
11311131
"""
@@ -1136,7 +1136,8 @@ def find_bin_for_target_hash(target_hash, number_of_bins):
11361136

11371137
low = prefix - (prefix % bin_size)
11381138

1139-
return "{num:0{len}x}".format(num=int(low/bin_size), len=prefix_length)
1139+
return "{pre}{num:0{len}x}".format(pre=prefix, num=int(low/bin_size),
1140+
len=prefix_length)
11401141

11411142

11421143

tuf/repository_tool.py

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2588,7 +2588,7 @@ def revoke(self, rolename):
25882588

25892589

25902590
def delegate_hashed_bins(self, list_of_targets, keys_of_hashed_bins,
2591-
number_of_bins=DEFAULT_NUM_BINS, succinct=False, prefix=None):
2591+
number_of_bins=DEFAULT_NUM_BINS, succinct=False, prefix=''):
25922592
"""
25932593
<Purpose>
25942594
Distribute a large number of target files over multiple delegated roles
@@ -2663,13 +2663,6 @@ def delegate_hashed_bins(self, list_of_targets, keys_of_hashed_bins,
26632663
securesystemslib.formats.ANYKEYLIST_SCHEMA.check_match(keys_of_hashed_bins)
26642664
tuf.formats.NUMBINS_SCHEMA.check_match(number_of_bins)
26652665

2666-
# determine the prefix for each bin. If one is not provided, use the
2667-
# default prefix
2668-
if prefix:
2669-
name_prefix = prefix + '-'
2670-
else:
2671-
name_prefix = self.rolename + '.hbd-'
2672-
26732666
prefix_length, prefix_count, bin_size = repo_lib.get_bin_numbers(number_of_bins)
26742667

26752668
logger.info('Creating hashed bin delegations.\n' +
@@ -2685,7 +2678,7 @@ def delegate_hashed_bins(self, list_of_targets, keys_of_hashed_bins,
26852678
# to be delegated to that bin
26862679
ordered_roles = []
26872680
for idx in range(0, number_of_bins):
2688-
name = name_prefix + "{num:0{len}x}".format(num=idx, len=prefix_length)
2681+
name = repo_lib.create_bin_name(idx, prefix_length, prefix)
26892682
if succinct:
26902683
# For succinct hased bin delegations, these metadata files will still
26912684
# be created, but they will not all be listed in the delegation
@@ -2777,19 +2770,19 @@ def delegate_hashed_bins(self, list_of_targets, keys_of_hashed_bins,
27772770
if succinct:
27782771
# For succinct delegations, each bin is not added, so add a single
27792772
# succinct delegation
2780-
roleinfo = {'name': name_prefix,
2773+
roleinfo = {'name': prefix,
27812774
'keyids': keyids,
27822775
'threshold': 1,
27832776
'terminating': False,
27842777
'delegation_hash_prefix_len' : prefix_length,
27852778
'paths': []}
27862779
delegated_roleinfos.append(roleinfo)
27872780

2788-
target = self._create_delegated_target(name_prefix, keyids, 1, [])
2781+
target = self._create_delegated_target(prefix, keyids, 1, [])
27892782
for key in keys_of_hashed_bins:
27902783
target.add_verification_key(key)
27912784

2792-
self.add_delegated_role(name_prefix, target)
2785+
self.add_delegated_role(prefix, target)
27932786

27942787

27952788
self._update_roledb_delegations(keydict, delegated_roleinfos)
@@ -2798,7 +2791,7 @@ def delegate_hashed_bins(self, list_of_targets, keys_of_hashed_bins,
27982791

27992792

28002793
def add_target_to_bin(self, target_filepath, number_of_bins=DEFAULT_NUM_BINS,
2801-
fileinfo=None, prefix=None):
2794+
fileinfo=None, prefix=''):
28022795
"""
28032796
<Purpose>
28042797
Add the fileinfo of 'target_filepath' to the expected hashed bin, if the
@@ -2824,8 +2817,7 @@ def add_target_to_bin(self, target_filepath, number_of_bins=DEFAULT_NUM_BINS,
28242817
providing full information about the file.
28252818
28262819
prefix:
2827-
The filename prefix used for bin names. If None, the default of
2828-
rolename.hbd-* will be used
2820+
The filename prefix used for bin names.
28292821
28302822
<Exceptions>
28312823
securesystemslib.exceptions.FormatError, if 'target_filepath' is
@@ -2852,13 +2844,9 @@ def add_target_to_bin(self, target_filepath, number_of_bins=DEFAULT_NUM_BINS,
28522844

28532845
# TODO: check target_filepath is sane
28542846

2855-
if prefix:
2856-
bin_name = prefix + '-'
2857-
else:
2858-
bin_name = self.rolename + ".hbd-"
2859-
28602847
path_hash = repo_lib.get_target_hash(target_filepath)
2861-
bin_name = bin_name + repo_lib.find_bin_for_target_hash(path_hash, number_of_bins)
2848+
bin_name = repo_lib.find_bin_for_target_hash(path_hash, number_of_bins,
2849+
prefix)
28622850

28632851
# Ensure the Targets object has delegated to hashed bins
28642852
if not self._delegated_roles.get(bin_name, None):
@@ -2873,7 +2861,7 @@ def add_target_to_bin(self, target_filepath, number_of_bins=DEFAULT_NUM_BINS,
28732861

28742862

28752863
def remove_target_from_bin(self, target_filepath,
2876-
number_of_bins=DEFAULT_NUM_BINS, prefix=None):
2864+
number_of_bins=DEFAULT_NUM_BINS, prefix=''):
28772865
"""
28782866
<Purpose>
28792867
Remove the fileinfo of 'target_filepath' from the expected hashed bin, if
@@ -2895,8 +2883,7 @@ def remove_target_from_bin(self, target_filepath,
28952883
Note: 'number_of_bins' must be a power of 2.
28962884
28972885
prefix:
2898-
The filename prefix used for bin names. If None, the default of
2899-
rolename.hbd-* will be used
2886+
The filename prefix used for bin names.
29002887
29012888
<Exceptions>
29022889
securesystemslib.exceptions.FormatError, if 'target_filepath' is
@@ -2923,13 +2910,9 @@ def remove_target_from_bin(self, target_filepath,
29232910

29242911
# TODO: check target_filepath is sane?
29252912

2926-
if prefix:
2927-
bin_name = prefix + '-'
2928-
else:
2929-
bin_name = self.rolename + '.hbd-'
2930-
29312913
path_hash = repo_lib.get_target_hash(target_filepath)
2932-
bin_name = bin_name + repo_lib.find_bin_for_target_hash(path_hash, number_of_bins)
2914+
bin_name = repo_lib.find_bin_for_target_hash(path_hash, number_of_bins,
2915+
prefix)
29332916

29342917
# Ensure the Targets object has delegated to hashed bins
29352918
if not self._delegated_roles.get(bin_name, None):
@@ -3241,8 +3224,7 @@ def load_repository(repository_directory, repository_name='default',
32413224
prefix_len = role['delegation_hash_prefix_len']
32423225
num_bins = 2 ** prefix_len
32433226
for i in range(num_bins):
3244-
bin_name = "{name}{num:0{len}x}".format(name=role['name'], num=i,
3245-
len=prefix_len)
3227+
bin_name = repo_lib.create_bin_name(i, prefix_len, role['name'])
32463228
succinct_role = {'name': bin_name,
32473229
'keyids': role['keyids'],
32483230
'threshold': role['threshold'],

0 commit comments

Comments
 (0)