通过策略和证书即时创buildAWS IoT“事物”

我正在使用NodeJS以及AWS JS SDK和AWS IoT Device JS SDK ,以便在连接到我的服务器后自动创build新事物并为其分配证书和策略。

我正在关注“即时注册”文章 ,以创build,注册和激活我的CA证书。 据我所知,CA证书已成功添加到AWS IoT,已激活并启用了自动注册。

我不明白的是这个步骤是如何执行的(引用文章):

当设备尝试连接AWS IoT未知的,但已通过在AWS IoT中注册的CA签名的X.509证书时,设备证书将由AWS IoT以新的PENDING_ACTIVATION状态自动注册。

我如何做一个“尝试”连接? 由于我使用aws-iot-device-sdk-js SDK和手动创build的证书,所以我通常会像这样连接我的设备:

 const device = deviceModule.device({ host: 'myendpoint.iot.us-east-1.amazonaws.com', region: 'us-east-1', keyPath: `certs/${deviceID}.key`, certPath: `certs/${deviceID}.pem`, caPath: 'certs/rootCA.pem', clientId: deviceID, baseReconnectTimeMs: 4000, keepalive: 30, protocol: 'mqtts', }); 

但是现在我没有在keyPathcertPath包含的证书和密钥,我不能在没有它的情况下实例化我的设备。

我试图自己创build证书,使用AWS SDK中的createKeysAndCertificate() ,将它们保存到磁盘,手动附加策略,手动附加主体,甚至尝试手动将证书标记为“主动”,如下所示:

 iot.createThing({ thingName: deviceID }, (err, d) => { if (err) { console.log(err); } else { allThings[d.thingName] = d; iot.createKeysAndCertificate({ setAsActive: true }, (e, c) => { if (e) { console.log(e); } else { fs.writeFile(`certs/${deviceID}.pem`, c.certificatePem, (ef, f) => { if (ef) throw ef; }); fs.writeFile(`certs/${deviceID}.key`, c.keyPair.PrivateKey, (ef, f) => { if (ef) throw ef; }); iot.attachPrincipalPolicy({ policyName: 'my-testing-policy', principal: c.certificateArn, }, (ee, cc) => { if (ee) { console.log(ee); } else { iot.attachThingPrincipal({ principal: c.certificateArn, thingName: deviceID, }, (prerr, prdata) => { if (prerr) { console.log(prerr); } else { iot.acceptCertificateTransfer({ certificateId: c.certificateId, setAsActive: true, }, (ce, cd) => { if (err) { console.log(err); } else { console.log('cert activated.'); } }); } }); } }); } }); } }); 

但是,毕竟,当我尝试发布的东西,我提出了一个错误:

 Error: unable to get local issuer certificate at Error (native) at TLSSocket.<anonymous> (_tls_wrap.js:1092:38) at emitNone (events.js:86:13) at TLSSocket.emit (events.js:185:7) at TLSSocket._finishInit (_tls_wrap.js:610:8) at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:440:38) 

我也尝试订阅特定的主题,如上面同一篇文章中提到的, aws/events/certificates/registered/e3f0a30...但是我从来没有见过关于该主题的单个消息…

我在这里错过了什么? 如何通过使用我的即时(Just-in-Time)证书来正确触发设备证书和私钥生成?