-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathmetadata.py
More file actions
418 lines (331 loc) · 14.5 KB
/
metadata.py
File metadata and controls
418 lines (331 loc) · 14.5 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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# Copyright New York University and the TUF contributors
# SPDX-License-Identifier: MIT OR Apache-2.0
"""The low-level Metadata API.
The low-level Metadata API in ``tuf.api.metadata`` module contains:
* Safe de/serialization of metadata to and from files.
* Access to and modification of signed metadata content.
* Signing metadata and verifying signatures.
Metadata API implements functionality at the metadata file level, it does
not provide TUF repository or client functionality on its own (but can be used
to implement them).
The API design is based on the file format defined in the `TUF specification
<https://theupdateframework.github.io/specification/latest/>`_ and the object
attributes generally follow the JSON format used in the specification.
The above principle means that a ``Metadata`` object represents a single
metadata file, and has a ``signed`` attribute that is an instance of one of the
four top level signed classes (``Root``, ``Timestamp``, ``Snapshot`` and
``Targets``). To make Python type annotations useful ``Metadata`` can be
type constrained: e.g. the signed attribute of ``Metadata[Root]``
is known to be ``Root``.
Currently Metadata API supports JSON as the file format.
A basic example of repository implementation using the Metadata is available in
`examples/repository <https://github.com/theupdateframework/python-tuf/tree/develop/examples/repository>`_.
"""
from __future__ import annotations
import logging
import tempfile
from typing import TYPE_CHECKING, Any, Generic, cast
from securesystemslib.signer import Signature, Signer
from securesystemslib.storage import FilesystemBackend, StorageBackendInterface
# Expose payload classes via ``tuf.api.metadata`` to maintain the API,
# even if they are unused in the local scope.
from tuf.api._payload import ( # noqa: F401
_ROOT,
_SNAPSHOT,
_TARGETS,
_TIMESTAMP,
SPECIFICATION_VERSION,
TOP_LEVEL_ROLE_NAMES,
BaseFile,
DelegatedRole,
Delegations,
Key,
LengthOrHashMismatchError,
MetaFile,
Role,
Root,
RootVerificationResult,
Signed,
Snapshot,
SuccinctRoles,
T,
TargetFile,
Targets,
Timestamp,
VerificationResult,
)
from tuf.api.exceptions import UnsignedMetadataError
if TYPE_CHECKING:
from tuf.api.serialization import (
MetadataDeserializer,
MetadataSerializer,
SignedSerializer,
)
logger = logging.getLogger(__name__)
class Metadata(Generic[T]):
"""A container for signed TUF metadata.
Provides methods to convert to and from dictionary, read and write to and
from file and to create and verify metadata signatures.
``Metadata[T]`` is a generic container type where T can be any one type of
[``Root``, ``Timestamp``, ``Snapshot``, ``Targets``]. The purpose of this
is to allow static type checking of the signed attribute in code using
Metadata::
root_md = Metadata[Root].from_file("root.json")
# root_md type is now Metadata[Root]. This means signed and its
# attributes like consistent_snapshot are now statically typed and the
# types can be verified by static type checkers and shown by IDEs
print(root_md.signed.consistent_snapshot)
Using a type constraint is not required but not doing so means T is not a
specific type so static typing cannot happen. Note that the type constraint
``[Root]`` is not validated at runtime (as pure annotations are not
available then).
New Metadata instances can be created from scratch with::
one_day = datetime.now(timezone.utc) + timedelta(days=1)
timestamp = Metadata(Timestamp(expires=one_day))
Apart from ``expires`` all of the arguments to the inner constructors have
reasonable default values for new metadata.
*All parameters named below are not just constructor arguments but also
instance attributes.*
Args:
signed: Actual metadata payload, i.e. one of ``Targets``,
``Snapshot``, ``Timestamp`` or ``Root``.
signatures: Ordered dictionary of keyids to ``Signature`` objects, each
signing the canonical serialized representation of ``signed``.
Default is an empty dictionary.
unrecognized_fields: Dictionary of all attributes that are not managed
by TUF Metadata API. These fields are NOT signed and it's preferable
if unrecognized fields are added to the Signed derivative classes.
"""
def __init__(
self,
signed: T,
signatures: dict[str, Signature] | None = None,
unrecognized_fields: dict[str, Any] | None = None,
):
self.signed: T = signed
self.signatures = signatures if signatures is not None else {}
if unrecognized_fields is None:
unrecognized_fields = {}
self.unrecognized_fields = unrecognized_fields
def __eq__(self, other: object) -> bool:
if not isinstance(other, Metadata):
return False
return (
self.signatures == other.signatures
# Order of the signatures matters (see issue #1788).
and list(self.signatures.items()) == list(other.signatures.items())
and self.signed == other.signed
and self.unrecognized_fields == other.unrecognized_fields
)
def __hash__(self) -> int:
return hash((self.signatures, self.signed, self.unrecognized_fields))
@property
def signed_bytes(self) -> bytes:
"""Default canonical json byte representation of ``self.signed``."""
# Use local scope import to avoid circular import errors
from tuf.api.serialization.json import CanonicalJSONSerializer # noqa: I001, PLC0415
return CanonicalJSONSerializer().serialize(self.signed)
@classmethod
def from_dict(cls, metadata: dict[str, Any]) -> Metadata[T]:
"""Create ``Metadata`` object from its json/dict representation.
Args:
metadata: TUF metadata in dict representation.
Raises:
ValueError, KeyError, TypeError: Invalid arguments.
Side Effect:
Destroys the metadata dict passed by reference.
Returns:
TUF ``Metadata`` object.
"""
# Dispatch to contained metadata class on metadata _type field.
_type = metadata["signed"]["_type"]
if _type == _TARGETS:
inner_cls: type[Signed] = Targets
elif _type == _SNAPSHOT:
inner_cls = Snapshot
elif _type == _TIMESTAMP:
inner_cls = Timestamp
elif _type == _ROOT:
inner_cls = Root
else:
raise ValueError(f'unrecognized metadata type "{_type}"')
# Make sure signatures are unique
signatures: dict[str, Signature] = {}
for sig_dict in metadata.pop("signatures"):
sig = Signature.from_dict(sig_dict)
if sig.keyid in signatures:
raise ValueError(
f"Multiple signatures found for keyid {sig.keyid}"
)
signatures[sig.keyid] = sig
return cls(
# Specific type T is not known at static type check time: use cast
signed=cast("T", inner_cls.from_dict(metadata.pop("signed"))),
signatures=signatures,
# All fields left in the metadata dict are unrecognized.
unrecognized_fields=metadata,
)
@classmethod
def from_file(
cls,
filename: str,
deserializer: MetadataDeserializer | None = None,
storage_backend: StorageBackendInterface | None = None,
) -> Metadata[T]:
"""Load TUF metadata from file storage.
Args:
filename: Path to read the file from.
deserializer: ``MetadataDeserializer`` subclass instance that
implements the desired wireline format deserialization. Per
default a ``JSONDeserializer`` is used.
storage_backend: Object that implements
``securesystemslib.storage.StorageBackendInterface``.
Default is ``FilesystemBackend`` (i.e. a local file).
Raises:
StorageError: The file cannot be read.
tuf.api.serialization.DeserializationError:
The file cannot be deserialized.
Returns:
TUF ``Metadata`` object.
"""
if storage_backend is None:
storage_backend = FilesystemBackend()
with storage_backend.get(filename) as file_obj:
return cls.from_bytes(file_obj.read(), deserializer)
@classmethod
def from_bytes(
cls,
data: bytes,
deserializer: MetadataDeserializer | None = None,
) -> Metadata[T]:
"""Load TUF metadata from raw data.
Args:
data: Metadata content.
deserializer: ``MetadataDeserializer`` implementation to use.
Default is ``JSONDeserializer``.
Raises:
tuf.api.serialization.DeserializationError:
The file cannot be deserialized.
Returns:
TUF ``Metadata`` object.
"""
if deserializer is None:
# Use local scope import to avoid circular import errors
from tuf.api.serialization.json import JSONDeserializer # noqa: I001, PLC0415
deserializer = JSONDeserializer()
return deserializer.deserialize(data)
def to_bytes(self, serializer: MetadataSerializer | None = None) -> bytes:
"""Return the serialized TUF file format as bytes.
Note that if bytes are first deserialized into ``Metadata`` and then
serialized with ``to_bytes()``, the two are not required to be
identical even though the signatures are guaranteed to stay valid. If
byte-for-byte equivalence is required (which is the case when content
hashes are used in other metadata), the original content should be used
instead of re-serializing.
Args:
serializer: ``MetadataSerializer`` instance that implements the
desired serialization format. Default is ``JSONSerializer``.
Raises:
tuf.api.serialization.SerializationError:
The metadata object cannot be serialized.
"""
if serializer is None:
# Use local scope import to avoid circular import errors
from tuf.api.serialization.json import JSONSerializer # noqa: I001, PLC0415
serializer = JSONSerializer(compact=True)
return serializer.serialize(self)
def to_dict(self) -> dict[str, Any]:
"""Return the dict representation of self."""
signatures = [sig.to_dict() for sig in self.signatures.values()]
return {
"signatures": signatures,
"signed": self.signed.to_dict(),
**self.unrecognized_fields,
}
def to_file(
self,
filename: str,
serializer: MetadataSerializer | None = None,
storage_backend: StorageBackendInterface | None = None,
) -> None:
"""Write TUF metadata to file storage.
Note that if a file is first deserialized into ``Metadata`` and then
serialized with ``to_file()``, the two files are not required to be
identical even though the signatures are guaranteed to stay valid. If
byte-for-byte equivalence is required (which is the case when file
hashes are used in other metadata), the original file should be used
instead of re-serializing.
Args:
filename: Path to write the file to.
serializer: ``MetadataSerializer`` instance that implements the
desired serialization format. Default is ``JSONSerializer``.
storage_backend: ``StorageBackendInterface`` implementation. Default
is ``FilesystemBackend`` (i.e. a local file).
Raises:
tuf.api.serialization.SerializationError:
The metadata object cannot be serialized.
StorageError: The file cannot be written.
"""
if storage_backend is None:
storage_backend = FilesystemBackend()
bytes_data = self.to_bytes(serializer)
with tempfile.TemporaryFile() as temp_file:
temp_file.write(bytes_data)
storage_backend.put(temp_file, filename)
# Signatures.
def sign(
self,
signer: Signer,
append: bool = False,
signed_serializer: SignedSerializer | None = None,
) -> Signature:
"""Create signature over ``signed`` and assigns it to ``signatures``.
Args:
signer: A ``securesystemslib.signer.Signer`` object that provides a
signing implementation to generate the signature.
append: ``True`` if the signature should be appended to
the list of signatures or replace any existing signatures. The
default behavior is to replace signatures.
signed_serializer: ``SignedSerializer`` that implements the desired
serialization format. Default is ``CanonicalJSONSerializer``.
Raises:
tuf.api.serialization.SerializationError:
``signed`` cannot be serialized.
UnsignedMetadataError: Signing errors.
Returns:
``securesystemslib.signer.Signature`` object that was added into
signatures.
"""
if signed_serializer is None:
bytes_data = self.signed_bytes
else:
bytes_data = signed_serializer.serialize(self.signed)
try:
signature = signer.sign(bytes_data)
except Exception as e:
raise UnsignedMetadataError(f"Failed to sign: {e}") from e
if not append:
self.signatures.clear()
self.signatures[signature.keyid] = signature
return signature
def verify_delegate(
self,
delegated_role: str,
delegated_metadata: Metadata,
signed_serializer: SignedSerializer | None = None,
) -> None:
"""Verify that ``delegated_metadata`` is signed with the required
threshold of keys for ``delegated_role``.
.. deprecated:: 3.1.0
Please use ``Root.verify_delegate()`` or
``Targets.verify_delegate()``.
"""
if self.signed.type not in ["root", "targets"]:
raise TypeError("Call is valid only on delegator metadata")
if signed_serializer is None:
payload = delegated_metadata.signed_bytes
else:
payload = signed_serializer.serialize(delegated_metadata.signed)
self.signed.verify_delegate(
delegated_role, payload, delegated_metadata.signatures
)