Skip to content

Commit de9633d

Browse files
committed
Metadata API: convenience tweak to expires setter
Practically were changing API if we start requiring that expires is non-naive because this no longer works: metadata.signed.expires = datetime(3000,1,1) We can make this work without API breaks though: * it the input is naive, just use UTC * if the input is not naive or UTC, raise Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
1 parent c2edd30 commit de9633d

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

tuf/api/_payload.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ class Signed(metaclass=abc.ABCMeta):
6060
version: Metadata version number. If None, then 1 is assigned.
6161
spec_version: Supported TUF specification version. If None, then the
6262
version currently supported by the library is assigned.
63-
expires: Metadata expiry date. If None, then current date and time is
64-
assigned.
63+
expires: Metadata expiry date in UTC timezone. If None, then current
64+
date and time is assigned.
6565
unrecognized_fields: Dictionary of all attributes that are not managed
6666
by TUF Metadata API
6767
@@ -79,16 +79,22 @@ def _type(self) -> str:
7979

8080
@property
8181
def expires(self) -> datetime:
82-
"""Get the metadata expiry date.
83-
84-
# Use 'datetime' module to e.g. expire in seven days from now
85-
obj.expires = now(timezone.utc) + timedelta(days=7)
86-
"""
82+
"""Get the metadata expiry date."""
8783
return self._expires
8884

8985
@expires.setter
9086
def expires(self, value: datetime) -> None:
87+
"""Set the metadata expiry date.
88+
89+
# Use 'datetime' module to e.g. expire in seven days from now
90+
obj.expires = now(timezone.utc) + timedelta(days=7)
91+
"""
9192
self._expires = value.replace(microsecond=0)
93+
if self._expires.tzinfo is None:
94+
# Naive datetime: just make it UTC
95+
self._expires = self._expires.replace(tzinfo=timezone.utc)
96+
elif self._expires.tzinfo != timezone.utc:
97+
raise ValueError(f"Expected tz UTC, not {self._expires.tzinfo}")
9298

9399
# NOTE: Signed is a stupid name, because this might not be signed yet, but
94100
# we keep it to match spec terminology (I often refer to this as "payload",

0 commit comments

Comments
 (0)