Skip to content

Commit 897ea53

Browse files
committed
misc changes for housekeeping
- move get_config to config.py - set more types - add more logging - remove required=False - sets more defaults
1 parent 87d4d9b commit 897ea53

File tree

3 files changed

+62
-59
lines changed

3 files changed

+62
-59
lines changed

awth/__init__.py

Lines changed: 34 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# import 1password
22

33
# all aws (apple with sauce)
4-
from awth.config import initial_setup
4+
from awth.config import initial_setup, get_config
55
from awth.util import log_error_and_exit
66
import boto3
77
from botocore.exceptions import ClientError, ParamValidationError
@@ -29,6 +29,7 @@
2929
HELP = options_help()
3030
LOG_LEVEL = 'warn'
3131
LOG_FILE = None
32+
USER = getpass.getuser()
3233

3334

3435
def setup_logger(level="", log_file=""):
@@ -74,7 +75,6 @@ def setup_logger(level="", log_file=""):
7475

7576
@command(cls=RichCommand)
7677
@option('--device',
77-
required=False,
7878
metavar='arn:aws:iam::123456788990:mfa/dudeman',
7979
help="The MFA Device ARN. This value can also be "
8080
"provided via the environment variable 'MFA_DEVICE' or"
@@ -92,59 +92,48 @@ def setup_logger(level="", log_file=""):
9292
help="If using profiles, specify the name here. The "
9393
"default profile name is 'default'. The value can "
9494
"also be provided via the environment variable "
95-
"'AWS_PROFILE'.",
96-
required=False)
97-
@option('--long-term-suffix', '--long-suffix',
95+
"'AWS_PROFILE'.")
96+
@option('--long-term-suffix', '--long-suffix', 'long_term_suffix',
9897
help="The suffix appended to the profile name to"
99-
"identify the long term credential section",
100-
required=False)
101-
@option('--short-term-suffix', '--short-suffix',
98+
"identify the long term credential section")
99+
@option('--short-term-suffix', '--short-suffix', 'short_term_suffix',
102100
help="The suffix appended to the profile name to"
103-
"identify the short term credential section",
104-
required=False)
101+
"identify the short term credential section")
105102
@option('--assume-role', '--assume',
106103
metavar='arn:aws:iam::123456788990:role/RoleName',
107104
help="The ARN of the AWS IAM Role you would like to "
108105
"assume, if specified. This value can also be provided"
109-
" via the environment variable 'MFA_ASSUME_ROLE'",
110-
required=False)
111-
@option('--role-session-name',
112-
help="Friendly session name required when using "
113-
"--assume-role",
114-
default=getpass.getuser(),
115-
required=False)
106+
" via the environment variable 'MFA_ASSUME_ROLE'")
107+
@option('--role-session-name', "role_session_name",
108+
help="Friendly session name required when using ",
109+
default=USER)
116110
@option('--force',
117-
help="Refresh credentials even if currently valid.",
118-
required=False)
119-
@option('--log_level',
111+
help="Refresh credentials even if currently valid.")
112+
@option('--log-level', 'log_level',
120113
type=Choice(['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG', 'NOTSET'],
121114
case_sensitive=False),
122115
help="Set log level",
123-
required=False,
124116
default='DEBUG')
125117
@option('--setup',
126118
help="Setup a new log term credentials section",
127-
is_flag=bool,
128-
required=False)
119+
is_flag=bool)
129120
@option('--token',
130121
help="Provide MFA token as an argument",
131-
required=False,
132122
default=None)
133123
@option('--region',
134124
help="AWS STS Region",
135-
required=False,
125+
default="eu-central-1",
136126
type=str)
137127
@option('--keychain',
138128
is_flag=bool,
139-
help="Use system keychain to store or retrieve long term credentials",
140-
required=False)
129+
help="Use system keychain to store or retrieve long term credentials")
141130
def main(device: str,
142131
duration: int,
143132
profile: str,
144133
long_term_suffix: str,
145134
short_term_suffix: str,
146135
assume_role: str,
147-
role_session_name: str,
136+
role_session_name: str = USER,
148137
force: bool = False,
149138
log_level: str = "INFO",
150139
setup: bool = False,
@@ -190,27 +179,12 @@ def main(device: str,
190179
force)
191180

192181

193-
def get_config(logger, aws_creds_path: str = ""):
194-
"""
195-
get the configuration and parse it
196-
"""
197-
config = configparser.RawConfigParser()
198-
199-
try:
200-
config.read(aws_creds_path)
201-
except configparser.ParsingError:
202-
e = sys.exc_info()[1]
203-
log_error_and_exit(logger,
204-
"There was a problem reading or parsing "
205-
f"your credentials file: {e.args[0]}")
206-
return config
207-
208-
209-
def validate(config,
210-
logger,
182+
def validate(config: configparser.RawConfigParser,
183+
logger: logging.Logger,
211184
profile: str = "",
212185
long_term_suffix: str = "",
213186
short_term_suffix: str = "",
187+
role_session_name: str = "",
214188
assume_role: bool = False,
215189
keychain: bool = False,
216190
device: str = "",
@@ -260,11 +234,15 @@ def validate(config,
260234
try:
261235
# if using the system keychain to store password
262236
if keychain:
237+
logger.info(f"Checking system keychain for AWS {long_term_name} credentials...")
263238
key_id = keyring.get_password('aws:access_key_id', long_term_name)
264239
access_key = keyring.get_password('aws:secret_access_key', long_term_name)
240+
device = keyring.get_password('aws:mfa_device', long_term_name)
265241
else:
242+
logger.info(f"Checking {AWS_CREDS_PATH} for AWS {long_term_name} credentials...")
266243
key_id = config.get(long_term_name, 'aws_access_key_id')
267244
access_key = config.get(long_term_name, 'aws_secret_access_key')
245+
device = config.get(long_term_name, 'aws_mfa_device')
268246
except NoSectionError:
269247
log_error_and_exit(logger,
270248
f"Long term credentials session '{long_term_name}' is missing. "
@@ -394,16 +372,16 @@ def validate(config,
394372
region)
395373

396374

397-
def get_credentials(logger,
398-
config,
399-
short_term_name,
400-
lt_key_id,
401-
lt_access_key,
402-
token,
403-
device,
404-
duration,
405-
assume_role,
406-
short_term_suffix,
375+
def get_credentials(logger: logging.Logger,
376+
config: configparser.RawConfigParser,
377+
short_term_name: str,
378+
lt_key_id: str,
379+
lt_access_key: str,
380+
token: str,
381+
device: str,
382+
duration: int,
383+
assume_role: str,
384+
short_term_suffix: str,
407385
role_session_name: str = "",
408386
region: str = ""):
409387
"""

awth/config.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from awth.util import log_error_and_exit
2+
import logging
23

34
from rich.prompt import Prompt
45

@@ -12,13 +13,17 @@
1213
import getpass
1314
import keyring
1415

16+
from sys import exc_info
1517

16-
def initial_setup(logger,
17-
config,
18+
19+
def initial_setup(logger: logging.Logger,
20+
config: configparser.RawConfigParser,
1821
config_path: str,
1922
keychain: bool = False):
2023
"""
2124
setup the credentials file
25+
26+
returns config object
2227
"""
2328
profile_name = Prompt.ask('Profile name to', default="default")
2429
profile_name = f"{profile_name}-long-term"
@@ -48,3 +53,23 @@ def initial_setup(logger,
4853
config.set(profile_name, 'aws_mfa_device', aws_mfa_device)
4954
with open(config_path, 'w') as configfile:
5055
config.write(configfile)
56+
57+
return config
58+
59+
60+
def get_config(logger,
61+
aws_creds_path: str = ""):
62+
"""
63+
get the configuration and parse it
64+
"""
65+
config = configparser.RawConfigParser()
66+
67+
try:
68+
config.read(aws_creds_path)
69+
except configparser.ParsingError:
70+
e = exc_info()[1]
71+
log_error_and_exit(logger,
72+
"There was a problem reading or parsing "
73+
f"your credentials file: {e.args[0]}")
74+
75+
return config

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "awth"
3-
version = "0.1.0a4"
3+
version = "0.1.0a5"
44
description = "awth your way into aws, again, with mfa"
55
authors = [
66
"jessebot <[email protected]>",

0 commit comments

Comments
 (0)