-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathaws.py
285 lines (243 loc) · 11.2 KB
/
aws.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
# -*- coding: utf-8 -*-
"""A utility class for introspecting AWS infrastructure."""
import json
# python stuff
import os
import socket
# our stuff
from openai_api.common.conf import Services, settings
from openai_api.common.utils import recursive_sort_dict
# pylint: disable=too-many-public-methods
class AWSInfrastructureConfig:
"""AWS Infrastructure Config"""
_domain = None
@property
def dump(self):
"""Return a dict of the AWS infrastructure config."""
retval = {}
if Services.enabled(Services.AWS_APIGATEWAY):
api = self.get_api(settings.aws_apigateway_name)
retval["apigateway"] = {
"api_id": api.get("id"),
"stage": self.get_api_stage(),
"domains": self.get_api_custom_domains(),
}
if Services.enabled(Services.AWS_S3):
retval["s3"] = {"bucket_name": self.get_bucket_by_prefix(settings.aws_s3_bucket_name)}
if Services.enabled(Services.AWS_DYNAMODB):
retval["dynamodb"] = {"table_name": self.get_dyanmodb_table_by_name(settings.aws_dynamodb_table_id)}
if Services.enabled(Services.AWS_REKOGNITION):
retval["rekognition"] = {
"collection_id": self.get_rekognition_collection_by_id(settings.aws_rekognition_collection_id)
}
if Services.enabled(Services.AWS_IAM):
retval["iam"] = {"policies": self.get_iam_policies(), "roles": self.get_iam_roles()}
if Services.enabled(Services.AWS_LAMBDA):
retval["lambda"] = self.get_lambdas()
if Services.enabled(Services.AWS_ROUTE53):
retval["route53"] = self.get_dns_record_from_hosted_zone()
return recursive_sort_dict(retval)
def get_lambdas(self):
"""Return a dict of the AWS Lambdas."""
lambda_client = settings.aws_session.client("lambda")
lambdas = lambda_client.list_functions()["Functions"]
retval = {
lambda_function["FunctionName"]: lambda_function["FunctionArn"]
for lambda_function in lambdas
if settings.shared_resource_identifier in lambda_function["FunctionName"]
}
return retval or {}
def get_iam_policies(self):
"""Return a dict of the AWS IAM policies."""
iam_client = settings.aws_session.client("iam")
policies = iam_client.list_policies()["Policies"]
retval = {}
for policy in policies:
if settings.shared_resource_identifier in policy["PolicyName"]:
policy_version = iam_client.get_policy(PolicyArn=policy["Arn"])["Policy"]["DefaultVersionId"]
policy_document = iam_client.get_policy_version(PolicyArn=policy["Arn"], VersionId=policy_version)[
"PolicyVersion"
]["Document"]
retval[policy["PolicyName"]] = {"Arn": policy["Arn"], "Policy": policy_document}
return retval
def get_iam_roles(self):
"""Return a dict of the AWS IAM roles."""
iam_client = settings.aws_session.client("iam")
roles = iam_client.list_roles()["Roles"]
retval = {}
for role in roles:
if settings.shared_resource_identifier in role["RoleName"]:
attached_policies = iam_client.list_attached_role_policies(RoleName=role["RoleName"])[
"AttachedPolicies"
]
retval[role["RoleName"]] = {
"Arn": role["Arn"],
"Role": role,
"AttachedPolicies": attached_policies,
}
return retval or {}
def get_api_stage(self) -> str:
"""Return the API stage."""
api = self.get_api(settings.aws_apigateway_name) or {}
api_id = api.get("id")
if api_id:
response = settings.aws_apigateway_client.get_stages(restApiId=api_id)
# Assuming you want the most recently deployed stage
stages = response.get("item", [])
if stages:
retval = stages[-1]["stageName"]
return retval
return ""
def get_api_custom_domains(self) -> list:
"""Return the API custom domains."""
def filter_dicts(lst):
return [d for d in lst if "domainName" in d and settings.shared_resource_identifier in d["domainName"]]
response = settings.aws_apigateway_client.get_domain_names()
retval = response.get("items", [])
return filter_dicts(retval)
def get_url(self, path) -> str:
"""Return the url for the given path."""
if settings.aws_apigateway_create_custom_domaim:
return f"https://{settings.aws_apigateway_domain_name}{path}"
return f"https://{settings.aws_apigateway_domain_name}/{self.get_api_stage()}{path}"
def aws_connection_works(self):
"""Test that the AWS connection works."""
try:
# pylint: disable=pointless-statement
settings.aws_session.region_name
return True
except Exception: # pylint: disable=broad-exception-caught
return False
@property
def domain(self):
"""Return the domain."""
if not self._domain:
if settings.aws_apigateway_create_custom_domaim:
self._domain = (
os.getenv(key="DOMAIN")
or "api." + settings.shared_resource_identifier + "." + settings.aws_apigateway_root_domain
)
return self._domain
response = settings.aws_apigateway_client.get_rest_apis()
for item in response["items"]:
if item["name"] == self.api_gateway_name:
api_id = item["id"]
self._domain = f"{api_id}.execute-api.{settings.aws_region}.amazonaws.com"
return self._domain
@property
def api_gateway_name(self):
"""Return the API Gateway name."""
return settings.shared_resource_identifier + "-api"
def domain_exists(self) -> bool:
"""Test that the domain exists."""
try:
socket.gethostbyname(settings.aws_apigateway_domain_name)
return True
except socket.gaierror:
return False
def get_bucket_by_prefix(self, bucket_prefix) -> str:
"""Return the bucket name given the bucket prefix."""
try:
for bucket in settings.aws_s3_client.list_buckets()["Buckets"]:
if bucket["Name"].startswith(bucket_prefix):
return f"arn:aws:s3:::{bucket['Name']}"
except TypeError:
# TypeError: startswith first arg must be str or a tuple of str, not NoneType
pass
return None
def bucket_exists(self, bucket_prefix) -> bool:
"""Test that the S3 bucket exists."""
bucket = self.get_bucket_by_prefix(bucket_prefix)
return bucket is not None
def get_dyanmodb_table_by_name(self, table_name) -> str:
"""Return the DynamoDB table given the table name."""
response = settings.aws_dynamodb_client.list_tables()
for table in response["TableNames"]:
if table == table_name:
table_description = settings.aws_dynamodb_client.describe_table(TableName=table_name)
return table_description["Table"]["TableArn"]
return None
def dynamodb_table_exists(self, table_name) -> bool:
"""Test that the DynamoDB table exists."""
table = self.get_dyanmodb_table_by_name(table_name)
return table is not None
def api_exists(self, api_name: str) -> bool:
"""Test that the API Gateway exists."""
response = settings.aws_apigateway_client.get_rest_apis()
for item in response["items"]:
if item["name"] == api_name:
return True
return False
def get_api(self, api_name: str) -> dict:
"""Test that the API Gateway exists."""
response = settings.aws_apigateway_client.get_rest_apis()
for item in response["items"]:
if item["name"] == api_name:
return item
return {}
def api_resource_and_method_exists(self, path, method) -> bool:
"""Test that the API Gateway resource and method exists."""
api = self.get_api(settings.aws_apigateway_name) or {}
api_id = api.get("id")
resources = settings.aws_apigateway_client.get_resources(restApiId=api_id)
for resource in resources["items"]:
if resource["path"] == path:
try:
settings.aws_apigateway_client.get_method(
restApiId=api_id, resourceId=resource["id"], httpMethod=method
)
return True
except settings.aws_apigateway_client.exceptions.NotFoundException:
return False
return False
def get_api_keys(self) -> str:
"""Test that the API Gateway exists."""
response = settings.aws_apigateway_client.get_api_keys(includeValues=True)
for item in response["items"]:
if item["name"] == settings.shared_resource_identifier:
return item["value"]
return False
def get_rekognition_collection_by_id(self, collection_id) -> str:
"""Return the Rekognition collection."""
response = settings.aws_rekognition_client.list_collections()
for collection in response["CollectionIds"]:
if collection == collection_id:
return collection
return None
def rekognition_collection_exists(self) -> bool:
"""Test that the Rekognition collection exists."""
collection = self.get_rekognition_collection_by_id(settings.aws_rekognition_collection_id)
return collection is not None
def get_hosted_zone(self, domain_name) -> str:
"""Return the hosted zone."""
response = settings.aws_route53_client.list_hosted_zones()
for hosted_zone in response["HostedZones"]:
if hosted_zone["Name"] == domain_name or hosted_zone["Name"] == f"{domain_name}.":
return hosted_zone["Id"]
return None
def get_dns_record_from_hosted_zone(self) -> str:
"""Return the DNS record from the hosted zone."""
hosted_zone_id = self.get_hosted_zone(settings.aws_apigateway_root_domain)
if hosted_zone_id:
response = settings.aws_route53_client.list_resource_record_sets(HostedZoneId=hosted_zone_id)
for record in response["ResourceRecordSets"]:
if (
record["Name"] == settings.aws_apigateway_domain_name
or record["Name"] == f"{settings.aws_apigateway_domain_name}."
):
return record
return None
class SingletonConfig:
"""Singleton for Settings"""
_instance = None
def __new__(cls):
"""Create a new instance of Settings"""
if cls._instance is None:
cls._instance = super(SingletonConfig, cls).__new__(cls)
cls._instance._config = AWSInfrastructureConfig()
return cls._instance
@property
def config(self) -> AWSInfrastructureConfig:
"""Return the settings"""
return self._config # pylint: disable=E1101
aws_infrastructure_config = SingletonConfig().config