Skip to content

Commit 6890599

Browse files
cryptography 42 and later compatibility
Signed-off-by: Mikael Arguedas <mikael.arguedas@gmail.com>
1 parent ef808a6 commit 6890599

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

sros2/package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<exec_depend>python3-cryptography</exec_depend>
1616
<exec_depend>python3-importlib-resources</exec_depend>
1717
<exec_depend>python3-lxml</exec_depend>
18+
<exec_depend>python3-semver</exec_depend>
1819
<exec_depend>rclpy</exec_depend>
1920
<exec_depend>ros2cli</exec_depend>
2021

sros2/sros2/_utilities.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
from cryptography.hazmat.primitives import serialization
2424
from cryptography.hazmat.primitives.asymmetric import ec
2525

26+
import semver
27+
2628
import sros2.errors
2729

2830
_DOMAIN_ID_ENV = 'ROS_DOMAIN_ID'
@@ -38,6 +40,10 @@ def create_symlink(*, src: pathlib.Path, dst: pathlib.Path):
3840
os.symlink(src, dst)
3941

4042

43+
def cryptography_version() -> semver.VersionInfo:
44+
return semver.parse_version_info("cryptography")
45+
46+
4147
def domain_id() -> str:
4248
return os.getenv(_DOMAIN_ID_ENV, '0')
4349

sros2/sros2/keystore/_permission.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,20 @@ def create_permission_file(path: pathlib.Path, domain_id, policy_element) -> Non
7676

7777
cert_path = path.parent.joinpath('cert.pem')
7878
cert_content = _utilities.load_cert(cert_path)
79-
# TODO replace "not_valid_before"/"not_valid_after" functions by
80-
# "not_valid_before_utc"/"not_valid_after_utc"
81-
# once cryptography 42 is supported on all target platforms
82-
kwargs['not_valid_before'] = etree.XSLT.strparam(
83-
cert_content.not_valid_before.replace(tzinfo=datetime.timezone.utc).isoformat()
84-
)
85-
kwargs['not_valid_after'] = etree.XSLT.strparam(
86-
cert_content.not_valid_after.replace(tzinfo=datetime.timezone.utc).isoformat()
87-
)
79+
if _utilities.cryptography_version().major >= 42:
80+
kwargs['not_valid_before'] = etree.XSLT.strparam(
81+
cert_content.not_valid_before_utc
82+
)
83+
kwargs['not_valid_after'] = etree.XSLT.strparam(
84+
cert_content.not_valid_after_utc
85+
)
86+
else:
87+
kwargs['not_valid_before'] = etree.XSLT.strparam(
88+
cert_content.not_valid_before.replace(tzinfo=datetime.timezone.utc).isoformat()
89+
)
90+
kwargs['not_valid_after'] = etree.XSLT.strparam(
91+
cert_content.not_valid_after.replace(tzinfo=datetime.timezone.utc).isoformat()
92+
)
8893

8994
if get_rmw_implementation_identifier() in _RMW_WITH_ROS_GRAPH_INFO_TOPIC:
9095
kwargs['allow_ros_discovery_topic'] = etree.XSLT.strparam('1')

sros2/test/sros2/commands/security/verbs/test_create_enclave.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,19 @@ def test_cert_pem(enclave_keys_dir):
124124
# Verify the cert is valid for the expected timespan
125125
utcnow = datetime.datetime.now(datetime.timezone.utc)
126126

127-
# TODO replace "not_valid_before"/"not_valid_after" functions by
128-
# "not_valid_before_utc"/"not_valid_after_utc"
129-
# once cryptography 42 is supported on all target platforms
127+
if _utilities.cryptography_version().major >= 42:
128+
cert_not_valid_before_value = cert.not_valid_before_utc
129+
cert_not_valid_after_value = cert.not_valid_after_utc
130+
else:
131+
cert_not_valid_before_value = cert.not_valid_before.replace(tzinfo=datetime.timezone.utc)
132+
cert_not_valid_after_value = cert.not_valid_after.replace(tzinfo=datetime.timezone.utc)
133+
130134
assert _datetimes_are_close(
131-
cert.not_valid_before.replace(tzinfo=datetime.timezone.utc),
135+
cert_not_valid_before_value,
132136
utcnow
133137
)
134138
assert _datetimes_are_close(
135-
cert.not_valid_after.replace(tzinfo=datetime.timezone.utc),
139+
cert_not_valid_after_value,
136140
utcnow + datetime.timedelta(days=3650)
137141
)
138142

0 commit comments

Comments
 (0)