delete-cloudcheckr-user.sh 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/bin/bash
  2. # Runs the same aws CLI command in "most" of the defined profiles
  3. # in $HOME/.aws/config
  4. #
  5. # You can pass in via an environment variable a "profile set"
  6. # of either "commercial", "govcloud", or "both". Default is "both"
  7. #
  8. # Does an "aws sts get-caller-identity" to confirm that your AssumeRole
  9. # and other necessities are properly set up before attempting to call the
  10. # actual AWS command.
  11. #
  12. # PROFILE_SET=commercial aws-all.sh ec2 describe-instances
  13. #
  14. set -eu -o pipefail
  15. AWS=${AWS:-/usr/local/bin/aws}
  16. PROFILE_SET=${PROFILE_SET:-both}
  17. ALL_PROFILES=$( egrep "\[profile" ~/.aws/config | \
  18. awk '{ print $2 }' | \
  19. sed "s/\]//" | \
  20. egrep -v "default|commercial|govcloud" )
  21. COMMERCIAL_PROFILES=""
  22. GOVCLOUD_PROFILES=""
  23. for i in $ALL_PROFILES; do
  24. if [[ "$i" =~ -gov$ ]]; then
  25. GOVCLOUD_PROFILES="$GOVCLOUD_PROFILES $i"
  26. else
  27. COMMERCIAL_PROFILES="$COMMERCIAL_PROFILES $i"
  28. fi
  29. done
  30. case $PROFILE_SET in
  31. both)
  32. PROFILES="$COMMERCIAL_PROFILES $GOVCLOUD_PROFILES"
  33. ;;
  34. govcloud)
  35. PROFILES="$GOVCLOUD_PROFILES"
  36. ;;
  37. commercial)
  38. PROFILES="$COMMERCIAL_PROFILES"
  39. ;;
  40. esac
  41. for i in $PROFILES; do
  42. echo "======================================================================================"
  43. export AWS_PROFILE=$i
  44. set +e
  45. ${AWS} sts get-caller-identity > /dev/null 2>&1
  46. RC=$?
  47. set -e
  48. if [[ $RC -eq 0 ]]; then
  49. echo "GetCallerIdentity (AssumeRole Test) for $i OK"
  50. echo delete-iam-user.py CloudCheckrUser
  51. delete-iam-user.py CloudCheckrUser
  52. else
  53. echo "GetCallerIdentity (AssumeRole Test) for $i FAILED"
  54. fi
  55. done