Skip to content

Commit 75764fc

Browse files
Converted Windows Cert PubSub to Windows Cert Connect
Also adjusted the README accordingly and fixed the connect samples using the wrong titles.
1 parent ca2e042 commit 75764fc

File tree

6 files changed

+133
-139
lines changed

6 files changed

+133
-139
lines changed

samples/README.md

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# Sample apps for the AWS IoT Device SDK v2 for Python
22

33
* [PubSub](#pubsub)
4-
* [PKCS#11 PubSub](#pkcs11-pubsub)
5-
* [Windows Certificate PubSub](#windows-certificate-pubsub)
4+
* [Basic Connect](#basic-connect)
5+
* [Websocket Connect](#websocket-connect)
6+
* [PKCS#11 Connect](#pkcs11-connect)
7+
* [Windows Certificate Connect](#windows-certificate-connect)
68
* [Shadow](#shadow)
79
* [Jobs](#jobs)
810
* [Fleet Provisioning](#fleet-provisioning)
@@ -156,7 +158,7 @@ python3 websocket_connect.py --endpoint <endpoint> --ca_file <file> --signing_re
156158

157159
Note that using Websockets will attempt to fetch the AWS credentials from your enviornment variables or local files. See the [authorizing direct AWS](https://docs.aws.amazon.com/iot/latest/developerguide/authorizing-direct-aws.html) page for documentation on how to get the AWS credentials, which then you can set to the `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS`, and `AWS_SESSION_TOKEN` environment variables.
158160

159-
## PKCS#11 PubSub
161+
## PKCS#11 Connect
160162

161163
This sample is similar to the [Basic Connect](#basic-connect),
162164
but the private key for mutual TLS is stored on a PKCS#11 compatible smart card or Hardware Security Module (HSM)
@@ -165,6 +167,31 @@ WARNING: Unix only. Currently, TLS integration with PKCS#11 is only available on
165167

166168
source: `samples/pkcs11_connect.py`
167169

170+
Your Thing's
171+
[Policy](https://docs.aws.amazon.com/iot/latest/developerguide/iot-policies.html)
172+
must provide privileges for this sample to connect, subscribe, publish,
173+
and receive.
174+
175+
<details>
176+
<summary>(see sample policy)</summary>
177+
<pre>
178+
{
179+
"Version": "2012-10-17",
180+
"Statement": [
181+
{
182+
"Effect": "Allow",
183+
"Action": [
184+
"iot:Connect"
185+
],
186+
"Resource": [
187+
"arn:aws:iot:<b>region</b>:<b>account</b>:client/test-*"
188+
]
189+
}
190+
]
191+
}
192+
</pre>
193+
</details>
194+
168195
To run this sample using [SoftHSM2](https://www.opendnssec.org/softhsm/) as the PKCS#11 device:
169196

170197
1) Create an IoT Thing with a certificate and key if you haven't already.
@@ -209,11 +236,11 @@ To run this sample using [SoftHSM2](https://www.opendnssec.org/softhsm/) as the
209236
python3 pkcs11_connect.py --endpoint <xxxx-ats.iot.xxxx.amazonaws.com> --ca_file <AmazonRootCA1.pem> --cert <certificate.pem.crt> --pkcs11_lib <libsofthsm2.so> --pin <user-pin> --token_label <token-label> --key_label <key-label>
210237
```
211238
212-
## Windows Certificate PubSub
239+
## Windows Certificate Connect
213240
214241
WARNING: Windows only
215242
216-
This sample is similar to the basic [PubSub](#pubsub),
243+
This sample is similar to the basic [Connect](#basic-connect),
217244
but your certificate and private key are in a
218245
[Windows certificate store](https://docs.microsoft.com/en-us/windows-hardware/drivers/install/certificate-stores),
219246
rather than simply being files on disk.
@@ -227,7 +254,32 @@ If your certificate and private key are in a
227254
[TPM](https://docs.microsoft.com/en-us/windows/security/information-protection/tpm/trusted-platform-module-overview),,
228255
you would use them by passing their certificate store path.
229256

230-
source: `samples/windows_cert_pubsub.py`
257+
source: `samples/windows_cert_connect.py`
258+
259+
Your Thing's
260+
[Policy](https://docs.aws.amazon.com/iot/latest/developerguide/iot-policies.html)
261+
must provide privileges for this sample to connect, subscribe, publish,
262+
and receive.
263+
264+
<details>
265+
<summary>(see sample policy)</summary>
266+
<pre>
267+
{
268+
"Version": "2012-10-17",
269+
"Statement": [
270+
{
271+
"Effect": "Allow",
272+
"Action": [
273+
"iot:Connect"
274+
],
275+
"Resource": [
276+
"arn:aws:iot:<b>region</b>:<b>account</b>:client/test-*"
277+
]
278+
}
279+
]
280+
}
281+
</pre>
282+
</details>
231283
232284
To run this sample with a basic certificate from AWS IoT Core:
233285
@@ -269,7 +321,7 @@ To run this sample with a basic certificate from AWS IoT Core:
269321
4) Now you can run the sample:
270322
271323
```sh
272-
python3 windows_cert_pubsub.py --endpoint xxxx-ats.iot.xxxx.amazonaws.com --root-ca AmazonRootCA.pem --cert CurrentUser\My\A11F8A9B5DF5B98BA3508FBCA575D09570E0D2C6
324+
python3 windows_cert_connect.py --endpoint xxxx-ats.iot.xxxx.amazonaws.com --ca_file AmazonRootCA.pem --cert CurrentUser\My\A11F8A9B5DF5B98BA3508FBCA575D09570E0D2C6
273325
```
274326
275327
## Shadow

samples/basic_connect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
# Parse arguments
1010
import command_line_utils
11-
cmdUtils = command_line_utils.CommandLineUtils("PubSub - Send and recieve messages through an MQTT connection.")
11+
cmdUtils = command_line_utils.CommandLineUtils("Basic Connect - Make a MQTT connection.")
1212
cmdUtils.add_common_mqtt_commands()
1313
cmdUtils.add_common_proxy_commands()
1414
cmdUtils.add_common_logging_commands()

samples/pkcs11_connect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
# Parse arguments
1818
import command_line_utils
19-
cmdUtils = command_line_utils.CommandLineUtils("PubSub - Send and recieve messages through an MQTT connection.")
19+
cmdUtils = command_line_utils.CommandLineUtils("PKCS11 Connect - Make a MQTT connection using PKCS11.")
2020
cmdUtils.add_common_mqtt_commands()
2121
cmdUtils.add_common_proxy_commands()
2222
cmdUtils.add_common_logging_commands()

samples/websocket_connect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
# Parse arguments
1010
import command_line_utils
11-
cmdUtils = command_line_utils.CommandLineUtils("PubSub - Send and recieve messages through an MQTT connection.")
11+
cmdUtils = command_line_utils.CommandLineUtils("Websocket Connect - Make a websocket MQTT connection.")
1212
cmdUtils.add_common_mqtt_commands()
1313
cmdUtils.add_common_proxy_commands()
1414
cmdUtils.add_common_logging_commands()

samples/windows_cert_connect.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0.
3+
4+
from awsiot import mqtt_connection_builder
5+
from uuid import uuid4
6+
7+
# This sample is similar to `samples/basic_connect.py` but the certificate
8+
# for mutual TLS is stored in a Windows certificate store.
9+
#
10+
# See `samples/README.md` for instructions on setting up your PC
11+
# to run this sample.
12+
#
13+
# WARNING: Windows only.
14+
15+
# Parse arguments
16+
import command_line_utils
17+
cmdUtils = command_line_utils.CommandLineUtils("Windows Cert Connect - Make a MQTT connection using Windows Store Certificates.")
18+
cmdUtils.add_common_mqtt_commands()
19+
cmdUtils.add_common_logging_commands()
20+
cmdUtils.register_command("port", "<int>",
21+
"Connection port for direct connection. " +
22+
"AWS IoT supports 433 and 8883 (optional, default=8883).",
23+
False, int)
24+
cmdUtils.register_command("client_id", "<str>",
25+
"Client ID to use for MQTT connection (optional, default='test-*').",
26+
default="test-" + str(uuid4()))
27+
cmdUtils.register_command("cert", "<path>",
28+
"Path to certificate in Windows certificate store. " +
29+
"e.g. \"CurrentUser\\MY\\6ac133ac58f0a88b83e9c794eba156a98da39b4c\"",
30+
True, str)
31+
# Needs to be called so the command utils parse the commands
32+
cmdUtils.get_args()
33+
34+
35+
def on_connection_interrupted(connection, error, **kwargs):
36+
# Callback when connection is accidentally lost.
37+
print("Connection interrupted. error: {}".format(error))
38+
39+
40+
def on_connection_resumed(connection, return_code, session_present, **kwargs):
41+
# Callback when an interrupted connection is re-established.
42+
print("Connection resumed. return_code: {} session_present: {}".format(return_code, session_present))
43+
44+
45+
if __name__ == '__main__':
46+
# Create MQTT connection
47+
mqtt_connection = mqtt_connection_builder.mtls_with_windows_cert_store_path(
48+
cert_store_path=cmdUtils.get_command_required("cert"),
49+
endpoint=cmdUtils.get_command_required(cmdUtils.m_cmd_endpoint),
50+
port=cmdUtils.get_command("port"),
51+
ca_filepath=cmdUtils.get_command(cmdUtils.m_cmd_ca_file),
52+
on_connection_interrupted=on_connection_interrupted,
53+
on_connection_resumed=on_connection_resumed,
54+
client_id=cmdUtils.get_command("client_id"),
55+
clean_session=False,
56+
keep_alive_secs=30)
57+
58+
print("Connecting to {} with client ID '{}'...".format(
59+
cmdUtils.get_command(cmdUtils.m_cmd_endpoint), cmdUtils.get_command("client_id")))
60+
61+
connect_future = mqtt_connection.connect()
62+
63+
# Future.result() waits until a result is available
64+
connect_future.result()
65+
print("Connected!")
66+
67+
# Disconnect
68+
print("Disconnecting...")
69+
disconnect_future = mqtt_connection.disconnect()
70+
disconnect_future.result()
71+
print("Disconnected!")

samples/windows_cert_pubsub.py

Lines changed: 0 additions & 129 deletions
This file was deleted.

0 commit comments

Comments
 (0)