Skip to content

Commit b350847

Browse files
authored
Merge pull request #1922 from jku/constructor-defaults
Add default args to Signed constructors
2 parents 8de43ab + 0d3bb68 commit b350847

File tree

5 files changed

+112
-166
lines changed

5 files changed

+112
-166
lines changed

examples/repo_example/basic_repo.py

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
Key,
3737
Metadata,
3838
MetaFile,
39-
Role,
4039
Root,
4140
Snapshot,
4241
TargetFile,
@@ -96,12 +95,7 @@ def _in(days: float) -> datetime:
9695
# The targets role guarantees integrity for the files that TUF aims to protect,
9796
# i.e. target files. It does so by listing the relevant target files, along
9897
# with their hash and length.
99-
roles["targets"] = Metadata[Targets](
100-
signed=Targets(
101-
version=1, spec_version=SPEC_VERSION, expires=_in(7), targets={}
102-
),
103-
signatures={},
104-
)
98+
roles["targets"] = Metadata(Targets(expires=_in(7)))
10599

106100
# For the purpose of this example we use the top-level targets role to protect
107101
# the integrity of this very example script. The metadata entry contains the
@@ -124,15 +118,7 @@ def _in(days: float) -> datetime:
124118
# by listing all available targets metadata files at their latest version. This
125119
# becomes relevant, when there are multiple targets metadata files in a
126120
# repository and we want to protect the client against mix-and-match attacks.
127-
roles["snapshot"] = Metadata[Snapshot](
128-
Snapshot(
129-
version=1,
130-
spec_version=SPEC_VERSION,
131-
expires=_in(7),
132-
meta={"targets.json": MetaFile(version=1)},
133-
),
134-
{},
135-
)
121+
roles["snapshot"] = Metadata(Snapshot(expires=_in(7)))
136122

137123
# Timestamp (freshness)
138124
# ---------------------
@@ -146,15 +132,7 @@ def _in(days: float) -> datetime:
146132
# format. But given that timestamp metadata always has only one entry in its
147133
# 'meta' field, i.e. for the latest snapshot file, the timestamp object
148134
# provides the shortcut 'snapshot_meta'.
149-
roles["timestamp"] = Metadata[Timestamp](
150-
Timestamp(
151-
version=1,
152-
spec_version=SPEC_VERSION,
153-
expires=_in(1),
154-
snapshot_meta=MetaFile(version=1),
155-
),
156-
{},
157-
)
135+
roles["timestamp"] = Metadata(Timestamp(expires=_in(1)))
158136

159137
# Root (root of trust)
160138
# --------------------
@@ -168,32 +146,19 @@ def _in(days: float) -> datetime:
168146
# 'keys' field), and a configuration parameter that describes whether a
169147
# repository uses consistent snapshots (see section 'Persist metadata' below
170148
# for more details).
171-
#
149+
150+
# Create root metadata object
151+
roles["root"] = Metadata(Root(expires=_in(365)))
152+
172153
# For this example, we generate one 'ed25519' key pair for each top-level role
173154
# using python-tuf's in-house crypto library.
174155
# See https://github.com/secure-systems-lab/securesystemslib for more details
175156
# about key handling, and don't forget to password-encrypt your private keys!
176157
for name in ["targets", "snapshot", "timestamp", "root"]:
177158
keys[name] = generate_ed25519_key()
178-
179-
# Create root metadata object
180-
roles["root"] = Metadata[Root](
181-
signed=Root(
182-
version=1,
183-
spec_version=SPEC_VERSION,
184-
expires=_in(365),
185-
keys={
186-
key["keyid"]: Key.from_securesystemslib_key(key)
187-
for key in keys.values()
188-
},
189-
roles={
190-
role: Role([key["keyid"]], threshold=1)
191-
for role, key in keys.items()
192-
},
193-
consistent_snapshot=True,
194-
),
195-
signatures={},
196-
)
159+
roles["root"].signed.add_key(
160+
name, Key.from_securesystemslib_key(keys[name])
161+
)
197162

198163
# NOTE: We only need the public part to populate root, so it is possible to use
199164
# out-of-band mechanisms to generate key pairs and only expose the public part

examples/repo_example/hashed_bin_delegation.py

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -147,22 +147,11 @@ def find_hash_bin(path: str) -> str:
147147

148148
# Create preliminary delegating targets role (bins) and add public key for
149149
# delegated targets (bin_n) to key store. Delegation details are update below.
150-
roles["bins"] = Metadata[Targets](
151-
signed=Targets(
152-
version=1,
153-
spec_version=SPEC_VERSION,
154-
expires=_in(365),
155-
targets={},
156-
delegations=Delegations(
157-
keys={
158-
keys["bin-n"]["keyid"]: Key.from_securesystemslib_key(
159-
keys["bin-n"]
160-
)
161-
},
162-
roles={},
163-
),
164-
),
165-
signatures={},
150+
roles["bins"] = Metadata(Targets(expires=_in(365)))
151+
bin_n_key = Key.from_securesystemslib_key(keys["bin-n"])
152+
roles["bins"].signed.delegations = Delegations(
153+
keys={bin_n_key.keyid: bin_n_key},
154+
roles={},
166155
)
167156

168157
# The hash bin generator yields an ordered list of incremental hash bin names
@@ -185,12 +174,7 @@ def find_hash_bin(path: str) -> str:
185174
)
186175

