-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathAWSAsync.py
224 lines (186 loc) · 8.52 KB
/
AWSAsync.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
import APIKey
import boto3
from botocore.auth import SigV4Auth
from botocore.awsrequest import AWSRequest
import xml.etree.ElementTree as ET
import asyncio
aws_regions = [
"us-east-1",
"us-west-2",
"ap-southeast-2",
"ap-south-1",
"eu-west-3",
]
async def check_aws(key: APIKey, session):
key.username = await get_username(key, session)
if key.username is None:
return
if not await test_invoke_perms(key, session, "anthropic.claude-3-sonnet-20240229-v1:0"):
return
if not key.bedrock_enabled and not key.useless:
await test_invoke_perms(key, session, "anthropic.claude-v2")
policies = await get_key_policies(key, session)
if not key.useless and key.bedrock_enabled:
await check_logging(key, session)
await retrieve_activated_models(key, session)
for region, models in key.models.items():
key.models[region] = list(set(models))
elif key.useless and policies is not None:
key.useless_reasons.append('Key policies lack Admin or User Creation perms')
return True
async def sign_request(key: APIKey, region, method, url, headers, data, service):
line = key.api_key.split(":")
access_key = line[0]
secret = line[1]
boto3_session = boto3.Session(
aws_access_key_id=access_key,
aws_secret_access_key=secret,
region_name=region
)
credentials = boto3_session.get_credentials()
signer = SigV4Auth(credentials, service, region)
request = AWSRequest(method=method, url=url, headers=headers, data=data)
signer.add_auth(request)
return request.headers, request.data
async def test_invoke_perms(key: APIKey, session, model):
async def check_region(region):
host = f'bedrock-runtime.{region}.amazonaws.com'
url = f'https://{host}/model/{model}/invoke'
signed_headers, signed_data = await sign_request(key, region, 'POST', url, headers, data, 'bedrock')
async with session.post(url, headers=signed_headers, data=signed_data) as response:
resp = await response.json()
message = resp.get('message')
if response.status == 403:
if message and ('The request signature we calculated does not match the signature you provided' in message or 'The security token included in the request is invalid' in message):
return False
elif response.status == 400 or response.status == 404:
if message and 'Malformed input request' in message:
if key.region == "":
key.region = region
else:
key.alt_regions.append(region)
key.bedrock_enabled = True
else:
return False
key.useless = False
return True
headers = {
'content-type': 'application/json',
'accept': '*/*',
}
data = {
"prompt": "\n\nHuman:\n\nAssistant:",
"max_tokens_to_sample": -1,
}
tasks = [asyncio.create_task(check_region(region)) for region in aws_regions]
results = await asyncio.gather(*tasks)
return any(results)
async def get_username(key: APIKey, session):
region = 'us-east-1'
url = 'https://sts.amazonaws.com/'
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
}
data = 'Action=GetCallerIdentity&Version=2011-06-15'
signed_headers, signed_data = await sign_request(key, region, 'POST', url, headers, data, 'sts')
async with session.post(url, headers=signed_headers, data=signed_data) as response:
resp = await response.text()
if 'ErrorResponse' in resp:
return
root = ET.fromstring(resp)
namespace = {'ns': 'https://sts.amazonaws.com/doc/2011-06-15/'}
arn = root.find('.//ns:Arn', namespaces=namespace).text
if not arn:
return "default"
username = arn.split('/')[-1]
if "iam::" in username:
return "default"
return username
async def get_key_policies(key: APIKey, session):
url = f'https://iam.amazonaws.com/?Action=ListAttachedUserPolicies&UserName={key.username}&Version=2010-05-08'
signed_headers, signed_data = await sign_request(key, 'us-east-1', 'GET', url, {}, None, 'iam')
async with session.get(url, headers=signed_headers) as response:
resp = await response.text()
root = ET.fromstring(resp)
namespace = {'iam': 'https://iam.amazonaws.com/doc/2010-05-08/'}
attached_policies = root.findall('.//iam:AttachedPolicies/iam:member', namespaces=namespace)
policy_names = []
if not attached_policies:
if not key.bedrock_enabled:
key.useless = True
key.useless_reasons.append('Failed Policy Fetch')
return
for policy in attached_policies:
policy_name = policy.find('iam:PolicyName', namespaces=namespace).text
policy_names.append(policy_name)
if policy_name == 'AdministratorAccess':
key.admin_priv = True
if 'AWSCompromisedKeyQuarantine' in policy_name:
if not key.bedrock_enabled:
key.useless = True
key.useless_reasons.append('Quarantined Key')
return policy_names
async def check_logging(key: APIKey, session):
region = key.region
host = f'bedrock.{region}.amazonaws.com'
url = f'https://{host}/logging/modelinvocations'
signed_headers, signed_data = await sign_request(key, region, 'GET', url, {'accept': 'application/json'}, {}, 'bedrock')
async with session.get(url, headers=signed_headers) as response:
if response.status == 200:
logging_config = await response.json()
if 'loggingConfig' in logging_config and logging_config['loggingConfig'] is not None and 'textDataDeliveryEnabled' in logging_config['loggingConfig']:
key.logged = logging_config['loggingConfig']['textDataDeliveryEnabled']
else:
key.logged = False
else:
key.logged = False
async def retrieve_activated_models(key: APIKey, session):
tasks = []
for region in aws_regions:
if region not in key.models:
key.models[region] = []
task = handle_region(key, session, region)
tasks.append(task)
await asyncio.gather(*tasks)
async def handle_region(key: APIKey, session, region):
listed_models = await retrieve_models(key, session, region)
tasks = [invoke_model(key, session, region, model) for model in listed_models]
await asyncio.gather(*tasks)
async def invoke_model(key: APIKey, session, region, model):
model_id, model_name = model
data = {
"prompt": "\n\nHuman:\n\nAssistant:",
"max_tokens_to_sample": -1,
}
host = f'bedrock-runtime.{region}.amazonaws.com'
url = f'https://{host}/model/{model_id}/invoke'
signed_headers, signed_data = await sign_request(key, region, 'POST', url, {'content-type': 'application/json', 'accept': '*/*'}, data, 'bedrock')
async with session.post(url, headers=signed_headers, data=signed_data) as response:
if response.status == 400:
resp = await response.json()
if resp['message'] and 'Malformed input request' in resp['message']:
key.models[region].append(model_name)
elif resp['message'] and 'Operation not' in resp['message']:
key.useless = True
key.useless_reasons.append('Quarantined Key')
async def retrieve_models(key: APIKey, session, region):
host = f'bedrock.{region}.amazonaws.com'
url = f'https://{host}/foundation-models'
signed_headers, signed_data = await sign_request(key, region, 'GET', url, {'accept': 'application/json'}, {}, 'bedrock')
async with session.get(url, headers=signed_headers) as response:
if response.status == 200:
response = await response.json()
models = response["modelSummaries"]
model_providers = ["Meta", "Anthropic", "Mistral AI"]
model_info = []
for model in models:
provider_name = model["providerName"]
model_id = model["modelId"]
model_name = model["modelName"]
if provider_name in model_providers or (provider_name == "Cohere" and ("Command R+" in model_name or "Command R" in model_name)):
parts = model_id.split(":")
if len(parts) <= 2:
model_info.append((model_id, model_name))
return model_info
else:
return []