From 5ee757dffba2cd359795143c3cdbf5c1f9cac23a Mon Sep 17 00:00:00 2001 From: Zane Bauman Date: Thu, 4 May 2023 16:40:00 -0400 Subject: [PATCH 1/2] docs: add examples using cert/key pair --- .../minimqtt_adafruitio_native_networking.py | 11 ++++++++++- .../minimqtt_pub_sub_blocking_native_networking.py | 11 ++++++++++- ..._sub_blocking_topic_callbacks_native_networking.py | 11 ++++++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/examples/native_networking/minimqtt_adafruitio_native_networking.py b/examples/native_networking/minimqtt_adafruitio_native_networking.py index 21661d31..b16aaf80 100644 --- a/examples/native_networking/minimqtt_adafruitio_native_networking.py +++ b/examples/native_networking/minimqtt_adafruitio_native_networking.py @@ -36,6 +36,7 @@ ### Code ### + # Define callback methods which are called when events occur # pylint: disable=unused-argument, redefined-outer-name def connected(client, userdata, flags, rc): @@ -59,6 +60,14 @@ def message(client, topic, message): # Create a socket pool pool = socketpool.SocketPool(wifi.radio) +ssl_context = ssl.create_default_context() + +# If you need to use certificate/key pair authentication (e.g. X.509), you can load them in the +# ssl context by uncommenting the line below +# ssl_context.load_cert_chain( +# certfile=secrets['device_cert_path'], +# keyfile=secrets['device_key_path'] +# ) # Set up a MiniMQTT Client mqtt_client = MQTT.MQTT( @@ -67,7 +76,7 @@ def message(client, topic, message): username=secrets["aio_username"], password=secrets["aio_key"], socket_pool=pool, - ssl_context=ssl.create_default_context(), + ssl_context=ssl_context, ) # Setup the callback methods above diff --git a/examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py b/examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py index 58dbc7f7..4016433d 100644 --- a/examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py +++ b/examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py @@ -32,6 +32,7 @@ # Setup a feed named `testfeed` for publishing. default_topic = secrets["aio_username"] + "/feeds/testfeed" + ### Code ### # Define callback methods which are called when events occur # pylint: disable=unused-argument, redefined-outer-name @@ -59,6 +60,14 @@ def message(client, topic, message): # Create a socket pool pool = socketpool.SocketPool(wifi.radio) +ssl_context = ssl.create_default_context() + +# If you need to use certificate/key pair authentication (e.g. X.509), you can load them in the +# ssl context by uncommenting the line below +# ssl_context.load_cert_chain( +# certfile=secrets['device_cert_path'], +# keyfile=secrets['device_key_path'] +# ) # Set up a MiniMQTT Client mqtt_client = MQTT.MQTT( @@ -67,7 +76,7 @@ def message(client, topic, message): username=secrets["aio_username"], password=secrets["aio_key"], socket_pool=pool, - ssl_context=ssl.create_default_context(), + ssl_context=ssl_context, ) # Setup the callback methods above diff --git a/examples/native_networking/minimqtt_pub_sub_blocking_topic_callbacks_native_networking.py b/examples/native_networking/minimqtt_pub_sub_blocking_topic_callbacks_native_networking.py index 2a2eddf3..433a4376 100644 --- a/examples/native_networking/minimqtt_pub_sub_blocking_topic_callbacks_native_networking.py +++ b/examples/native_networking/minimqtt_pub_sub_blocking_topic_callbacks_native_networking.py @@ -29,6 +29,7 @@ ### Code ### + # Define callback methods which are called when events occur # pylint: disable=unused-argument, redefined-outer-name def connected(client, userdata, flags, rc): @@ -66,6 +67,14 @@ def on_message(client, topic, message): # Create a socket pool pool = socketpool.SocketPool(wifi.radio) +ssl_context = ssl.create_default_context() + +# If you need to use certificate/key pair authentication (e.g. X.509), you can load them in the +# ssl context by uncommenting the line below +# ssl_context.load_cert_chain( +# certfile=secrets['device_cert_path'], +# keyfile=secrets['device_key_path'] +# ) # Set up a MiniMQTT Client client = MQTT.MQTT( @@ -74,7 +83,7 @@ def on_message(client, topic, message): username=secrets["aio_username"], password=secrets["aio_key"], socket_pool=pool, - ssl_context=ssl.create_default_context(), + ssl_context=ssl_context, ) # Setup the callback methods above From 877f7bd6537ec10066fdc0f456409fdbcca444bf Mon Sep 17 00:00:00 2001 From: Zane Bauman Date: Fri, 5 May 2023 07:55:48 -0400 Subject: [PATCH 2/2] docs: add more context --- .../minimqtt_adafruitio_native_networking.py | 8 +++++--- .../minimqtt_pub_sub_blocking_native_networking.py | 8 +++++--- ..._pub_sub_blocking_topic_callbacks_native_networking.py | 8 +++++--- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/examples/native_networking/minimqtt_adafruitio_native_networking.py b/examples/native_networking/minimqtt_adafruitio_native_networking.py index b16aaf80..75720b31 100644 --- a/examples/native_networking/minimqtt_adafruitio_native_networking.py +++ b/examples/native_networking/minimqtt_adafruitio_native_networking.py @@ -63,10 +63,12 @@ def message(client, topic, message): ssl_context = ssl.create_default_context() # If you need to use certificate/key pair authentication (e.g. X.509), you can load them in the -# ssl context by uncommenting the line below +# ssl context by uncommenting the lines below and adding the following keys to the "secrets" +# dictionary in your secrets.py file: +# "device_cert_path" - Path to the Device Certificate +# "device_key_path" - Path to the RSA Private Key # ssl_context.load_cert_chain( -# certfile=secrets['device_cert_path'], -# keyfile=secrets['device_key_path'] +# certfile=secrets["device_cert_path"], keyfile=secrets["device_key_path"] # ) # Set up a MiniMQTT Client diff --git a/examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py b/examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py index 4016433d..b296eacc 100644 --- a/examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py +++ b/examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py @@ -63,10 +63,12 @@ def message(client, topic, message): ssl_context = ssl.create_default_context() # If you need to use certificate/key pair authentication (e.g. X.509), you can load them in the -# ssl context by uncommenting the line below +# ssl context by uncommenting the lines below and adding the following keys to the "secrets" +# dictionary in your secrets.py file: +# "device_cert_path" - Path to the Device Certificate +# "device_key_path" - Path to the RSA Private Key # ssl_context.load_cert_chain( -# certfile=secrets['device_cert_path'], -# keyfile=secrets['device_key_path'] +# certfile=secrets["device_cert_path"], keyfile=secrets["device_key_path"] # ) # Set up a MiniMQTT Client diff --git a/examples/native_networking/minimqtt_pub_sub_blocking_topic_callbacks_native_networking.py b/examples/native_networking/minimqtt_pub_sub_blocking_topic_callbacks_native_networking.py index 433a4376..f38b627d 100644 --- a/examples/native_networking/minimqtt_pub_sub_blocking_topic_callbacks_native_networking.py +++ b/examples/native_networking/minimqtt_pub_sub_blocking_topic_callbacks_native_networking.py @@ -70,10 +70,12 @@ def on_message(client, topic, message): ssl_context = ssl.create_default_context() # If you need to use certificate/key pair authentication (e.g. X.509), you can load them in the -# ssl context by uncommenting the line below +# ssl context by uncommenting the lines below and adding the following keys to the "secrets" +# dictionary in your secrets.py file: +# "device_cert_path" - Path to the Device Certificate +# "device_key_path" - Path to the RSA Private Key # ssl_context.load_cert_chain( -# certfile=secrets['device_cert_path'], -# keyfile=secrets['device_key_path'] +# certfile=secrets["device_cert_path"], keyfile=secrets["device_key_path"] # ) # Set up a MiniMQTT Client