aws.plugin.zsh 7.1 KB

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