33 * SPDX-License-Identifier: Apache-2.0.
44 */
55
6- import { iotidentity } from 'aws-iot-device-sdk-v2' ;
6+ import { iotidentity , mqtt5 , iot } from 'aws-iot-device-sdk-v2' ;
77import { once } from "events"
88import fs from "fs" ;
9-
10- type Args = { [ index : string ] : any } ;
11- const yargs = require ( 'yargs' ) ;
12-
13- // The relative path is '../../../util/cli_args' from here, but the compiled javascript file gets put one level
14- // deeper inside the 'dist' folder
15- const common_args = require ( '../../../../util/cli_args' ) ;
16-
17- yargs . command ( '*' , false , ( yargs : any ) => {
18- common_args . add_direct_connection_establishment_arguments ( yargs ) ;
19- yargs
20- . option ( 'csr_file' , {
21- alias : 'csr' ,
22- description : '<path>: Path to a CSR file in PEM format.' ,
23- type : 'string' ,
24- required : true
25- } )
26- . option ( 'template_name' , {
27- alias : 't' ,
28- description : 'Template Name.' ,
29- type : 'string' ,
30- required : true
31- } )
32- . option ( 'template_parameters' , {
33- alias : 'tp' ,
34- description : '<json>: Template parameters json.' ,
35- type : 'string' ,
36- required : false
37- } )
38- } , main ) . parse ( ) ;
39-
40- async function main ( argv : Args ) {
41- common_args . apply_sample_arguments ( argv ) ;
42-
43- const csr : string = fs . readFileSync ( argv . csr_file , 'utf8' ) ;
9+ import { v4 as uuidv4 } from 'uuid' ;
10+
11+ const TIMEOUT = 100000 ;
12+
13+ // --------------------------------- ARGUMENT PARSING -----------------------------------------
14+ const args = require ( 'yargs' )
15+ . option ( 'endpoint' , {
16+ alias : 'e' ,
17+ description : 'IoT endpoint hostname' ,
18+ type : 'string' ,
19+ required : true
20+ } )
21+ . option ( 'cert' , {
22+ alias : 'c' ,
23+ description : 'Path to the certificate file to use during mTLS connection establishment' ,
24+ type : 'string' ,
25+ required : true
26+ } )
27+ . option ( 'key' , {
28+ alias : 'k' ,
29+ description : 'Path to the private key file to use during mTLS connection establishment' ,
30+ type : 'string' ,
31+ required : true
32+ } )
33+ . option ( 'client_id' , {
34+ alias : 'C' ,
35+ description : 'Client ID' ,
36+ type : 'string' ,
37+ default : `fleet-provisioning-csr-${ uuidv4 ( ) . substring ( 0 , 8 ) } `
38+ } )
39+ . option ( 'csr_file' , {
40+ alias : 'csr' ,
41+ description : '<path>: Path to a CSR file in PEM format' ,
42+ type : 'string' ,
43+ required : true
44+ } )
45+ . option ( 'template_name' , {
46+ alias : 't' ,
47+ description : 'Template Name' ,
48+ type : 'string' ,
49+ required : true
50+ } )
51+ . option ( 'template_parameters' , {
52+ alias : 'tp' ,
53+ description : '<json>: Template parameters json' ,
54+ type : 'string' ,
55+ required : false
56+ } )
57+ . help ( )
58+ . argv ;
59+
60+ // --------------------------------- ARGUMENT PARSING END -----------------------------------------
61+
62+ async function main ( ) {
63+ const csr : string = fs . readFileSync ( args . csr_file , 'utf8' ) ;
4464
4565 console . log ( "Connecting..." ) ;
46- let protocolClient = common_args . build_mqtt5_client_from_cli_args ( argv ) ;
66+
67+ // Create MQTT5 client using mutual TLS via X509 Certificate and Private Key
68+ const builder = iot . AwsIotMqtt5ClientConfigBuilder . newDirectMqttBuilderWithMtlsFromPath (
69+ args . endpoint ,
70+ args . cert ,
71+ args . key
72+ ) ;
73+
74+ builder . withConnectProperties ( {
75+ clientId : args . client_id ,
76+ keepAliveIntervalSeconds : 1200
77+ } ) ;
78+
79+ const config = builder . build ( ) ;
80+ const protocolClient = new mqtt5 . Mqtt5Client ( config ) ;
81+
4782 let identityClient = iotidentity . IotIdentityClientv2 . newFromMqtt5 ( protocolClient , {
4883 maxRequestResponseSubscriptions : 2 ,
4984 maxStreamingSubscriptions : 0 ,
@@ -53,7 +88,10 @@ async function main(argv: Args) {
5388 const connectionSuccess = once ( protocolClient , "connectionSuccess" ) ;
5489 protocolClient . start ( ) ;
5590
56- await connectionSuccess ;
91+ await Promise . race ( [
92+ connectionSuccess ,
93+ new Promise ( ( _ , reject ) => setTimeout ( ( ) => reject ( new Error ( "Connection timeout" ) ) , TIMEOUT ) )
94+ ] ) ;
5795 console . log ( "Connected!" ) ;
5896
5997 let createCertificateFromCsrResponse = await identityClient . createCertificateFromCsr ( {
@@ -62,12 +100,12 @@ async function main(argv: Args) {
62100 console . log ( `CreateCertificateFromCsr Response: ${ JSON . stringify ( createCertificateFromCsrResponse ) } ` ) ;
63101
64102 let registerThingRequest : iotidentity . model . RegisterThingRequest = {
65- templateName : argv . template_name ,
103+ templateName : args . template_name ,
66104 certificateOwnershipToken : createCertificateFromCsrResponse . certificateOwnershipToken ,
67105 } ;
68106
69- if ( argv . template_parameters ) {
70- registerThingRequest . parameters = JSON . parse ( argv . template_parameters ) ;
107+ if ( args . template_parameters ) {
108+ registerThingRequest . parameters = JSON . parse ( args . template_parameters ) ;
71109 }
72110
73111 let registerThingResponse = await identityClient . registerThing ( registerThingRequest ) ;
@@ -84,3 +122,8 @@ async function main(argv: Args) {
84122 console . log ( "Disconnected" ) ;
85123 process . exit ( 0 ) ;
86124}
125+
126+ main ( ) . catch ( ( error ) => {
127+ console . error ( error ) ;
128+ process . exit ( 1 ) ;
129+ } ) ;
0 commit comments