Skip to content

Commit 7d3f4a4

Browse files
authored
Add cipher preference option in builder (#659)
* add cipher suite option in mqtt builder * fix positional argument * fix parameter & change parameter naming * move cipher suite to builder option * fix format * add cipher_pref in builder doc * update awscrt to 0.28.3 * validate TlsCipherPref
1 parent 6adcfe7 commit 7d3f4a4

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

awsiot/mqtt5_client_builder.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@
168168
169169
**ca_bytes** (`bytes`): Override default trust store with CA certificates from these PEM formatted bytes.
170170
171+
**cipher_pref** (:class:`awscrt.io.TlsCipherPref`): Cipher preference to use for TLS connection. Default is `TlsCipherPref.DEFAULT`.
172+
171173
**enable_metrics_collection** (`bool`): Whether to send the SDK version number in the CONNECT packet.
172174
Default is True.
173175
@@ -243,8 +245,11 @@ def _builder(
243245
use_websockets=False,
244246
websocket_handshake_transform=None,
245247
use_custom_authorizer=False,
248+
cipher_pref=awscrt.io.TlsCipherPref.DEFAULT,
246249
**kwargs):
247250

251+
assert isinstance(cipher_pref, awscrt.io.TlsCipherPref)
252+
248253
username = _get(kwargs, 'username', '')
249254
if _get(kwargs, 'enable_metrics_collection', True):
250255
username += _get_metrics_str(username)
@@ -345,6 +350,8 @@ def _builder(
345350
elif ca_filepath or ca_dirpath:
346351
tls_ctx_options.override_default_trust_store_from_path(ca_dirpath, ca_filepath)
347352

353+
tls_ctx_options.cipher_pref = cipher_pref
354+
348355
if client_options.port is None:
349356
# prefer 443, even for direct MQTT connections, since it's less likely to be blocked by firewalls
350357
if use_websockets or awscrt.io.is_alpn_available():
@@ -453,6 +460,7 @@ def mtls_with_pkcs11(*,
453460
cert_file_contents=cert_bytes)
454461
return _builder(tls_ctx_options, **kwargs)
455462

463+
456464
def mtls_with_pkcs12(*,
457465
pkcs12_filepath: str,
458466
pkcs12_password: str,
@@ -543,7 +551,10 @@ def _sign_websocket_handshake_request(transform_args, **kwargs):
543551
except Exception as e:
544552
transform_args.set_done(e)
545553

546-
return websockets_with_custom_handshake(_sign_websocket_handshake_request, websocket_proxy_options, **kwargs)
554+
return websockets_with_custom_handshake(
555+
_sign_websocket_handshake_request,
556+
websocket_proxy_options,
557+
**kwargs)
547558

548559

549560
def websockets_with_custom_handshake(

awsiot/mqtt_connection_builder.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@
111111
112112
**ca_bytes** (`bytes`): Override default trust store with CA certificates from these PEM formatted bytes.
113113
114+
**cipher_pref** (:class:`awscrt.io.TlsCipherPref`): Cipher preference to use for TLS connection. Default is `TlsCipherPref.DEFAULT`.
115+
114116
**enable_metrics_collection** (`bool`): Whether to send the SDK version number in the CONNECT packet.
115117
Default is True.
116118
@@ -181,8 +183,11 @@ def _builder(
181183
use_websockets=False,
182184
websocket_handshake_transform=None,
183185
use_custom_authorizer=False,
186+
cipher_pref=awscrt.io.TlsCipherPref.DEFAULT,
184187
**kwargs):
185188

189+
assert isinstance(cipher_pref, awscrt.io.TlsCipherPref)
190+
186191
ca_bytes = _get(kwargs, 'ca_bytes')
187192
ca_filepath = _get(kwargs, 'ca_filepath')
188193
ca_dirpath = _get(kwargs, 'ca_dirpath')
@@ -202,6 +207,8 @@ def _builder(
202207
if port == 443 and awscrt.io.is_alpn_available() and use_custom_authorizer is False:
203208
tls_ctx_options.alpn_list = ['http/1.1'] if use_websockets else ['x-amzn-mqtt-ca']
204209

210+
tls_ctx_options.cipher_pref = cipher_pref
211+
205212
socket_options = awscrt.io.SocketOptions()
206213
socket_options.connect_timeout_ms = _get(kwargs, 'tcp_connect_timeout_ms', 5000)
207214
# These have been inconsistent between keepalive/keep_alive. Resolve both for now to ease transition.
@@ -350,6 +357,7 @@ def mtls_with_pkcs11(*,
350357

351358
return _builder(tls_ctx_options, **kwargs)
352359

360+
353361
def mtls_with_pkcs12(*,
354362
pkcs12_filepath: str,
355363
pkcs12_password: str,
@@ -552,6 +560,7 @@ def direct_with_custom_authorizer(
552560
use_websockets=False,
553561
**kwargs)
554562

563+
555564
def websockets_with_custom_authorizer(
556565
region=None,
557566
credentials_provider=None,
@@ -590,7 +599,7 @@ def websockets_with_custom_authorizer(
590599
auth_authorizer_signature (`str`): The digital signature of the token value in the `auth_token_value`
591600
parameter. The signature must be based on the private key associated with the custom authorizer. The
592601
signature must be base64 encoded.
593-
Required if the custom authorizer has signing enabled.
602+
Required if the custom authorizer has signing enabled.
594603
595604
auth_token_key_name (`str`): Key used to extract the custom authorizer token from MQTT username query-string
596605
properties.
@@ -616,15 +625,15 @@ def websockets_with_custom_authorizer(
616625

617626

618627
def _with_custom_authorizer(auth_username=None,
619-
auth_authorizer_name=None,
620-
auth_authorizer_signature=None,
621-
auth_password=None,
622-
auth_token_key_name=None,
623-
auth_token_value=None,
624-
use_websockets=False,
625-
websockets_credentials_provider=None,
626-
websockets_region=None,
627-
**kwargs) -> awscrt.mqtt.Connection:
628+
auth_authorizer_name=None,
629+
auth_authorizer_signature=None,
630+
auth_password=None,
631+
auth_token_key_name=None,
632+
auth_token_value=None,
633+
use_websockets=False,
634+
websockets_credentials_provider=None,
635+
websockets_region=None,
636+
**kwargs) -> awscrt.mqtt.Connection:
628637
"""
629638
Helper function that contains the setup needed for custom authorizers
630639
"""
@@ -657,7 +666,7 @@ def _with_custom_authorizer(auth_username=None,
657666
kwargs["password"] = auth_password
658667

659668
tls_ctx_options = awscrt.io.TlsContextOptions()
660-
if use_websockets == False:
669+
if not use_websockets:
661670
kwargs["port"] = 443
662671
tls_ctx_options.alpn_list = ["mqtt"]
663672

0 commit comments

Comments
 (0)