Daniel Gato
01/19/2022, 1:35 PMOmi Chowdhury
01/19/2022, 3:28 PMDaniel Gato
01/19/2022, 3:50 PMAdam Fanello
01/19/2022, 3:59 PMDaniel Gato
01/19/2022, 4:58 PMDaniel Gato
01/22/2022, 12:23 PMimport * as sst from '@serverless-stack/resources';
import * as customResource from '@aws-cdk/custom-resources';
import * as iot from '@aws-cdk/aws-iot';
export default class IotStack extends sst.Stack {
iotEndpointAddress;
attachPolicyFunction;
constructor(scope, id, props) {
super(scope, id, props);
const getIoTEndpoint = new customResource.AwsCustomResource(this, 'IoTEndpoint', {
onCreate: {
service: 'Iot',
action: 'describeEndpoint',
physicalResourceId: customResource.PhysicalResourceId.fromResponse('endpointAddress'),
parameters: {
"endpointType": "iot:Data-ATS"
}
},
policy: customResource.AwsCustomResourcePolicy.fromSdkCalls({
resources: customResource.AwsCustomResourcePolicy.ANY_RESOURCE
})
});
this.iotEndpointAddress = getIoTEndpoint.getResponseField('endpointAddress');
const iotAuthPolicy = new iot.CfnPolicy(this, 'IotAuthPolicy', {
policyName: 'iot-auth-policy',
policyDocument: {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "iot:*",
"Resource": "*"
}
]
},
});
this.attachPolicyFunction = new sst.Function(this, 'AttachPolicy', {
handler: 'src/functions/attachPolicy.main',
permissions: ["iot:AttachPrincipalPolicy"],
environment: {
IOT_AUTH_POLICY: iotAuthPolicy.ref
},
});
}
}
attach policy to user:
import { Iot } from 'aws-sdk';
import handler from '../util/handler';
export const main = handler(async (event) => {
await new Iot().attachPrincipalPolicy({
principal: event.requestContext.authorizer.iam.cognitoIdentity.identityId,
policyName: process.env.IOT_AUTH_POLICY,
}).promise();
});
On my ReactSite, I the calls to PubSub have to be in a certain order:
index.js
import Amplify, { PubSub } from 'aws-amplify';
import { AWSIoTProvider } from '@aws-amplify/pubsub/lib/Providers';
Amplify.addPluggable(new AWSIoTProvider({
aws_pubsub_region: config.pubsub.REGION,
aws_pubsub_endpoint: config.pubsub.ENDPOINT,
}));
Amplify.configure({...});
PubSub.configure();
In my note.js
const sub = PubSub.subscribe(`updateStatus-${id}`).subscribe({
next: data => alert(JSON.stringify(data.value)),
error: error => console.error(error),
close: () => alert('Done'),
});
// sub.unsubscribe();
Finally, send the message:
import { IotData } from 'aws-sdk';
import handler from '../util/handler';
export const main = handler(async (event) => {
const data = ...;
await new IotData({ endpoint: process.env.IOT_ENDPOINT }).publish({
topic: `updateStatus-${newImage.noteId}`,
payload: JSON.stringify(data);
qos: 0
}).promise();
});
Adam Fanello
01/23/2022, 7:49 PM"Effect": "Allow",
"Action": "iot:*"
Looks like you are giving every authenticated user access to listen in to all traffic and send data spoofing anybody else.