From 139bfc0ea98e0358a32ae84a14c033c45b780340 Mon Sep 17 00:00:00 2001 From: Martin Vrachev Date: Wed, 28 Apr 2021 18:52:42 +0300 Subject: [PATCH] BREAKING CHANGE: Make delegations optional According to the spec, delegations in targets are marked as optional: https://theupdateframework.github.io/specification/latest/#file-formats-targets and a pr, clarifying that even more, is approved: https://github.com/theupdateframework/specification/pull/157. This is a possible breaking change. Signed-off-by: Martin Vrachev --- tests/test_api.py | 8 ++++++++ tuf/api/metadata.py | 13 +++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/tests/test_api.py b/tests/test_api.py index f16ee6c15c..3305c447c4 100755 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -445,6 +445,14 @@ def test_metadata_targets(self): # Verify that data is updated self.assertEqual(targets.signed.targets[filename], fileinfo) + # Test from_dict/to_dict Targets without delegations + targets_dict = targets.to_dict() + del targets_dict["signed"]["delegations"] + tmp_dict = targets_dict["signed"].copy() + targets_obj = Targets.from_dict(tmp_dict) + tar_d = targets_obj.to_dict() + self.assertEqual(targets_dict["signed"], targets_obj.to_dict()) + def setup_dict_with_unrecognized_field(self, file_path, field, value): json_dict = {} with open(file_path) as f: diff --git a/tuf/api/metadata.py b/tuf/api/metadata.py index d4fd2ae1bc..ceea676ad7 100644 --- a/tuf/api/metadata.py +++ b/tuf/api/metadata.py @@ -806,7 +806,7 @@ def __init__( spec_version: str, expires: datetime, targets: Mapping[str, Any], - delegations: Mapping[str, Any], + delegations: Optional[Mapping[str, Any]] = None, unrecognized_fields: Optional[Mapping[str, Any]] = None, ) -> None: super().__init__(version, spec_version, expires, unrecognized_fields) @@ -819,19 +819,16 @@ def from_dict(cls, targets_dict: Mapping[str, Any]) -> "Targets": """Creates Targets object from its dict representation.""" common_args = cls._common_fields_from_dict(targets_dict) targets = targets_dict.pop("targets") - delegations = targets_dict.pop("delegations") + delegations = targets_dict.pop("delegations", None) # All fields left in the targets_dict are unrecognized. return cls(*common_args, targets, delegations, targets_dict) def to_dict(self) -> Dict[str, Any]: """Returns the dict representation of self.""" targets_dict = self._common_fields_to_dict() - targets_dict.update( - { - "targets": self.targets, - "delegations": self.delegations, - } - ) + targets_dict["targets"] = self.targets + if self.delegations: + targets_dict["delegations"] = self.delegations return targets_dict # Modification.