-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathcreate-sensors.js
145 lines (114 loc) · 4.51 KB
/
create-sensors.js
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
const fs = require('fs').promises;
const {
IoTClient,
DescribeEndpointCommand,
UpdateIndexingConfigurationCommand,
CreatePolicyCommand,
CreateKeysAndCertificateCommand,
CreateThingTypeCommand,
CreateThingCommand,
AttachPolicyCommand,
AttachThingPrincipalCommand
} = require("@aws-sdk/client-iot");
const { ArgumentParser } = require('argparse');
//constants used in the app - do not change
const SENSORS_FILE = './sensors.json';
const CERT_FOLDER = './certs/';
const POLICY_FILE = './policy.json';
const ROOT_CA_FILE = 'AmazonRootCA1.pem';
//open sensor definition file
var sensors = require(SENSORS_FILE);
const policyDocument = require(POLICY_FILE);
async function createSensors(profile, region){
try {
const iotClient = new IoTClient({ profile: profile, region: region });
// get the regional IOT endpoint
var params = { endpointType: 'iot:Data-ATS'};
var command = new DescribeEndpointCommand(params);
const response = await iotClient.send(command);
const host = response.endpointAddress;
//enable thing fleet indexing to enable searching things
params = {
thingIndexingConfiguration: {
thingIndexingMode: "REGISTRY_AND_SHADOW"
}
}
command = new UpdateIndexingConfigurationCommand(params)
var result = await iotClient.send(command)
//iterate over all sensors and create policies, certs, and things
sensors.forEach(async (sensor) => {
//set the iot core endpoint
sensor.settings.host = host;
//create the IOT policy
var policyName = 'Policy-' + sensor.settings.clientId;
var policy = { policyName: policyName, policyDocument: JSON.stringify(policyDocument)};
command = new CreatePolicyCommand(policy)
result = await iotClient.send(command)
//create the certificates
command = new CreateKeysAndCertificateCommand({setAsActive:true});
result = await iotClient.send(command)
sensor.settings.certificateArn = result.certificateArn;
const certificatePem = result.certificatePem;
const privateKey = result.keyPair.PrivateKey;
//save the certificate
var fileName = CERT_FOLDER + sensor.settings.clientId + '-certificate.pem.crt';
sensor.settings.certPath = fileName;
await fs.writeFile(fileName, certificatePem);
//save the private key
fileName = CERT_FOLDER + sensor.settings.clientId + '-private.pem.key';
sensor.settings.keyPath = fileName;
await fs.writeFile(fileName, privateKey);
//save the AWS root certificate
sensor.settings.caPath = CERT_FOLDER + ROOT_CA_FILE;
// //create the thing type
params = {
thingTypeName: sensor.thingTypeName
}
command = new CreateThingTypeCommand(params)
result = await iotClient.send(command)
//create the thing
params = {
thingName: sensor.settings.clientId,
attributePayload: {
attributes: {
'Manufacturer': sensor.manufacturer,
'Model': sensor.model,
'Firmware': sensor.firmware
},
merge: false
},
thingTypeName: sensor.thingTypeName
};
command = new CreateThingCommand(params)
await iotClient.send(command)
//attach policy to certificate
command = new AttachPolicyCommand({ policyName: policyName, target: sensor.settings.certificateArn})
await iotClient.send(command)
//attach thing to certificate
command = new AttachThingPrincipalCommand({thingName: sensor.settings.clientId, principal: sensor.settings.certificateArn})
await iotClient.send(command)
//save the updated settings file
let data = JSON.stringify(sensors, null, 2);
await fs.writeFile(SENSORS_FILE, data);
})
//display results
console.log('IoT Things provisioned');
console.log('AWS Profile: ' + profile);
console.log('AWS Region: ' + region);
sensors.forEach((sensor) => {
console.log('Thing Name: ' + sensor.settings.clientId);
})
}
catch (err) {
console.log('Error creating sensors');
console.log(err.message);
}
}
// parse for profile command line arguent
const parser = new ArgumentParser({
description: 'Creates IoT Things for sensors defined in sensors.json'
});
parser.add_argument('--profile', {default: 'default'});
parser.add_argument('--region', {default: 'us-east-1'});
args = parser.parse_args()
createSensors(args.profile, args.region);