Skip to content

Commit 21d52a1

Browse files
committed
add cipher suite option in mqtt builder
1 parent dae5b0b commit 21d52a1

File tree

2 files changed

+75
-18
lines changed

2 files changed

+75
-18
lines changed

awsiot/mqtt5_client_builder.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,8 @@ def _builder(
362362
return client
363363

364364

365-
def mtls_from_path(cert_filepath, pri_key_filepath, **kwargs) -> awscrt.mqtt5.Client:
365+
def mtls_from_path(cert_filepath, pri_key_filepath, cipher_suite=awscrt.io.TlsCipherPref.DEFAULT,
366+
**kwargs) -> awscrt.mqtt5.Client:
366367
"""
367368
This builder creates an :class:`awscrt.mqtt5.Client`, configured for an mTLS MQTT5 Client to AWS IoT.
368369
TLS arguments are passed as filepaths.
@@ -377,10 +378,15 @@ def mtls_from_path(cert_filepath, pri_key_filepath, **kwargs) -> awscrt.mqtt5.Cl
377378
"""
378379
_check_required_kwargs(**kwargs)
379380
tls_ctx_options = awscrt.io.TlsContextOptions.create_client_with_mtls_from_path(cert_filepath, pri_key_filepath)
381+
tls_ctx_options.cipher_pref = cipher_suite
380382
return _builder(tls_ctx_options, **kwargs)
381383

382384

383-
def mtls_from_bytes(cert_bytes, pri_key_bytes, **kwargs) -> awscrt.mqtt5.Client:
385+
def mtls_from_bytes(
386+
cert_bytes,
387+
pri_key_bytes,
388+
cipher_suite=awscrt.io.TlsCipherPref.DEFAULT,
389+
**kwargs) -> awscrt.mqtt5.Client:
384390
"""
385391
This builder creates an :class:`awscrt.mqtt5.Client`, configured for an mTLS MQTT5 Client to AWS IoT.
386392
TLS arguments are passed as in-memory bytes.
@@ -395,6 +401,7 @@ def mtls_from_bytes(cert_bytes, pri_key_bytes, **kwargs) -> awscrt.mqtt5.Client:
395401
"""
396402
_check_required_kwargs(**kwargs)
397403
tls_ctx_options = awscrt.io.TlsContextOptions.create_client_with_mtls(cert_bytes, pri_key_bytes)
404+
tls_ctx_options.cipher_pref = cipher_suite
398405
return _builder(tls_ctx_options, **kwargs)
399406

400407

@@ -406,6 +413,7 @@ def mtls_with_pkcs11(*,
406413
private_key_label: str = None,
407414
cert_filepath: str = None,
408415
cert_bytes=None,
416+
cipher_suite=awscrt.io.TlsCipherPref.DEFAULT,
409417
**kwargs) -> awscrt.mqtt5.Client:
410418
"""
411419
This builder creates an :class:`awscrt.mqtt5.Client`, configured for an mTLS MQTT connection to AWS IoT,
@@ -451,11 +459,14 @@ def mtls_with_pkcs11(*,
451459
private_key_label=private_key_label,
452460
cert_file_path=cert_filepath,
453461
cert_file_contents=cert_bytes)
462+
tls_ctx_options.cipher_pref = cipher_suite
454463
return _builder(tls_ctx_options, **kwargs)
455464

465+
456466
def mtls_with_pkcs12(*,
457467
pkcs12_filepath: str,
458468
pkcs12_password: str,
469+
cipher_suite=awscrt.io.TlsCipherPref.DEFAULT,
459470
**kwargs) -> awscrt.mqtt.Connection:
460471
"""
461472
This builder creates an :class:`awscrt.mqtt.Connection`, configured for an mTLS MQTT connection to AWS IoT,
@@ -476,11 +487,13 @@ def mtls_with_pkcs12(*,
476487
tls_ctx_options = awscrt.io.TlsContextOptions.create_client_with_mtls_pkcs12(
477488
pkcs12_filepath=pkcs12_filepath,
478489
pkcs12_password=pkcs12_password)
490+
tls_ctx_options.cipher_pref = cipher_suite
479491
return _builder(tls_ctx_options, **kwargs)
480492

481493

482494
def mtls_with_windows_cert_store_path(*,
483495
cert_store_path: str,
496+
cipher_suite=awscrt.io.TlsCipherPref.DEFAULT,
484497
**kwargs) -> awscrt.mqtt5.Client:
485498
"""
486499
This builder creates an :class:`awscrt.mqtt5.Client`, configured for an mTLS MQTT5 Client to AWS IoT,
@@ -499,13 +512,15 @@ def mtls_with_windows_cert_store_path(*,
499512
_check_required_kwargs(**kwargs)
500513

501514
tls_ctx_options = awscrt.io.TlsContextOptions.create_client_with_mtls_windows_cert_store_path(cert_store_path)
515+
tls_ctx_options.cipher_pref = cipher_suite
502516
return _builder(tls_ctx_options, **kwargs)
503517

504518

505519
def websockets_with_default_aws_signing(
506520
region,
507521
credentials_provider,
508522
websocket_proxy_options=None,
523+
cipher_suite=awscrt.io.TlsCipherPref.DEFAULT,
509524
**kwargs) -> awscrt.mqtt5.Client:
510525
"""
511526
This builder creates an :class:`awscrt.mqtt5.Client`, configured for an MQTT5 Client over websockets to AWS IoT.
@@ -543,12 +558,17 @@ def _sign_websocket_handshake_request(transform_args, **kwargs):
543558
except Exception as e:
544559
transform_args.set_done(e)
545560

546-
return websockets_with_custom_handshake(_sign_websocket_handshake_request, websocket_proxy_options, **kwargs)
561+
return websockets_with_custom_handshake(
562+
_sign_websocket_handshake_request,
563+
websocket_proxy_options,
564+
cipher_suite,
565+
**kwargs)
547566

548567

549568
def websockets_with_custom_handshake(
550569
websocket_handshake_transform,
551570
websocket_proxy_options=None,
571+
cipher_suite=awscrt.io.TlsCipherPref.DEFAULT,
552572
**kwargs) -> awscrt.mqtt5.Client:
553573
"""
554574
This builder creates an :class:`awscrt.mqtt5.Client`, configured for an MQTT5 Client over websockets,
@@ -576,6 +596,7 @@ def websockets_with_custom_handshake(
576596
"""
577597
_check_required_kwargs(**kwargs)
578598
tls_ctx_options = awscrt.io.TlsContextOptions()
599+
tls_ctx_options.cipher_pref = cipher_suite
579600
return _builder(tls_ctx_options=tls_ctx_options,
580601
use_websockets=True,
581602
websocket_handshake_transform=websocket_handshake_transform,
@@ -607,6 +628,7 @@ def direct_with_custom_authorizer(
607628
auth_password=None,
608629
auth_token_key_name=None,
609630
auth_token_value=None,
631+
cipher_suite=awscrt.io.TlsCipherPref.DEFAULT,
610632
**kwargs) -> awscrt.mqtt5.Client:
611633
"""
612634
This builder creates an :class:`awscrt.mqtt5.Client`, configured for an MQTT5 Client using a custom
@@ -673,6 +695,7 @@ def direct_with_custom_authorizer(
673695

674696
tls_ctx_options = awscrt.io.TlsContextOptions()
675697
tls_ctx_options.alpn_list = ["mqtt"]
698+
tls_ctx_options.cipher_pref = cipher_suite
676699

677700
return _builder(tls_ctx_options=tls_ctx_options,
678701
use_websockets=False,
@@ -688,6 +711,7 @@ def websockets_with_custom_authorizer(
688711
websocket_proxy_options=None,
689712
auth_token_key_name=None,
690713
auth_token_value=None,
714+
cipher_suite=awscrt.io.TlsCipherPref.DEFAULT,
691715
**kwargs) -> awscrt.mqtt5.Client:
692716
"""
693717
This builder creates an :class:`awscrt.mqtt5.Client`, configured for an MQTT5 Client using a custom
@@ -757,6 +781,7 @@ def websockets_with_custom_authorizer(
757781
kwargs["password"] = auth_password
758782

759783
tls_ctx_options = awscrt.io.TlsContextOptions()
784+
tls_ctx_options.cipher_pref = cipher_suite
760785

761786
def _sign_websocket_handshake_request(transform_args, **kwargs):
762787
# transform_args need to know when transform is done
@@ -773,7 +798,7 @@ def _sign_websocket_handshake_request(transform_args, **kwargs):
773798
**kwargs)
774799

775800

776-
def new_default_builder(**kwargs) -> awscrt.mqtt5.Client:
801+
def new_default_builder(cipher_suite=awscrt.io.TlsCipherPref.DEFAULT, **kwargs) -> awscrt.mqtt5.Client:
777802
"""
778803
This builder creates an :class:`awscrt.mqtt5.Client`, without any configuration besides the default TLS context options.
779804
@@ -782,6 +807,7 @@ def new_default_builder(**kwargs) -> awscrt.mqtt5.Client:
782807
"""
783808
_check_required_kwargs(**kwargs)
784809
tls_ctx_options = awscrt.io.TlsContextOptions()
810+
tls_ctx_options.cipher_pref = cipher_suite
785811
return _builder(tls_ctx_options=tls_ctx_options,
786812
use_websockets=False,
787813
**kwargs)

awsiot/mqtt_connection_builder.py

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,11 @@ def _builder(
258258
)
259259

260260

261-
def mtls_from_path(cert_filepath, pri_key_filepath, **kwargs) -> awscrt.mqtt.Connection:
261+
def mtls_from_path(
262+
cert_filepath,
263+
pri_key_filepath,
264+
cipher_suite: awscrt.io.TlsCipherPref = awscrt.io.TlsCipherPref.DEFAULT,
265+
**kwargs) -> awscrt.mqtt.Connection:
262266
"""
263267
This builder creates an :class:`awscrt.mqtt.Connection`, configured for an mTLS MQTT connection to AWS IoT.
264268
TLS arguments are passed as filepaths.
@@ -273,10 +277,15 @@ def mtls_from_path(cert_filepath, pri_key_filepath, **kwargs) -> awscrt.mqtt.Con
273277
"""
274278
_check_required_kwargs(**kwargs)
275279
tls_ctx_options = awscrt.io.TlsContextOptions.create_client_with_mtls_from_path(cert_filepath, pri_key_filepath)
280+
tls_ctx_options.cipher_pref = cipher_suite
276281
return _builder(tls_ctx_options, **kwargs)
277282

278283

279-
def mtls_from_bytes(cert_bytes, pri_key_bytes, **kwargs) -> awscrt.mqtt.Connection:
284+
def mtls_from_bytes(
285+
cert_bytes,
286+
pri_key_bytes,
287+
cipher_suite: awscrt.io.TlsCipherPref = awscrt.io.TlsCipherPref.DEFAULT,
288+
**kwargs) -> awscrt.mqtt.Connection:
280289
"""
281290
This builder creates an :class:`awscrt.mqtt.Connection`, configured for an mTLS MQTT connection to AWS IoT.
282291
TLS arguments are passed as in-memory bytes.
@@ -291,6 +300,7 @@ def mtls_from_bytes(cert_bytes, pri_key_bytes, **kwargs) -> awscrt.mqtt.Connecti
291300
"""
292301
_check_required_kwargs(**kwargs)
293302
tls_ctx_options = awscrt.io.TlsContextOptions.create_client_with_mtls(cert_bytes, pri_key_bytes)
303+
tls_ctx_options.cipher_pref = cipher_suite
294304
return _builder(tls_ctx_options, **kwargs)
295305

296306

@@ -302,6 +312,7 @@ def mtls_with_pkcs11(*,
302312
private_key_label: str = None,
303313
cert_filepath: str = None,
304314
cert_bytes=None,
315+
cipher_suite: awscrt.io.TlsCipherPref = awscrt.io.TlsCipherPref.DEFAULT,
305316
**kwargs) -> awscrt.mqtt.Connection:
306317
"""
307318
This builder creates an :class:`awscrt.mqtt.Connection`, configured for an mTLS MQTT connection to AWS IoT,
@@ -347,12 +358,15 @@ def mtls_with_pkcs11(*,
347358
private_key_label=private_key_label,
348359
cert_file_path=cert_filepath,
349360
cert_file_contents=cert_bytes)
361+
tls_ctx_options.cipher_pref = cipher_suite
350362

351363
return _builder(tls_ctx_options, **kwargs)
352364

365+
353366
def mtls_with_pkcs12(*,
354367
pkcs12_filepath: str,
355368
pkcs12_password: str,
369+
cipher_suite: awscrt.io.TlsCipherPref = awscrt.io.TlsCipherPref.DEFAULT,
356370
**kwargs) -> awscrt.mqtt.Connection:
357371
"""
358372
This builder creates an :class:`awscrt.mqtt.Connection`, configured for an mTLS MQTT connection to AWS IoT,
@@ -373,11 +387,13 @@ def mtls_with_pkcs12(*,
373387
tls_ctx_options = awscrt.io.TlsContextOptions.create_client_with_mtls_pkcs12(
374388
pkcs12_filepath=pkcs12_filepath,
375389
pkcs12_password=pkcs12_password)
390+
tls_ctx_options.cipher_suite = cipher_suite
376391
return _builder(tls_ctx_options, **kwargs)
377392

378393

379394
def mtls_with_windows_cert_store_path(*,
380395
cert_store_path: str,
396+
cipher_suite: awscrt.io.TlsCipherPref = awscrt.io.TlsCipherPref.DEFAULT,
381397
**kwargs) -> awscrt.mqtt.Connection:
382398
"""
383399
This builder creates an :class:`awscrt.mqtt.Connection`, configured for an mTLS MQTT connection to AWS IoT,
@@ -396,6 +412,7 @@ def mtls_with_windows_cert_store_path(*,
396412
_check_required_kwargs(**kwargs)
397413

398414
tls_ctx_options = awscrt.io.TlsContextOptions.create_client_with_mtls_windows_cert_store_path(cert_store_path)
415+
tls_ctx_options.cipher_pref = cipher_suite
399416

400417
return _builder(tls_ctx_options, **kwargs)
401418

@@ -404,6 +421,7 @@ def websockets_with_default_aws_signing(
404421
region,
405422
credentials_provider,
406423
websocket_proxy_options=None,
424+
cipher_suite: awscrt.io.TlsCipherPref = awscrt.io.TlsCipherPref.DEFAULT,
407425
**kwargs) -> awscrt.mqtt.Connection:
408426
"""
409427
This builder creates an :class:`awscrt.mqtt.Connection`, configured for an MQTT connection over websockets to AWS IoT.
@@ -441,12 +459,17 @@ def _sign_websocket_handshake_request(transform_args, **kwargs):
441459
except Exception as e:
442460
transform_args.set_done(e)
443461

444-
return websockets_with_custom_handshake(_sign_websocket_handshake_request, websocket_proxy_options, **kwargs)
462+
return websockets_with_custom_handshake(
463+
_sign_websocket_handshake_request,
464+
cipher_suite,
465+
websocket_proxy_options,
466+
**kwargs)
445467

446468

447469
def websockets_with_custom_handshake(
448470
websocket_handshake_transform,
449471
websocket_proxy_options=None,
472+
cipher_suite: awscrt.io.TlsCipherPref = awscrt.io.TlsCipherPref.DEFAULT,
450473
**kwargs) -> awscrt.mqtt.Connection:
451474
"""
452475
This builder creates an :class:`awscrt.mqtt.Connection`, configured for an MQTT connection over websockets,
@@ -474,6 +497,7 @@ def websockets_with_custom_handshake(
474497
"""
475498
_check_required_kwargs(**kwargs)
476499
tls_ctx_options = awscrt.io.TlsContextOptions()
500+
tls_ctx_options.cipher_pref = cipher_suite
477501
return _builder(tls_ctx_options=tls_ctx_options,
478502
use_websockets=True,
479503
websocket_handshake_transform=websocket_handshake_transform,
@@ -505,6 +529,7 @@ def direct_with_custom_authorizer(
505529
auth_password=None,
506530
auth_token_key_name=None,
507531
auth_token_value=None,
532+
cipher_suite: awscrt.io.TlsCipherPref = awscrt.io.TlsCipherPref.DEFAULT,
508533
**kwargs) -> awscrt.mqtt.Connection:
509534
"""
510535
This builder creates an :class:`awscrt.mqtt.Connection`, configured for an MQTT connection using a custom
@@ -550,8 +575,10 @@ def direct_with_custom_authorizer(
550575
auth_token_key_name=auth_token_key_name,
551576
auth_token_value=auth_token_value,
552577
use_websockets=False,
578+
cipher_suite: awscrt.io.TlsCipherPref=awscrt.io.TlsCipherPref.DEFAULT,
553579
**kwargs)
554580

581+
555582
def websockets_with_custom_authorizer(
556583
region=None,
557584
credentials_provider=None,
@@ -561,6 +588,7 @@ def websockets_with_custom_authorizer(
561588
auth_password=None,
562589
auth_token_key_name=None,
563590
auth_token_value=None,
591+
cipher_suite: awscrt.io.TlsCipherPref = awscrt.io.TlsCipherPref.DEFAULT,
564592
**kwargs) -> awscrt.mqtt.Connection:
565593
"""
566594
This builder creates an :class:`awscrt.mqtt.Connection`, configured for an MQTT connection using a custom
@@ -590,7 +618,7 @@ def websockets_with_custom_authorizer(
590618
auth_authorizer_signature (`str`): The digital signature of the token value in the `auth_token_value`
591619
parameter. The signature must be based on the private key associated with the custom authorizer. The
592620
signature must be base64 encoded.
593-
Required if the custom authorizer has signing enabled.
621+
Required if the custom authorizer has signing enabled.
594622
595623
auth_token_key_name (`str`): Key used to extract the custom authorizer token from MQTT username query-string
596624
properties.
@@ -612,19 +640,21 @@ def websockets_with_custom_authorizer(
612640
use_websockets=True,
613641
websockets_region=region,
614642
websockets_credentials_provider=credentials_provider,
643+
cipher_suite: awscrt.io.TlsCipherPref=awscrt.io.TlsCipherPref.DEFAULT,
615644
**kwargs)
616645

617646

618647
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:
648+
auth_authorizer_name=None,
649+
auth_authorizer_signature=None,
650+
auth_password=None,
651+
auth_token_key_name=None,
652+
auth_token_value=None,
653+
use_websockets=False,
654+
websockets_credentials_provider=None,
655+
websockets_region=None,
656+
cipher_suite: awscrt.io.TlsCipherPref = awscrt.io.TlsCipherPref.DEFAULT,
657+
**kwargs) -> awscrt.mqtt.Connection:
628658
"""
629659
Helper function that contains the setup needed for custom authorizers
630660
"""
@@ -657,7 +687,8 @@ def _with_custom_authorizer(auth_username=None,
657687
kwargs["password"] = auth_password
658688

659689
tls_ctx_options = awscrt.io.TlsContextOptions()
660-
if use_websockets == False:
690+
tls_ctx_options.cipher_pref = cipher_suite
691+
if not use_websockets:
661692
kwargs["port"] = 443
662693
tls_ctx_options.alpn_list = ["mqtt"]
663694

0 commit comments

Comments
 (0)