Commit d02a81de authored by Vincent Pelletier's avatar Vincent Pelletier

caucase.utils: Fix support for timezone-naive-only cryptography versions

Use UTC but timezone-naive datetime everywhere when cryptography does not
expose the timezone-aware version of its date attributes.
Fixes support for Python 2.7 with cryptography 3.3.2 (at least).
parent bdb0f795
...@@ -652,30 +652,31 @@ def _getAuthorityKeyIdentifier(cert): ...@@ -652,30 +652,31 @@ def _getAuthorityKeyIdentifier(cert):
x509.AuthorityKeyIdentifier, x509.AuthorityKeyIdentifier,
).value.key_identifier ).value.key_identifier
try: if hasattr(x509.Certificate, 'not_valid_after_utc'): # pragma: no cover
_UTC = datetime.timezone.utc try:
except AttributeError: # pragma: no cover _UTC = datetime.timezone.utc
# BBB except AttributeError:
_ZERO_TIMEDELTA = datetime.timedelta(0) # BBB
class _UTC(datetime.tzinfo): _ZERO_TIMEDELTA = datetime.timedelta(0)
"""UTC""" class _UTC(datetime.tzinfo):
"""UTC"""
def utcoffset(self, dt): def utcoffset(self, dt):
return _ZERO_TIMEDELTA return _ZERO_TIMEDELTA
def tzname(self, dt): def tzname(self, dt):
return "UTC" return "UTC"
def dst(self, dt): def dst(self, dt):
return _ZERO_TIMEDELTA return _ZERO_TIMEDELTA
_UTC = _UTC()
def utcnow(): def utcnow():
""" """
Get a datetime instance of the current time in UTC. Get a datetime instance of the current time in UTC.
""" """
return datetime.datetime.now(_UTC) return datetime.datetime.now(_UTC)
if hasattr(x509.Certificate, 'not_valid_after_utc'): # pragma: no cover
def getNotValidAfter(obj): def getNotValidAfter(obj):
""" """
Return the not_valid_after date from given cyrptography object. Return the not_valid_after date from given cyrptography object.
...@@ -707,6 +708,15 @@ if hasattr(x509.Certificate, 'not_valid_after_utc'): # pragma: no cover ...@@ -707,6 +708,15 @@ if hasattr(x509.Certificate, 'not_valid_after_utc'): # pragma: no cover
return obj.revocation_date_utc return obj.revocation_date_utc
else: # pragma: no cover else: # pragma: no cover
# BBB # BBB
_UTC = None
def utcnow():
"""
Get a datetime instance of the current time in UTC.
"""
return datetime.datetime.utcnow()
def getNotValidAfter(obj): def getNotValidAfter(obj):
""" """
Return the not_valid_after date from given cyrptography object. Return the not_valid_after date from given cyrptography object.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment