aws-creds 0.0.1__py3.7.egg → 0.1.0__py3.7.egg

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
EGG-INFO/PKG-INFO CHANGED
@@ -1,14 +1,40 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: aws-creds
3
- Version: 0.0.1
3
+ Version: 0.1.0
4
4
  Summary: Switch between AWS Credential profiles.
5
5
  Home-page: https://github.com/HammoTime/aws-creds
6
6
  Author: Adam Hammond
7
7
  Author-email: adam@hammo.io
8
8
  License: UNKNOWN
9
- Description: # aws-creds
10
- Makes it easier to use multiple AWS accounts when you don't have SSO available.
9
+ Description: # AWS Credential Picker (aws-creds)
10
+ Makes it easier to use multiple AWS accounts when you don't have SSO available. You can easily save multiple profiles, and then log into them with an simple set of commands.
11
11
 
12
+ ## Installing
13
+
14
+ This utility is available in PyPi and can be installed by running:
15
+ ```
16
+ pip install aws-creds
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ```
22
+ MacBook-Pro:aws-creds HammoTime$ aws-creds help
23
+ Usage: aws-creds [add|rm|ls|login|logout|version|status|help]
24
+ ```
25
+
26
+ ## Commands
27
+
28
+ The following commands are available within the AWS Credential Picker.
29
+
30
+ 1. ```add```: Add a new credential.
31
+ 2. ```rm```: Remove a credential.
32
+ 3. ```ls```: List all the saved credentials.
33
+ 4. ```login```: Login using a credential.
34
+ 5. ```logout```: Remove all of the files used by the aws-cli in ~/.aws.
35
+ 6. ```version```: Output version details.
36
+ 7. ```status```: Print details about the current session (account/user).
37
+ 8. ```help```: Print out **Usage** details.
12
38
  Platform: UNKNOWN
13
39
  Classifier: Development Status :: 4 - Beta
14
40
  Classifier: Environment :: Console
Binary file
Binary file
Binary file
Binary file
bin/cred.py CHANGED
@@ -1,6 +1,7 @@
1
1
  import json
2
2
  import bin.util as util
3
3
  import os
4
+ import boto3
4
5
 
5
6
  # aws-creds add --profile <profile> --access-key <access_key> --secret-key <secret_key> --region
6
7
  # aws-creds rm --profile <profile>
@@ -74,6 +75,10 @@ def login(cred_config):
74
75
  credential_file.write(credential_file_contents)
75
76
  credential_file.close()
76
77
 
78
+ current_profile_file = open(util.get_current_profile_file_name(), "w+")
79
+ current_profile_file.write(cred_config["profile"].upper())
80
+ current_profile_file.close()
81
+
77
82
  options_file = open(util.get_config_file_name(), "w+")
78
83
  options_file.write(options_file_contents)
79
84
  options_file.close()
@@ -82,9 +87,18 @@ def login(cred_config):
82
87
 
83
88
  return True
84
89
 
85
- def logout(cred_config):
86
- print("NYI: Not Yet Implemented.")
87
- return False
90
+ def logout():
91
+ if os.path.exists(util.get_current_profile_file_name()):
92
+ util.add_login_history("LOGOUT", util.get_current_profile())
93
+ os.remove(util.get_current_profile_file_name())
94
+
95
+ if os.path.exists(util.get_credential_file_name()):
96
+ os.remove(util.get_credential_file_name())
97
+
98
+ if os.path.exists(util.get_config_file_name()):
99
+ os.remove(util.get_config_file_name())
100
+
101
+ return True
88
102
 
89
103
  def get_all():
90
104
  accounts_file = open(util.get_accounts_file_name(), "r")
@@ -92,3 +106,27 @@ def get_all():
92
106
  accounts_file.close()
93
107
  return accounts
94
108
 
109
+ def status():
110
+ sts = boto3.client("sts")
111
+ iam = boto3.client("iam")
112
+ session = boto3.Session()
113
+ status_details = {
114
+ "account": None,
115
+ "username": None,
116
+ "aliases": None,
117
+ "access_key": None
118
+ }
119
+
120
+ id = sts.get_caller_identity()
121
+ status_details["account"] = id["Account"]
122
+ status_details["username"] = id["Arn"].split("/")[1]
123
+
124
+ # List account aliases through the pagination interface
125
+ paginator = iam.get_paginator("list_account_aliases")
126
+ for response in paginator.paginate():
127
+ status_details["aliases"] = response["AccountAliases"][0]
128
+
129
+ credentials = session.get_credentials()
130
+ status_details["access_key"] = credentials.access_key
131
+
132
+ return status_details
bin/run.py CHANGED
@@ -5,7 +5,7 @@ import sys
5
5
 
6
6
  def main():
7
7
  util.init_config()
8
- subcommands = ["add", "rm", "ls", "login", "logout", "help"]
8
+ subcommands = ["add", "rm", "ls", "login", "logout", "version", "status", "help"]
9
9
 
10
10
  if len(sys.argv) >= 2:
11
11
  command = sys.argv[1]
bin/ui.py CHANGED
@@ -46,11 +46,43 @@ def login():
46
46
 
47
47
  if account < len(accounts):
48
48
  cred.login(accounts[account])
49
+ days_old = util.get_access_key_age()
50
+
51
+ if days_old == 1:
52
+ time_string = "1 day"
53
+ else:
54
+ time_string = str(days_old) + " days"
55
+
56
+ if days_old < 50:
57
+ print('\033[92m' + "Your current access key is " + time_string + " old." + '\033[0m')
58
+ elif days_old < 60:
59
+ print('\033[93m' + "Your current access key is " + time_string + " old." + '\033[0m')
60
+ else:
61
+ print('\033[91m' + "Your current access key is " + time_string + " old." + '\033[0m')
62
+ print('\033[91m' + "Please rotate it immediately (reason: older than 60 days)." '\033[0m')
49
63
  else:
50
64
  print("Sorry, you haven't picked an option between 1 and " + str(len(accounts)) + ".")
51
65
  except:
52
66
  print("Sorry, you haven't entered a number.")
53
67
 
54
-
55
68
  def logout():
56
- print("Not Yet Implemented!")
69
+ cred.logout()
70
+ print("Logged out successfully.")
71
+
72
+ def update():
73
+ print("Not Yet Implemented!")
74
+
75
+ def version():
76
+ print("Version: 0.1.0")
77
+
78
+ def status():
79
+ if os.path.exists(util.get_current_profile_file_name()):
80
+ details = cred.status()
81
+
82
+ print("Current Profile: " + util.get_current_profile())
83
+ print("Account Number: " + details["account"])
84
+ print("Account Aliases: " + str(details["aliases"]))
85
+ print("Username: " + details["username"])
86
+ print("Access Key: " + details["access_key"])
87
+ else:
88
+ print("Not logged in.")
bin/util.py CHANGED
@@ -1,6 +1,7 @@
1
1
  import os
2
2
  import json
3
3
  import datetime
4
+ import boto3
4
5
 
5
6
  def init_config():
6
7
  home_dir = os.path.expanduser("~/.aws")
@@ -34,6 +35,10 @@ def get_config_file_name():
34
35
  home_dir = os.path.expanduser("~/.aws")
35
36
  return home_dir + "/config"
36
37
 
38
+ def get_current_profile_file_name():
39
+ home_dir = os.path.expanduser("~/.aws")
40
+ return home_dir + "/profile.cred"
41
+
37
42
  def add_login_history(action_type, profile_name):
38
43
  login_history_file = open(get_login_history_file_name(), "a")
39
44
  login_history_file.write(action_type + "," + profile_name + "," + str(datetime.datetime.now()) + "\n")
@@ -63,3 +68,29 @@ def get_usage_string(arguments, subcommand=None):
63
68
  usage_string = usage_string[0:(len(usage_string) - 1)] + "]"
64
69
 
65
70
  return usage_string
71
+
72
+ def get_current_profile():
73
+ current_profile_file = open(get_current_profile_file_name(), "r")
74
+ current_profile = current_profile_file.read()
75
+ current_profile_file.close()
76
+ return current_profile
77
+
78
+ def get_access_key_age():
79
+ session = boto3.Session()
80
+ credentials = session.get_credentials()
81
+ access_key_id = credentials.access_key
82
+ iam = boto3.client('iam')
83
+ access_keys = iam.list_access_keys()
84
+ days_old = 0
85
+
86
+ for access_key_metadata in access_keys["AccessKeyMetadata"]:
87
+ if access_key_metadata["AccessKeyId"] == access_key_id:
88
+ create_date = access_key_metadata["CreateDate"].replace(tzinfo=None)
89
+ current_time = datetime.datetime.utcnow().replace(tzinfo=None)
90
+ time_string = str(current_time - create_date).split(",")[0]
91
+ try:
92
+ days_old = int(time_string.split(" ")[0])
93
+ except:
94
+ days_old = 0
95
+
96
+ return days_old