aws.plugin.zsh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN
  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. local exists="$(aws configure get aws_access_key_id --profile $1)"
  19. local role_arn="$(aws configure get role_arn --profile $1)"
  20. local aws_access_key_id=""
  21. local aws_secret_access_key=""
  22. local aws_session_token=""
  23. if [[ -n $exists || -n $role_arn ]]; then
  24. if [[ -n $role_arn ]]; then
  25. local mfa_serial="$(aws configure get mfa_serial --profile $1)"
  26. local mfa_token=""
  27. local mfa_opt=""
  28. if [[ -n $mfa_serial ]]; then
  29. echo "Please enter your MFA token for $mfa_serial:"
  30. read mfa_token
  31. echo "Please enter the session duration in seconds (900-43200; default: 3600, which is the default maximum for a role):"
  32. read sess_duration
  33. if [[ -z $sess_duration ]]; then
  34. sess_duration = 3600
  35. fi
  36. mfa_opt="--serial-number $mfa_serial --token-code $mfa_token --duration-seconds $sess_duration"
  37. fi
  38. local ext_id="$(aws configure get external_id --profile $1)"
  39. local extid_opt=""
  40. if [[ -n $ext_id ]]; then
  41. extid_opt="--external-id $ext_id"
  42. fi
  43. local profile=$1
  44. local source_profile="$(aws configure get source_profile --profile $1)"
  45. if [[ -n $source_profile ]]; then
  46. profile=$source_profile
  47. fi
  48. echo "Assuming role $role_arn using profile $profile"
  49. local assume_cmd=(aws sts assume-role "--profile=$profile" "--role-arn $role_arn" "--role-session-name "$profile"" "$mfa_opt" "$extid_opt")
  50. local JSON="$(eval ${assume_cmd[@]})"
  51. aws_access_key_id="$(echo $JSON | jq -r '.Credentials.AccessKeyId')"
  52. aws_secret_access_key="$(echo $JSON | jq -r '.Credentials.SecretAccessKey')"
  53. aws_session_token="$(echo $JSON | jq -r '.Credentials.SessionToken')"
  54. else
  55. aws_access_key_id="$(aws configure get aws_access_key_id --profile $1)"
  56. aws_secret_access_key="$(aws configure get aws_secret_access_key --profile $1)"
  57. aws_session_token="$(aws configure get aws_session_token --profile $1)"
  58. fi
  59. export AWS_DEFAULT_PROFILE=$1
  60. export AWS_PROFILE=$1
  61. export AWS_EB_PROFILE=$1
  62. export AWS_ACCESS_KEY_ID=$aws_access_key_id
  63. export AWS_SECRET_ACCESS_KEY=$aws_secret_access_key
  64. [[ -z "$aws_session_token" ]] && unset AWS_SESSION_TOKEN || export AWS_SESSION_TOKEN=$aws_session_token
  65. echo "Switched to AWS Profile: $1";
  66. fi
  67. }
  68. function aws_change_access_key() {
  69. if [[ -z "$1" ]]; then
  70. echo "usage: $0 <profile>"
  71. return 1
  72. fi
  73. echo Insert the credentials when asked.
  74. asp "$1" || return 1
  75. AWS_PAGER="" aws iam create-access-key
  76. AWS_PAGER="" aws configure --profile "$1"
  77. echo You can now safely delete the old access key running \`aws iam delete-access-key --access-key-id ID\`
  78. echo Your current keys are:
  79. AWS_PAGER="" aws iam list-access-keys
  80. }
  81. function aws_profiles() {
  82. [[ -r "${AWS_CONFIG_FILE:-$HOME/.aws/config}" ]] || return 1
  83. grep --color=never -Eo '\[.*\]' "${AWS_CONFIG_FILE:-$HOME/.aws/config}" | sed -E 's/^[[:space:]]*\[(profile)?[[:space:]]*([-_[:alnum:]\.@]+)\][[:space:]]*$/\2/g'
  84. }
  85. function _aws_profiles() {
  86. reply=($(aws_profiles))
  87. }
  88. compctl -K _aws_profiles asp aws_change_access_key
  89. # AWS prompt
  90. function aws_prompt_info() {
  91. [[ -z $AWS_PROFILE ]] && return
  92. echo "${ZSH_THEME_AWS_PREFIX:=<aws:}${AWS_PROFILE}${ZSH_THEME_AWS_SUFFIX:=>}"
  93. }
  94. if [ "$SHOW_AWS_PROMPT" != false ]; then
  95. RPROMPT='$(aws_prompt_info)'"$RPROMPT"
  96. fi
  97. # Load awscli completions
  98. # AWS CLI v2 comes with its own autocompletion. Check if that is there, otherwise fall back
  99. if command -v aws_completer &> /dev/null; then
  100. autoload -Uz bashcompinit && bashcompinit
  101. complete -C aws_completer aws
  102. else
  103. function _awscli-homebrew-installed() {
  104. # check if Homebrew is installed
  105. (( $+commands[brew] )) || return 1
  106. # speculatively check default brew prefix
  107. if [ -h /usr/local/opt/awscli ]; then
  108. _brew_prefix=/usr/local/opt/awscli
  109. else
  110. # ok, it is not in the default prefix
  111. # this call to brew is expensive (about 400 ms), so at least let's make it only once
  112. _brew_prefix=$(brew --prefix awscli)
  113. fi
  114. }
  115. # get aws_zsh_completer.sh location from $PATH
  116. _aws_zsh_completer_path="$commands[aws_zsh_completer.sh]"
  117. # otherwise check common locations
  118. if [[ -z $_aws_zsh_completer_path ]]; then
  119. # Homebrew
  120. if _awscli-homebrew-installed; then
  121. _aws_zsh_completer_path=$_brew_prefix/libexec/bin/aws_zsh_completer.sh
  122. # Ubuntu
  123. elif [[ -e /usr/share/zsh/vendor-completions/_awscli ]]; then
  124. _aws_zsh_completer_path=/usr/share/zsh/vendor-completions/_awscli
  125. # NixOS
  126. elif [[ -e "${commands[aws]:P:h:h}/share/zsh/site-functions/aws_zsh_completer.sh" ]]; then
  127. _aws_zsh_completer_path="${commands[aws]:P:h:h}/share/zsh/site-functions/aws_zsh_completer.sh"
  128. # RPM
  129. else
  130. _aws_zsh_completer_path=/usr/share/zsh/site-functions/aws_zsh_completer.sh
  131. fi
  132. fi
  133. [[ -r $_aws_zsh_completer_path ]] && source $_aws_zsh_completer_path
  134. unset _aws_zsh_completer_path _brew_prefix
  135. fi