From 94a0481f182b4a2cc87e76af8761e00c07de79dc Mon Sep 17 00:00:00 2001 From: Basti <107778224+krakenbite@users.noreply.github.com> Date: Wed, 27 Dec 2023 10:59:05 +0100 Subject: feat(aws): add sso logout capabilities (#12113) --- plugins/aws/README.md | 1 + 1 file changed, 1 insertion(+) (limited to 'plugins/aws/README.md') diff --git a/plugins/aws/README.md b/plugins/aws/README.md index 9e1e055b8..4850e223e 100644 --- a/plugins/aws/README.md +++ b/plugins/aws/README.md @@ -16,6 +16,7 @@ plugins=(... aws) It also sets `$AWS_EB_PROFILE` to `` for the Elastic Beanstalk CLI. It sets `$AWS_PROFILE_REGION` for display in `aws_prompt_info`. Run `asp` without arguments to clear the profile. * `asp [] login`: If AWS SSO has been configured in your aws profile, it will run the `aws sso login` command following profile selection. +* `asp [] logout`: If AWS SSO has been configured in your aws profile, it will run the `aws sso logout` command following profile selection. * `asr []`: sets `$AWS_REGION` and `$AWS_DEFAULT_REGION` (legacy) to ``. Run `asr` without arguments to clear the profile. -- cgit v1.2.3-70-g09d2 From 3e92d251ba7a950d050234d8780ce75085b1b1d4 Mon Sep 17 00:00:00 2001 From: "B. Hamonangan" <70834922+hamonangann@users.noreply.github.com> Date: Wed, 27 Dec 2023 17:09:23 +0700 Subject: feat(aws): add support for sso sessions login (#12090) Co-authored-by: Carlo Sala --- plugins/aws/README.md | 1 + plugins/aws/aws.plugin.zsh | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) (limited to 'plugins/aws/README.md') diff --git a/plugins/aws/README.md b/plugins/aws/README.md index 4850e223e..dbf8dcfeb 100644 --- a/plugins/aws/README.md +++ b/plugins/aws/README.md @@ -16,6 +16,7 @@ plugins=(... aws) It also sets `$AWS_EB_PROFILE` to `` for the Elastic Beanstalk CLI. It sets `$AWS_PROFILE_REGION` for display in `aws_prompt_info`. Run `asp` without arguments to clear the profile. * `asp [] login`: If AWS SSO has been configured in your aws profile, it will run the `aws sso login` command following profile selection. +* `asp [] login []`: In addition to `asp [] login`, if SSO session has been configured in your aws profile, it will run the `aws sso login --sso-session ` command following profile selection. * `asp [] logout`: If AWS SSO has been configured in your aws profile, it will run the `aws sso logout` command following profile selection. * `asr []`: sets `$AWS_REGION` and `$AWS_DEFAULT_REGION` (legacy) to ``. diff --git a/plugins/aws/aws.plugin.zsh b/plugins/aws/aws.plugin.zsh index 0d7040f92..fd00714e7 100644 --- a/plugins/aws/aws.plugin.zsh +++ b/plugins/aws/aws.plugin.zsh @@ -29,7 +29,11 @@ function asp() { export AWS_PROFILE_REGION=$(aws configure get region) if [[ "$2" == "login" ]]; then - aws sso login + if [[ -n "$3" ]]; then + aws sso login --sso-session $3 + else + aws sso login + fi elif [[ "$2" == "logout" ]]; then aws sso logout fi -- cgit v1.2.3-70-g09d2 From 05bf69c604e34300b02406462d4376c86d595470 Mon Sep 17 00:00:00 2001 From: 0rxa Date: Thu, 1 Feb 2024 17:11:13 +0100 Subject: feat(aws): implement permanent state (#12018) Co-authored-by: Rei Arifi Co-authored-by: Hysen Ndregjoni --- plugins/aws/README.md | 5 +++++ plugins/aws/aws.plugin.zsh | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) (limited to 'plugins/aws/README.md') diff --git a/plugins/aws/README.md b/plugins/aws/README.md index dbf8dcfeb..0d0773f63 100644 --- a/plugins/aws/README.md +++ b/plugins/aws/README.md @@ -47,6 +47,11 @@ plugins=(... aws) Some themes might overwrite the value of RPROMPT instead of appending to it, so they'll need to be fixed to see the AWS profile/region prompt. +* Set `AWS_PROFILE_STATE_ENABLED=true` in your zshrc file if you want the aws profile to persist between shell sessions. + This option might slow down your shell startup time. + By default the state file path is `/tmp/.aws_current_profile`. This means that the state won't survive a reboot or otherwise GC. + You can control the state file path using the `AWS_STATE_FILE` environment variable. + ## Theme The plugin creates an `aws_prompt_info` function that you can use in your theme, which displays diff --git a/plugins/aws/aws.plugin.zsh b/plugins/aws/aws.plugin.zsh index 7a2e08a7c..071dd1f0b 100644 --- a/plugins/aws/aws.plugin.zsh +++ b/plugins/aws/aws.plugin.zsh @@ -6,10 +6,26 @@ function agr() { echo $AWS_REGION } +# Update state file if enabled +function _aws_update_state() { + if [[ "$AWS_PROFILE_STATE_ENABLED" == true ]]; then + test -d $(dirname ${AWS_STATE_FILE}) || exit 1 + echo "${AWS_PROFILE} ${AWS_REGION}" > "${AWS_STATE_FILE}" + fi +} + +function _aws_clear_state() { + if [[ "$AWS_PROFILE_STATE_ENABLED" == true ]]; then + test -d $(dirname ${AWS_STATE_FILE}) || exit 1 + echo -n > "${AWS_STATE_FILE}" + fi +} + # AWS profile selection function asp() { if [[ -z "$1" ]]; then unset AWS_DEFAULT_PROFILE AWS_PROFILE AWS_EB_PROFILE AWS_PROFILE_REGION + _aws_clear_state echo AWS profile cleared. return fi @@ -28,6 +44,8 @@ function asp() { export AWS_PROFILE_REGION=$(aws configure get region) + _aws_update_state + if [[ "$2" == "login" ]]; then if [[ -n "$3" ]]; then aws sso login --sso-session $3 @@ -43,6 +61,7 @@ function asp() { function asr() { if [[ -z "$1" ]]; then unset AWS_DEFAULT_REGION AWS_REGION + _aws_update_state echo AWS region cleared. return fi @@ -56,6 +75,7 @@ function asr() { export AWS_REGION=$1 export AWS_DEFAULT_REGION=$1 + _aws_update_state } # AWS profile switch @@ -255,6 +275,22 @@ if [[ "$SHOW_AWS_PROMPT" != false && "$RPROMPT" != *'$(aws_prompt_info)'* ]]; th RPROMPT='$(aws_prompt_info)'"$RPROMPT" fi +if [[ "$AWS_PROFILE_STATE_ENABLED" == true ]]; then + AWS_STATE_FILE="${AWS_STATE_FILE:-/tmp/.aws_current_profile}" + test -s "${AWS_STATE_FILE}" || return + + aws_state=($(cat $AWS_STATE_FILE)) + + export AWS_DEFAULT_PROFILE="${aws_state[1]}" + export AWS_PROFILE="$AWS_DEFAULT_PROFILE" + export AWS_EB_PROFILE="$AWS_DEFAULT_PROFILE" + + test -z "${aws_state[2]}" && AWS_REGION=$(aws configure get region) + + export AWS_REGION=${AWS_REGION:-$aws_state[2]} + export AWS_DEFAULT_REGION="$AWS_REGION" +fi + # Load awscli completions # AWS CLI v2 comes with its own autocompletion. Check if that is there, otherwise fall back -- cgit v1.2.3-70-g09d2