Skip to content

Instantly share code, notes, and snippets.

@nijine
Created October 15, 2020 21:43
Show Gist options
  • Save nijine/14508919e99b9bb0da8565ae4d0a464d to your computer and use it in GitHub Desktop.
Save nijine/14508919e99b9bb0da8565ae4d0a464d to your computer and use it in GitHub Desktop.
Rudimentary AWS credentials observation tool, written for python 3.x, requires botocore module to be installed (packaged with boto3 module).
#!/usr/bin/env python3
import os
import traceback
from botocore import exceptions, session
try:
session_obj = session.get_session()
credentials = session_obj.get_credentials()
if credentials is None:
print("Credentials we're not found on the system.")
else:
access_key = credentials.access_key
print(f"Access key ID: {access_key}")
secret_key = credentials.secret_key
print(f"Secret Key: {secret_key}")
region = session_obj.get_config_variable('region')
print(f"Region: {region}")
home_path = os.environ.get('HOME')
if home_path is not None:
creds_path = f"{home_path}/.aws/credentials"
if os.path.exists(creds_path):
with open(creds_path, 'r') as f:
print(f"{creds_path} file contents:")
for l in f.readlines():
print(l)
else:
print(f"Credentials file missing ({creds_path}).")
access_key_var = os.environ.get('AWS_ACCESS_KEY_ID')
secret_key_var = os.environ.get('AWS_SECRET_ACCESS_KEY')
if access_key_var is not None:
if access_key_var in '':
print("Environment variable AWS_ACCESS_KEY_ID has a blank value.")
else:
print(f"Environment variable AWS_ACCESS_KEY_ID={access_key_var}")
else:
print(f"Environment variable AWS_ACCESS_KEY_ID not found.")
if secret_key_var is not None:
if secret_key_var in '':
print("Environment variable AWS_SECRET_ACCESS_KEY has a blank value.")
else:
print(f"Environment variable AWS_SECRET_ACCESS_KEY={secret_key_var}")
else:
print(f"Environment variable AWS_SECRET_ACCESS_KEY not found.")
except exceptions.ConfigParseError:
print("Configuration file is malformed.")
except Exception:
print("An error occurred.")
traceback.print_exc()
@nijine
Copy link
Author

nijine commented Oct 15, 2020

Download, make it executable, and then run.

chmod +x <filename>
./<filename>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment