-
Notifications
You must be signed in to change notification settings - Fork 275
/
Copy pathjson.py
104 lines (80 loc) · 3.58 KB
/
json.py
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
# Copyright New York University and the TUF contributors
# SPDX-License-Identifier: MIT OR Apache-2.0
"""``tuf.api.serialization.json`` module provides concrete implementations to
serialize and deserialize TUF role metadata to and from the JSON wireline
format for transportation, and to serialize the 'signed' part of TUF role
metadata to the OLPC Canonical JSON format for signature generation and
verification.
"""
import json
from typing import Optional
from securesystemslib.formats import encode_canonical
# pylint: disable=cyclic-import
# ... to allow de/serializing Metadata and Signed objects here, while also
# creating default de/serializers there (see metadata local scope imports).
# NOTE: A less desirable alternative would be to add more abstraction layers.
from tuf.api.metadata import Metadata, Signed
from tuf.api.serialization import (
DeserializationError,
MetadataDeserializer,
MetadataSerializer,
SerializationError,
SignedSerializer,
)
class JSONDeserializer(MetadataDeserializer):
"""Provides JSON to Metadata deserialize method."""
def deserialize(self, raw_data: bytes) -> Metadata:
"""Deserialize utf-8 encoded JSON bytes into Metadata object."""
try:
json_dict = json.loads(raw_data.decode("utf-8"))
metadata_obj = Metadata.from_dict(json_dict)
except Exception as e:
raise DeserializationError("Failed to deserialize JSON") from e
return metadata_obj
class JSONSerializer(MetadataSerializer):
"""Provides Metadata to JSON serialize method.
Args:
compact: A boolean indicating if the JSON bytes generated in
'serialize' should be compact by excluding whitespace.
validate: Check that the metadata object can be deserialized again
without change of contents and thus find common mistakes.
This validation might slow down serialization significantly.
"""
def __init__(self, compact: bool = False, validate: Optional[bool] = False):
self.compact = compact
self.validate = validate
def serialize(self, metadata_obj: Metadata) -> bytes:
"""Serialize Metadata object into utf-8 encoded JSON bytes."""
try:
indent = None if self.compact else 1
separators = (",", ":") if self.compact else (",", ": ")
json_bytes = json.dumps(
metadata_obj.to_dict(),
indent=indent,
separators=separators,
sort_keys=True,
).encode("utf-8")
if self.validate:
try:
new_md_obj = JSONDeserializer().deserialize(json_bytes)
if metadata_obj != new_md_obj:
raise ValueError(
"Metadata changes if you serialize and deserialize."
)
except Exception as e:
raise ValueError("Metadata cannot be validated!") from e
except Exception as e:
raise SerializationError("Failed to serialize JSON") from e
return json_bytes
class CanonicalJSONSerializer(SignedSerializer):
"""Provides Signed to OLPC Canonical JSON serialize method."""
def serialize(self, signed_obj: Signed) -> bytes:
"""Serialize Signed object into utf-8 encoded OLPC Canonical JSON
bytes.
"""
try:
signed_dict = signed_obj.to_dict()
canonical_bytes = encode_canonical(signed_dict).encode("utf-8")
except Exception as e:
raise SerializationError from e
return canonical_bytes