Skip to content

Commit 760d453

Browse files
author
Jussi Kukkonen
committed
Metadata API: Use OrderedDict for signatures
Dict ordering is part of regular Dict from Python 3.7: Use OrderedDict for signatures to make sure signatures are serialized in a reproducible order even on 3.6. The added benefit is that reader will immediately understand that the order has some significance. Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
1 parent 15bbf14 commit 760d453

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

tuf/api/metadata.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,17 @@
1818
import abc
1919
import tempfile
2020
from datetime import datetime, timedelta
21-
from typing import Any, ClassVar, Dict, List, Mapping, Optional, Tuple, Type
21+
from typing import (
22+
Any,
23+
ClassVar,
24+
Dict,
25+
List,
26+
Mapping,
27+
Optional,
28+
OrderedDict,
29+
Tuple,
30+
Type,
31+
)
2232

2333
from securesystemslib.keys import verify_signature
2434
from securesystemslib.signer import Signature, Signer
@@ -48,11 +58,13 @@ class Metadata:
4858
signed: A subclass of Signed, which has the actual metadata payload,
4959
i.e. one of Targets, Snapshot, Timestamp or Root.
5060
51-
signatures: A dict of keyids to Securesystemslib Signature objects,
52-
each signing the canonical serialized representation of 'signed'.
61+
signatures: An ordered dictionary of keyids to Signature objects, each
62+
signing the canonical serialized representation of 'signed'.
5363
"""
5464

55-
def __init__(self, signed: "Signed", signatures: Dict[str, Signature]):
65+
def __init__(
66+
self, signed: "Signed", signatures: OrderedDict[str, Signature]
67+
):
5668
self.signed = signed
5769
self.signatures = signatures
5870

@@ -89,7 +101,7 @@ def from_dict(cls, metadata: Dict[str, Any]) -> "Metadata":
89101
raise ValueError(f'unrecognized metadata type "{_type}"')
90102

91103
# Make sure signatures are unique
92-
signatures: Dict[str, Signature] = {}
104+
signatures = OrderedDict[str, Signature]()
93105
for sig_dict in metadata.pop("signatures"):
94106
sig = Signature.from_dict(sig_dict)
95107
if sig.keyid in signatures:
@@ -249,7 +261,9 @@ def sign(
249261
if append:
250262
self.signatures[signature.keyid] = signature
251263
else:
252-
self.signatures = {signature.keyid: signature}
264+
self.signatures = OrderedDict[str, Signature](
265+
{signature.keyid: signature}
266+
)
253267

254268
return signature
255269

0 commit comments

Comments
 (0)