-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change securesystemslib.dsse.Envelope.signatures to dict upstream #743
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,12 +17,15 @@ class Envelope: | |
Attributes: | ||
payload: Arbitrary byte sequence of serialized body. | ||
payload_type: string that identifies how to interpret payload. | ||
signatures: list of Signature. | ||
signatures: dict of Signature key id and Signatures. | ||
|
||
""" | ||
|
||
def __init__( | ||
self, payload: bytes, payload_type: str, signatures: List[Signature] | ||
self, | ||
payload: bytes, | ||
payload_type: str, | ||
signatures: Dict[str, Signature], | ||
): | ||
self.payload = payload | ||
self.payload_type = payload_type | ||
|
@@ -58,18 +61,23 @@ def from_dict(cls, data: dict) -> "Envelope": | |
payload = b64dec(data["payload"]) | ||
payload_type = data["payloadType"] | ||
|
||
signatures = [] | ||
signatures = {} | ||
for signature in data["signatures"]: | ||
signature["sig"] = b64dec(signature["sig"]).hex() | ||
signatures.append(Signature.from_dict(signature)) | ||
signature = Signature.from_dict(signature) | ||
if signature.keyid in signatures: | ||
raise ValueError( | ||
f"Multiple signatures found for keyid {signature.keyid}" | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the check. Would you mind adding a small test? |
||
signatures[signature.keyid] = signature | ||
|
||
return cls(payload, payload_type, signatures) | ||
|
||
def to_dict(self) -> dict: | ||
"""Returns the JSON-serializable dictionary representation of self.""" | ||
|
||
signatures = [] | ||
for signature in self.signatures: | ||
for signature in list(self.signatures.values()): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason to convert to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah no - I just followed the format here comment . Will change to just iterating over dict_values There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that PR has a few such conversions for type checking reasons. |
||
sig_dict = signature.to_dict() | ||
sig_dict["sig"] = b64enc(bytes.fromhex(sig_dict["sig"])) | ||
signatures.append(sig_dict) | ||
|
@@ -101,7 +109,7 @@ def sign(self, signer: Signer) -> Signature: | |
""" | ||
|
||
signature = signer.sign(self.pae()) | ||
self.signatures.append(signature) | ||
self.signatures[signature.keyid] = signature | ||
|
||
return signature | ||
|
||
|
@@ -140,7 +148,7 @@ def verify(self, keys: List[Key], threshold: int) -> Dict[str, Key]: | |
if len(keys) < threshold: | ||
raise ValueError("Number of keys can't be less than threshold") | ||
|
||
for signature in self.signatures: | ||
for signature in list(self.signatures.values()): | ||
for key in keys: | ||
# If Signature keyid doesn't match with Key, skip. | ||
if not key.keyid == signature.keyid: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't sure if I should change the last param from signatures: List[Signature] to signatures: Dict[str, Signature] or if it is fine to leave it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question! Please do change the constructor argument to
Dict[str, Signature]
and implement the translation infrom_dict
andto_dict
. This is also what we do with python-tufMetadata
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also just saw that we raise, if there are duplicate (by keyid) signatures there. I suggest to do the same in
Envelope.from_dict
.