Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(aws): Update to aws-iot-device-sdk-v2 to fix AWS authentication issues #680

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions lib/connection/aws.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,46 @@
import { resolve } from 'path';
import { readFileSync } from 'fs';
import { EOL } from 'os';
import url from 'url';
import { device as iotDevice } from 'aws-iot-device-sdk';
import { iot, mqtt } from 'aws-iot-device-sdk-v2';
import platformConsts from '../utils/constants.js';
import { parseError } from '../utils/functions.js';
import platformLang from '../utils/lang-en.js';

const dirname = url.fileURLToPath(new URL('.', import.meta.url));
const mqttClient = new mqtt.MqttClient();
let mqttConfig;

export default class {
constructor(platform, iotFile) {
this.accountTopic = platform.accountTopic;

this.device = iotDevice({
privateKey: Buffer.from(iotFile.key, 'utf8'),
clientCert: Buffer.from(iotFile.cert, 'utf8'),
caPath: resolve(dirname, './cert/AmazonRootCA1.pem'),
clientId: `AP/${platform.accountId}/${platform.clientId}`,
host: platform.iotEndpoint,
enableMetrics: false,
});
if (!mqttConfig) {
const certWithCA = [
Buffer.from(iotFile.cert, 'utf8'),
readFileSync('/cert/AmazonRootCA1.pem', 'utf-8'),
].join(EOL);
mqttConfig = iot.AwsIotMqttConnectionConfigBuilder.new_mtls_builder(
certWithCA,
iotFile.key,
).with_client_id(`AP/${platform.accountId}/a${platform.clientId}`)
.with_endpoint(platform.iotEndpoint)
.with_clean_session(false)
.build;
}
this.device = mqttClient.new_connection(mqttConfig);

// A listener event for if the connection closes
this.device.on('close', () => {
this.device.on('closed', () => {
platform.log.debugWarn('[AWS] %s.', platformLang.awsEventClose);
this.connected = false;
});

// A listener event for if the connection reconnects
this.device.on('reconnect', () => {
this.device.on('resume', () => {
platform.log.debug('[AWS] %s.', platformLang.awsEventReconnect);
this.connected = true;
});

// A listener event for if the connection goes offline
this.device.on('offline', () => {
this.device.on('interrupt', () => {
platform.log.debugWarn('[AWS] %s.', platformLang.awsEventOffline);
this.connected = false;
});
Expand Down Expand Up @@ -96,7 +103,7 @@ export default class {

async connect() {
return new Promise((res, rej) => {
this.device.subscribe(this.accountTopic, {}, (err) => {
this.device.subscribe(this.accountTopic, mqtt.QoS.AtLeastOnce, (err) => {
if (err) {
rej(err);
} else {
Expand Down Expand Up @@ -130,7 +137,7 @@ export default class {

// Send the update over AWS
return new Promise((res, rej) => {
this.device.publish(accessory.context.awsTopic, JSON.stringify(payload), {}, (err) => {
this.device.publish(accessory.context.awsTopic, JSON.stringify(payload), mqtt.QoS.AtLeastOnce, (err) => {
if (err) {
rej(err);
} else {
Expand Down Expand Up @@ -165,7 +172,7 @@ export default class {

// Send the update over AWS
return new Promise((res, rej) => {
this.device.publish(accessory.context.awsTopic, JSON.stringify(payload), {}, (err) => {
this.device.publish(accessory.context.awsTopic, JSON.stringify(payload), mqtt.QoS.AtLeastOnce, (err) => {
if (err) {
rej(err);
} else {
Expand Down
Loading
Loading