Skip to content

Commit 67c5298

Browse files
author
Jussi Kukkonen
authored
Merge pull request #1446 from MVrachev/snapshot-property
Metadata API: change meta type in Timestamp
2 parents 234cf64 + bf12e75 commit 67c5298

File tree

6 files changed

+35
-41
lines changed

6 files changed

+35
-41
lines changed

tests/repository_simulator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ def _initialize(self):
100100
snapshot = Snapshot(1, SPEC_VER, expiry, meta)
101101
self.md_snapshot = Metadata(snapshot, OrderedDict())
102102

103-
meta = {"snapshot.json": MetaFile(snapshot.version)}
104-
timestamp = Timestamp(1, SPEC_VER, expiry, meta)
103+
snapshot_meta = MetaFile(snapshot.version)
104+
timestamp = Timestamp(1, SPEC_VER, expiry, snapshot_meta)
105105
self.md_timestamp = Metadata(timestamp, OrderedDict())
106106

107107
root = Root(1, SPEC_VER, expiry, {}, {}, True)
@@ -175,7 +175,7 @@ def _fetch_metadata(self, role: str, version: Optional[int] = None) -> bytes:
175175
return md.to_bytes(JSONSerializer())
176176

177177
def update_timestamp(self):
178-
self.timestamp.meta["snapshot.json"].version = self.snapshot.version
178+
self.timestamp.snapshot_meta.version = self.snapshot.version
179179

180180
self.timestamp.version += 1
181181

tests/test_api.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,11 @@ def test_metadata_timestamp(self):
350350
fileinfo = MetaFile(2, 520, hashes)
351351

352352
self.assertNotEqual(
353-
timestamp.signed.meta['snapshot.json'].to_dict(), fileinfo.to_dict()
353+
timestamp.signed.snapshot_meta.to_dict(), fileinfo.to_dict()
354354
)
355355
timestamp.signed.update(fileinfo)
356356
self.assertEqual(
357-
timestamp.signed.meta['snapshot.json'].to_dict(), fileinfo.to_dict()
357+
timestamp.signed.snapshot_meta.to_dict(), fileinfo.to_dict()
358358
)
359359

360360

@@ -563,7 +563,7 @@ def test_length_and_hash_validation(self):
563563
timestamp_path = os.path.join(
564564
self.repo_dir, 'metadata', 'timestamp.json')
565565
timestamp = Metadata[Timestamp].from_file(timestamp_path)
566-
snapshot_metafile = timestamp.signed.meta["snapshot.json"]
566+
snapshot_metafile = timestamp.signed.snapshot_meta
567567

568568
snapshot_path = os.path.join(
569569
self.repo_dir, 'metadata', 'snapshot.json')

tests/test_trusted_metadata_set.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ def setUpClass(cls):
7070
cls.keystore[role] = SSlibSigner(key_dict)
7171

7272
def hashes_length_modifier(timestamp: Timestamp) -> None:
73-
timestamp.meta["snapshot.json"].hashes = None
74-
timestamp.meta["snapshot.json"].length = None
73+
timestamp.snapshot_meta.hashes = None
74+
timestamp.snapshot_meta.length = None
7575

