#!/usr/bin/env bash
set -Eeuo pipefail
trap 'declare rc=$?; >&2 echo "Unexpected error executing $BASH_COMMAND at ${BASH_SOURCE[0]} line $LINENO"; exit $rc' ERR
main () {
declare user_name="${1:-}" group_name="" policy_name="" policy_arn=""
if [[ -z "$user_name" ]] ; then
user_name="$(aws sts get-caller-identity | jq -r .Arn | cut -d/ -f2)"
fi
while read -r group_name
do
while read -r policy_arn
do
aws iam get-policy --policy-arn "$policy_arn"
done < <(aws iam list-attached-group-policies --group-name "$group_name" | jq -r '.AttachedPolicies[]|.PolicyArn')
while read -r policy_name
do
aws iam get-group-policy --group-name "$group_name" --policy-name "$policy_name"
done < <(aws iam list-group-policies --group-name "$group_name" | jq -r '.PolicyNames[]|.')
done < <(aws iam list-groups-for-user --user-name "$user_name" | jq -r '.Groups[]|.GroupName')
while read -r policy_arn
do
aws iam get-policy --policy-arn "$policy_arn"
done < <(aws iam list-attached-user-policies --user-name "$user_name" | jq -r '.AttachedPolicies[]|.PolicyArn')
while read -r policy_name
do
aws iam get-user-policy --user-name "$user_name" --policy-name "$policy_name"
done < <(aws iam list-user-policies --user-name "$user_name" | jq -r '.PolicyNames[]|.')
}
main "$@"