]> git.sur5r.net Git - freertos/blob - FreeRTOS-Labs/Demo/FreeRTOS_IoT_Libraries/tools/aws_config_quick_start/SetupAWS.py
Add the Labs projects provided in the V10.2.1_191129 zip file.
[freertos] / FreeRTOS-Labs / Demo / FreeRTOS_IoT_Libraries / tools / aws_config_quick_start / SetupAWS.py
1 #!/usr/bin/env python\r
2 \r
3 import os\r
4 import sys\r
5 import json\r
6 import pprint\r
7 import argparse\r
8 import boto3\r
9 import misc\r
10 import certs\r
11 import thing\r
12 import policy\r
13 \r
14 pp = pprint.PrettyPrinter(indent=4)\r
15 \r
16 \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
21         sys.exit(1)\r
22 \r
23 \r
24 def prereq():\r
25     with open('configure.json') as configure_file:\r
26         json_text = json.load(configure_file)\r
27 \r
28     # Create a Thing\r
29     thing_name = json_text['thing_name']\r
30     thing_obj = thing.Thing(thing_name)\r
31     if not thing_obj.create():\r
32 \r
33         # Create a Certificate\r
34         cert_obj = certs.Certificate()\r
35         result = cert_obj.create()\r
36 \r
37         # Store certId\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
45 \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
54 \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
63 \r
64         # Create a Policy\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
68         policy_obj.create()\r
69 \r
70         # Attach certificate to Thing\r
71         cert_obj.attach_thing(thing_name)\r
72 \r
73         # Attach policy to certificate\r
74         cert_obj.attach_policy(policy_name)\r
75 \r
76 \r
77 def update_credential_file():\r
78     with open('configure.json') as configure_file:\r
79         json_text = json.load(configure_file)\r
80 \r
81     source_dir = os.path.expanduser(json_text['FreeRTOS_source_dir'])\r
82     thing_name = json_text['thing_name']\r
83 \r
84     # Read cert_pem from file\r
85     cert_pem_filename = thing_name + '_cert_pem_file.pem'\r
86     try:\r
87         cert_pem_file = open(cert_pem_filename, 'r')\r
88     except IOError:\r
89         print("{} file not found. Run prerequisite step"\r
90               .format(cert_pem_filename))\r
91         sys.exit(1)\r
92     else:\r
93         cert_pem = cert_pem_file.read()\r
94 \r
95     # Read private_key_pem from file\r
96     private_key_pem_filename = thing_name + '_private_key_pem_file.pem'\r
97     try:\r
98         private_key_pem_file = open(private_key_pem_filename, 'r')\r
99     except IOError:\r
100         print("{} file not found. Run prerequisite step"\r
101               .format(private_key_pem_filename))\r
102         sys.exit(1)\r
103     else:\r
104         private_key_pem = private_key_pem_file.read()\r
105 \r
106     # Modify 'iot_clientcredential.h' file\r
107     misc.write_client_credentials(\r
108         source_dir,\r
109         thing_name=thing_name,\r
110         client_certificate_pem=cert_pem,\r
111         client_private_key_pem=private_key_pem,\r
112         cleanup=False)\r
113 \r
114 \r
115 def delete_prereq():\r
116     with open('configure.json') as configure_file:\r
117         json_text = json.load(configure_file)\r
118 \r
119     # Delete Thing\r
120     thing_name = json_text['thing_name']\r
121     thing_obj = thing.Thing(thing_name)\r
122     if thing_obj.exists():\r
123         thing_obj.delete()\r
124 \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
131         cert_obj.delete()\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
136 \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
143 \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
149 \r
150     # Delete policy\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
155 \r
156 \r
157 def cleanup_creds():\r
158     with open('configure.json') as file:\r
159         json_text = json.load(file)\r
160 \r
161     source_dir = os.path.expanduser(json_text['FreeRTOS_source_dir'])\r
162 \r
163     # Cleanup 'iot_clientcredential.h' file\r
164     misc.write_client_credentials(source_dir, cleanup=True)\r
165 \r
166 \r
167 def setup():\r
168     prereq()\r
169     update_credential_file()\r
170     print("Setup Completed")\r
171 \r
172 \r
173 def cleanup():\r
174     delete_prereq()\r
175     cleanup_creds()\r
176     print("Cleanup Completed")\r
177 \r
178 \r
179 def list_certificates():\r
180     client = boto3.client('iot')\r
181     certs = client.list_certificates()['certificates']\r
182     pp.pprint(certs)\r
183 \r
184 \r
185 def list_things():\r
186     client = boto3.client('iot')\r
187     things = client.list_things()['things']\r
188     pp.pprint(things)\r
189 \r
190 \r
191 def list_policies():\r
192     client = boto3.client('iot')\r
193     policies = client.list_policies()['policies']\r
194     pp.pprint(policies)\r
195 \r
196 \r
197 if __name__ == "__main__":\r
198 \r
199     arg_parser = argparse.ArgumentParser()\r
200     subparsers = arg_parser.add_subparsers(help='Available commands',\r
201                                            dest='command')\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
213 \r
214     if args.command == 'setup':\r
215         setup()\r
216     elif args.command == 'cleanup':\r
217         cleanup()\r
218     elif args.command == 'list_certificates':\r
219         list_certificates()\r
220     elif args.command == 'list_things':\r
221         list_things()\r
222     elif args.command == 'list_policies':\r
223         list_policies()\r
224     elif args.command == 'prereq':\r
225         prereq()\r
226     elif args.command == 'update_creds':\r
227         update_credential_file()\r
228     elif args.command == 'delete_prereq':\r
229         delete_prereq()\r
230     elif args.command == 'cleanup_creds':\r
231         cleanup_creds()\r
232     else:\r
233         print("Command does not exist")\r
234 \r
235     sys.exit(1)