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):
x509.AuthorityKeyIdentifier,
).value.key_identifier
try:
_UTC = datetime.timezone.utc
except AttributeError: # pragma: no cover
# BBB
_ZERO_TIMEDELTA = datetime.timedelta(0)
class _UTC(datetime.tzinfo):
"""UTC"""
if hasattr(x509.Certificate, 'not_valid_after_utc'): # pragma: no cover
try:
_UTC = datetime.timezone.utc
except AttributeError:
# BBB
_ZERO_TIMEDELTA = datetime.timedelta(0)
class _UTC(datetime.tzinfo):
"""UTC"""
def utcoffset(self, dt):
return _ZERO_TIMEDELTA
def utcoffset(self, dt):
return _ZERO_TIMEDELTA
def tzname(self, dt):
return "UTC"
def tzname(self, dt):
return "UTC"
def dst(self, dt):
return _ZERO_TIMEDELTA
def dst(self, dt):
return _ZERO_TIMEDELTA
_UTC = _UTC()
def utcnow():
"""
Get a datetime instance of the current time in UTC.
"""
return datetime.datetime.now(_UTC)
def utcnow():
"""
Get a datetime instance of the current time in UTC.
"""
return datetime.datetime.now(_UTC)
if hasattr(x509.Certificate, 'not_valid_after_utc'): # pragma: no cover
def getNotValidAfter(obj):
"""
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
return obj.revocation_date_utc
else: # pragma: no cover
# BBB
_UTC = None
def utcnow():
"""
Get a datetime instance of the current time in UTC.
"""
return datetime.datetime.utcnow()
def getNotValidAfter(obj):
"""
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