Skip to content

Commit

Permalink
use timezone aware datetime, remove -1 day hack in validity
Browse files Browse the repository at this point in the history
Python 3.8 compatible because windows

Signed-off-by: Mikael Arguedas <[email protected]>
  • Loading branch information
mikaelarguedas committed May 8, 2024
1 parent 47b643f commit 1752d32
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
7 changes: 2 additions & 5 deletions sros2/sros2/_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,14 @@ def build_key_and_cert(subject_name, *, ca=False, ca_key=None, issuer_name=''):
else:
extension = x509.BasicConstraints(ca=False, path_length=None)

utcnow = datetime.datetime.utcnow()
utcnow = datetime.datetime.now(datetime.timezone.utc)
builder = x509.CertificateBuilder(
).issuer_name(
issuer_name
).serial_number(
x509.random_serial_number()
).not_valid_before(
# Using a day earlier here to prevent Connext (5.3.1) from complaining
# when extracting it from the permissions file and thinking it's in the future
# https://github.com/ros2/ci/pull/436#issuecomment-624874296
utcnow - datetime.timedelta(days=1)
utcnow
).not_valid_after(
# TODO: This should not be hard-coded
utcnow + datetime.timedelta(days=3650)
Expand Down
11 changes: 9 additions & 2 deletions sros2/sros2/keystore/_permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,15 @@ def create_permission_file(path: pathlib.Path, domain_id, policy_element) -> Non

cert_path = path.parent.joinpath('cert.pem')
cert_content = _utilities.load_cert(cert_path)
kwargs['not_valid_before'] = etree.XSLT.strparam(cert_content.not_valid_before.isoformat())
kwargs['not_valid_after'] = etree.XSLT.strparam(cert_content.not_valid_after.isoformat())
# TODO replace "not_valid_before"/"not_valid_after" functions by
# "not_valid_before_utc"/"not_valid_after_utc"
# once cryptography 42 is supported on all target platforms
kwargs['not_valid_before'] = etree.XSLT.strparam(
cert_content.not_valid_before.replace(tzinfo=datetime.timezone.utc).isoformat()
)
kwargs['not_valid_after'] = etree.XSLT.strparam(
cert_content.not_valid_after.replace(tzinfo=datetime.timezone.utc).isoformat()
)

if get_rmw_implementation_identifier() in _RMW_WITH_ROS_GRAPH_INFO_TOPIC:
kwargs['allow_ros_discovery_topic'] = etree.XSLT.strparam('1')
Expand Down
20 changes: 13 additions & 7 deletions sros2/test/sros2/commands/security/verbs/test_create_enclave.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,19 @@ def test_cert_pem(enclave_keys_dir):
assert isinstance(cert.signature_hash_algorithm, hashes.SHA256)

# Verify the cert is valid for the expected timespan
utcnow = datetime.datetime.utcnow()

# Using a day earlier here to prevent Connext (5.3.1) from complaining
# when extracting it from the permissions file and thinking it's in the future
# https://github.com/ros2/ci/pull/436#issuecomment-624874296
assert _datetimes_are_close(cert.not_valid_before, utcnow - datetime.timedelta(days=1))
assert _datetimes_are_close(cert.not_valid_after, utcnow + datetime.timedelta(days=3650))
utcnow = datetime.datetime.now(datetime.timezone.utc)

# TODO replace "not_valid_before"/"not_valid_after" functions by
# "not_valid_before_utc"/"not_valid_after_utc"
# once cryptography 42 is supported on all target platforms
assert _datetimes_are_close(
cert.not_valid_before.replace(tzinfo=datetime.timezone.utc),
utcnow
)
assert _datetimes_are_close(
cert.not_valid_after.replace(tzinfo=datetime.timezone.utc),
utcnow + datetime.timedelta(days=3650)
)

# Verify that the cert ensures this key cannot be used to sign others as a CA
assert len(cert.extensions) == 1
Expand Down

0 comments on commit 1752d32

Please sign in to comment.