Skip to content

Commit

Permalink
Add aws-creds-clear-session
Browse files Browse the repository at this point in the history
The `aws-creds-clear-session` shell function is loaded to the shell
while executing either `session-ic` or `session-access-key` commands.
It keeps all the environment variables that should be unset if you want
to log out. Thus, calling this function immediately wipes the session
credentials from the current session.
  • Loading branch information
extsoft committed May 30, 2024
1 parent 6384eeb commit 6360d68
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ to the shell configuration profile file like `.bashrc`, `.zshrc`, etc. Once done
you need to authenticate a specific connection, you run the appropriate alias. It authenticates depending on
the configuration and exports the AWS session environment variables to the current shell session. The tools
such as `aws`, `terraform`, `boto3`, and others will automatically use those variables while executing
requested commands.
requested commands. Once you no longer need the session, you can run `aws-creds-clear-session`
to unset the environment variables.

After the successful authentication, the following AWS-related environment variables are set:
- `AWS_ACCESS_KEY_ID`
Expand Down
56 changes: 56 additions & 0 deletions aws-creds.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

_prog = Path(__file__).name.split(".")[0]
_dependencies_home = Path.home().joinpath(".cache").joinpath(_prog)
_clear_session_function_name = f"{_prog}-clear-session"


def _remove_contents(directory: Path) -> None:
Expand Down Expand Up @@ -174,6 +175,18 @@ def _print_identity_center_alias(
)


def _clear_session_function(*variables: str) -> str:
return "\n".join(
[f"{_clear_session_function_name}() {{", "\n".join([f" unset {variable}" for variable in variables]), "}"]
)


def _print_session_commands_footer():
print("\nUseful tips:", file=sys.stderr)
print(f"1. Run `{_prog}` describes current CLI credentials.", file=sys.stderr)
print(f"2. Run `{_clear_session_function_name}` resets current CLI credentials.", file=sys.stderr)


def _session_ic(ic: IdentityCenter, account_id: str, role: str) -> None:
sso = Session().create_client("sso", region_name=ic.ic_region)
token = _token(ic)
Expand All @@ -194,6 +207,20 @@ def _session_ic(ic: IdentityCenter, account_id: str, role: str) -> None:
print(f'export AWS_SESSION_TOKEN="{role_creds["sessionToken"]}"', file=sys.stdout)
print("AWS environment variables are exported!\n", file=sys.stderr)
_print_ic_information(account_name, account_id, role)
print(
_clear_session_function(
"AWS_CREDS_SESSION_TYPE",
"AWS_CREDS_ACCOUNT_NAME",
"AWS_CREDS_ACCOUNT_ID",
"AWS_CREDS_ROLE_NAME",
"AWS_DEFAULT_REGION",
"AWS_ACCESS_KEY_ID",
"AWS_SECRET_ACCESS_KEY",
"AWS_SESSION_TOKEN",
),
file=sys.stdout,
)
_print_session_commands_footer()


def _print_ic_information(account_name: str, account_id: str, role_name: str) -> None:
Expand Down Expand Up @@ -302,6 +329,21 @@ def perform(self, mfa_device: Optional[str], mfa_code: Optional[str]) -> None:
print(f'export AWS_DEFAULT_REGION="{self._region}"', file=sys.stdout)
print("AWS environment variables are exported!\n", file=sys.stderr)
_print_assume_role(self._session_name, self._user_name, self._account_id, self._region, self._role_arn)
print(
_clear_session_function(
"AWS_CREDS_SESSION_TYPE",
"AWS_CREDS_SESSION_NAME",
"AWS_CREDS_SESSION_ROLE",
"AWS_CREDS_USER_NAME",
"AWS_CREDS_ACCOUNT_ID",
"AWS_ACCESS_KEY_ID",
"AWS_SECRET_ACCESS_KEY",
"AWS_SESSION_TOKEN",
"AWS_DEFAULT_REGION",
),
file=sys.stdout,
)
_print_session_commands_footer()


def _print_access_key(session_name: str, user: str, account_id: str, region: str) -> None:
Expand Down Expand Up @@ -336,6 +378,20 @@ def perform(self, mfa_device: Optional[str], mfa_code: Optional[str]) -> None:
print(f'export AWS_DEFAULT_REGION="{self._region}"', file=sys.stdout)
print("AWS environment variables are exported!\n", file=sys.stderr)
_print_access_key(self._session_name, self._user_name, self._account_id, self._region)
print(
_clear_session_function(
"AWS_CREDS_SESSION_TYPE",
"AWS_CREDS_SESSION_NAME",
"AWS_CREDS_USER_NAME",
"AWS_CREDS_ACCOUNT_ID",
"AWS_ACCESS_KEY_ID",
"AWS_SECRET_ACCESS_KEY",
"AWS_SESSION_TOKEN",
"AWS_DEFAULT_REGION",
),
file=sys.stdout,
)
_print_session_commands_footer()


def _session_access_key(name: str, access_key: str, secret_key: str, region: str, role_arn: Optional[str]) -> None:
Expand Down

0 comments on commit 6360d68

Please sign in to comment.