Skip to content

Commit 0a9fc93

Browse files
committed
Address review comments in metadata.py (WIP)
- Fix typos - Add todos and comment - Add missing type hint - Remove unused import - Remove 3rd-party iso8601 import and use datetime instead. TODO: Squash with previous commits after reivew Signed-off-by: Lukas Puehringer <[email protected]>
1 parent 88e64a1 commit 0a9fc93

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

tests/test_api.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
# SPDX-License-Identifier: MIT OR Apache-2.0
55
""" Unit tests for api/metadata.py
66
7-
Skipped on Python < 3.6.
8-
97
"""
108

119
import json
@@ -19,6 +17,7 @@
1917
from datetime import datetime, timedelta
2018
from dateutil.relativedelta import relativedelta
2119

20+
# TODO: Remove case handling when fully dropping support for versions >= 3.6
2221
IS_PY_VERSION_SUPPORTED = sys.version_info >= (3, 6)
2322

2423
# Use setUpModule to tell unittest runner to skip this test module gracefully.

tuf/api/metadata.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@
1616
disable check as there might be a justified reason to write WIP
1717
metadata to json.
1818
19+
- consider using in-toto style ValidationMixin, e.g.:
20+
https://github.com/in-toto/in-toto/blob/74da7a/in_toto/models/common.py#L27-L40
21+
https://github.com/in-toto/in-toto/blob/74da7a/in_toto/models/layout.py#L420-L438
22+
1923
* Add Root metadata class
2024
2125
* Add classes for other complex metadata attributes, see 'signatures' (in
2226
Metadata) 'meta'/'targets' (in Timestamp, Snapshot, Targets), 'delegations'
2327
(in Targets), 'keys'/'roles' (in not yet existent 'Delegation'), ...
2428
29+
* Ticketize this todo list (on GitHub)
30+
2531
"""
2632
# Imports
2733
from datetime import datetime, timedelta
@@ -39,11 +45,7 @@
3945
)
4046
from securesystemslib.storage import StorageBackendInterface
4147
from securesystemslib.keys import create_signature, verify_signature
42-
from tuf.repository_lib import (
43-
_strip_version_number
44-
)
4548

46-
import iso8601
4749
import tuf.formats
4850
import tuf.exceptions
4951

@@ -213,8 +215,9 @@ def sign(self, key: JsonDict, append: bool = False) -> JsonDict:
213215
214216
Arguments:
215217
key: A securesystemslib-style private key object used for signing.
216-
append: A boolean indicating if the signature should be appended
217-
to the list of signatures or replace them.
218+
append: A boolean indicating if the signature should be appended to
219+
the list of signatures or replace any existing signatures. The
220+
default behaviour is to replace signatures.
218221
219222
Raises:
220223
securesystemslib.exceptions.FormatError: Key argument is malformed.
@@ -275,7 +278,7 @@ def verify(self, key: JsonDict) -> bool:
275278
class Signed:
276279
"""A base class for the signed part of TUF metadata.
277280
278-
Objects with base class Signed are usually included in a Metablock object
281+
Objects with base class Signed are usually included in a Metadata object
279282
on the signed attribute. This class provides attributes and methods that
280283
are common for all TUF metadata types (roles).
281284
@@ -284,7 +287,7 @@ class Signed:
284287
version: The metadata version number.
285288
spec_version: The TUF specification version number (semver) the
286289
metadata format adheres to.
287-
expires: The metadata expiration date in 'YYYY-MM-DDTHH:MM:SSZ' format.
290+
expires: The metadata expiration datetime object
288291
289292
"""
290293
# NOTE: Signed is a stupid name, because this might not be signed yet, but
@@ -308,14 +311,15 @@ def __init__(
308311

309312
# Deserialization (factories).
310313
@classmethod
311-
def from_dict(cls, signed_dict) -> 'Signed':
314+
def from_dict(cls, signed_dict: JsonDict) -> 'Signed':
312315
"""Creates Signed object from its JSON/dict representation. """
313316

314317
# Convert 'expires' TUF metadata string to a datetime object, which is
315318
# what the constructor expects and what we store. The inverse operation
316319
# is implemented in 'to_dict'.
317-
signed_dict['expires'] = iso8601.parse_date(
318-
signed_dict['expires']).replace(tzinfo=None)
320+
signed_dict['expires'] = datetime.strptime(
321+
signed_dict['expires'],
322+
"%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=None)
319323
# NOTE: We write the converted 'expires' back into 'signed_dict' above
320324
# so that we can pass it to the constructor as '**signed_dict' below,
321325
# along with other fields that belong to Signed subclasses.

0 commit comments

Comments
 (0)