Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove iso8601 dependency #1176

Merged
merged 2 commits into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion requirements-pinned.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ cryptography==3.1.1 # via securesystemslib
enum34==1.1.6 ; python_version < '3' # via cryptography
idna==2.10 # via requests
ipaddress==1.0.23 ; python_version < '3' # via cryptography
iso8601==0.1.13
pycparser==2.20 # via cffi
pynacl==1.4.0 # via securesystemslib
python-dateutil==2.8.1 # via securesystemslib
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,3 @@
securesystemslib[colors, crypto, pynacl]
requests
six
iso8601
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@
},
python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, <4",
install_requires = [
'iso8601>=0.1.12',
'requests>=2.19.1',
'six>=1.11.0',
'securesystemslib>=0.16.0'
Expand Down
17 changes: 17 additions & 0 deletions tests/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,23 @@ def test_build_dict_conforming_to_schema(self):



def test_expiry_string_to_datetime(self):
dt = tuf.formats.expiry_string_to_datetime('1985-10-21T13:20:00Z')
self.assertEqual(dt, datetime.datetime(1985, 10, 21, 13, 20, 0))
dt = tuf.formats.expiry_string_to_datetime('2038-01-19T03:14:08Z')
self.assertEqual(dt, datetime.datetime(2038, 1, 19, 3, 14, 8))

# First 3 fail via securesystemslib schema, last one because of strptime()
invalid_inputs = [
'2038-1-19T03:14:08Z', # leading zeros not optional
'2038-01-19T031408Z', # strict time parsing
'2038-01-19T03:14:08Z-06:00', # timezone not allowed
'2038-13-19T03:14:08Z', # too many months
]
for invalid_input in invalid_inputs:
with self.assertRaises(securesystemslib.exceptions.FormatError):
tuf.formats.expiry_string_to_datetime(invalid_input)



def test_unix_timestamp_to_datetime(self):
Expand Down
5 changes: 2 additions & 3 deletions tuf/api/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,8 @@ def from_dict(cls, signed_dict: JsonDict) -> 'Signed':
# Convert 'expires' TUF metadata string to a datetime object, which is
# what the constructor expects and what we store. The inverse operation
# is implemented in 'to_dict'.
signed_dict['expires'] = datetime.strptime(
signed_dict['expires'],
"%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=None)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that I'm not doing the replace() in my version: I could not think of a case where it would be useful.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes perfect sense. I guess I thought that strptime might adopt the system timezone. Thanks for removing unnecessary code!

signed_dict['expires'] = tuf.formats.expiry_string_to_datetime(
signed_dict['expires'])
# NOTE: We write the converted 'expires' back into 'signed_dict' above
# so that we can pass it to the constructor as '**signed_dict' below,
# along with other fields that belong to Signed subclasses.
Expand Down
26 changes: 7 additions & 19 deletions tuf/client/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@
import securesystemslib.keys
import securesystemslib.util
import six
import iso8601
import requests.exceptions

# The Timestamp role does not have signed metadata about it; otherwise we
Expand All @@ -163,11 +162,6 @@
# See 'log.py' to learn how logging is handled in TUF.
logger = logging.getLogger(__name__)

# Disable 'iso8601' logger messages to prevent 'iso8601' from clogging the
# log file.
iso8601_logger = logging.getLogger('iso8601')
iso8601_logger.disabled = True


class MultiRepoUpdater(object):
"""
Expand Down Expand Up @@ -2377,30 +2371,24 @@ def _ensure_not_expired(self, metadata_object, metadata_rolename):
<Exceptions>
tuf.exceptions.ExpiredMetadataError:
If 'metadata_rolename' has expired.

securesystemslib.exceptions.FormatError:
If the expiration cannot be parsed correctly
<Side Effects>
None.

<Returns>
None.
"""

# Extract the expiration time.
expires = metadata_object['expires']

# If the current time has surpassed the expiration date, raise an
# exception. 'expires' is in
# 'securesystemslib.formats.ISO8601_DATETIME_SCHEMA' format (e.g.,
# '1985-10-21T01:22:00Z'.) Convert it to a unix timestamp and compare it
# Extract the expiration time. Convert it to a unix timestamp and compare it
# against the current time.time() (also in Unix/POSIX time format, although
# with microseconds attached.)
current_time = int(time.time())

# Generate a user-friendly error message if 'expires' is less than the
# current time (i.e., a local time.)
expires_datetime = iso8601.parse_date(expires)
expires_datetime = tuf.formats.expiry_string_to_datetime(
metadata_object['expires'])
expires_timestamp = tuf.formats.datetime_to_unix_timestamp(expires_datetime)

current_time = int(time.time())

if expires_timestamp < current_time:
message = 'Metadata '+repr(metadata_rolename)+' expired on ' + \
expires_datetime.ctime() + ' (UTC).'
Expand Down
31 changes: 31 additions & 0 deletions tuf/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,37 @@ def build_dict_conforming_to_schema(schema, **kwargs):




def expiry_string_to_datetime(expires):
"""
<Purpose>
Convert an expiry string to a datetime object.
<Arguments>
expires:
The expiry date-time string in the ISO8601 format that is defined
in securesystemslib.ISO8601_DATETIME_SCHEMA. E.g. '2038-01-19T03:14:08Z'
<Exceptions>
securesystemslib.exceptions.FormatError, if 'expires' cannot be
parsed correctly.
<Side Effects>
None.
<Returns>
A datetime object representing the expiry time.
"""

# Raise 'securesystemslib.exceptions.FormatError' if there is a mismatch.
securesystemslib.formats.ISO8601_DATETIME_SCHEMA.check_match(expires)

try:
return datetime.datetime.strptime(expires, "%Y-%m-%dT%H:%M:%SZ")
except ValueError as error:
six.raise_from(securesystemslib.exceptions.FormatError(
'Failed to parse ' + repr(expires) + ' as an expiry time'),
error)




def datetime_to_unix_timestamp(datetime_object):
"""
<Purpose>
Expand Down
9 changes: 2 additions & 7 deletions tuf/repository_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import securesystemslib.hash
import securesystemslib.interface
import securesystemslib.util
import iso8601
import six

import securesystemslib.storage
Expand All @@ -61,11 +60,6 @@
# See 'log.py' to learn how logging is handled in TUF.
logger = logging.getLogger(__name__)

# Disable 'iso8601' logger messages to prevent 'iso8601' from clogging the
# log file.
iso8601_logger = logging.getLogger('iso8601')
iso8601_logger.disabled = True

# The extension of TUF metadata.
METADATA_EXTENSION = '.json'

Expand Down Expand Up @@ -704,7 +698,8 @@ def _log_warning_if_expires_soon(rolename, expires_iso8601_timestamp,
# unix timestamp, subtract from current time.time() (also in POSIX time)
# and compare against 'seconds_remaining_to_warn'. Log a warning message
# to console if 'rolename' expires soon.
datetime_object = iso8601.parse_date(expires_iso8601_timestamp)
datetime_object = tuf.formats.expiry_string_to_datetime(
expires_iso8601_timestamp)
expires_unix_timestamp = \
tuf.formats.datetime_to_unix_timestamp(datetime_object)
seconds_until_expires = expires_unix_timestamp - int(time.time())
Expand Down
12 changes: 3 additions & 9 deletions tuf/repository_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import securesystemslib.keys
import securesystemslib.formats
import securesystemslib.util
import iso8601
import six

import securesystemslib.storage
Expand Down Expand Up @@ -1317,15 +1316,12 @@ def expiration(self):
<Purpose>
A getter method that returns the role's expiration datetime.

>>>
>>>
>>>

<Arguments>
None.

<Exceptions>
None.
securesystemslib.exceptions.FormatError, if the expiration cannot be
parsed correctly

<Side Effects>
None.
Expand All @@ -1337,9 +1333,7 @@ def expiration(self):
roleinfo = tuf.roledb.get_roleinfo(self.rolename, self._repository_name)
expires = roleinfo['expires']

expires_datetime_object = iso8601.parse_date(expires)

return expires_datetime_object
return tuf.formats.expiry_string_to_datetime(expires)



Expand Down
6 changes: 0 additions & 6 deletions tuf/sig.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,6 @@
# See 'log.py' to learn how logging is handled in TUF.
logger = logging.getLogger(__name__)

# Disable 'iso8601' logger messages to prevent 'iso8601' from clogging the
# log file.
iso8601_logger = logging.getLogger('iso8601')
iso8601_logger.disabled = True


def get_signature_status(signable, role=None, repository_name='default',
threshold=None, keyids=None):
"""
Expand Down