Skip to content

Commit

Permalink
clean up AWS metadata handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kreynoldsf5 committed Mar 28, 2024
1 parent 97fbd66 commit 489a3cb
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,21 @@ def fetch_metadata(url: str, max_retries=5) -> dict|None:
print("Max retries reached. Giving up.")
return None

def find_aws_cloudAccount(cloud_accounts: dict) -> dict|None:
def find_aws_metadata(cloud_accounts: dict) -> dict|None:
"""
Find the first cloud account with "type": "AWS_API_CREDENTIAL".
Return a dict containing the credential and fastest region.
"""
for account in cloud_accounts.get("cloudAccounts", []):
for credential in account.get("credentials", []):
if credential.get("type") == "AWS_API_CREDENTIAL":
return account
return None
try:
for account in cloud_accounts.get("cloudAccounts", []):
for credential in account.get("credentials", []):
if credential.get("type") == "AWS_API_CREDENTIAL":
return {
"aws_cred": credential,
"region": find_aws_region(account)
}
except Exception as e:
return None

def find_aws_region(cloud_account: dict, default_region: str='us-west-2') -> str:
"""
Expand Down Expand Up @@ -61,39 +67,35 @@ def query_metadata(metadata_base_url: str) -> dict|None:

deployment = fetch_metadata(deployment_url)
if deployment is None:
print("Unable to find deployment data.")
return None

deployment_tags = fetch_metadata(deployment_tags_url)
if deployment_tags is None:
print("Unable to find deployment tags.")
return None

cloud_account = find_aws_cloudAccount(fetch_metadata(cloud_accounts_url))
if cloud_account is None:
aws_metadata = find_aws_metadata(fetch_metadata(cloud_accounts_url))
if aws_metadata is None:
print("Unable to find AWS metadata.")
return None

try:
dep_id = deployment.get("deployment")["id"]
deployer = deployment.get("deployment")["deployer"]
lab_id = deployment_tags.get("LabID")
sqs_url = deployment_tags.get("SQS")
aws_credential = cloud_account.get("credentials"),
region = find_aws_region(cloud_account)

if aws_credential is None:
print("AWS API Credentials not found.")
return None

aws_secret = aws_credential.get("secret")
aws_key = aws_credential.get("key")
aws_credential = aws_metadata.get("aws_cred")

return {
"depID": dep_id,
"deployer": deployer,
"labID": lab_id,
"sqsURL": sqs_url,
"awsSecret": aws_secret,
"awsKey": aws_key,
"region": region
"awsSecret": aws_credential.get("secret"),
"awsKey": aws_credential.get("key"),
"region": aws_metadata.get("region")
}
except (KeyError, IndexError) as e:
print(f"Error extracting metadata: {e}")
Expand Down

0 comments on commit 489a3cb

Please sign in to comment.