forked from theupdateframework/python-tuf
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_trusted_metadata_set.py
More file actions
402 lines (316 loc) · 16.4 KB
/
test_trusted_metadata_set.py
File metadata and controls
402 lines (316 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
import logging
from typing import Optional, Union, Callable
import os
import sys
import unittest
from datetime import datetime
from tuf import exceptions
from tuf.api.metadata import (
Metadata,
Signed,
Root,
Timestamp,
Snapshot,
MetaFile,
Targets
)
from tuf.ngclient._internal.trusted_metadata_set import TrustedMetadataSet
from securesystemslib.signer import SSlibSigner
from securesystemslib.interface import(
import_ed25519_privatekey_from_file,
import_rsa_privatekey_from_file
)
from tests import utils
logger = logging.getLogger(__name__)
class TestTrustedMetadataSet(unittest.TestCase):
def modify_metadata(
self, rolename: str, modification_func: Callable[["Signed"], None]
) -> bytes:
"""Instantiate metadata from rolename type, call modification_func and
sign it again with self.keystore[rolename] signer.
Attributes:
rolename: A denoting the name of the metadata which will be modified.
modification_func: Function that will be called to modify the signed
portion of metadata bytes.
"""
metadata = Metadata.from_bytes(self.metadata[rolename])
modification_func(metadata.signed)
metadata.sign(self.keystore[rolename])
return metadata.to_bytes()
@classmethod
def setUpClass(cls):
cls.repo_dir = os.path.join(
os.getcwd(), 'repository_data', 'repository', 'metadata'
)
cls.metadata = {}
for md in ["root", "timestamp", "snapshot", "targets", "role1", "role2"]:
with open(os.path.join(cls.repo_dir, f"{md}.json"), "rb") as f:
cls.metadata[md] = f.read()
keystore_dir = os.path.join(os.getcwd(), 'repository_data', 'keystore')
cls.keystore = {}
root_key_dict = import_rsa_privatekey_from_file(
os.path.join(keystore_dir, "root" + '_key'),
password="password"
)
cls.keystore["root"] = SSlibSigner(root_key_dict)
for role in ["delegation", "snapshot", "targets", "timestamp"]:
key_dict = import_ed25519_privatekey_from_file(
os.path.join(keystore_dir, role + '_key'),
password="password"
)
cls.keystore[role] = SSlibSigner(key_dict)
def hashes_length_modifier(timestamp: Timestamp) -> None:
timestamp.meta["snapshot.json"].hashes = None
timestamp.meta["snapshot.json"].length = None
cls.metadata["timestamp"] = cls.modify_metadata(
cls, "timestamp", hashes_length_modifier
)
def setUp(self) -> None:
self.trusted_set = TrustedMetadataSet(self.metadata["root"])
def _update_all_besides_targets(
self,
timestamp_bytes: Optional[bytes] = None,
snapshot_bytes: Optional[bytes] = None,
):
"""Update all metadata roles besides targets.
Args:
timestamp_bytes:
Bytes used when calling trusted_set.update_timestamp().
Default self.metadata["timestamp"].
snapshot_bytes:
Bytes used when calling trusted_set.update_snapshot().
Default self.metadata["snapshot"].
"""
timestamp_bytes = timestamp_bytes or self.metadata["timestamp"]
self.trusted_set.update_timestamp(timestamp_bytes)
snapshot_bytes = snapshot_bytes or self.metadata["snapshot"]
self.trusted_set.update_snapshot(snapshot_bytes)
def test_update(self):
self.trusted_set.update_timestamp(self.metadata["timestamp"])
self.trusted_set.update_snapshot(self.metadata["snapshot"])
self.trusted_set.update_targets(self.metadata["targets"])
self.trusted_set.update_delegated_targets(
self.metadata["role1"], "role1", "targets"
)
self.trusted_set.update_delegated_targets(
self.metadata["role2"], "role2", "role1"
)
# the 4 top level metadata objects + 2 additional delegated targets
self.assertTrue(len(self.trusted_set), 6)
count = 0
for md in self.trusted_set:
self.assertIsInstance(md, Metadata)
count += 1
self.assertTrue(count, 6)
def test_out_of_order_ops(self):
# Update snapshot before timestamp
with self.assertRaises(RuntimeError):
self.trusted_set.update_snapshot(self.metadata["snapshot"])
self.trusted_set.update_timestamp(self.metadata["timestamp"])
# Update root after timestamp
with self.assertRaises(RuntimeError):
self.trusted_set.update_root(self.metadata["root"])
# Update targets before snapshot
with self.assertRaises(RuntimeError):
self.trusted_set.update_targets(self.metadata["targets"])
self.trusted_set.update_snapshot(self.metadata["snapshot"])
# update timestamp after snapshot
with self.assertRaises(RuntimeError):
self.trusted_set.update_timestamp(self.metadata["timestamp"])
# Update delegated targets before targets
with self.assertRaises(RuntimeError):
self.trusted_set.update_delegated_targets(
self.metadata["role1"], "role1", "targets"
)
self.trusted_set.update_targets(self.metadata["targets"])
# Update snapshot after sucessful targets update
with self.assertRaises(RuntimeError):
self.trusted_set.update_snapshot(self.metadata["snapshot"])
self.trusted_set.update_delegated_targets(
self.metadata["role1"], "role1", "targets"
)
def test_update_with_invalid_json(self):
# root.json not a json file at all
with self.assertRaises(exceptions.RepositoryError):
TrustedMetadataSet(b"")
# root.json is invalid
root = Metadata.from_bytes(self.metadata["root"])
root.signed.version += 1
with self.assertRaises(exceptions.RepositoryError):
TrustedMetadataSet(root.to_bytes())
# update_root called with the wrong metadata type
with self.assertRaises(exceptions.RepositoryError):
self.trusted_set.update_root(self.metadata["snapshot"])
top_level_md = [
(self.metadata["timestamp"], self.trusted_set.update_timestamp),
(self.metadata["snapshot"], self.trusted_set.update_snapshot),
(self.metadata["targets"], self.trusted_set.update_targets),
]
for metadata, update_func in top_level_md:
md = Metadata.from_bytes(metadata)
# metadata is not json
with self.assertRaises(exceptions.RepositoryError):
update_func(b"")
# metadata is invalid
md.signed.version += 1
with self.assertRaises(exceptions.RepositoryError):
update_func(md.to_bytes())
# metadata is of wrong type
with self.assertRaises(exceptions.RepositoryError):
update_func(self.metadata["root"])
update_func(metadata)
def test_update_root_new_root(self):
# test that root can be updated with a new valid version
def root_new_version_modifier(root: Root) -> None:
root.version += 1
root = self.modify_metadata("root", root_new_version_modifier)
self.trusted_set.update_root(root)
def test_update_root_new_root_cannot_be_verified_with_threshold(self):
# new_root data with threshold which cannot be verified.
root = Metadata.from_bytes(self.metadata["root"])
# remove root role keyids representing root signatures
root.signed.roles["root"].keyids = []
with self.assertRaises(exceptions.UnsignedMetadataError):
self.trusted_set.update_root(root.to_bytes())
def test_update_root_new_root_ver_same_as_trusted_root_ver(self):
with self.assertRaises(exceptions.ReplayedMetadataError):
self.trusted_set.update_root(self.metadata["root"])
def test_root_expired_final_root(self):
def root_expired_modifier(root: Root) -> None:
root.expires = datetime(1970, 1, 1)
# intermediate root can be expired
root = self.modify_metadata("root", root_expired_modifier)
tmp_trusted_set = TrustedMetadataSet(root)
# update timestamp to trigger final root expiry check
with self.assertRaises(exceptions.ExpiredMetadataError):
tmp_trusted_set.update_timestamp(self.metadata["timestamp"])
def test_update_timestamp_new_timestamp_ver_below_trusted_ver(self):
# new_timestamp.version < trusted_timestamp.version
def version_modifier(timestamp: Timestamp) -> None:
timestamp.version = 3
timestamp = self.modify_metadata("timestamp", version_modifier)
self.trusted_set.update_timestamp(timestamp)
with self.assertRaises(exceptions.ReplayedMetadataError):
self.trusted_set.update_timestamp(self.metadata["timestamp"])
def test_update_timestamp_snapshot_ver_below_current(self):
def bump_snapshot_version(timestamp: Timestamp) -> None:
timestamp.meta["snapshot.json"].version = 2
# set current known snapshot.json version to 2
timestamp = self.modify_metadata("timestamp", bump_snapshot_version)
self.trusted_set.update_timestamp(timestamp)
# newtimestamp.meta["snapshot.json"].version < trusted_timestamp.meta["snapshot.json"].version
with self.assertRaises(exceptions.ReplayedMetadataError):
self.trusted_set.update_timestamp(self.metadata["timestamp"])
def test_update_timestamp_expired(self):
# new_timestamp has expired
def timestamp_expired_modifier(timestamp: Timestamp) -> None:
timestamp.expires = datetime(1970, 1, 1)
# intermediate timestamp is allowed to be expired
timestamp = self.modify_metadata("timestamp", timestamp_expired_modifier)
self.trusted_set.update_timestamp(timestamp)
# update snapshot to trigger final timestamp expiry check
with self.assertRaises(exceptions.ExpiredMetadataError):
self.trusted_set.update_snapshot(self.metadata["snapshot"])
def test_update_snapshot_length_or_hash_mismatch(self):
def modify_snapshot_length(timestamp: Timestamp) -> None:
timestamp.meta["snapshot.json"].length = 1
# set known snapshot.json length to 1
timestamp = self.modify_metadata("timestamp", modify_snapshot_length)
self.trusted_set.update_timestamp(timestamp)
with self.assertRaises(exceptions.RepositoryError):
self.trusted_set.update_snapshot(self.metadata["snapshot"])
def test_update_snapshot_cannot_verify_snapshot_with_threshold(self):
self.trusted_set.update_timestamp(self.metadata["timestamp"])
snapshot = Metadata.from_bytes(self.metadata["snapshot"])
snapshot.signatures.clear()
with self.assertRaises(exceptions.UnsignedMetadataError):
self.trusted_set.update_snapshot(snapshot.to_bytes())
def test_update_snapshot_version_different_timestamp_snapshot_version(self):
def timestamp_version_modifier(timestamp: Timestamp) -> None:
timestamp.meta["snapshot.json"].version = 2
timestamp = self.modify_metadata("timestamp", timestamp_version_modifier)
self.trusted_set.update_timestamp(timestamp)
#intermediate snapshot is allowed to not match meta version
self.trusted_set.update_snapshot(self.metadata["snapshot"])
# final snapshot must match meta version
with self.assertRaises(exceptions.BadVersionNumberError):
self.trusted_set.update_targets(self.metadata["targets"])
def test_update_snapshot_file_removed_from_meta(self):
self._update_all_besides_targets(self.metadata["timestamp"])
def remove_file_from_meta(snapshot: Snapshot) -> None:
del snapshot.meta["targets.json"]
# Test removing a meta_file in new_snapshot compared to the old snapshot
snapshot = self.modify_metadata("snapshot", remove_file_from_meta)
with self.assertRaises(exceptions.RepositoryError):
self.trusted_set.update_snapshot(snapshot)
def test_update_snapshot_meta_version_decreases(self):
self.trusted_set.update_timestamp(self.metadata["timestamp"])
def version_meta_modifier(snapshot: Snapshot) -> None:
snapshot.meta["targets.json"].version += 1
snapshot = self.modify_metadata("snapshot", version_meta_modifier)
self.trusted_set.update_snapshot(snapshot)
with self.assertRaises(exceptions.BadVersionNumberError):
self.trusted_set.update_snapshot(self.metadata["snapshot"])
def test_update_snapshot_expired_new_snapshot(self):
self.trusted_set.update_timestamp(self.metadata["timestamp"])
def snapshot_expired_modifier(snapshot: Snapshot) -> None:
snapshot.expires = datetime(1970, 1, 1)
# intermediate snapshot is allowed to be expired
snapshot = self.modify_metadata("snapshot", snapshot_expired_modifier)
self.trusted_set.update_snapshot(snapshot)
# update targets to trigger final snapshot expiry check
with self.assertRaises(exceptions.ExpiredMetadataError):
self.trusted_set.update_targets(self.metadata["targets"])
def test_update_snapshot_successful_rollback_checks(self):
def meta_version_bump(timestamp: Timestamp) -> None:
timestamp.meta["snapshot.json"].version += 1
def version_bump(snapshot: Snapshot) -> None:
snapshot.version += 1
# load a "local" timestamp, then update to newer one:
self.trusted_set.update_timestamp(self.metadata["timestamp"])
new_timestamp = self.modify_metadata("timestamp", meta_version_bump)
self.trusted_set.update_timestamp(new_timestamp)
# load a "local" snapshot, then update to newer one:
self.trusted_set.update_snapshot(self.metadata["snapshot"])
new_snapshot = self.modify_metadata("snapshot", version_bump)
self.trusted_set.update_snapshot(new_snapshot)
# update targets to trigger final snapshot meta version check
self.trusted_set.update_targets(self.metadata["targets"])
def test_update_targets_no_meta_in_snapshot(self):
def no_meta_modifier(snapshot: Snapshot) -> None:
snapshot.meta = {}
snapshot = self.modify_metadata("snapshot", no_meta_modifier)
self._update_all_besides_targets(self.metadata["timestamp"], snapshot)
# remove meta information with information about targets from snapshot
with self.assertRaises(exceptions.RepositoryError):
self.trusted_set.update_targets(self.metadata["targets"])
def test_update_targets_hash_different_than_snapshot_meta_hash(self):
def meta_length_modifier(snapshot: Snapshot) -> None:
for metafile_path in snapshot.meta:
snapshot.meta[metafile_path] = MetaFile(version=1, length=1)
snapshot = self.modify_metadata("snapshot", meta_length_modifier)
self._update_all_besides_targets(self.metadata["timestamp"], snapshot)
# observed_hash != stored hash in snapshot meta for targets
with self.assertRaises(exceptions.RepositoryError):
self.trusted_set.update_targets(self.metadata["targets"])
def test_update_targets_version_different_snapshot_meta_version(self):
def meta_modifier(snapshot: Snapshot) -> None:
for metafile_path in snapshot.meta:
snapshot.meta[metafile_path] = MetaFile(version=2)
snapshot = self.modify_metadata("snapshot", meta_modifier)
self._update_all_besides_targets(self.metadata["timestamp"], snapshot)
# new_delegate.signed.version != meta.version stored in snapshot
with self.assertRaises(exceptions.BadVersionNumberError):
self.trusted_set.update_targets(self.metadata["targets"])
def test_update_targets_expired_new_target(self):
self._update_all_besides_targets()
# new_delegated_target has expired
def target_expired_modifier(target: Targets) -> None:
target.expires = datetime(1970, 1, 1)
targets = self.modify_metadata("targets", target_expired_modifier)
with self.assertRaises(exceptions.ExpiredMetadataError):
self.trusted_set.update_targets(targets)
# TODO test updating over initial metadata (new keys, newer timestamp, etc)
if __name__ == '__main__':
utils.configure_test_logging(sys.argv)
unittest.main()