Skip to content

Instantly share code, notes, and snippets.

@dcloud9
Created January 22, 2020 16:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcloud9/b41e1c2d69ced9377b9afd21dbe8e1c5 to your computer and use it in GitHub Desktop.
Save dcloud9/b41e1c2d69ced9377b9afd21dbe8e1c5 to your computer and use it in GitHub Desktop.
gcp-gcloud-get-serviceaccounts-roles
#! /usr/bin/env bash
# Get all roles attached to all service accounts, users, groups per project per environment in GCP
# Dependencies: Create and auth GCP named config using $gcloud config configurations create <env>|<named config>
# Requires: gcloud, jq
set -e
ENVLIS="dev tst stg prd"
PROJECTLIST="/tmp/projects"
SALIST="/tmp/sa"
TIMESTAMP=$(date "+%Y%m%d%H%M")
OUTLIST="/tmp/out-${TIMESTAMP}"
for ENV in ${ENVLIS}
do
echo "enabling GCP creds for ${ENV}..."
gcloud config configurations activate ${ENV}
gcloud projects list --format=json |jq -r .[].projectId | grep ${ENV} | sort -u > ${PROJECTLIST}-${ENV}
for PROJECT in $(cat ${PROJECTLIST}-${ENV})
do
echo -e "\nProject: ${PROJECT}" | tee -a ${OUTLIST}-${ENV}.txt
gcloud projects get-iam-policy ${PROJECT} --flatten="bindings[].members" --format=json | jq -r .[].bindings.members | sort -u > ${SALIST}-${PROJECT}-${ENV}
for SA in $(cat ${SALIST}-${PROJECT}-${ENV})
do
gcloud projects get-iam-policy ${PROJECT} --flatten="bindings[].members" --filter="bindings.members:${SA}" --format='table[no-heading](bindings.members,bindings.role)' | tee -a ${OUTLIST}-${ENV}.txt
done
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment