1 #!/usr/bin/env python
\r
14 pp = pprint.PrettyPrinter(indent=4)
\r
17 def check_aws_configuration():
\r
18 mysession = boto3.session.Session()
\r
19 if not mysession._session._config['profiles']:
\r
20 print("AWS not configured. Please run `aws configure`.")
\r
25 with open('configure.json') as configure_file:
\r
26 json_text = json.load(configure_file)
\r
29 thing_name = json_text['thing_name']
\r
30 thing_obj = thing.Thing(thing_name)
\r
31 if not thing_obj.create():
\r
33 # Create a Certificate
\r
34 cert_obj = certs.Certificate()
\r
35 result = cert_obj.create()
\r
38 cert_id = result['certificateId']
\r
39 cert_id_filename = thing_name + '_cert_id_file.txt'
\r
40 cert_id_file = open(cert_id_filename, 'w')
\r
41 cert_id_file.write(cert_id)
\r
42 cert_id_file_path = os.path.abspath(cert_id_filename)
\r
43 os.chmod(cert_id_file_path, 0o444)
\r
44 cert_id_file.close()
\r
46 # Store cert_pem as file
\r
47 cert_pem = result['certificatePem']
\r
48 cert_pem_filename = thing_name + '_cert_pem_file.pem'
\r
49 cert_pem_file = open(cert_pem_filename, 'w')
\r
50 cert_pem_file.write(cert_pem)
\r
51 cert_pem_file_path = os.path.abspath(cert_pem_filename)
\r
52 os.chmod(cert_pem_file_path, 0o444)
\r
53 cert_pem_file.close()
\r
55 # Store private key PEM as file
\r
56 private_key_pem = result['keyPair']['PrivateKey']
\r
57 private_key_pem_filename = thing_name + '_private_key_pem_file.pem'
\r
58 private_key_pem_file = open(private_key_pem_filename, 'w')
\r
59 private_key_pem_file.write(private_key_pem)
\r
60 private_key_pem_file_path = os.path.abspath(private_key_pem_filename)
\r
61 os.chmod(private_key_pem_file_path, 0o444)
\r
62 private_key_pem_file.close()
\r
65 policy_document = misc.create_policy_document()
\r
66 policy_name = thing_name + '_amazon_freertos_policy'
\r
67 policy_obj = policy.Policy(policy_name, policy_document)
\r
70 # Attach certificate to Thing
\r
71 cert_obj.attach_thing(thing_name)
\r
73 # Attach policy to certificate
\r
74 cert_obj.attach_policy(policy_name)
\r
77 def update_credential_file():
\r
78 with open('configure.json') as configure_file:
\r
79 json_text = json.load(configure_file)
\r
81 source_dir = os.path.expanduser(json_text['FreeRTOS_source_dir'])
\r
82 thing_name = json_text['thing_name']
\r
84 # Read cert_pem from file
\r
85 cert_pem_filename = thing_name + '_cert_pem_file.pem'
\r
87 cert_pem_file = open(cert_pem_filename, 'r')
\r
89 print("{} file not found. Run prerequisite step"
\r
90 .format(cert_pem_filename))
\r
93 cert_pem = cert_pem_file.read()
\r
95 # Read private_key_pem from file
\r
96 private_key_pem_filename = thing_name + '_private_key_pem_file.pem'
\r
98 private_key_pem_file = open(private_key_pem_filename, 'r')
\r
100 print("{} file not found. Run prerequisite step"
\r
101 .format(private_key_pem_filename))
\r
104 private_key_pem = private_key_pem_file.read()
\r
106 # Modify 'iot_clientcredential.h' file
\r
107 misc.write_client_credentials(
\r
109 thing_name=thing_name,
\r
110 client_certificate_pem=cert_pem,
\r
111 client_private_key_pem=private_key_pem,
\r
115 def delete_prereq():
\r
116 with open('configure.json') as configure_file:
\r
117 json_text = json.load(configure_file)
\r
120 thing_name = json_text['thing_name']
\r
121 thing_obj = thing.Thing(thing_name)
\r
122 if thing_obj.exists():
\r
125 # Delete certificate
\r
126 cert_id_filename = thing_name + '_cert_id_file.txt'
\r
127 if os.path.exists(cert_id_filename):
\r
128 cert_id_file = open(cert_id_filename, 'r')
\r
129 cert_id = cert_id_file.read()
\r
130 cert_obj = certs.Certificate(cert_id)
\r
132 cert_id_file.close()
\r
133 cert_id_file_path = os.path.abspath(cert_id_filename)
\r
134 os.chmod(cert_id_file_path, 0o666)
\r
135 os.remove(cert_id_filename)
\r
137 # Delete cert_pem file and private_key_pem file
\r
138 cert_pem_filename = thing_name + '_cert_pem_file.pem'
\r
139 if os.path.exists(cert_pem_filename):
\r
140 cert_pem_file_path = os.path.abspath(cert_pem_filename)
\r
141 os.chmod(cert_pem_file_path, 0o666)
\r
142 os.remove(cert_pem_filename)
\r
144 private_key_pem_filename = thing_name + '_private_key_pem_file.pem'
\r
145 if os.path.exists(private_key_pem_filename):
\r
146 private_key_pem_file_path = os.path.abspath(private_key_pem_filename)
\r
147 os.chmod(private_key_pem_file_path, 0o666)
\r
148 os.remove(private_key_pem_filename)
\r
151 policy_name = thing_name + '_amazon_freertos_policy'
\r
152 policy_obj = policy.Policy(policy_name)
\r
153 if policy_obj.exists():
\r
154 policy_obj.delete()
\r
157 def cleanup_creds():
\r
158 with open('configure.json') as file:
\r
159 json_text = json.load(file)
\r
161 source_dir = os.path.expanduser(json_text['FreeRTOS_source_dir'])
\r
163 # Cleanup 'iot_clientcredential.h' file
\r
164 misc.write_client_credentials(source_dir, cleanup=True)
\r
169 update_credential_file()
\r
170 print("Setup Completed")
\r
176 print("Cleanup Completed")
\r
179 def list_certificates():
\r
180 client = boto3.client('iot')
\r
181 certs = client.list_certificates()['certificates']
\r
186 client = boto3.client('iot')
\r
187 things = client.list_things()['things']
\r
191 def list_policies():
\r
192 client = boto3.client('iot')
\r
193 policies = client.list_policies()['policies']
\r
194 pp.pprint(policies)
\r
197 if __name__ == "__main__":
\r
199 arg_parser = argparse.ArgumentParser()
\r
200 subparsers = arg_parser.add_subparsers(help='Available commands',
\r
202 subparsers.add_parser('setup', help='Setup AWS IoT')
\r
203 subparsers.add_parser('cleanup', help='Cleanup AWS IoT')
\r
204 subparsers.add_parser('list_certificates', help='List certificates')
\r
205 subparsers.add_parser('list_things', help='List things')
\r
206 subparsers.add_parser('list_policies', help='List policies')
\r
207 subparsers.add_parser('prereq', help='Setup prerequisites for AWS IoT')
\r
208 subparsers.add_parser('update_creds', help='Update credential files')
\r
209 subparsers.add_parser('delete_prereq', help='Delete prerequisites created')
\r
210 subparsers.add_parser('cleanup_creds', help='Cleanup credential files')
\r
211 args = arg_parser.parse_args()
\r
212 check_aws_configuration()
\r
214 if args.command == 'setup':
\r
216 elif args.command == 'cleanup':
\r
218 elif args.command == 'list_certificates':
\r
219 list_certificates()
\r
220 elif args.command == 'list_things':
\r
222 elif args.command == 'list_policies':
\r
224 elif args.command == 'prereq':
\r
226 elif args.command == 'update_creds':
\r
227 update_credential_file()
\r
228 elif args.command == 'delete_prereq':
\r
230 elif args.command == 'cleanup_creds':
\r
233 print("Command does not exist")
\r