credentials.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import sys
  2. import os.path
  3. import json
  4. import base64
  5. import getpass
  6. import config
  7. from prompt_util import clear_screen, get_input
  8. class VaporfileCredentialException(Exception):
  9. pass
  10. def prompt_save_credentials(args):
  11. clear_screen()
  12. print(" WARNING WARNING WARNING ".center(80,"+"))
  13. print("This will save your Amazon AWS credentials in your home directory.")
  14. print("The file ~/.vaporfile will be readable only by your user account.")
  15. print("It is still your responsibility to secure your computer from unwanted")
  16. print("access. This should be perfectly safe contingent upon proper system security.")
  17. print("".center(80,"+"))
  18. print("")
  19. print("For reference, your amazon credentials can be found at:")
  20. print("https://aws-portal.amazon.com/gp/aws/developer/account/index.html?action=access-key")
  21. print("")
  22. cred = {"access_key":get_input("What is your Access key? : ",accept_blank=False),
  23. "secret_key":get_input("What is your Secret key? : ",accept_blank=False)}
  24. print("")
  25. store_credentials(cred)
  26. clear_screen()
  27. print("Credentials saved in {0}".format(config.__config_file__))
  28. print("File access restricted to your user account ({0}).".format(getpass.getuser()))
  29. print("")
  30. def check_credentials(user_config=None):
  31. if not user_config:
  32. user_config = config.get_config()
  33. try:
  34. user_config["credentials"]["access_key"]
  35. user_config["credentials"]["secret_key"]
  36. except KeyError:
  37. raise VaporfileCredentialException(
  38. "Missing Amazon credentials in config")
  39. return True
  40. def store_credentials(credentials):
  41. """Store Amazon AWS credentials in user's home directory"""
  42. c = config.get_config()
  43. c["credentials"] = credentials
  44. config.save_config(c)
  45. def remove_credentials(credentials):
  46. """Store Amazon AWS credentials in user's home directory"""
  47. c = config.get_config()
  48. del c["credentials"]
  49. config.save_config(c)