|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0. |
| 3 | + |
| 4 | +from awscrt import io, http, auth |
| 5 | +from uuid import uuid4 |
| 6 | +from awsiot import mqtt_connection_builder |
| 7 | + |
| 8 | +# This sample shows how to create a MQTT connection using X509 files to connect. |
| 9 | +# This sample is intended to be used as a reference for making MQTT connections via X509. |
| 10 | + |
| 11 | +# Parse arguments |
| 12 | +import utils.command_line_utils as command_line_utils |
| 13 | +cmdUtils = command_line_utils.CommandLineUtils("X509 Connect - Make a MQTT connection using X509.") |
| 14 | +cmdUtils.add_common_mqtt_commands() |
| 15 | +cmdUtils.add_common_proxy_commands() |
| 16 | +cmdUtils.add_common_logging_commands() |
| 17 | +cmdUtils.add_common_x509_commands() |
| 18 | +cmdUtils.register_command("signing_region", "<str>", |
| 19 | + "The signing region used for the websocket signer", |
| 20 | + True, str) |
| 21 | +cmdUtils.register_command("client_id", "<str>", |
| 22 | + "Client ID to use for MQTT connection (optional, default='test-*').", |
| 23 | + default="test-" + str(uuid4())) |
| 24 | +cmdUtils.register_command("is_ci", "<str>", "If present the sample will run in CI mode (optional, default='None')") |
| 25 | +# Needs to be called so the command utils parse the commands |
| 26 | +cmdUtils.get_args() |
| 27 | +is_ci = cmdUtils.get_command("is_ci", None) is not None |
| 28 | + |
| 29 | +# Callback when connection is accidentally lost. |
| 30 | +def on_connection_interrupted(connection, error, **kwargs): |
| 31 | + print(f"Connection interrupted. error: {error}") |
| 32 | + |
| 33 | +# Callback when an interrupted connection is re-established. |
| 34 | +def on_connection_resumed(connection, return_code, session_present, **kwargs): |
| 35 | + print(f"Connection resumed. return_code: {return_code} session_present: {session_present}") |
| 36 | + |
| 37 | + |
| 38 | +if __name__ == '__main__': |
| 39 | + |
| 40 | + ############################################################ |
| 41 | + # Pull data from the command line |
| 42 | + ############################################################ |
| 43 | + input_endpoint = cmdUtils.get_command_required("endpoint") |
| 44 | + input_signing_region = cmdUtils.get_command_required("signing_region") |
| 45 | + input_ca_file = cmdUtils.get_command("ca_file") |
| 46 | + input_client_id = cmdUtils.get_command_required("client_id") |
| 47 | + |
| 48 | + input_proxy_host = cmdUtils.get_command("proxy_host") |
| 49 | + input_proxy_port = cmdUtils.get_command("proxy_port") |
| 50 | + |
| 51 | + input_x509_endpoint = cmdUtils.get_command_required("x509_endpoint") |
| 52 | + input_x509_thing_name = cmdUtils.get_command_required("x509_thing_name") |
| 53 | + input_x509_role_alias = cmdUtils.get_command_required("x509_role_alias") |
| 54 | + input_x509_cert = cmdUtils.get_command_required("x509_cert") |
| 55 | + input_x509_key = cmdUtils.get_command_required("x509_key") |
| 56 | + input_x509_ca_file = cmdUtils.get_command("x509_ca_file") |
| 57 | + |
| 58 | + ############################################################ |
| 59 | + # Set up and create the MQTT connection |
| 60 | + ############################################################ |
| 61 | + |
| 62 | + # Set up the config needed to make a MQTT connection |
| 63 | + |
| 64 | + proxy_options = None |
| 65 | + if input_proxy_host is not None and input_proxy_port is not None: |
| 66 | + proxy_options = http.HttpProxyOptions( |
| 67 | + host_name=input_proxy_host, |
| 68 | + port=input_proxy_port) |
| 69 | + |
| 70 | + x509_tls_options = io.TlsContextOptions.create_client_with_mtls_from_path(input_x509_cert, input_x509_key) |
| 71 | + x509_tls_options.ca_dirpath = input_x509_ca_file |
| 72 | + x509_tls_context = io.ClientTlsContext(x509_tls_options) |
| 73 | + |
| 74 | + x509_provider = auth.AwsCredentialsProvider.new_x509( |
| 75 | + endpoint=input_x509_endpoint, |
| 76 | + thing_name=input_x509_thing_name, |
| 77 | + role_alias=input_x509_role_alias, |
| 78 | + tls_ctx=x509_tls_context, |
| 79 | + http_proxy_options=proxy_options |
| 80 | + ) |
| 81 | + |
| 82 | + # Create the MQTT connection from the configuration |
| 83 | + |
| 84 | + mqtt_connection = mqtt_connection_builder.websockets_with_default_aws_signing( |
| 85 | + endpoint=input_endpoint, |
| 86 | + region=input_signing_region, |
| 87 | + credentials_provider=x509_provider, |
| 88 | + http_proxy_options=proxy_options, |
| 89 | + ca_filepath=input_ca_file, |
| 90 | + on_connection_interrupted=on_connection_interrupted, |
| 91 | + on_connection_resumed=on_connection_resumed, |
| 92 | + client_id=input_client_id, |
| 93 | + clean_session=False, |
| 94 | + keep_alive_secs=30) |
| 95 | + |
| 96 | + ############################################################ |
| 97 | + # Use the MQTT connection to connect and disconnect |
| 98 | + ############################################################ |
| 99 | + |
| 100 | + if not is_ci: |
| 101 | + print (f"Connecting to {input_endpoint} with client ID '{input_client_id}'...") |
| 102 | + else: |
| 103 | + print("Connecting to endpoint with client ID") |
| 104 | + |
| 105 | + # Connect |
| 106 | + connect_future = mqtt_connection.connect() |
| 107 | + |
| 108 | + # Future.result() waits until a result is available |
| 109 | + connect_future.result() |
| 110 | + print("Connected!") |
| 111 | + |
| 112 | + # Disconnect |
| 113 | + print("Disconnecting...") |
| 114 | + disconnect_future = mqtt_connection.disconnect() |
| 115 | + disconnect_future.result() |
| 116 | + print("Disconnected!") |
0 commit comments