Skip to content

Instantly share code, notes, and snippets.

@sobi3ch
Last active February 14, 2023 23:36
Show Gist options
  • Save sobi3ch/77892a6f6803968d64c6537b3c76ca22 to your computer and use it in GitHub Desktop.
Save sobi3ch/77892a6f6803968d64c6537b3c76ca22 to your computer and use it in GitHub Desktop.
whoami in az and aws cli versions + get-policy-document for aws
# general
alias aws.whoami='aws iam get-user --query User.Arn --output text'
alias az.whoami='az ad signed-in-user show --query userPrincipalName --output tsv'
# In ~/.aws/credencials|config leave [default] profile empty and name it each one of it so `aws-env -l` can list all of them
# aws.profile # show current profile
# aws.profile profile-name # set profile name
# Double tab completion works
aws.profile ()
{
CMDS="aws-env";
for COMMAND in $CMDS;
do
command -v $COMMAND > /dev/null && continue || {
echo "'$COMMAND' command not found";
return 1
};
done;
complete -W "$(aws-env -l)" aws.profile;
if [ ! -z "$1" ]; then
echo -e "$(aws-env -l)" | grep --color=always -w "$1" > /dev/null;
if [ $? -eq 0 ]; then
$(aws-env $1);
echo "AWS profile set: $AWS_PROFILE";
else
echo "Error: Looks like profile '$1' doesn't exist" 1>&2;
fi;
else
if [ -z "$AWS_PROFILE" ]; then
echo "AWS profile is not set";
else
echo $AWS_PROFILE;
fi;
fi
}
function aws.get-policy-document() {
if [ $# -eq 0 ]
then
echo "Error: No policy name supplied"
else
POLICY_NAME="\`${1}\`"
ARN=$(aws iam list-policies --query "Policies[?PolicyName==${POLICY_NAME}].Arn" --output text)
DEFAULT_VERSION=$(aws iam list-policy-versions --policy-arn $ARN | jq -r '.Versions[] | select(.IsDefaultVersion == true) | .VersionId')
aws iam get-policy-version --policy-arn $ARN --version-id $DEFAULT_VERSION --query PolicyVersion.Document
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment