Remove Signed.read_from_json metadata method

Remove metadata factory on Signed class, for the sake of API
simplicity/non-ambiguity, i.e. it's enough to have one
way of loading any Metadata, that is:
Metadata.read_from_json

Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
This commit is contained in:
Lukas Puehringer 2020-09-02 17:23:37 +02:00
parent 21de660b66
commit e61ae1bea3
2 changed files with 5 additions and 53 deletions

View file

@ -91,15 +91,10 @@ def test_generic_read(self):
path = os.path.join(self.repo_dir, 'metadata', metadata + '.json')
metadata_obj = Metadata.read_from_json(path)
# Assert that generic method ...
# ... instantiates the right inner class for each metadata type
# Assert that generic method instantiates the right inner class for
# each metadata type
self.assertTrue(
isinstance(metadata_obj.signed, inner_metadata_cls))
# ... and reads the same metadata file as the corresponding method
# on the inner class would do (compare their dict representation)
self.assertDictEqual(
metadata_obj.as_dict(),
inner_metadata_cls.read_from_json(path).as_dict())
# Assert that it chokes correctly on an unknown metadata type
bad_metadata_path = 'bad-metadata.json'
@ -185,7 +180,7 @@ def test_metadata_base(self):
# with real data
snapshot_path = os.path.join(
self.repo_dir, 'metadata', 'snapshot.json')
md = Snapshot.read_from_json(snapshot_path)
md = Metadata.read_from_json(snapshot_path)
self.assertEqual(md.signed.version, 1)
md.signed.bump_version()
@ -200,7 +195,7 @@ def test_metadata_base(self):
def test_metadata_snapshot(self):
snapshot_path = os.path.join(
self.repo_dir, 'metadata', 'snapshot.json')
snapshot = Snapshot.read_from_json(snapshot_path)
snapshot = Metadata.read_from_json(snapshot_path)
# Create a dict representing what we expect the updated data to be
fileinfo = snapshot.signed.meta
@ -216,7 +211,7 @@ def test_metadata_snapshot(self):
def test_metadata_timestamp(self):
timestamp_path = os.path.join(
self.repo_dir, 'metadata', 'timestamp.json')
timestamp = Timestamp.read_from_json(timestamp_path)
timestamp = Metadata.read_from_json(timestamp_path)
self.assertEqual(timestamp.signed.version, 1)
timestamp.signed.bump_version()

View file

@ -32,9 +32,6 @@
from securesystemslib.util import load_json_file, persist_temp_file
from securesystemslib.storage import StorageBackendInterface
from securesystemslib.keys import create_signature, verify_signature
from tuf.repository_lib import (
_strip_version_number
)
import iso8601
import tuf.formats
@ -288,46 +285,6 @@ def as_dict(self) -> JsonDict:
'expires': self.expires
}
@classmethod
def read_from_json(
cls, filename: str,
storage_backend: Optional[StorageBackendInterface] = None
) -> Metadata:
signable = load_json_file(filename, storage_backend)
"""Loads corresponding JSON-formatted metadata from file storage.
Arguments:
filename: The path to read the file from.
storage_backend: An object that implements
securesystemslib.storage.StorageBackendInterface. Per default
a (local) FilesystemBackend is used.
Raises:
securesystemslib.exceptions.StorageError: The file cannot be read.
securesystemslib.exceptions.Error, ValueError: The metadata cannot
be parsed.
Returns:
A TUF Metadata object whose signed attribute contains an object
of this class.
"""
# FIXME: It feels dirty to access signable["signed"]["version"] here in
# order to do this check, and also a bit random (there are likely other
# things to check), but later we don't have the filename anymore. If we
# want to stick to the check, which seems reasonable, we should maybe
# think of a better place.
_, fn_prefix = _strip_version_number(filename, True)
if fn_prefix and fn_prefix != signable['signed']['version']:
raise ValueError(
f'version filename prefix ({fn_prefix}) must align with '
f'version in metadata ({signable["signed"]["version"]}).')
return Metadata(
signed=cls(**signable['signed']),
signatures=signable['signatures'])
class Timestamp(Signed):
"""A container for the signed part of timestamp metadata.