Skip to content
This repository has been archived by the owner on Sep 18, 2023. It is now read-only.

Unset environment variables more consistently. #242

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
27 changes: 15 additions & 12 deletions mozilla_aws_cli/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@
"SecretAccessKey": "AWS_SECRET_ACCESS_KEY",
"SessionToken": "AWS_SESSION_TOKEN",
}
# List of environment variables that will be cleared if they are not set.
ENV_VARIABLE_NAMES = [
"AWS_ACCESS_KEY_ID",
"AWS_PROFILE",
"AWS_SECRET_ACCESS_KEY",
"AWS_SECURITY_TOKEN",
"AWS_SESSION_EXPIRATION",
"AWS_SESSION_TOKEN",
"AWS_SHARED_CREDENTIALS_FILE",
]


class Login:
Expand Down Expand Up @@ -414,13 +424,9 @@ def print_output(self):

if self.output == "envvar":
output_map.update(
{ENV_VARIABLE_NAME_MAP[x]: self.credentials[x]
for x in self.credentials
if x in ENV_VARIABLE_NAME_MAP})
output_map.update({
"AWS_PROFILE": None,
"AWS_SHARED_CREDENTIALS_FILE": None,
"MAWS_PROMPT": self.display_name})
{var: self.credentials.get(key)
for key, var in ENV_VARIABLE_NAME_MAP.item()})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Encountered error : Unable to contact AWS : Traceback (most recent call last):
File "/usr/local/lib/python3.9/dist-packages/mozilla_aws_cli-1.2.4-py3.9.egg/mozilla_aws_cli/login.py", line 363, in exchange_token_for_credentials
self.print_output()
File "/usr/local/lib/python3.9/dist-packages/mozilla_aws_cli-1.2.4-py3.9.egg/mozilla_aws_cli/login.py", line 428, in print_output
for key, var in ENV_VARIABLE_NAME_MAP.item()})
AttributeError: 'dict' object has no attribute 'item'

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about that! Should be .items(). Fixed now (togerther with a few other clean-ups). This time I at least did a basic test that it's still working.

output_map["MAWS_PROMPT"] = self.display_name
elif self.output == "shared":
# Write the credentials
path = write_aws_shared_credentials(
Expand All @@ -431,18 +437,13 @@ def print_output(self):
"AWS_PROFILE": self.profile_name,
"AWS_SHARED_CREDENTIALS_FILE": path,
"MAWS_PROMPT": self.display_name})
output_map.update({
x: None for x in ENV_VARIABLE_NAME_MAP.values()})
elif self.output == "awscli":
# Call into aws a bunch of times
if write_aws_cli_credentials(self.profile_name,
self.credentials):
output_map.update({
"AWS_PROFILE": self.profile_name,
"AWS_SHARED_CREDENTIALS_FILE": None,
"MAWS_PROMPT": self.display_name})
output_map.update({
x: None for x in ENV_VARIABLE_NAME_MAP.values()})
else:
logger.error("Unable to write credentials with aws-cli.")
elif self.output == "boto":
Expand Down Expand Up @@ -476,6 +477,8 @@ def print_output(self):
self.role_arn) if self.print_role_arn else None

if output_map and self.print_output_map:
for name in ENV_VARIABLE_NAMES:
output_map.setdefault(name, None)
print(output_set_env_vars(output_map, message))

if self.web_console or self.print_url:
Expand Down