Automating GCP IAM Audit: Exporting Service Account Permissions to csv using gcloud + bash
Automating GCP IAM Audit: Exporting Service Account Permissions to csv using gcloud + bash Here you will find a bash script to use in GCP to get the Service Accounts permissions at the Project Level and at the Resources (or assets) level us
Here you will find a bash script to use in GCP to get the Service Accounts permissions at the Project Level and at the Resources (or assets) level using the Cloud Assets API.
The script is really straightforward it uses gcloud commands to talk with GCP and ask for a list of all the service accounts and their permissions. We use the Cloud Assets API to get the iam permissions at the resources level. Then we use jq to handle the json ouputs and convert them to csv files.
The script
Clone the script to you computer from this repo.
git clone https://github.com/mdiloreto/gcp-sa-iam-scriptYou will get the Service_account-proj-iam.sh script with the following code:
#!/bin/bash
output_csv="gcp_service_accounts_permissions.csv"# print headerecho "Project,ServiceAccount,Role,Member" > "$output_csv"
organization_id="{your ID}" #<<<<<<Replace with your ORG ID>>>>>
# Set timeout for gcloud commandsGCLOUD_TIMEOUT=300
# Get a list of all projectsPROJECTS=$(gcloud projects list --format="value(projectId)")
echo "Projects: $PROJECTS"
# Iterate all projects and get the iam permissionsfor project in $PROJECTS; do echo "Processing project: $project" # proj IAM project_iam=$(timeout $GCLOUD_TIMEOUT gcloud projects get-iam-policy "$project" --format=json) echo "Project IAM $project. Done!"
# get svc acc service_accounts=$(timeout $GCLOUD_TIMEOUT gcloud iam service-accounts list --project="$project" --format="value(email)") echo "Service Accounts: $service_accounts"
for service_account in $service_accounts; do echo "Processing service account: $service_account"
# formating csv echo "$project_iam" | jq -r --arg sa "$service_account" --arg proj "$project" ' .bindings[] | select(.members[] | contains($sa)) | .role as $role | .members[] | select(contains($sa)) | [$proj, $sa, $role, .] | @csv' >> "$output_csv"
donedoneecho "Export completed: $output_csv"
echo "Getting all IAM Permissions at organization level"
# Use Google Cloud Assets API to sarch all the iam policies in json formatgcloud asset search-all-iam-policies --scope=organizations/${organization_id} --format=json > policies.json
# process the JSON and conver to csv(echo "Project,Folder,Resource,Member,Role"jq -r '.[] | .project as $project | (.folders[]? // empty) as $folder | .resource as $resource | .policy.bindings[] | select(.members != null and .role != null) | .role as $role | .members[] | [$project, $folder, $resource, ., $role] | @csv' policies.json) > service_accounts_iam_permissions_org.csvPrerequisites
Before running this script, you will need the following:
- Enable Resource Manager, IAM and Cloud Assets APIs in GCP.
- Access to a GCP account with the necessary permissions to view IAM policies and service accounts.
- The
gcloudCLI installed and configured on your system or you can run it onCloud Shell. - The
jqutility for processing JSON.
Setup
- Clone the repository or download the script to your local machine.
- Ensure that the
gcloudCLI is authorized and configured with your GCP account. You can do this by running:
gcloud auth login3. Set the organization_id variable in the script to your GCP organization ID.
Run the script in Bash
Now you run the script in bash! In this case i’ve done it form Cloud Shell.
mdiloreto@cloudshell:~/script $ ./gcp_service_accounts_permissions_exporter.shOutput
In the same directory of the script you will get two files:
gcp_service_accounts_permissions.csv- Contains service account permissions for each project.service_accounts_iam_permissions_org.csv- Contains service account permissions in al Assets at the organization level.

Enjoy!
Thanks for reading. The learning continues in the next article.