7676
cls.metadata["timestamp"] = cls.modify_metadata(
7777
cls, "timestamp", hashes_length_modifier
@@ -245,13 +245,13 @@ def version_modifier(timestamp: Timestamp) -> None:
245245

246246
def test_update_timestamp_snapshot_ver_below_current(self):
247247
def bump_snapshot_version(timestamp: Timestamp) -> None:
248-
timestamp.meta["snapshot.json"].version = 2
248+
timestamp.snapshot_meta.version = 2
249249

250250
# set current known snapshot.json version to 2
251251
timestamp = self.modify_metadata("timestamp", bump_snapshot_version)
252252
self.trusted_set.update_timestamp(timestamp)
253253

254-
# newtimestamp.meta["snapshot.json"].version < trusted_timestamp.meta["snapshot.json"].version
254+
# newtimestamp.meta.version < trusted_timestamp.meta.version
255255
with self.assertRaises(exceptions.ReplayedMetadataError):
256256
self.trusted_set.update_timestamp(self.metadata["timestamp"])
257257

@@ -271,7 +271,7 @@ def timestamp_expired_modifier(timestamp: Timestamp) -> None:
271271

272272
def test_update_snapshot_length_or_hash_mismatch(self):
273273
def modify_snapshot_length(timestamp: Timestamp) -> None:
274-
timestamp.meta["snapshot.json"].length = 1
274+
timestamp.snapshot_meta.length = 1
275275

276276
# set known snapshot.json length to 1
277277
timestamp = self.modify_metadata("timestamp", modify_snapshot_length)
@@ -289,7 +289,7 @@ def test_update_snapshot_cannot_verify_snapshot_with_threshold(self):
289289

290290
def test_update_snapshot_version_different_timestamp_snapshot_version(self):
291291
def timestamp_version_modifier(timestamp: Timestamp) -> None:
292-
timestamp.meta["snapshot.json"].version = 2
292+
timestamp.snapshot_meta.version = 2
293293

294294
timestamp = self.modify_metadata("timestamp", timestamp_version_modifier)
295295
self.trusted_set.update_timestamp(timestamp)
@@ -341,7 +341,7 @@ def snapshot_expired_modifier(snapshot: Snapshot) -> None:
341341

342342
def test_update_snapshot_successful_rollback_checks(self):
343343
def meta_version_bump(timestamp: Timestamp) -> None:
344-
timestamp.meta["snapshot.json"].version += 1
344+
timestamp.snapshot_meta.version += 1
345345

346346
def version_bump(snapshot: Snapshot) -> None:
347347
snapshot.version += 1

tuf/api/metadata.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -923,11 +923,11 @@ def verify_length_and_hashes(self, data: Union[bytes, IO[bytes]]) -> None:
923923
class Timestamp(Signed):
924924
"""A container for the signed part of timestamp metadata.
925925
926-
Timestamp contains information about the snapshot Metadata file.
926+
TUF file format uses a dictionary to contain the snapshot information:
927+
this is not the case with Timestamp.snapshot_meta which is a MetaFile.
927928
928929
Attributes:
929-
meta: A dictionary of filenames to MetaFiles. The only valid key value
930-
is the snapshot filename, as defined by the specification.
930+
snapshot_meta: MetaFile instance with the snapshot meta information.
931931
"""
932932

933933
_signed_type = "timestamp"
@@ -937,33 +937,31 @@ def __init__(
937937
version: int,
938938
spec_version: str,
939939
expires: datetime,
940-
meta: Dict[str, MetaFile],
940+
snapshot_meta: MetaFile,
941941
unrecognized_fields: Optional[Mapping[str, Any]] = None,
942942
) -> None:
943943
super().__init__(version, spec_version, expires, unrecognized_fields)
944-
self.meta = meta
944+
self.snapshot_meta = snapshot_meta
945945

946946
@classmethod
947947
def from_dict(cls, signed_dict: Dict[str, Any]) -> "Timestamp":
948948
"""Creates Timestamp object from its dict representation."""
949949
common_args = cls._common_fields_from_dict(signed_dict)
950950
meta_dict = signed_dict.pop("meta")
951-
meta = {"snapshot.json": MetaFile.from_dict(meta_dict["snapshot.json"])}
951+
snapshot_meta = MetaFile.from_dict(meta_dict["snapshot.json"])
952952
# All fields left in the timestamp_dict are unrecognized.
953-
return cls(*common_args, meta, signed_dict)
953+
return cls(*common_args, snapshot_meta, signed_dict)
954954

955955
def to_dict(self) -> Dict[str, Any]:
956956
"""Returns the dict representation of self."""
957957
res_dict = self._common_fields_to_dict()
958-
res_dict["meta"] = {
959-
"snapshot.json": self.meta["snapshot.json"].to_dict()
960-
}
958+
res_dict["meta"] = {"snapshot.json": self.snapshot_meta.to_dict()}
961959
return res_dict
962960

963961
# Modification.
964962
def update(self, snapshot_meta: MetaFile) -> None:
965-
"""Assigns passed info about snapshot metadata to meta dict."""
966-
self.meta["snapshot.json"] = snapshot_meta
963+
"""Assigns passed info about snapshot metadata."""
964+
self.snapshot_meta = snapshot_meta
967965

968966

969967
class Snapshot(Signed):

tuf/ngclient/_internal/trusted_metadata_set.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,13 @@ def update_timestamp(self, data: bytes) -> None:
233233
)
234234
# Prevent rolling back snapshot version
235235
if (
236-
new_timestamp.signed.meta["snapshot.json"].version
237-
< self.timestamp.signed.meta["snapshot.json"].version
236+
new_timestamp.signed.snapshot_meta.version
237+
< self.timestamp.signed.snapshot_meta.version
238238
):
239239
raise exceptions.ReplayedMetadataError(
240240
"snapshot",
241-
new_timestamp.signed.meta["snapshot.json"].version,
242-
self.timestamp.signed.meta["snapshot.json"].version,
241+
new_timestamp.signed.snapshot_meta.version,
242+
self.timestamp.signed.snapshot_meta.version,
243243
)
244244

