Skip to content

Commit 54d5611

Browse files
committed
Return a timezone aware datetime object when cryptography >= 42
1 parent 7259700 commit 54d5611

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

cert_chain_resolver/models.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,20 @@ def signature_hash_algorithm(self):
135135
@property
136136
def not_valid_before(self):
137137
# type: () -> datetime.datetime
138-
"""Date from the underlying :py:class:`cryptography.x509.Certificate` object"""
139-
return self._x509.not_valid_before
138+
"""Date from the underlying :py:class:`cryptography.x509.Certificate` object. returns the UTC version if cryptography version is 42.0 or higher"""
139+
if hasattr(self._x509, 'not_valid_before_utc'):
140+
return self._x509.not_valid_before_utc
141+
else:
142+
return self._x509.not_valid_before
140143

141144
@property
142145
def not_valid_after(self):
143146
# type: () -> datetime.datetime
144-
"""Date from the underlying :py:class:`cryptography.x509.Certificate` object"""
145-
return self._x509.not_valid_after
147+
"""Date from the underlying :py:class:`cryptography.x509.Certificate` object. returns the UTC version if cryptography version is 42.0 or higher"""
148+
if hasattr(self._x509, 'not_valid_after_utc'):
149+
return self._x509.not_valid_after_utc
150+
else:
151+
return self._x509.not_valid_after
146152

147153
@property
148154
def fingerprint(self):

tests/_utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import cryptography
2+
import datetime
3+
4+
CRYPTOGRAPHY_MAJOR = int(cryptography.__version__.split(".")[0])
5+
6+
def make_utc_aware_if_cryptography_above_42(dt):
7+
if CRYPTOGRAPHY_MAJOR >= 42:
8+
return dt.replace(tzinfo=datetime.timezone.utc)
9+
return dt

tests/test_cli.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from cert_chain_resolver.cli import cli, main, parse_args
88
from cert_chain_resolver.castore.file_system import FileSystemStore
9+
from tests._utils import CRYPTOGRAPHY_MAJOR
910
from .fixtures import BUNDLE_FIXTURES, certfixture_to_id
1011

1112
try:
@@ -146,8 +147,8 @@ def test_display_flag_is_properly_formatted(capsys):
146147
"""== Certificate #1 ==
147148
Subject: CN=github.com,O=GitHub\\, Inc.,L=San Francisco,ST=California,C=US
148149
Issuer: CN=DigiCert SHA2 High Assurance Server CA,OU=www.digicert.com,O=DigiCert Inc,C=US
149-
NotBefore: 2020-05-05T00:00:00
150-
NotAfter: 2022-05-10T12:00:00
150+
NotBefore: 2020-05-05T00:00:00{tz}
151+
NotAfter: 2022-05-10T12:00:00{tz}
151152
Serial: 7101927171473588541993819712332065657
152153
Sha256Fingeprint: b6b9a6af3e866cbe0e6a307e7dda173b372b2d3ac3f06af15f97718773848008
153154
CAIssuerLoc: http://cacerts.digicert.com/DigiCertSHA2HighAssuranceServerCA.crt
@@ -162,8 +163,8 @@ def test_display_flag_is_properly_formatted(capsys):
162163
== Certificate #2 ==
163164
Subject: CN=DigiCert SHA2 High Assurance Server CA,OU=www.digicert.com,O=DigiCert Inc,C=US
164165
Issuer: CN=DigiCert High Assurance EV Root CA,OU=www.digicert.com,O=DigiCert Inc,C=US
165-
NotBefore: 2013-10-22T12:00:00
166-
NotAfter: 2028-10-22T12:00:00
166+
NotBefore: 2013-10-22T12:00:00{tz}
167+
NotAfter: 2028-10-22T12:00:00{tz}
167168
Serial: 6489877074546166222510380951761917343
168169
Sha256Fingeprint: 19400be5b7a31fb733917700789d2f0a2471c0c9d506c0e504c06c16d7cb17c0
169170
@@ -173,7 +174,7 @@ def test_display_flag_is_properly_formatted(capsys):
173174
Common name: DigiCert SHA2 High Assurance Server CA
174175
175176
"""
176-
)
177+
).format(tz="+00:00" if CRYPTOGRAPHY_MAJOR > 42 else "")
177178

178179
assert expected == captured
179180

tests/test_models.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from cryptography.hazmat.primitives import hashes
1010
from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15
1111
from cryptography.exceptions import InvalidSignature
12+
from ._utils import make_utc_aware_if_cryptography_above_42
13+
1214

1315

1416
try:
@@ -51,8 +53,8 @@ def test_certcontainer_x509_helper_props(cert):
5153
assert fixture["ca"] == c.is_ca
5254
assert fixture["serial"] == c.serial
5355
assert fixture["signature_algorithm"] == c.signature_hash_algorithm
54-
assert fixture["not_before"] == c.not_valid_before
55-
assert fixture["not_after"] == c.not_valid_after
56+
assert make_utc_aware_if_cryptography_above_42(fixture["not_before"]) == c.not_valid_before
57+
assert make_utc_aware_if_cryptography_above_42(fixture["not_after"]) == c.not_valid_after
5658
assert fixture["fingerprint_sha256"] == c.fingerprint
5759
assert fixture["ca_issuer_access_location"] == c.ca_issuer_access_location
5860

0 commit comments

Comments
 (0)