Skip to content

Commit ec4b27b

Browse files
authored
Merge branch 'main' into enhance-samples
2 parents d0cabfd + d1fba5b commit ec4b27b

File tree

6 files changed

+562
-16
lines changed

6 files changed

+562
-16
lines changed

codebuild/samples/linux-smoke-tests.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ phases:
1414
build:
1515
commands:
1616
- echo Build started on `date`
17-
- $CODEBUILD_SRC_DIR/codebuild/samples/setup-linux.sh
1817
post_build:
1918
commands:
2019
- echo Build completed on `date`

codebuild/samples/setup-linux.sh

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

samples/node/pub_sub/README.md

Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
# Node: MQTT5 PubSub
2+
3+
[**Return to main sample list**](../../README.md)
4+
5+
This sample uses the
6+
[Message Broker](https://docs.aws.amazon.com/iot/latest/developerguide/iot-message-broker.html)
7+
for AWS IoT to send and receive messages through an MQTT connection using MQTT5.
8+
9+
MQTT5 introduces additional features and enhancements that improve the development experience with MQTT. You can read more about MQTT5 in the Java V2 SDK by checking out the [MQTT5 user guide](https://github.com/awslabs/aws-crt-nodejs/blob/main/MQTT5-UserGuide.md).
10+
11+
Your IoT Core Thing's [Policy](https://docs.aws.amazon.com/iot/latest/developerguide/iot-policies.html) must provide privileges for this sample to connect, subscribe, publish, and receive. Below is a sample policy that can be used on your IoT Core Thing that will allow this sample to run as intended.
12+
13+
<details>
14+
<summary>(see sample policy)</summary>
15+
<pre>
16+
{
17+
"Version": "2012-10-17",
18+
"Statement": [
19+
{
20+
"Effect": "Allow",
21+
"Action": [
22+
"iot:Publish",
23+
"iot:Receive"
24+
],
25+
"Resource": [
26+
"arn:aws:iot:<b>region</b>:<b>account</b>:topic/test/topic"
27+
]
28+
},
29+
{
30+
"Effect": "Allow",
31+
"Action": [
32+
"iot:Subscribe"
33+
],
34+
"Resource": [
35+
"arn:aws:iot:<b>region</b>:<b>account</b>:topicfilter/test/topic"
36+
]
37+
},
38+
{
39+
"Effect": "Allow",
40+
"Action": [
41+
"iot:Connect"
42+
],
43+
"Resource": [
44+
"arn:aws:iot:<b>region</b>:<b>account</b>:client/test-*"
45+
]
46+
}
47+
]
48+
}
49+
</pre>
50+
51+
Replace with the following with the data from your AWS account:
52+
* `<region>`: The AWS IoT Core region where you created your AWS IoT Core thing you wish to use with this sample. For example `us-east-1`.
53+
* `<account>`: Your AWS IoT Core account ID. This is the set of numbers in the top right next to your AWS account name when using the AWS IoT Core website.
54+
55+
Note that in a real application, you may want to avoid the use of wildcards in your ClientID or use them selectively. Please follow best practices when working with AWS on production applications using the SDK. Also, for the purposes of this sample, please make sure your policy allows a client ID of `test-*` to connect or use `--client_id <client ID here>` to send the client ID your policy supports.
56+
57+
</details>
58+
59+
## How to run
60+
61+
### Direct MQTT via mTLS
62+
63+
To Run this sample using a direct MQTT connection with a key and certificate, go to the `node/pub_sub_mqtt5` folder and run the following commands:
64+
65+
``` sh
66+
npm install
67+
node dist/index.js --endpoint <endpoint> --cert <file> --key <file>
68+
```
69+
70+
You can also pass a Certificate Authority file (CA) if your certificate and key combination requires it:
71+
72+
``` sh
73+
npm install
74+
node dist/index.js --endpoint <endpoint> --cert <file> --key <file> --ca_file <path to root CA>
75+
```
76+
77+
### Websockets
78+
79+
To Run this sample using Websockets, go to the `node/pub_sub_mqtt5` folder and run the follow commands:
80+
81+
``` sh
82+
npm install
83+
node dist/index.js --endpoint <endpoint> --region <signing region>
84+
```
85+
86+
Note that to run this sample using Websockets, you will need to set your AWS credentials in your environment 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_KEY`, and `AWS_SESSION_TOKEN` environment variables.
87+
88+
## Alternate Connection Configuration Methods supported by AWS IoT Core
89+
We strongly recommend using the AwsIotMqtt5ClientConfigBuilder class to configure MQTT5 clients when connecting to AWS IoT Core. The builder
90+
simplifies configuration for all authentication methods supported by AWS IoT Core. This section shows samples for all authentication
91+
possibilities.
92+
93+
### Authentication Methods
94+
* [Direct MQTT with X509-based mutual TLS](#direct-mqtt-with-x509-based-mutual-tls)
95+
* [MQTT over Websockets with Sigv4 authentication](#mqtt-over-websockets-with-sigv4-authentication)
96+
* [Direct MQTT with Custom Authentication](#direct-mqtt-with-custom-authentication)
97+
* [Direct MQTT with PKCS11](#direct-mqtt-with-pkcs11-method)
98+
* [Direct MQTT with PKCS12](#direct-mqtt-with-pkcs12-method)
99+
### HTTP Proxy
100+
* [Adding an HTTP Proxy](#adding-an-http-proxy)
101+
102+
#### Direct MQTT with X509-based mutual TLS
103+
For X509 based mutual TLS, you can create a client where the certificate and private key are configured by path:
104+
105+
```typescript
106+
let builder = AwsIotMqtt5ClientConfigBuilder.newDirectMqttBuilderWithMtlsFromPath(
107+
<account-specific endpoint>,
108+
<path-to-X509-certificate-pem-file>,
109+
<path-to-private-key-pem-file>
110+
);
111+
112+
// other builder configuration
113+
// ...
114+
let client : Mqtt5Client = new Mqtt5Client(builder.build()));
115+
```
116+
117+
You can also create a client where the certificate and private key are in memory:
118+
119+
```typescript
120+
let cert = fs.readFileSync(<path to certificate pem file>,'utf8');
121+
let key = fs.readFileSync(<path to private key pem file>,'utf8');
122+
let builder = AwsIotMqtt5ClientConfigBuilder.newDirectMqttBuilderWithMtlsFromMemory(
123+
<account-specific endpoint>,
124+
cert,
125+
key
126+
);
127+
128+
// other builder configuration
129+
// ...
130+
let client : Mqtt5Client = new Mqtt5Client(builder.build());
131+
```
132+
133+
#### MQTT over Websockets with Sigv4 authentication
134+
Sigv4-based authentication requires a credentials provider capable of sourcing valid AWS credentials. Sourced credentials
135+
will sign the websocket upgrade request made by the client while connecting. The default credentials provider chain supported by
136+
the SDK is capable of resolving credentials in a variety of environments according to a chain of priorities:
137+
138+
```
139+
Environment -> Profile (local file system) -> STS Web Identity -> IMDS (ec2) or ECS
140+
```
141+
142+
If the default credentials provider chain and built-in AWS region extraction logic are sufficient, you do not need to specify
143+
any additional configuration:
144+
145+
```typescript
146+
let builder = AwsIotMqtt5ClientConfigBuilder.newWebsocketMqttBuilderWithSigv4Auth(
147+
<account-specific endpoint>
148+
);
149+
// other builder configuration
150+
// ...
151+
let client : Mqtt5Client = new Mqtt5Client(builder.build());
152+
```
153+
154+
Alternatively, if you're connecting to a special region for which standard pattern matching does not work, or if you
155+
need a specific credentials provider, you can specify advanced websocket configuration options.
156+
157+
```typescript
158+
// sourcing credentials from the Cognito service in this example
159+
let cognitoConfig: CognitoCredentialsProviderConfig = {
160+
endpoint: "<cognito endpoint to query credentials from>",
161+
identity: "<cognito identity to query credentials relative to>"
162+
};
163+
164+
let overrideProvider: CredentialsProvider = AwsCredentialsProvider.newCognito(cognitoConfig);
165+
166+
let wsConfig : WebsocketSigv4Config = {
167+
credentialsProvider: overrideProvider,
168+
region: "<special case region>"
169+
};
170+
171+
let builder = AwsIotMqtt5ClientConfigBuilder.newWebsocketMqttBuilderWithSigv4Auth(
172+
"<account-specific endpoint>",
173+
wsConfig
174+
);
175+
// other builder configuration
176+
// ...
177+
let client : Mqtt5Client = new Mqtt5Client(builder.build());
178+
```
179+
180+
#### Direct MQTT with Custom Authentication
181+
AWS IoT Core Custom Authentication allows you to use a lambda to gate access to IoT Core resources. For this authentication method,
182+
you must supply an additional configuration structure containing fields relevant to AWS IoT Core Custom Authentication.
183+
184+
If your custom authenticator does not use signing, you don't specify anything related to the token signature:
185+
186+
```typescript
187+
let customAuthConfig : MqttConnectCustomAuthConfig = {
188+
authorizerName: "<Name of your custom authorizer>",
189+
username: "<Value of the username field that should be passed to the authorizer's lambda>",
190+
password: <Binary data value of the password field to be passed to the authorizer lambda>
191+
};
192+
let builder = AwsIotMqtt5ClientConfigBuilder.newDirectMqttBuilderWithCustomAuth(
193+
"<account-specific endpoint>",
194+
customAuthConfig
195+
);
196+
let client : Mqtt5Client = new mqtt5.Mqtt5Client(builder.build());
197+
```
198+
199+
If your custom authorizer uses signing, you must specify the three signed token properties as well. The token signature must be
200+
the URI-encoding of the base64 encoding of the digital signature of the token value via the private key associated with the public key
201+
that was registered with the custom authorizer. It is your responsibility to URI-encode the token signature.
202+
203+
```typescript
204+
let customAuthConfig : MqttConnectCustomAuthConfig = {
205+
authorizerName: "<Name of your custom authorizer>",
206+
username: "<Value of the username field that should be passed to the authorizer's lambda>",
207+
password: <Binary data value of the password field to be passed to the authorizer lambda>,
208+
tokenKeyName: "<Name of the username query param that will contain the token value>",
209+
tokenValue: "<Value of the username query param that holds the token value that has been signed>",
210+
tokenSignature: "<URI-encoded base64-encoded digital signature of tokenValue>"
211+
};
212+
let builder = AwsIotMqtt5ClientConfigBuilder.newDirectMqttBuilderWithCustomAuth(
213+
"<account-specific endpoint>",
214+
customAuthConfig
215+
);
216+
let client : Mqtt5Client = new mqtt5.Mqtt5Client(builder.build());
217+
```
218+
219+
In both cases, the builder will construct a final CONNECT packet username field value for you based on the values configured. Do not add the
220+
token-signing fields to the value of the username that you assign within the custom authentication config structure. Similarly, do not
221+
add any custom authentication related values to the username in the CONNECT configuration optionally attached to the client configuration.
222+
The builder will do everything for you.
223+
224+
#### Direct MQTT with PKCS11 Method
225+
226+
A MQTT5 direct connection can be made using a PKCS11 device rather than using a PEM encoded private key, the private key for mutual TLS is stored on a PKCS#11 compatible smart card or Hardware Security Module (HSM). To create a MQTT5 builder configured for this connection, see the following code:
227+
228+
```typescript
229+
let pkcs11Options : Pkcs11Options = {
230+
pkcs11_lib: "<path to PKCS11 library>",
231+
user_pin: "<Optional pin for PKCS11 device>",
232+
slot_id: "<Optional slot ID containing PKCS11 token>",
233+
token_label: "<Optional label of the PKCS11 token>",
234+
private_key_object_label: "<Optional label of the private key object on the PKCS#11 token>",
235+
cert_file_path: "<Path to certificate file. Not necessary if cert_file_contents is used>",
236+
cert_file_contents: "<Contents of certificate file. Not necessary if cert_file_path is used>"
237+
};
238+
let builder = AwsIotMqtt5ClientConfigBuilder.newDirectMqttBuilderWithMtlsFromPkcs11(
239+
"<account-specific endpoint>",
240+
pkcs11Options
241+
);
242+
let client : Mqtt5Client = new mqtt5.Mqtt5Client(builder.build());
243+
```
244+
245+
Note: Currently, TLS integration with PKCS#11 is only available on Unix devices.
246+
247+
#### Direct MQTT with PKCS12 Method
248+
249+
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:
250+
251+
```typescript
252+
let builder = AwsIotMqtt5ClientConfigBuilder.newDirectMqttBuilderWithMtlsFromPkcs12(
253+
"<account-specific endpoint>",
254+
"<PKCS12 file>",
255+
"<PKCS12 password>"
256+
);
257+
let client : Mqtt5Client = new mqtt5.Mqtt5Client(builder.build());
258+
```
259+
260+
Note: Currently, TLS integration with PKCS#12 is only available on MacOS devices.
261+
262+
### Adding An HTTP Proxy
263+
No matter what your connection transport or authentication method is, you may connect through an HTTP proxy
264+
by applying proxy configuration to the builder:
265+
266+
```typescript
267+
let builder = AwsIoTMqtt5ClientConfigBuilder.<authentication method>(...);
268+
let proxyOptions : HttpProxyOptions = new HttpProxyOptions("<proxy host>", <proxy port>);
269+
builder.withHttpProxyOptions(proxyOptions);
270+
271+
let client : Mqtt5Client = new Mqtt5Client(builder.build());
272+
```
273+
274+
SDK Proxy support also includes support for basic authentication and TLS-to-proxy. SDK proxy support does not include any additional
275+
proxy authentication methods (kerberos, NTLM, etc...) nor does it include non-HTTP proxies (SOCKS5, for example).

0 commit comments

Comments
 (0)