credential-reports.sh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 -u -o pipefail # can't use -e since a pipe intentionally fails below
  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. export AWS_PAGER="" # Don't paginate output
  24. for i in $ALL_PROFILES; do
  25. if [[ "$i" =~ -gov$ ]]; then
  26. GOVCLOUD_PROFILES="$GOVCLOUD_PROFILES $i"
  27. else
  28. COMMERCIAL_PROFILES="$COMMERCIAL_PROFILES $i"
  29. fi
  30. done
  31. case $PROFILE_SET in
  32. both)
  33. PROFILES="$COMMERCIAL_PROFILES $GOVCLOUD_PROFILES"
  34. ;;
  35. govcloud)
  36. PROFILES="$GOVCLOUD_PROFILES"
  37. ;;
  38. commercial)
  39. PROFILES="$COMMERCIAL_PROFILES"
  40. ;;
  41. esac
  42. for i in $PROFILES; do
  43. echo "======================================================================================"
  44. export AWS_PROFILE=$i
  45. ${AWS} sts get-caller-identity > /dev/null 2>&1
  46. RC=$?
  47. if [[ $RC -eq 0 ]]; then
  48. echo "GetCallerIdentity (AssumeRole Test) for $i OK"
  49. aws iam generate-credential-report --output=text
  50. sleep 5
  51. aws iam get-credential-report | jq -r .Content | base64 --decode | tee $i.tmp
  52. echo "" >> $i.tmp
  53. else
  54. echo "GetCallerIdentity (AssumeRole Test) for $i FAILED"
  55. fi
  56. done
  57. echo Combining
  58. { cat *.tmp | head -1; cat *.tmp | grep -v 'user,arn,user_creation_time' | sort -u; } > combined.tmp2
  59. # Only the columsn we want. Since format is likely to change, this may be a bad idea
  60. echo cutting
  61. cut -d, -f1,2,5,6,8,9,10,11,14,15,16,19,20,21,22 combined.tmp2 > combined.csv
  62. rm -f *.tmp *.tmp2