Skip to content

Commit 0c3131b

Browse files
committed
Make keyids in Role a set
From the specification: "Clients MUST ensure that for any KEYID represented in this key list and in other files, only one unique key has that KEYID." The “only one unique key has that KEYID” is a requirement which can’t be achieved if two keyids are the same. So, in order to mandate that requirement it makes sense to use a set which will guarantee us the keyid’s uniqueness. Signed-off-by: Martin Vrachev <[email protected]>
1 parent ef71c2d commit 0c3131b

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

tuf/api/metadata.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -463,11 +463,17 @@ class Role:
463463

464464
def __init__(
465465
self,
466-
keyids: set,
466+
keyids: list,
467467
threshold: int,
468468
unrecognized_fields: Optional[Mapping[str, Any]] = None,
469469
) -> None:
470-
self.keyids = keyids
470+
keyids_set = set(keyids)
471+
if len(keyids_set) != len(keyids):
472+
raise ValueError(
473+
f"keyids should be a list of unique strings,"
474+
f" instead got {keyids}"
475+
)
476+
self.keyids = keyids_set
471477
self.threshold = threshold
472478
self.unrecognized_fields = unrecognized_fields or {}
473479

@@ -482,7 +488,7 @@ def from_dict(cls, role_dict: Mapping[str, Any]) -> "Role":
482488
def to_dict(self) -> Dict:
483489
"""Returns the dictionary representation of self."""
484490
return {
485-
"keyids": self.keyids,
491+
"keyids": list(self.keyids),
486492
"threshold": self.threshold,
487493
**self.unrecognized_fields,
488494
}
@@ -570,7 +576,7 @@ def add_key(
570576
) -> None:
571577
"""Adds new key for 'role' and updates the key store."""
572578
if keyid not in self.roles[role].keyids:
573-
self.roles[role].keyids.append(keyid)
579+
self.roles[role].keyids.add(keyid)
574580
self.keys[keyid] = key_metadata
575581

576582
# Remove key for a role.

0 commit comments

Comments
 (0)