Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pop explicit credentials when switching profiles #178

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions awsshell/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class AWSShell(object):
"""

def __init__(self, completer, model_completer, docs,
input=None, output=None):
env=None, input=None, output=None):
self.completer = completer
self.model_completer = model_completer
self.history = InMemoryHistory()
Expand All @@ -232,10 +232,12 @@ def __init__(self, completer, model_completer, docs,
self.refresh_cli = False
self.key_manager = None
self._dot_cmd = DotCommandHandler()
self._env = os.environ.copy()
self._profile = None
self._input = input
self._output = output
self._env = env
if self._env is None:
self._env = os.environ.copy()

# These attrs come from the config file.
self.config_obj = None
Expand Down Expand Up @@ -483,6 +485,11 @@ def profile(self, new_profile_name):
# it's worth adding an event system or observers just yet.
# If this gets hard to manage, the complexity of those systems
# would be worth it.

# Remove explicit keys to ensure the profile will be used
self._env.pop('AWS_ACCESS_KEY_ID', None)
self._env.pop('AWS_SECRET_ACCESS_KEY', None)

self._env['AWS_DEFAULT_PROFILE'] = new_profile_name
self.completer.change_profile(new_profile_name)
self._profile = new_profile_name
14 changes: 14 additions & 0 deletions tests/unit/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ def test_delegates_to_complete_changing_profile():
assert shell.profile == 'mynewprofile'


def test_change_profile_pops_explicit_keys():
env = {
'AWS_ACCESS_KEY_ID': 'test',
'AWS_SECRET_ACCESS_KEY': 'test',
}
shell = app.AWSShell(mock.Mock(), mock.Mock(), mock.Mock(), env=env)
shell.profile = 'newprofile'
# ensure the profile is set
assert shell.profile == 'newprofile'
# ensure the credentials are not explicityly set in env
assert env.get('AWS_ACCESS_KEY_ID', None) is None
assert env.get('AWS_SECRET_ACCESS_KEY', None) is None


def test_cd_handler_can_chdir():
chdir = mock.Mock()
handler = app.ChangeDirHandler(chdir=chdir)
Expand Down