245245
# expiry not checked to allow old timestamp to be used for rollback
@@ -286,11 +286,11 @@ def update_snapshot(self, data: bytes) -> None:
286286
# Snapshot cannot be loaded if final timestamp is expired
287287
self._check_final_timestamp()
288288

289-
meta = self.timestamp.signed.meta["snapshot.json"]
289+
snapshot_meta = self.timestamp.signed.snapshot_meta
290290

291291
# Verify against the hashes in timestamp, if any
292292
try:
293-
meta.verify_length_and_hashes(data)
293+
snapshot_meta.verify_length_and_hashes(data)
294294
except exceptions.LengthOrHashMismatchError as e:
295295
raise exceptions.RepositoryError(
296296
"Snapshot length or hashes do not match"
@@ -345,14 +345,10 @@ def _check_final_snapshot(self) -> None:
345345
assert self.timestamp is not None # nosec
346346
if self.snapshot.signed.is_expired(self.reference_time):
347347
raise exceptions.ExpiredMetadataError("snapshot.json is expired")
348-
349-
if (
350-
self.snapshot.signed.version
351-
!= self.timestamp.signed.meta["snapshot.json"].version
352-
):
348+
snapshot_meta = self.timestamp.signed.snapshot_meta
349+
if self.snapshot.signed.version != snapshot_meta.version:
353350
raise exceptions.BadVersionNumberError(
354-
f"Expected snapshot version "
355-
f"{self.timestamp.signed.meta['snapshot.json'].version}, "
351+
f"Expected snapshot version {snapshot_meta.version}, "
356352
f"got {self.snapshot.signed.version}"
357353
)
358354

tuf/ngclient/updater.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,11 @@ def _load_snapshot(self) -> None:
352352
logger.debug("Local snapshot not valid as final: %s", e)
353353

354354
assert self._trusted_set.timestamp is not None # nosec
355-
metainfo = self._trusted_set.timestamp.signed.meta["snapshot.json"]
356-
length = metainfo.length or self.config.snapshot_max_length
355+
snapshot_meta = self._trusted_set.timestamp.signed.snapshot_meta
356+
length = snapshot_meta.length or self.config.snapshot_max_length
357357
version = None
358358
if self._trusted_set.root.signed.consistent_snapshot:
359-
version = metainfo.version
359+
version = snapshot_meta.version
360360

361361
data = self._download_metadata("snapshot", length, version)
362362
self._trusted_set.update_snapshot(data)

0 commit comments

Comments
 (0)