aws.plugin.zsh 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. function agp() {
  2. echo $AWS_PROFILE
  3. }
  4. # AWS profile selection
  5. function asp() {
  6. if [[ -z "$1" ]]; then
  7. unset AWS_DEFAULT_PROFILE AWS_PROFILE AWS_EB_PROFILE
  8. echo AWS profile cleared.
  9. return
  10. fi
  11. local -a available_profiles
  12. available_profiles=($(aws_profiles))
  13. if [[ -z "${available_profiles[(r)$1]}" ]]; then
  14. echo "${fg[red]}Profile '$1' not found in '${AWS_CONFIG_FILE:-$HOME/.aws/config}'" >&2
  15. echo "Available profiles: ${(j:, :)available_profiles:-no profiles found}${reset_color}" >&2
  16. return 1
  17. fi
  18. export AWS_DEFAULT_PROFILE=$1
  19. export AWS_PROFILE=$1
  20. export AWS_EB_PROFILE=$1
  21. if [[ "$2" == "login" ]]; then
  22. aws sso login
  23. fi
  24. }
  25. # AWS profile switch
  26. function acp() {
  27. if [[ -z "$1" ]]; then
  28. unset AWS_DEFAULT_PROFILE AWS_PROFILE AWS_EB_PROFILE
  29. unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN
  30. echo AWS profile cleared.
  31. return
  32. fi
  33. local -a available_profiles
  34. available_profiles=($(aws_profiles))
  35. if [[ -z "${available_profiles[(r)$1]}" ]]; then
  36. echo "${fg[red]}Profile '$1' not found in '${AWS_CONFIG_FILE:-$HOME/.aws/config}'" >&2
  37. echo "Available profiles: ${(j:, :)available_profiles:-no profiles found}${reset_color}" >&2
  38. return 1
  39. fi
  40. local profile="$1"
  41. # Get fallback credentials for if the aws command fails or no command is run
  42. local aws_access_key_id="$(aws configure get aws_access_key_id --profile $profile)"
  43. local aws_secret_access_key="$(aws configure get aws_secret_access_key --profile $profile)"
  44. local aws_session_token="$(aws configure get aws_session_token --profile $profile)"
  45. # First, if the profile has MFA configured, lets get the token and session duration
  46. local mfa_serial="$(aws configure get mfa_serial --profile $profile)"
  47. local sess_duration="$(aws configure get duration_seconds --profile $profile)"
  48. if [[ -n "$mfa_serial" ]]; then
  49. local -a mfa_opt
  50. local mfa_token
  51. echo -n "Please enter your MFA token for $mfa_serial: "
  52. read -r mfa_token
  53. if [[ -z "$sess_duration" ]]; then
  54. echo -n "Please enter the session duration in seconds (900-43200; default: 3600, which is the default maximum for a role): "
  55. read -r sess_duration
  56. fi
  57. mfa_opt=(--serial-number "$mfa_serial" --token-code "$mfa_token" --duration-seconds "${sess_duration:-3600}")
  58. fi
  59. # Now see whether we need to just MFA for the current role, or assume a different one
  60. local role_arn="$(aws configure get role_arn --profile $profile)"
  61. local sess_name="$(aws configure get role_session_name --profile $profile)"
  62. if [[ -n "$role_arn" ]]; then
  63. # Means we need to assume a specified role
  64. aws_command=(aws sts assume-role --role-arn "$role_arn" "${mfa_opt[@]}")
  65. # Check whether external_id is configured to use while assuming the role
  66. local external_id="$(aws configure get external_id --profile $profile)"
  67. if [[ -n "$external_id" ]]; then
  68. aws_command+=(--external-id "$external_id")
  69. fi
  70. # Get source profile to use to assume role
  71. local source_profile="$(aws configure get source_profile --profile $profile)"
  72. if [[ -z "$sess_name" ]]; then
  73. sess_name="${source_profile:-profile}"
  74. fi
  75. aws_command+=(--profile="${source_profile:-profile}" --role-session-name "${sess_name}")
  76. echo "Assuming role $role_arn using profile ${source_profile:-profile}"
  77. else
  78. # Means we only need to do MFA
  79. aws_command=(aws sts get-session-token --profile="$profile" "${mfa_opt[@]}")
  80. echo "Obtaining session token for profile $profile"
  81. fi
  82. # Format output of aws command for easier processing
  83. aws_command+=(--query '[Credentials.AccessKeyId,Credentials.SecretAccessKey,Credentials.SessionToken]' --output text)
  84. # Run the aws command to obtain credentials
  85. local -a credentials
  86. credentials=(${(ps:\t:)"$(${aws_command[@]})"})
  87. if [[ -n "$credentials" ]]; then
  88. aws_access_key_id="${credentials[1]}"
  89. aws_secret_access_key="${credentials[2]}"
  90. aws_session_token="${credentials[3]}"
  91. fi
  92. # Switch to AWS profile
  93. if [[ -n "${aws_access_key_id}" && -n "$aws_secret_access_key" ]]; then
  94. export AWS_DEFAULT_PROFILE="$profile"
  95. export AWS_PROFILE="$profile"
  96. export AWS_EB_PROFILE="$profile"
  97. export AWS_ACCESS_KEY_ID="$aws_access_key_id"
  98. export AWS_SECRET_ACCESS_KEY="$aws_secret_access_key"
  99. if [[ -n "$aws_session_token" ]]; then
  100. export AWS_SESSION_TOKEN="$aws_session_token"
  101. else
  102. unset AWS_SESSION_TOKEN
  103. fi
  104. echo "Switched to AWS Profile: $profile"
  105. fi
  106. }
  107. function aws_change_access_key() {
  108. if [[ -z "$1" ]]; then
  109. echo "usage: $0 <profile>"
  110. return 1
  111. fi
  112. echo "Insert the credentials when asked."
  113. asp "$1" || return 1
  114. AWS_PAGER="" aws iam create-access-key
  115. AWS_PAGER="" aws configure --profile "$1"
  116. echo "You can now safely delete the old access key running \`aws iam delete-access-key --access-key-id ID\`"
  117. echo "Your current keys are:"
  118. AWS_PAGER="" aws iam list-access-keys
  119. }
  120. function aws_profiles() {
  121. [[ -r "${AWS_CONFIG_FILE:-$HOME/.aws/config}" ]] || return 1
  122. grep --color=never -Eo '\[.*\]' "${AWS_CONFIG_FILE:-$HOME/.aws/config}" | sed -E 's/^[[:space:]]*\[(profile)?[[:space:]]*([-_[:alnum:]\.@]+)\][[:space:]]*$/\2/g'
  123. }
  124. function _aws_profiles() {
  125. reply=($(aws_profiles))
  126. }
  127. compctl -K _aws_profiles asp acp aws_change_access_key
  128. # AWS prompt
  129. function aws_prompt_info() {
  130. [[ -z $AWS_PROFILE ]] && return
  131. echo "${ZSH_THEME_AWS_PREFIX:=<aws:}${AWS_PROFILE}${ZSH_THEME_AWS_SUFFIX:=>}"
  132. }
  133. if [[ "$SHOW_AWS_PROMPT" != false && "$RPROMPT" != *'$(aws_prompt_info)'* ]]; then
  134. RPROMPT='$(aws_prompt_info)'"$RPROMPT"
  135. fi
  136. # Load awscli completions
  137. # AWS CLI v2 comes with its own autocompletion. Check if that is there, otherwise fall back
  138. if command -v aws_completer &> /dev/null; then
  139. autoload -Uz bashcompinit && bashcompinit
  140. complete -C aws_completer aws
  141. else
  142. function _awscli-homebrew-installed() {
  143. # check if Homebrew is installed
  144. (( $+commands[brew] )) || return 1
  145. # speculatively check default brew prefix
  146. if [ -h /usr/local/opt/awscli ]; then
  147. _brew_prefix=/usr/local/opt/awscli
  148. else
  149. # ok, it is not in the default prefix
  150. # this call to brew is expensive (about 400 ms), so at least let's make it only once
  151. _brew_prefix=$(brew --prefix awscli)
  152. fi
  153. }
  154. # get aws_zsh_completer.sh location from $PATH
  155. _aws_zsh_completer_path="$commands[aws_zsh_completer.sh]"
  156. # otherwise check common locations
  157. if [[ -z $_aws_zsh_completer_path ]]; then
  158. # Homebrew
  159. if _awscli-homebrew-installed; then
  160. _aws_zsh_completer_path=$_brew_prefix/libexec/bin/aws_zsh_completer.sh
  161. # Ubuntu
  162. elif [[ -e /usr/share/zsh/vendor-completions/_awscli ]]; then
  163. _aws_zsh_completer_path=/usr/share/zsh/vendor-completions/_awscli
  164. # NixOS
  165. elif [[ -e "${commands[aws]:P:h:h}/share/zsh/site-functions/aws_zsh_completer.sh" ]]; then
  166. _aws_zsh_completer_path="${commands[aws]:P:h:h}/share/zsh/site-functions/aws_zsh_completer.sh"
  167. # RPM
  168. else
  169. _aws_zsh_completer_path=/usr/share/zsh/site-functions/aws_zsh_completer.sh
  170. fi
  171. fi
  172. [[ -r $_aws_zsh_completer_path ]] && source $_aws_zsh_completer_path
  173. unset _aws_zsh_completer_path _brew_prefix
  174. fi