envaws 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/bin/bash
  2. function ah(){
  3. local opts=$(cat << EOF
  4. aws.sshkeygen.pem :: $(printf "ssh-keygen -m PEM -t rsa -b 2048 -f '%s.pem' -q -N '' && chmod 400 '%s.pem'" "${1}" "${1}")
  5. aws.sshkeygen.pub :: $(printf "ssh-keygen -f '%s' -y" "${1}")
  6. aws.instcon :: $(printf "aws ec2-instance-connect --region eu-central-1 ssh --instance-id %s" "$(xsel -bo | dos2unix | head -n 1 | tr -d '\n\r ')" )
  7. aws.selProf :: awsProfileSelect
  8. aws.store :: awsStoreSecrets
  9. EOF
  10. )
  11. local selOpt=$(echo "${opts}" | grep -vE "^##" | nl -i1 -n'rz' -v0 -w3 | sort -ru | fzf)
  12. if [ "${selOpt}" == "" ]; then
  13. echo ">>>>>>> nothing selected, cancel ..."
  14. return
  15. fi
  16. selOpt=$( echo "${selOpt}" | sed -E 's/(^.*)[ ]*::[ ]*(.*$)/\2/g' )
  17. echo ">>>>>>> ${selOpt}"
  18. history -s "${selOpt}"
  19. history -s "ah"
  20. eval "${selOpt}"
  21. }
  22. function awscliMFA(){
  23. expiration=''
  24. unset $(env | grep -E '^AWS' | grep -v 'PROFILE' | cut -d '=' -f 1)
  25. local aws_mfa_serial=""
  26. local aws_profile=""
  27. local aws_role_arn=""
  28. local aws_source_profile=""
  29. aws_profile=$(cat ~/.aws/config | grep -E '^\[profile' | sed -E 's|(^\[profile )(.*)(\].*)|\2|g' | fzf)
  30. aws_source_profile=$(aws configure --profile "${aws_profile}" get source_profile | sed -E 's|(.*)(_MFA)|\1|g')
  31. aws_role_arn=$(aws configure --profile "${aws_profile}" get role_arn)
  32. if [ "${aws_source_profile}" == "" ]; then
  33. aws_source_profile="${aws_profile}"
  34. fi
  35. aws_mfa_serial=$(aws configure --profile "${aws_source_profile}" get mfa_serial)
  36. if [ "${aws_role_arn}" == "" ]; then
  37. local content=$(
  38. echo "[${aws_profile}_MFA]"
  39. while read -r line; do
  40. cmd=''
  41. key=$(echo $line | cut -d ':' -f 1 )
  42. value=$(echo $line | cut -d ':' -f 2-99 | sed -E 's|^[ ]*||g' )
  43. if [ "${key}" == 'AccessKeyId' ];then echo "aws_access_key_id = ${value}"
  44. elif [ "${key}" == 'SecretAccessKey' ];then echo "aws_secret_access_key = ${value}"
  45. elif [ "${key}" == 'SessionToken' ];then echo "aws_session_token = ${value}"
  46. fi
  47. done < <(aws sts get-session-token --profile "${aws_source_profile}" --serial-number "${aws_mfa_serial}" --output yaml --token-code "${1}")
  48. )
  49. content=$(printf "##>>> %s\n%s\n##<<< %s" "${aws_profile}_MFA" "${content}" "${aws_profile}_MFA")
  50. echo "${content}"
  51. echo "${content}" | xsel -bi
  52. sed -i '/^##>>> '"${aws_profile}_MFA"'/,/^##<<< '"${aws_profile}_MFA"'/{/^##>>> '"${aws_profile}_MFA"'/!{/^##<<< '"${aws_profile}_MFA"'/!d}}' ~/.aws/credentials
  53. while read -r line; do
  54. sed -i -E 's|^##<<< '"${aws_profile}_MFA"'$|'"${line}"'\n##<<< '"${aws_profile}_MFA"'|g' ~/.aws/credentials
  55. done < <(echo "${content}" | sed '1d')
  56. sed -i -e '/##<<< '"${aws_profile}_MFA"'/{n;d}' ~/.aws/credentials
  57. fi
  58. if [ "${aws_role_arn}" != "" ]; then
  59. local content=$(
  60. echo "[${aws_profile}]"
  61. while read -r line; do
  62. cmd=''
  63. key=$(echo $line | cut -d ':' -f 1 )
  64. value=$(echo $line | cut -d ':' -f 2-99 | sed -E 's|^[ ]*||g' )
  65. if [ "${key}" == 'AccessKeyId' ];then echo "aws_access_key_id = ${value}"
  66. elif [ "${key}" == 'SecretAccessKey' ];then echo "aws_secret_access_key = ${value}"
  67. elif [ "${key}" == 'SessionToken' ];then echo "aws_session_token = ${value}"
  68. fi
  69. done < <(aws sts assume-role --profile "${aws_source_profile}" --role-arn "${aws_role_arn}" --role-session-name admin --serial-number "${aws_mfa_serial}" --output yaml --token-code "${1}")
  70. )
  71. content=$(printf "##>>> %s\n%s\n##<<< %s" "${aws_profile}" "${content}" "${aws_profile}")
  72. echo "${content}"
  73. echo "${content}" | xsel -bi
  74. sed -i '/^##>>> '"${aws_profile}"'/,/^##<<< '"${aws_profile}"'/{/^##>>> '"${aws_profile}"'/!{/^##<<< '"${aws_profile}"'/!d}}' ~/.aws/credentials
  75. while read -r line; do
  76. sed -i -E 's|^##<<< '"${aws_profile}"'$|'"${line}"'\n##<<< '"${aws_profile}"'|g' ~/.aws/credentials
  77. done < <(echo "${content}" | sed '1d')
  78. sed -i -e '/##<<< '"${aws_profile}"'/{n;d}' ~/.aws/credentials
  79. fi
  80. }
  81. function awsProfileSelect(){
  82. selection=$(
  83. while read -r line; do
  84. clearName=$(echo "${line}" | cut -d ' ' -f 1)
  85. accountName=$(echo "${line}" | cut -d ' ' -f 2)
  86. printf "%-30s %s\n" "${clearName}" "${accountName}"
  87. done < <(cat ~/.aws/config | grep -E '^\[profile.*##' | sed -E 's|(^[profile[ ]*)(.*)(\].*)(.*##)(.*)|\5 \2|g')
  88. )
  89. selection=$(echo "${selection}" | fzf)
  90. selection=$(echo "${selection}" | tr -s ' ' | cut -d ' ' -f 2)
  91. echo "${selection}"
  92. export AWS_PROFILE="${selection}"
  93. }
  94. function awsStoreSecrets(){
  95. hit="0"
  96. content="$(xsel -bo | dos2unix )"
  97. headline="$(printf "${content}" | head -n 1)"
  98. collector=''
  99. while read line; do
  100. ##echo ">>>${headline}<<< <<<$(printf "${line}")>>> ===${hit}==="
  101. if [ 1 == 0 ] ; then echo 'huhu' ;
  102. elif [ "${headline}" == "${line}" ] && [ "${hit}" == "0" ] ; then hit='1' ; collector=$(printf "${collector}%s" "$(printf "${content}")\n") ;
  103. elif [ "${line}" == "" ] && [ "${hit}" == "1" ] ; then hit='0' ;
  104. fi
  105. if [ "${hit}" == "0" ]; then collector=$(printf "${collector}%s" "${line}\n"); fi
  106. done < <(cat ~/.aws/credentials | dos2unix)
  107. printf "${collector}" >~/.aws/credentials
  108. }