Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. set -e
  3. set -o pipefail
  4.  
  5. # FORMATTING ###################################################################
  6.  
  7. DEFAULT=`tput sgr0`
  8. BOLD=`tput bold`
  9. RED=`tput setaf 1`
  10. GREEN=`tput setaf 2`
  11. YELLOW=`tput setaf 3`
  12. CYAN=`tput setaf 6`
  13.  
  14. # FUNCTIONS ####################################################################
  15.  
  16. # log - log to console with color output
  17. function log {
  18. if [ $# -lt 2 ]; then
  19. local log_msg="$1"
  20. else
  21. local log_type="$1"
  22. local log_msg="$2"
  23. fi
  24. case "$log_type" in
  25. info) echo -e "\n${BOLD}${CYAN}${log_msg}${DEFAULT}" ;;
  26. error) echo -e "\n${BOLD}${RED}ERROR: ${log_msg}${DEFAULT}\n" ;;
  27. *) echo -e "\n${log_msg}" ;;
  28. esac
  29. }
  30.  
  31. function usage {
  32. echo "Usage:"
  33. echo " $(basname $0) <role> [cmd] Run AWS command as role"
  34. echo " $(basname $0) <role> Generate env vars for assuming role"
  35. echo ""
  36. echo "Examples:"
  37. echo " \$ $(basename $0) dev-deploy aws s3 ls" # Run AWS command as dev-deploy
  38. echo " \$ $(basename $0) operations-admin" # Print out env vars for operations-admin
  39. echo ""
  40. }
  41.  
  42. function lookup_account {
  43. ar_env=$1
  44.  
  45. account=$(sed -n -e '/variable "aws_accounts"/,/}/ p' ${env_root}/variables.tf | \
  46. sed -nE -e "s/[[:space:]]*${ar_env}[[:space:]]*=[[:space:]]*\"(.*)\"/\1/p"
  47. )
  48.  
  49. if [[ -z $account ]]; then
  50. log error "Failed to lookup account for ${ar_env}" 1>&2
  51. exit 1
  52. fi
  53.  
  54. log info $account
  55. }
  56.  
  57. function aws_assume_role {
  58. ar_env=$1
  59. ar_role=$2
  60.  
  61. if [[ $ar_env == "identity" ]]; then
  62. log info "Do not need to assume_role for the ops environment" 1>&2
  63. return
  64. fi
  65.  
  66. account=$(lookup_account $ar_env)
  67.  
  68. role="arn:aws:iam::${account}:role/${ar_role}"
  69. aws_tmp=$(mktemp -t aws-XXXX.json)
  70.  
  71. aws sts assume-role --role-arn ${role} --role-session-name terraform > ${aws_tmp}
  72.  
  73. aws_key=$(cat ${aws_tmp} | jq -r ".Credentials.AccessKeyId")
  74. aws_secret=$(cat ${aws_tmp} | jq -r ".Credentials.SecretAccessKey")
  75. aws_session_token=$(cat ${aws_tmp} | jq -r ".Credentials.SessionToken")
  76. aws_session_expiration=$(cat ${aws_tmp} | jq -r ".Credentials.Expiration")
  77. }
  78.  
  79. function discover_aws_credentials {
  80. aws_region=${AWS_DEFAULT_REGION:-$(aws configure get region)}
  81. aws_key=${AWS_ACCESS_KEY_ID:-$(aws configure get aws_access_key_id)}
  82. aws_secret=${AWS_SECRET_ACCESS_KEY:-$(aws configure get aws_secret_access_key)}
  83.  
  84. if [[ -z $aws_region || -z $aws_key || -z $aws_secret ]]; then
  85. log error "Could not get AWS credentials" 1>&2
  86. log info "Run 'aws configure' or set AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY/AWS_DEFAULT_REGION" 1>&2
  87. exit 1
  88. fi
  89. }
  90.  
  91. function aws_env {
  92. if [[ -n $aws_session_token ]]; then
  93. export AWS_SESSION_TOKEN="$aws_session_token"
  94. fi
  95. export AWS_ACCESS_KEY_ID="$aws_key"
  96. export AWS_SECRET_ACCESS_KEY="$aws_secret"
  97. export AWS_DEFAULT_REGION="$aws_region"
  98. export TF_VAR_aws_region="$aws_region"
  99. }
  100.  
  101. # Get ARGS
  102. ENV=${1}
  103. ROLE=${2:-deployer}
  104. ARGS=${@:3}
  105.  
  106. if [[ $# < 1 ]]; then
  107. log error "Missing arguments"
  108. usage && exit 1
  109. fi
  110.  
  111. unset AWS_DEFAULT_REGION \
  112. AWS_ACCESS_KEY_ID \
  113. AWS_SECRET_ACCESS_KEY \
  114. AWS_SESSION_TOKEN \
  115. AWS_SESSION_EXPIRATION
  116.  
  117. discover_aws_credentials
  118. aws_assume_role $ENV $ROLE
  119.  
  120. if [[ $# > 0 ]]; then
  121. AWS_DEFAULT_REGION="$aws_region" \
  122. AWS_ACCESS_KEY_ID="$aws_key" \
  123. AWS_SECRET_ACCESS_KEY="$aws_secret" \
  124. AWS_SESSION_TOKEN="$aws_session_token" \
  125. AWS_SESSION_EXPIRATION="$aws_session_expiration" \
  126. "$@"
  127. else
  128. echo export AWS_DEFAULT_REGION=\"$aws_region\"
  129. echo export AWS_ACCESS_KEY_ID=\"$aws_key\"
  130. echo export AWS_SECRET_ACCESS_KEY=\"$aws_secret\"
  131. echo export AWS_SESSION_TOKEN=\"$aws_session_token\"
  132. echo export AWS_SESSION_EXPIRATION=\"$aws_session_expiration\"
  133. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement