Skip to content

Instantly share code, notes, and snippets.

@Slater-Victoroff
Last active August 29, 2015 14:01
Show Gist options
  • Save Slater-Victoroff/3080190502be46ec48c2 to your computer and use it in GitHub Desktop.
Save Slater-Victoroff/3080190502be46ec48c2 to your computer and use it in GitHub Desktop.
AWS Credentials
import os
CREDENTIALS = ['AWSAccessKeyId', 'AWSSecretKey']
def credentials(filename=None):
"""
Generally made for standard AWS export format. Turns conf file into python dict
If no filename is given, will attempt to read environment variables instead, then global variables
"""
if filename:
var_list = filter(None, open(filename).read().split('\n'))
credential_dict = dict([var.split('=') for var in var_list])
elif set(CREDENTIALS).issubset(set(os.environ.keys())): # Checking Environment Variables
credential_dict = {var: os.environ.get[var] for var in CREDENTIALS}
elif set(CREDENTIALS).issubset(set(globals().keys())): # Checking Global Variables
credential_dict = {var: globals()[var] for var in CREDENTIALS}
else:
raise MissingCredentialsException
return credential_dict
class MissingCredentialsException(Exception):
def __init__(self, credentials_dict):
self.data = credentials_dict
def value_check(self, test_key):
"""
Checks that the indicated key is present within the dict, the key has a non-false value, and is a string
"""
return (test_key in self.data and bool(self.data[test_key]) and isinstance(self.data[test_key], str))
def __str__(self):
validity_list = [self.value_check(credential) for credential in CREDENTIALS]
missing_credential_list = filter(None, [a*b for a, b in zip(validity_list, CREDENTIALS)])
return "Missing Expected Credentials: %s" % missing_credential_list.__repr__()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment