From 63ab2c5302b5aab0d11304c3649ca253f1d16dfe Mon Sep 17 00:00:00 2001 From: Cell <> Date: Mon, 2 Mar 2020 12:46:43 +0100 Subject: [PATCH 1/3] ADD propagation of AWS_PROFILE when user/profile given --- awsudo/config.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/awsudo/config.py b/awsudo/config.py index b042320..10bbc27 100644 --- a/awsudo/config.py +++ b/awsudo/config.py @@ -25,6 +25,9 @@ def set(key, value): if value: env[key] = value + if profile: + set('AWS_PROFILE', profile) + set('AWS_ACCESS_KEY_ID', creds.access_key) set('AWS_SECRET_ACCESS_KEY', creds.secret_key) From 23330d47cd32b54de588225fc243298aef41096e Mon Sep 17 00:00:00 2001 From: Cell <> Date: Mon, 2 Mar 2020 12:47:40 +0100 Subject: [PATCH 2/3] ADD usage of AWS_PROFILE env variable if set by default. Argument, when given, overwrites. --- awsudo/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/awsudo/main.py b/awsudo/main.py index 017cf3f..58ff649 100644 --- a/awsudo/main.py +++ b/awsudo/main.py @@ -49,7 +49,7 @@ def parseArgs(): if not (args): usage() - profile = None + profile = os.environ.get('AWS_PROFILE') for (option, value) in options: if option == '-u': profile = value From 545afdb62b36e03e94338cad31bbbe8aad8fdd6f Mon Sep 17 00:00:00 2001 From: Cell <> Date: Sun, 29 Mar 2020 09:34:42 +0200 Subject: [PATCH 3/3] ADD test relatest to AWS_PROFILE --- awsudo/test/test_main.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/awsudo/test/test_main.py b/awsudo/test/test_main.py index 0dd69a8..9065850 100644 --- a/awsudo/test/test_main.py +++ b/awsudo/test/test_main.py @@ -16,6 +16,45 @@ def test_no_args(capsys, monkeypatch): assert 'Usage:' in err +def test_only_option(capsys, monkeypatch): + '''With only options, awsudo exits with usage.''' + monkeypatch.setattr(sys, 'argv', ['awsudo', '-u', 'default']) + + with pytest.raises(SystemExit): + main.main() + + out, err = capsys.readouterr() + assert 'Usage:' in err + + +def test_parseArgs_env_profile(monkeypatch): + '''Env vars is taken if no option are given.''' + environ = { + 'AWS_PROFILE': 'profile' + } + monkeypatch.setattr(os, 'environ', environ) + monkeypatch.setattr(sys, 'argv', ['awsudo', 'command']) + + profile, args = main.parseArgs() + + assert profile == 'profile' + assert args == ['command'] + + +def test_parseArgs_option_over_environ(monkeypatch): + '''Options values are taken even if environment variables are set.''' + environ = { + 'AWS_PROFILE': 'profile-environ' + } + monkeypatch.setattr(os, 'environ', environ) + monkeypatch.setattr(sys, 'argv', ['awsudo', '-u', 'profile-option', 'command']) + + profile, args = main.parseArgs() + + assert profile == 'profile-option' + assert args == ['command'] + + def test_cleanEnvironment(monkeypatch): '''cleanEnvironment strips AWS and boto configuration.''' environ = {