/
index.ts
335 lines (293 loc) · 12.6 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
import { mqtt, auth, http, io, iot, iotidentity } from 'aws-iot-device-sdk-v2';
type Args = { [index: string]: any };
const fs = require('fs')
const yargs = require('yargs');
yargs.command('*', false, (yargs: any) => {
yargs
.option('endpoint', {
alias: 'e',
description: "Your AWS IoT custom endpoint, not including a port. " +
"Ex: \"abcd123456wxyz-ats.iot.us-east-1.amazonaws.com\"",
type: 'string',
required: true
})
.option('ca_file', {
alias: 'r',
description: 'FILE: path to a Root CA certficate file in PEM format.',
type: 'string',
required: false
})
.option('cert', {
alias: 'c',
description: 'FILE: path to a PEM encoded certificate to use with mTLS',
type: 'string',
required: true
})
.option('key', {
alias: 'k',
description: 'FILE: Path to a PEM encoded private key that matches cert.',
type: 'string',
required: true
})
.option('csr_file', {
alias: 'csr',
description: 'FILE: Path to a CSR file in PEM format',
type: 'string',
required: false
})
.option('client_id', {
alias: 'C',
description: 'Client ID for MQTT connection.',
type: 'string',
required: false
})
.option('template_name', {
alias: 't',
description: 'STRING: Template Name',
type: 'string',
required: true
})
.option('template_parameters', {
alias: 'tp',
description: 'Template parameters json ',
type: 'string',
required: false
})
.option('use_websocket', {
alias: 'W',
default: false,
description: 'To use a websocket instead of raw mqtt. If you ' +
'specify this option you must specify a region for signing, you can also enable proxy mode.',
type: 'boolean',
required: false
})
.option('signing_region', {
alias: 's',
default: 'us-east-1',
description: 'If you specify --use_websocket, this ' +
'is the region that will be used for computing the Sigv4 signature',
type: 'string',
required: false
})
.option('proxy_host', {
alias: 'H',
description: 'Hostname for proxy to connect to. Note: if you use this feature, ' +
'you will likely need to set --ca_file to the ca for your proxy.',
type: 'string',
required: false
})
.option('proxy_port', {
alias: 'P',
default: 8080,
description: 'Port for proxy to connect to.',
type: 'number',
required: false
})
.option('verbosity', {
alias: 'v',
description: 'BOOLEAN: Verbose output',
type: 'string',
default: 'none',
choices: ['fatal', 'error', 'warn', 'info', 'debug', 'trace', 'none']
})
.help()
.alias('help', 'h')
.showHelpOnFail(false)
}, main).parse();
async function execute_keys(identity: iotidentity.IotIdentityClient, argv: Args) {
return new Promise(async (resolve, reject) => {
try {
function keysAccepted(error?: iotidentity.IotIdentityError, response?: iotidentity.model.CreateKeysAndCertificateResponse) {
if (response) {
console.log("CreateKeysAndCertificateResponse for certificateId=" + response.certificateId);
}
if (error || !response) {
console.log("Error occurred..");
reject(error);
} else {
resolve(response.certificateOwnershipToken);
}
}
function keysRejected(error?: iotidentity.IotIdentityError, response?: iotidentity.model.ErrorResponse) {
if (response) {
console.log("CreateKeysAndCertificate ErrorResponse for " +
" statusCode=:" + response.statusCode +
" errorCode=:" + response.errorCode +
" errorMessage=:" + response.errorMessage);
}
if (error) {
console.log("Error occurred..");
}
reject(error);
}
console.log("Subscribing to CreateKeysAndCertificate Accepted and Rejected topics..");
const keysSubRequest: iotidentity.model.CreateKeysAndCertificateSubscriptionRequest = {};
await identity.subscribeToCreateKeysAndCertificateAccepted(
keysSubRequest,
mqtt.QoS.AtLeastOnce,
(error, response) => keysAccepted(error, response));
await identity.subscribeToCreateKeysAndCertificateRejected(
keysSubRequest,
mqtt.QoS.AtLeastOnce,
(error, response) => keysRejected(error, response));
console.log("Publishing to CreateKeysAndCertificate topic..");
const keysRequest: iotidentity.model.CreateKeysAndCertificateRequest = {toJSON() {return {};}};
await identity.publishCreateKeysAndCertificate(
keysRequest,
mqtt.QoS.AtLeastOnce);
}
catch (error) {
reject(error);
}
});
}
async function execute_register_thing(identity: iotidentity.IotIdentityClient, token: string, argv: Args) {
return new Promise(async (resolve, reject) => {
try {
function registerAccepted(error?: iotidentity.IotIdentityError, response?: iotidentity.model.RegisterThingResponse) {
if (response) {
console.log("RegisterThingResponse for thingName=" + response.thingName);
}
if (error) {
console.log("Error occurred..");
}
resolve();
}
function registerRejected(error?: iotidentity.IotIdentityError, response?: iotidentity.model.ErrorResponse) {
if (response) {
console.log("RegisterThing ErrorResponse for " +
"statusCode=:" + response.statusCode +
"errorCode=:" + response.errorCode +
"errorMessage=:" + response.errorMessage);
}
if (error) {
console.log("Error occurred..");
}
resolve();
}
console.log("Subscribing to RegisterThing Accepted and Rejected topics..");
const registerThingSubRequest: iotidentity.model.RegisterThingSubscriptionRequest = {templateName: argv.template_name};
await identity.subscribeToRegisterThingAccepted(
registerThingSubRequest,
mqtt.QoS.AtLeastOnce,
(error, response) => registerAccepted(error, response));
await identity.subscribeToRegisterThingRejected(
registerThingSubRequest,
mqtt.QoS.AtLeastOnce,
(error, response) => registerRejected(error, response));
console.log("Publishing to RegisterThing topic..");
const map: {[key: string]: string} = JSON.parse(argv.template_parameters);
console.log("token=" + token);
const registerThing: iotidentity.model.RegisterThingRequest = {parameters: map, templateName: argv.template_name, certificateOwnershipToken: token};
await identity.publishRegisterThing(
registerThing,
mqtt.QoS.AtLeastOnce);
}
catch (error) {
reject(error);
}
});
}
async function execute_csr(identity: iotidentity.IotIdentityClient, argv: Args) {
return new Promise(async (resolve, reject) => {
try {
function csrAccepted(error?: iotidentity.IotIdentityError, response?: iotidentity.model.CreateCertificateFromCsrResponse) {
if (response) {
console.log("CreateCertificateFromCsrResponse for certificateId=" + response.certificateId);
}
if (error || !response) {
console.log("Error occurred..");
reject(error);
} else {
resolve(response.certificateOwnershipToken);
}
}
function csrRejected(error?: iotidentity.IotIdentityError, response?: iotidentity.model.ErrorResponse) {
if (response) {
console.log("CreateCertificateFromCsr ErrorResponse for " +
"statusCode=:" + response.statusCode +
" errorCode=:" + response.errorCode +
" errorMessage=:" + response.errorMessage);
}
if (error) {
console.log("Error occurred..");
}
reject(error);
}
let csr: string = "";
try {
csr = fs.readFileSync(argv.csr_file, 'utf8');
} catch(e) {
console.log('Error reading CSR PEM file:', e.stack);
}
console.log("Subscribing to CreateCertificateFromCsr Accepted and Rejected topics..");
const csrSubRequest: iotidentity.model.CreateCertificateFromCsrSubscriptionRequest = {};
await identity.subscribeToCreateCertificateFromCsrAccepted(
csrSubRequest,
mqtt.QoS.AtLeastOnce,
(error, response) => csrAccepted(error, response));
await identity.subscribeToCreateCertificateFromCsrRejected(
csrSubRequest,
mqtt.QoS.AtLeastOnce,
(error, response) => csrRejected(error, response));
console.log("Publishing to CreateCertficateFromCsr topic..");
const csrRequest: iotidentity.model.CreateCertificateFromCsrRequest = { certificateSigningRequest: csr };
await identity.publishCreateCertificateFromCsr(
csrRequest,
mqtt.QoS.AtLeastOnce);
}
catch (error) {
reject(error);
}
});
}
async function main(argv: Args) {
if (argv.verbose != 'none') {
const level : io.LogLevel = parseInt(io.LogLevel[argv.verbosity.toUpperCase()]);
io.enable_logging(level);
}
const client_bootstrap = new io.ClientBootstrap();
let config_builder = null;
if(argv.use_websocket) {
let proxy_options = undefined;
if (argv.proxy_host) {
proxy_options = new http.HttpProxyOptions(argv.proxy_host, argv.proxy_port);
}
config_builder = iot.AwsIotMqttConnectionConfigBuilder.new_with_websockets({
region: argv.signing_region,
credentials_provider: auth.AwsCredentialsProvider.newDefault(client_bootstrap),
proxy_options: proxy_options
});
} else {
config_builder = iot.AwsIotMqttConnectionConfigBuilder.new_mtls_builder_from_path(argv.cert, argv.key);
}
if (argv.ca_file != null) {
config_builder.with_certificate_authority_from_path(undefined, argv.ca_file);
}
config_builder.with_clean_session(false);
config_builder.with_client_id(argv.client_id || "test-" + Math.floor(Math.random() * 100000000));
config_builder.with_endpoint(argv.endpoint);
// force node to wait 60 seconds before killing itself, promises do not keep node alive
const timer = setTimeout(() => {}, 60 * 1000);
const config = config_builder.build();
const client = new mqtt.MqttClient(client_bootstrap);
const connection = client.new_connection(config);
const identity = new iotidentity.IotIdentityClient(connection);
await connection.connect();
if (argv.csr_file) {
//Csr workflow
let token = await execute_csr(identity, argv);
await execute_register_thing(identity, token as string, argv);
} else {
//Keys workflow
let token = await execute_keys(identity, argv);
await execute_register_thing(identity, token as string, argv);
}
await connection.disconnect();
// Allow node to die if the promise above resolved
clearTimeout(timer);
}