187176
# Create delegated targets roles (bin_n)
188-
roles[bin_n_name] = Metadata[Targets](
189-
signed=Targets(
190-
version=1, spec_version=SPEC_VERSION, expires=_in(7), targets={}
191-
),
192-
signatures={},
193-
)
177+
roles[bin_n_name] = Metadata(Targets(expires=_in(7)))
194178

195179
# Add target file
196180
# ---------------

tests/generated_data/generate_md.py

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,7 @@
1111
from securesystemslib.signer import SSlibSigner
1212

1313
from tests import utils
14-
from tuf.api.metadata import (
15-
SPECIFICATION_VERSION,
16-
TOP_LEVEL_ROLE_NAMES,
17-
Key,
18-
Metadata,
19-
MetaFile,
20-
Role,
21-
Root,
22-
Snapshot,
23-
Targets,
24-
Timestamp,
25-
)
14+
from tuf.api.metadata import Key, Metadata, Root, Snapshot, Targets, Timestamp
2615
from tuf.api.serialization.json import JSONSerializer
2716

2817
# Hardcode keys and expiry time to achieve reproducibility.
@@ -61,13 +50,11 @@
6150

6251
expires_str = "2050-01-01T00:00:00Z"
6352
EXPIRY = datetime.strptime(expires_str, "%Y-%m-%dT%H:%M:%SZ")
64-
SPEC_VERSION = ".".join(SPECIFICATION_VERSION)
6553
OUT_DIR = "generated_data/ed25519_metadata"
6654
if not os.path.exists(OUT_DIR):
6755
os.mkdir(OUT_DIR)
6856

6957
SERIALIZER = JSONSerializer()
70-
ROLES = {role_name: Role([], 1) for role_name in TOP_LEVEL_ROLE_NAMES}
7158

7259

7360
def verify_generation(md: Metadata, path: str) -> None:
@@ -97,23 +84,15 @@ def generate_all_files(
9784
verify: Whether to verify the newly generated files with the
9885
local staored.
9986
"""
100-
root = Root(1, SPEC_VERSION, EXPIRY, {}, ROLES, True)
101-
root.add_key("root", keys["ed25519_0"])
102-
root.add_key("timestamp", keys["ed25519_1"])
103-
root.add_key("snapshot", keys["ed25519_2"])
104-
root.add_key("targets", keys["ed25519_3"])
105-
106-
md_root: Metadata[Root] = Metadata(root, {})
107-
108-
timestamp = Timestamp(1, SPEC_VERSION, EXPIRY, MetaFile(1))
109-
md_timestamp: Metadata[Timestamp] = Metadata(timestamp, {})
110-
111-
meta: Dict[str, MetaFile] = {"targets.json": MetaFile(1)}
112-
snapshot = Snapshot(1, SPEC_VERSION, EXPIRY, meta)
113-
md_snapshot: Metadata[Snapshot] = Metadata(snapshot, {})
114-
115-
targets = Targets(1, SPEC_VERSION, EXPIRY, {})
116-
md_targets: Metadata[Targets] = Metadata(targets, {})
87+
md_root = Metadata(Root(expires=EXPIRY))
88+
md_timestamp = Metadata(Timestamp(expires=EXPIRY))
89+
md_snapshot = Metadata(Snapshot(expires=EXPIRY))
90+
md_targets = Metadata(Targets(expires=EXPIRY))
91+
92+
md_root.signed.add_key("root", keys["ed25519_0"])
93+
md_root.signed.add_key("timestamp", keys["ed25519_1"])
94+
md_root.signed.add_key("snapshot", keys["ed25519_2"])
95+
md_root.signed.add_key("targets", keys["ed25519_3"])
11796

11897
for i, md in enumerate([md_root, md_timestamp, md_snapshot, md_targets]):
11998
assert isinstance(md, Metadata)

tests/repository_simulator.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
Key,
6666
Metadata,
6767
MetaFile,
68-
Role,
6968
Root,
7069
Snapshot,
7170
TargetFile,
@@ -176,26 +175,16 @@ def rotate_keys(self, role: str) -> None:
176175
def _initialize(self) -> None:
177176
"""Setup a minimal valid repository."""
178177

179-
targets = Targets(1, SPEC_VER, self.safe_expiry, {}, None)
180-
self.md_targets = Metadata(targets, {})
181-
182-
meta = {"targets.json": MetaFile(targets.version)}
183-
snapshot = Snapshot(1, SPEC_VER, self.safe_expiry, meta)
184-
self.md_snapshot = Metadata(snapshot, {})
185-
186-
snapshot_meta = MetaFile(snapshot.version)
187-
timestamp = Timestamp(1, SPEC_VER, self.safe_expiry, snapshot_meta)
188-
self.md_timestamp = Metadata(timestamp, {})
189-
190-
roles = {role_name: Role([], 1) for role_name in TOP_LEVEL_ROLE_NAMES}
191-
root = Root(1, SPEC_VER, self.safe_expiry, {}, roles, True)
178+
self.md_targets = Metadata(Targets(expires=self.safe_expiry))
179+
self.md_snapshot = Metadata(Snapshot(expires=self.safe_expiry))
180+
self.md_timestamp = Metadata(Timestamp(expires=self.safe_expiry))
181+
self.md_root = Metadata(Root(expires=self.safe_expiry))
192182

193183
for role in TOP_LEVEL_ROLE_NAMES:
194184
key, signer = self.create_key()
195-
root.add_key(role, key)
185+
self.md_root.signed.add_key(role, key)
196186
self.add_signer(role, signer)
197187

198-
self.md_root = Metadata(root, {})
199188
self.publish_root()
200189

201190
def publish_root(self) -> None:

0 commit comments

Comments
 (0)