-
Notifications
You must be signed in to change notification settings - Fork 0
/
iot.py
331 lines (261 loc) · 9.51 KB
/
iot.py
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Script for automation of AWS IoT
"""
import os
import sys
import json
import boto3
import click
from pprint import pprint
from os import environ
import glob
session = None
iot = None
@click.group()
@click.option('--region', default=None,
help='Use a given AWS region.')
def cli(region):
"""Commands for IoT automation."""
global session, iot
if environ.get('AWS_ACCESS_KEY_ID') is None or environ.get('AWS_SECRET_ACCESS_KEY') is None:
print('Please specify AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY')
sys.exit()
session = boto3.Session(
aws_access_key_id = environ['AWS_ACCESS_KEY_ID'],
aws_secret_access_key = environ['AWS_SECRET_ACCESS_KEY'],
region_name = region
)
# print(session)
iot = session.client('iot')
@cli.command('create-and-setup-thing')
@click.argument('thing-name')
def create_and_setup_thing(thing_name):
"""Create a Thing, and set it up."""
# print(session)
# print(session.region_name)
THING_EXISTS = False
try:
response = iot.describe_thing(
thingName=thing_name
)
print('Thing with name: {} exists in this region {}. Pick another name or region. Exiting program.'.format(thing_name, session.region_name))
THING_EXISTS = True
# sys.exit()
except:
print()
# print('An exception happened - {} thing name not found'.format(thing_name))
if THING_EXISTS:
sys.exit()
print('Creating a Thing with the name: {}'.format(thing_name))
print('First create keys and certificate..')
keys_cert = iot.create_keys_and_certificate(setAsActive=True)
print('Created, copying to the keys subdirectory')
with open('./keys/' + thing_name + '-certificate.pem', 'w') as key_file:
key_file.write(keys_cert['certificatePem'])
with open('./keys/' + thing_name + '-private.key', 'w') as key_file:
key_file.write(keys_cert['keyPair']['PrivateKey'])
with open('./keys/' + thing_name + '-public.key', 'w') as key_file:
key_file.write(keys_cert['keyPair']['PublicKey'])
iot_thing = iot.create_thing(thingName=thing_name)
iot.attach_thing_principal(
thingName=iot_thing['thingName'],
principal=keys_cert['certificateArn'])
policy_doc = {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["iot:Publish", "iot:Subscribe", "iot:Connect", "iot:Receive", "iot:GetThingShadow", "iot:DeleteThingShadow", "iot:UpdateThingShadow"],
"Resource": ["arn:aws:iot:" + session.region_name + ":*:*"]
}
]
}
policyNameString = thing_name + '_Thing_Policy'
policy = iot.create_policy(
policyName=policyNameString,
policyDocument=json.dumps(policy_doc))
iot.attach_principal_policy(
policyName=policy['policyName'],
principal=keys_cert['certificateArn'])
@cli.command('delete-and-cleanup-thing')
@click.argument('thing-name')
# @click.option('thing-type')
def delete_and_cleanup_thing(thing_name):
"""Deletes a thing, and associated policy, certificates."""
THING_EXISTS = False
try:
response = iot.describe_thing(
thingName=thing_name
)
print('Thing with name: {} exists in this region {}.'.format(thing_name, session.region_name))
THING_EXISTS = True
# sys.exit()
except:
print()
# print('An exception happened - {} thing name not found'.format(thing_name))
if not THING_EXISTS:
print('Thing does not exist, exiting program')
sys.exit()
print('Getting read to delete & cleanup... ')
print()
print('Thing: ' + response['thingName'])
print('Thing ID: ' + response['thingId'])
print('Thing ARN: ' + response['thingId'])
response = iot.list_thing_principals(
thingName = thing_name
)
for prnc in response['principals']:
print(prnc)
args = {}
args['target'] = prnc
while True:
pols = iot.list_attached_policies(**args)
for pol in pols['policies']:
print('----------------------------------------------')
pprint(pol)
polName = pol['policyName']
# target = prnc
tmpargs = {}
tmpargs['policyName'] = polName
tmpargs['target'] = prnc
result = iot.detach_policy(**tmpargs)
tmpargs = {}
tmpargs['policyName'] = polName
result = iot.delete_policy(**tmpargs)
if 'nextMarker' in pols:
args = pols['nextMarker']
else:
break
# Detach the certificate from the thing, and delete
args = {}
args['principal'] = prnc
args['thingName'] = thing_name
print(args)
result = iot.detach_thing_principal(**args)
print('Deleting the certificate... ')
# No easy way of getting certificateId from above...
args = {}
while True:
certs = iot.list_certificates(**args)
for cert in certs['certificates']:
# print('----------------------------------------------')
# pprint(cert)
if cert['certificateArn'] == prnc:
print(prnc)
print('Certificate ARN: ', cert['certificateArn'])
print('Certificate ID: ', cert['certificateId'])
ags = {}
ags['certificateId'] = cert['certificateId']
ags['newStatus'] = 'INACTIVE'
response = iot.update_certificate(**ags)
ags = {}
ags['certificateId'] = cert['certificateId']
ags['forceDelete'] = True
response = iot.delete_certificate(**ags)
if 'nextMarker' in certs:
args = certs['nextMarker']
else:
break
# Delete the thing now
args = {}
args['thingName'] = thing_name
result = iot.delete_thing(**args)
# Now delete files in the keys directory
fileremovalstring = './keys/' + thing_name + '-' + '*'
print(fileremovalstring)
for f in glob.glob(fileremovalstring):
os.remove(f)
# print()
# print('Number of Policies: ', len(pols['policies']))
@cli.command('list-things')
def list_things():
"""List IoT Things."""
args = {}
while True:
things = iot.list_things(**args)
for thing in things['things']:
print('----------------------------------------------')
pprint(thing)
if 'NextToken' in things:
args = things['NextToken']
else:
break
print()
print('Number of Things: ', len(things['things']))
@cli.command('list-certificates')
def list_certificates():
"""List Certificates"""
args = {}
while True:
certs = iot.list_certificates(**args)
for cert in certs['certificates']:
print('----------------------------------------------')
pprint(cert)
if 'nextMarker' in certs:
args = certs['nextMarker']
else:
break
print()
print('Number of Certs: ', len(certs['certificates']))
@cli.command('list-policies')
def list_policies():
"""List Policies"""
args = {}
while True:
pols = iot.list_policies(**args)
for pol in pols['policies']:
print('----------------------------------------------')
pprint(pol)
if 'nextMarker' in pols:
args = pols['nextMarker']
else:
break
print()
print('Number of Policies: ', len(pols['policies']))
@cli.command('list-attached-policies', help='Certificate ARN for which you want to find attached policies')
@click.argument('targetarn')
def list_attached_policies(targetarn):
"""List Attached Policies"""
args = {}
args['target'] = targetarn
# args['recursive'] = False
print(args)
response = iot.list_attached_policies(**args)
# print(response)
while True:
pols = iot.list_attached_policies(**args)
for pol in pols['policies']:
print('----------------------------------------------')
pprint(pol)
if 'nextMarker' in pols:
args = pols['nextMarker']
else:
break
print()
print('Number of Policies: ', len(pols['policies']))
def get_iot_endpoints(iot):
args = {}
args['endpointType'] = 'iot:Data-ATS'
atsDataEndpoint = iot.describe_endpoint(**args)
args = {}
args['endpointType'] = 'iot:Data'
verisignDataEndpoint = iot.describe_endpoint(**args)
args = {}
args['endpointType'] = 'iot:Jobs'
jobsEndpoint = iot.describe_endpoint(**args)
args = {}
args['endpointType'] = 'iot:CredentialProvider'
credentialsEndpoint = iot.describe_endpoint(**args)
return atsDataEndpoint, verisignDataEndpoint, jobsEndpoint, credentialsEndpoint
@cli.command('describe-endpoints', help='List all the IoT endpoints')
def describe_endpoint():
# response = get_iot_endpoints(iot)
atsDataEndpoint, verisignDataEndpoint, jobsEndpoint, credentialsEndpoint = get_iot_endpoints(iot)
# pprint (response)
print(atsDataEndpoint['endpointAddress'])
print(verisignDataEndpoint['endpointAddress'])
print(jobsEndpoint['endpointAddress'])
print(credentialsEndpoint['endpointAddress'])
if __name__ == '__main__':
cli()