9
9
from datetime import datetime , timedelta
10
10
from typing import Any , Dict , Optional
11
11
12
- import json
13
12
import tempfile
14
13
15
14
from securesystemslib .util import persist_temp_file
24
23
import tuf .exceptions
25
24
26
25
27
-
28
26
# Types
29
27
JsonDict = Dict [str , Any ]
30
28
@@ -57,49 +55,6 @@ def __init__(self, signed: 'Signed', signatures: list) -> None:
57
55
self .signatures = signatures
58
56
59
57
60
- # Deserialization (factories).
61
- @classmethod
62
- def from_dict (cls , metadata : JsonDict ) -> 'Metadata' :
63
- """Creates Metadata object from its JSON/dict representation.
64
-
65
- Calls 'from_dict' for any complex metadata attribute represented by a
66
- class also that has a 'from_dict' factory method. (Currently this is
67
- only the signed attribute.)
68
-
69
- Arguments:
70
- metadata: TUF metadata in JSON/dict representation, as e.g.
71
- returned by 'json.loads'.
72
-
73
- Raises:
74
- KeyError: The metadata dict format is invalid.
75
- ValueError: The metadata has an unrecognized signed._type field.
76
-
77
- Returns:
78
- A TUF Metadata object.
79
-
80
- """
81
- # Dispatch to contained metadata class on metadata _type field.
82
- _type = metadata ['signed' ]['_type' ]
83
-
84
- if _type == 'targets' :
85
- inner_cls = Targets
86
- elif _type == 'snapshot' :
87
- inner_cls = Snapshot
88
- elif _type == 'timestamp' :
89
- inner_cls = Timestamp
90
- elif _type == 'root' :
91
- inner_cls = Root
92
- else :
93
- raise ValueError (f'unrecognized metadata type "{ _type } "' )
94
-
95
- # NOTE: If Signature becomes a class, we should iterate over
96
- # metadata['signatures'], call Signature.from_dict for each item, and
97
- # pass a list of Signature objects to the Metadata constructor intead.
98
- return cls (
99
- signed = inner_cls .from_dict (metadata ['signed' ]),
100
- signatures = metadata ['signatures' ])
101
-
102
-
103
58
@classmethod
104
59
def from_file (
105
60
cls , filename : str , deserializer : MetadataDeserializer = None ,
@@ -140,24 +95,6 @@ def from_file(
140
95
return deserializer .deserialize (raw_data )
141
96
142
97
143
- # Serialization.
144
- def to_dict (self ) -> JsonDict :
145
- """Returns the JSON-serializable dictionary representation of self. """
146
- return {
147
- 'signatures' : self .signatures ,
148
- 'signed' : self .signed .to_dict ()
149
- }
150
-
151
-
152
- def to_json (self , compact : bool = False ) -> None :
153
- """Returns the optionally compacted JSON representation of self. """
154
- return json .dumps (
155
- self .to_dict (),
156
- indent = (None if compact else 1 ),
157
- separators = ((',' , ':' ) if compact else (',' , ': ' )),
158
- sort_keys = True )
159
-
160
-
161
98
def to_file (self , filename : str , serializer : MetadataSerializer = None ,
162
99
storage_backend : StorageBackendInterface = None ) -> None :
163
100
"""Writes TUF metadata to file storage.
@@ -313,39 +250,6 @@ def __init__(
313
250
self .version = version
314
251
315
252
316
- # Deserialization (factories).
317
- @classmethod
318
- def from_dict (cls , signed_dict : JsonDict ) -> 'Signed' :
319
- """Creates Signed object from its JSON/dict representation. """
320
-
321
- # Convert 'expires' TUF metadata string to a datetime object, which is
322
- # what the constructor expects and what we store. The inverse operation
323
- # is implemented in 'to_dict'.
324
- signed_dict ['expires' ] = tuf .formats .expiry_string_to_datetime (
325
- signed_dict ['expires' ])
326
- # NOTE: We write the converted 'expires' back into 'signed_dict' above
327
- # so that we can pass it to the constructor as '**signed_dict' below,
328
- # along with other fields that belong to Signed subclasses.
329
- # Any 'from_dict'(-like) conversions of fields that correspond to a
330
- # subclass should be performed in the 'from_dict' method of that
331
- # subclass and also be written back into 'signed_dict' before calling
332
- # super().from_dict.
333
-
334
- # NOTE: cls might be a subclass of Signed, if 'from_dict' was called on
335
- # that subclass (see e.g. Metadata.from_dict).
336
- return cls (** signed_dict )
337
-
338
-
339
- def to_dict (self ) -> JsonDict :
340
- """Returns the JSON-serializable dictionary representation of self. """
341
- return {
342
- '_type' : self ._type ,
343
- 'version' : self .version ,
344
- 'spec_version' : self .spec_version ,
345
- 'expires' : self .expires .isoformat () + 'Z'
346
- }
347
-
348
-
349
253
# Modification.
350
254
def bump_expiration (self , delta : timedelta = timedelta (days = 1 )) -> None :
351
255
"""Increments the expires attribute by the passed timedelta. """
@@ -357,6 +261,7 @@ def bump_version(self) -> None:
357
261
self .version += 1
358
262
359
263
264
+
360
265
class Root (Signed ):
361
266
"""A container for the signed part of root metadata.
362
267
@@ -406,18 +311,6 @@ def __init__(
406
311
self .roles = roles
407
312
408
313
409
- # Serialization.
410
- def to_dict (self ) -> JsonDict :
411
- """Returns the JSON-serializable dictionary representation of self. """
412
- json_dict = super ().to_dict ()
413
- json_dict .update ({
414
- 'consistent_snapshot' : self .consistent_snapshot ,
415
- 'keys' : self .keys ,
416
- 'roles' : self .roles
417
- })
418
- return json_dict
419
-
420
-
421
314
# Update key for a role.
422
315
def add_key (self , role : str , keyid : str , key_metadata : JsonDict ) -> None :
423
316
"""Adds new key for 'role' and updates the key store. """
@@ -439,7 +332,6 @@ def remove_key(self, role: str, keyid: str) -> None:
439
332
440
333
441
334
442
-
443
335
class Timestamp (Signed ):
444
336
"""A container for the signed part of timestamp metadata.
445
337
@@ -467,16 +359,6 @@ def __init__(
467
359
self .meta = meta
468
360
469
361
470
- # Serialization.
471
- def to_dict (self ) -> JsonDict :
472
- """Returns the JSON-serializable dictionary representation of self. """
473
- json_dict = super ().to_dict ()
474
- json_dict .update ({
475
- 'meta' : self .meta
476
- })
477
- return json_dict
478
-
479
-
480
362
# Modification.
481
363
def update (self , version : int , length : int , hashes : JsonDict ) -> None :
482
364
"""Assigns passed info about snapshot metadata to meta dict. """
@@ -487,6 +369,7 @@ def update(self, version: int, length: int, hashes: JsonDict) -> None:
487
369
}
488
370
489
371
372
+
490
373
class Snapshot (Signed ):
491
374
"""A container for the signed part of snapshot metadata.
492
375
@@ -520,15 +403,6 @@ def __init__(
520
403
# TODO: Add class for meta
521
404
self .meta = meta
522
405
523
- # Serialization.
524
- def to_dict (self ) -> JsonDict :
525
- """Returns the JSON-serializable dictionary representation of self. """
526
- json_dict = super ().to_dict ()
527
- json_dict .update ({
528
- 'meta' : self .meta
529
- })
530
- return json_dict
531
-
532
406
533
407
# Modification.
534
408
def update (
@@ -545,6 +419,7 @@ def update(
545
419
self .meta [metadata_fn ]['hashes' ] = hashes
546
420
547
421
422
+
548
423
class Targets (Signed ):
549
424
"""A container for the signed part of targets metadata.
550
425
@@ -612,16 +487,6 @@ def __init__(
612
487
self .delegations = delegations
613
488
614
489
615
- # Serialization.
616
- def to_dict (self ) -> JsonDict :
617
- """Returns the JSON-serializable dictionary representation of self. """
618
- json_dict = super ().to_dict ()
619
- json_dict .update ({
620
- 'targets' : self .targets ,
621
- 'delegations' : self .delegations ,
622
- })
623
- return json_dict
624
-
625
490
# Modification.
626
491
def update (self , filename : str , fileinfo : JsonDict ) -> None :
627
492
"""Assigns passed target file info to meta dict. """
0 commit comments