Skip to content

Instantly share code, notes, and snippets.

@johnludwigm
Last active January 26, 2018 00:20
Show Gist options
  • Save johnludwigm/d6a82099956d7d44b6aa6fb64125b75c to your computer and use it in GitHub Desktop.
Save johnludwigm/d6a82099956d7d44b6aa6fb64125b75c to your computer and use it in GitHub Desktop.
Securely storing JSON credentials for AWS.
>>> aws_access_key_id = "BLPEVU91RZCTCC4CE01J"
>>> aws_secret_access_key = "AOIjf2CEW2faefAErwrt43G/9OaJfijvansEJfib"
>>> region_name = "us-east-1"
>>> the_password_I_type = "MySecretPassword"
>>> ciphertext = encrypt(the_password_I_type.encode(), aws_secret_access_key.encode())
>>> #Let's take a look!
>>> ciphertext
'OJ+T8lHBRqxGyZjTsJjP634iBhjJImYyaep/nv03aCnQkv9+8+I/m+2m1dsAv9JGu6JjnUcVyQSd5WPUEAqSYg=='
>>> unencrypted = decrypted(the_password_I_type.encode(), ciphertext)
>>> #Take a look:
>>> unencrypted
'AOIjf2CEW2faefAErwrt43G/9OaJfijvansEJfib'
>>> #Now store it in json.
>>> settings = {"aws_access_key_id":
"BLPEVU91RZCTCC4CE01J",
"aws_secret_access_key_ciphertext":
"OJ+T8lHBRqxGyZjTsJjP634iBhjJImYyaep/nv03aCnQkv9+8+I/m+2m1dsAv9JGu6JjnUcVyQSd5WPUEAqSYg==",
"region_name":
"us-east-1"}
>>> import json
>>> import os
>>> if not os.path.exists(os.path.expanduser("~/.aws/")):
os.mkdir(os.path.expanduser("~/.aws/"))
>>> with open(os.path.expanduser("~/.aws/config"), "w", encoding="utf-8") as jsonfile:
jsonfile.write(json.dumps(settings, indent="\t"))
>>> #You can retrieve the credentials automatically using the AWS CLI after you properly configure it.
>>> #Otherwise, just load the JSON.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment