Skip to content

Commit 3e0c530

Browse files
authored
prioritize mqtt5 samples (#553)
* mqtt5 samples prioritization in readme
1 parent 1a0c4bc commit 3e0c530

File tree

3 files changed

+168
-42
lines changed

3 files changed

+168
-42
lines changed

documents/MQTT5_Userguide.md

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
* [Connecting to AWS IoT Core](#connecting-to-aws-iot-core)
1111
* [How to create a MQTT5 Client based on desired connection method](#how-to-create-a-mqtt5-client-based-on-desired-connection-method)
1212
* [Direct MQTT with X509-based mutual TLS](#direct-mqtt-with-x509-based-mutual-tls)
13-
* [MQTT over Websockets with Sigv4 authentication](#mqtt-over-websockets-with-sigv4-authentication)
1413
* [Direct MQTT with Custom Authentication](#direct-mqtt-with-custom-authentication)
1514
* [Direct MQTT with PKCS11 Method](#direct-mqtt-with-pkcs11-method)
1615
* [Direct MQTT with PKCS12 Method](#direct-mqtt-with-pkcs12-method)
16+
* [MQTT over Websockets with Sigv4 authentication](#mqtt-over-websockets-with-sigv4-authentication)
1717
* [MQTT over Websockets with Cognito authentication](#mqtt-over-websockets-with-cognito-authentication)
18-
* [HTTP Proxy](#http-proxy)
18+
* [Adding an HTTP Proxy](#adding-an-http-proxy)
1919
* [Client Lifecycle Management](#client-lifecycle-management)
2020
* [Lifecycle Events](#lifecycle-events)
2121
* [Client Operations](#client-operations)
@@ -77,9 +77,9 @@ For X509 based mutual TLS, you can create a client where the certificate and pri
7777

7878
```python
7979
# X.509 based certificate file
80-
certificate_file_path = "<certificate file path>"
80+
cert_file_path = "<certificate file path>"
8181
# PKCS#1 or PKCS#8 PEM encoded private key file
82-
private_key_filePath = "<private key file path>"
82+
pri_key_filepath = "<private key file path>"
8383

8484
# other builder configurations can be added using **kwargs in the builder
8585

@@ -90,30 +90,6 @@ For X509 based mutual TLS, you can create a client where the certificate and pri
9090
pri_key_filepath=private_key_filePath))
9191
```
9292

93-
#### **MQTT over Websockets with Sigv4 authentication**
94-
Sigv4-based authentication requires a credentials provider capable of sourcing valid AWS credentials. Sourced credentials
95-
will sign the websocket upgrade request made by the client while connecting. The default credentials provider chain supported by
96-
the SDK is capable of resolving credentials in a variety of environments according to a chain of priorities:
97-
98-
```Environment -> Profile (local file system) -> STS Web Identity -> IMDS (ec2) or ECS```
99-
100-
If the default credentials provider chain and built-in AWS region extraction logic are sufficient, you do not need to specify
101-
any additional configuration:
102-
103-
```python
104-
# The signing region. e.x.: 'us-east-1'
105-
signing_region = "<signing region>"
106-
credentials_provider = auth.AwsCredentialsProvider.new_default_chain()
107-
108-
# other builder configurations can be added using **kwargs in the builder
109-
110-
# Create an MQTT5 Client using mqtt5_client_builder
111-
client = mqtt5_client_builder.websockets_with_default_aws_signing(
112-
endpoint = "<account-specific endpoint>",
113-
region = signing_region,
114-
credentials_provider=credentials_provider))
115-
```
116-
11793
#### **Direct MQTT with Custom Authentication**
11894
AWS IoT Core Custom Authentication allows you to use a lambda to gate access to IoT Core resources. For this authentication method,
11995
you must supply an additional configuration structure containing fields relevant to AWS IoT Core Custom Authentication.
@@ -129,7 +105,7 @@ If your custom authenticator does not use signing, you don't specify anything re
129105
auth_password = <Binary data value of the password field to be passed to the authorizer lambda>)
130106
```
131107

132-
If your custom authorizer uses signing, you must specify the three signed token properties as well. The token signature must be the URI-encoding of the base64 encoding of the digital signature of the token value via the private key associated with the public key that was registered with the custom authorizer. It is your responsibility to URI-encode the token signature.
108+
If your custom authorizer uses signing, you must specify the three signed token properties as well. It is your responsibility to URI-encode the auth_username, auth_authorizer_name, and auth_token_key_name parameters.
133109

134110
```python
135111
# other builder configurations can be added using **kwargs in the builder
@@ -182,6 +158,30 @@ A MQTT5 direct connection can be made using a PKCS12 file rather than using a PE
182158

183159
**Note**: Currently, TLS integration with PKCS#12 is only available on MacOS devices.
184160

161+
#### **MQTT over Websockets with Sigv4 authentication**
162+
Sigv4-based authentication requires a credentials provider capable of sourcing valid AWS credentials. Sourced credentials
163+
will sign the websocket upgrade request made by the client while connecting. The default credentials provider chain supported by
164+
the SDK is capable of resolving credentials in a variety of environments according to a chain of priorities:
165+
166+
```Environment -> Profile (local file system) -> STS Web Identity -> IMDS (ec2) or ECS```
167+
168+
If the default credentials provider chain and built-in AWS region extraction logic are sufficient, you do not need to specify
169+
any additional configuration:
170+
171+
```python
172+
# The signing region. e.x.: 'us-east-1'
173+
signing_region = "<signing region>"
174+
credentials_provider = auth.AwsCredentialsProvider.new_default_chain()
175+
176+
# other builder configurations can be added using **kwargs in the builder
177+
178+
# Create an MQTT5 Client using mqtt5_client_builder
179+
client = mqtt5_client_builder.websockets_with_default_aws_signing(
180+
endpoint = "<account-specific endpoint>",
181+
region = signing_region,
182+
credentials_provider=credentials_provider))
183+
```
184+
185185
#### **MQTT over Websockets with Cognito authentication**
186186

187187
A MQTT5 websocket connection can be made using Cognito to authenticate rather than the AWS credentials located on the device or via key and certificate. Instead, Cognito can authenticate the connection using a valid Cognito identity ID. This requires a valid Cognito identity ID, which can be retrieved from a Cognito identity pool. A Cognito identity pool can be created from the AWS console.
@@ -211,7 +211,7 @@ To create a MQTT5 builder configured for this connection, see the following code
211211

212212
**Note**: A Cognito identity ID is different from a Cognito identity pool ID and trying to connect with a Cognito identity pool ID will not work. If you are unable to connect, make sure you are passing a Cognito identity ID rather than a Cognito identity pool ID.
213213

214-
#### **HTTP Proxy**
214+
### **Adding an HTTP Proxy**
215215
No matter what your connection transport or authentication method is, you may connect through an HTTP proxy
216216
by adding the http_proxy_options keyword argument to the builder:
217217

samples/README.md

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,43 @@
11
# Sample apps for the AWS IoT Device SDK v2 for Python
2-
2+
## MQTT5 Samples
3+
#### MQTT5 is the recommended MQTT Client. It has many benefits over MQTT311 outlined in the [MQTT5 User Guide](../documents/MQTT5_Userguide.md)
34
* [MQTT5 PubSub](./mqtt5_pubsub.md)
4-
* [PubSub](./pubsub.md)
5+
+ [Direct MQTT with X509-based mutual TLS](./mqtt5_pubsub.md#direct-mqtt-with-x509-based-mutual-tls)
6+
+ [Direct MQTT with PKCS12 Method](./mqtt5_pubsub.md#direct-mqtt-with-pkcs12-method)
7+
+ [MQTT over Websockets with Sigv4 authentication](./mqtt5_pubsub.md#mqtt-over-websockets-with-sigv4-authentication)
8+
+ [MQTT over Websockets with Cognito authentication](./mqtt5_pubsub.md#mqtt-over-websockets-with-cognito-authentication)
59
* [MQTT5 Shared Subscription](./mqtt5_shared_subscription.md)
10+
* [MQTT5 PKCS#11 Connect](./mqtt5_pkcs11_connect.md)
11+
* [MQTT5 Custom Authorizer Connect](./mqtt5_custom_authorizer_connect.md)
12+
* [MQTT5 Shadow](./shadow_mqtt5.md)
13+
* [MQTT5 Jobs](./jobs_mqtt5.md)
14+
* [MQTT5 Fleet Provisioning](./fleetprovisioning_mqtt5.md)
15+
## MQTT311 Samples
16+
* [PubSub](./pubsub.md)
617
* [Basic Connect](./basic_connect.md)
718
* [Websocket Connect](./websocket_connect.md)
8-
* [MQTT5 PKCS#11 Connect](./mqtt5_pkcs11_connect.md)
919
* [PKCS#11 Connect](./pkcs11_connect.md)
1020
* [PKCS#12 Connect](./pkcs12_connect.md)
1121
* [Windows Certificate Connect](./windows_cert_connect/README.md)
12-
* [MQTT5 Custom Authorizer Connect](./mqtt5_custom_authorizer_connect.md)
1322
* [Custom Authorizer Connect](./custom_authorizer_connect.md)
1423
* [Cognito Connect](./cognito_connect.md)
1524
* [X509 Connect](./x509_connect.md)
1625
* [Shadow](./shadow.md)
17-
* [MQTT5 Shadow](./shadow_mqtt5.md)
1826
* [Jobs](./jobs.md)
19-
* [MQTT5 Jobs](./jobs_mqtt5.md)
2027
* [Fleet Provisioning](./fleetprovisioning.md)
21-
* [MQTT5 Fleet Provisioning](./fleetprovisioning_mqtt5.md)
28+
## Other
2229
* [Greengrass Discovery](./basic_discovery.md)
2330
* [Greengrass IPC](./ipc_greengrass.md)
2431

2532
### Build instructions
2633

2734
First, install the `aws-iot-devices-sdk-python-v2` with following the instructions from [Installation](../README.md#Installation).
2835

29-
Each sample README has instructions on how to run each sample with the same name as the sample itself. For example, the [PubSub README](./pubsub.md) is `pubsub.md` and it can be run with the following:
36+
Each sample README has instructions on how to run each sample with the same name as the sample itself. For example, the [MQTT5 PubSub README](./mqtt5_pubsub.md) is `mqtt5_pubsub.md` and it can be run with the following:
3037

3138
``` sh
3239
# For Windows: replace 'python3' with 'python' and '/' with '\'
33-
python3 pubsub.py --endpoint <endpoint> --cert <path to certificate> --key <path to private key>
40+
python3 mqtt5_pubsub.py --endpoint <endpoint> --cert <path to certificate> --key <path to private key>
3441
```
3542

3643
### Sample Help
@@ -39,7 +46,7 @@ All samples will show their options by passing in `--help`. For example:
3946

4047
``` sh
4148
# For Windows: replace 'python3' with 'python' and '/' with '\'
42-
python3 pubsub.py --help
49+
python3 mqtt5_pubsub.py --help
4350
```
4451

4552
Which will result in output showing all of the options that can be passed in at the command line, along with descriptions of what each does and whether they are optional or not.
@@ -48,9 +55,9 @@ Which will result in output showing all of the options that can be passed in at
4855

4956
To enable logging in the samples, you need to pass the `--verbosity` as an additional argument. `--verbosity` controls the level of logging shown. `--verbosity` can be set to `Trace`, `Debug`, `Info`, `Warn`, `Error`, `Fatal`, or `None`.
5057

51-
For example, to run [PubSub](./pubsub/README.md) sample with logging you could use the following:
58+
For example, to run [MQTT5 PubSub](./mqtt5_pubsub.md) sample with logging you could use the following:
5259

5360
``` sh
5461
# For Windows: replace 'python3' with 'python' and '/' with '\'
55-
python3 pubsub.py <other arguments> --verbosity Debug
62+
python3 mqtt5_pubsub.py <other arguments> --verbosity Debug
5663
```

samples/mqtt5_pubsub.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,122 @@ You can also pass a Certificate Authority file (CA) if your certificate and key
7171
# For Windows: replace 'python3' with 'python' and '/' with '\'
7272
python3 mqtt5_pubsub.py --endpoint <endpoint> --cert <file> --key <file> --ca_file <file>
7373
```
74+
75+
## **Alternate Connection Configuration Methods supported by AWS IoT Core**
76+
### **Optional Keyword Arguments**
77+
All lifecycle events and the callback for publishes received by the MQTT5 Client should be added to the builder on creation of the Client. A full list of accepted arguments can be found in the API guide.
78+
79+
* [Direct MQTT with X509-based mutual TLS](#direct-mqtt-with-x509-based-mutual-tls)
80+
* [Direct MQTT with Custom Authentication](./mqtt5_custom_authorizer_connect.md)
81+
* [Direct MQTT with PKCS11 Method](./mqtt5_pkcs11_connect.md)
82+
* [Direct MQTT with PKCS12 Method](#direct-mqtt-with-pkcs12-method)
83+
* [MQTT over Websockets with Sigv4 authentication](#mqtt-over-websockets-with-sigv4-authentication)
84+
* [MQTT over Websockets with Cognito authentication](#mqtt-over-websockets-with-cognito-authentication)
85+
### HTTP Proxy
86+
* [Adding an HTTP Proxy](#adding-an-http-proxy)
87+
88+
#### **Direct MQTT with X509-based mutual TLS**
89+
For X509 based mutual TLS, you can create a client where the certificate and private key are configured by path:
90+
91+
```python
92+
# X.509 based certificate file
93+
cert_file_path = "<certificate file path>"
94+
# PKCS#1 or PKCS#8 PEM encoded private key file
95+
pri_key_filepath = "<private key file path>"
96+
97+
# other builder configurations can be added using **kwargs in the builder
98+
99+
# Create an MQTT5 Client using mqtt5_client_builder
100+
client = mqtt5_client_builder.mtls_from_path(
101+
endpoint = "<account-specific endpoint>",
102+
cert_filepath=certificate_file_path,
103+
pri_key_filepath=private_key_filePath))
104+
```
105+
106+
#### **Direct MQTT with PKCS12 Method**
107+
108+
A MQTT5 direct connection can be made using a PKCS12 file rather than using a PEM encoded private key. To create a MQTT5 builder configured for this connection, see the following code:
109+
110+
```python
111+
# other builder configurations can be added using **kwargs in the builder
112+
113+
client = mqtt5_client_builder.mtls_with_pkcs12(
114+
pkcs12_filepath = "<PKCS12 file path>,
115+
pkcs12_password = "<PKCS12 password>
116+
endpoint = "<account-specific endpoint>")
117+
```
118+
119+
**Note**: Currently, TLS integration with PKCS#12 is only available on MacOS devices.
120+
121+
#### **MQTT over Websockets with Sigv4 authentication**
122+
Sigv4-based authentication requires a credentials provider capable of sourcing valid AWS credentials. Sourced credentials
123+
will sign the websocket upgrade request made by the client while connecting. The default credentials provider chain supported by
124+
the SDK is capable of resolving credentials in a variety of environments according to a chain of priorities:
125+
126+
```Environment -> Profile (local file system) -> STS Web Identity -> IMDS (ec2) or ECS```
127+
128+
If the default credentials provider chain and built-in AWS region extraction logic are sufficient, you do not need to specify
129+
any additional configuration:
130+
131+
```python
132+
# The signing region. e.x.: 'us-east-1'
133+
signing_region = "<signing region>"
134+
credentials_provider = auth.AwsCredentialsProvider.new_default_chain()
135+
136+
# other builder configurations can be added using **kwargs in the builder
137+
138+
# Create an MQTT5 Client using mqtt5_client_builder
139+
client = mqtt5_client_builder.websockets_with_default_aws_signing(
140+
endpoint = "<account-specific endpoint>",
141+
region = signing_region,
142+
credentials_provider=credentials_provider))
143+
```
144+
145+
#### **MQTT over Websockets with Cognito authentication**
146+
147+
A MQTT5 websocket connection can be made using Cognito to authenticate rather than the AWS credentials located on the device or via key and certificate. Instead, Cognito can authenticate the connection using a valid Cognito identity ID. This requires a valid Cognito identity ID, which can be retrieved from a Cognito identity pool. A Cognito identity pool can be created from the AWS console.
148+
149+
To create a MQTT5 builder configured for this connection, see the following code:
150+
151+
```python
152+
# The signing region. e.x.: 'us-east-1'
153+
signing_region = "<signing region>"
154+
155+
# See https://docs.aws.amazon.com/general/latest/gr/cognito_identity.html for Cognito endpoints
156+
cognito_endpoint = "cognito-identity." + signing_region + ".amazonaws.com"
157+
cognito_identity_id = "<Cognito identity ID>"
158+
credentials_provider = auth.AwsCredentialsProvider.new_cognito(
159+
endpoint=cognito_endpoint,
160+
identity=cognito_identity_id,
161+
tls_ctx=io.ClientTlsContext(TlsContextOptions()))
162+
163+
# other builder configurations can be added using **kwargs in the builder
164+
165+
# Create an MQTT5 Client using mqtt5_client_builder
166+
client = mqtt5_client_builder.websockets_with_default_aws_signing(
167+
endpoint = "<account-specific endpoint>",
168+
region = signing_region,
169+
credentials_provider=credentials_provider))
170+
```
171+
172+
**Note**: A Cognito identity ID is different from a Cognito identity pool ID and trying to connect with a Cognito identity pool ID will not work. If you are unable to connect, make sure you are passing a Cognito identity ID rather than a Cognito identity pool ID.
173+
174+
## **Adding an HTTP Proxy**
175+
No matter what your connection transport or authentication method is, you may connect through an HTTP proxy
176+
by adding the http_proxy_options keyword argument to the builder:
177+
178+
```python
179+
http_proxy_options = http.HttpProxyOptions(
180+
host_name = "<proxy host>",
181+
port = <proxy port>)
182+
183+
# Create an MQTT5 Client using mqtt5_client_builder with proxy options as keyword argument
184+
client = mqtt5_client_builder.mtls_from_path(
185+
endpoint = "<account-specific endpoint>",
186+
cert_filepath = "<certificate file path>",
187+
pri_key_filepath = "<private key file path>",
188+
http_proxy_options = http_proxy_options))
189+
```
190+
191+
SDK Proxy support also includes support for basic authentication and TLS-to-proxy. SDK proxy support does not include any additional
192+
proxy authentication methods (kerberos, NTLM, etc...) nor does it include non-HTTP proxies (SOCKS5, for example).

0 commit comments

Comments
 (0)