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.

Bash
git clone https://github.com/mdiloreto/gcp-sa-iam-script

You will get the Service_account-proj-iam.sh script with the following code:

Plain text
#!/bin/bash
output_csv="gcp_service_accounts_permissions.csv"
# print header
echo "Project,ServiceAccount,Role,Member" > "$output_csv"
organization_id="{your ID}" #<<<<<<Replace with your ORG ID>>>>>
# Set timeout for gcloud commands
GCLOUD_TIMEOUT=300
# Get a list of all projects
PROJECTS=$(gcloud projects list --format="value(projectId)")
echo "Projects: $PROJECTS"
# Iterate all projects and get the iam permissions
for 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"
done
done
echo "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 format
gcloud 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.csv

Prerequisites

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 gcloud CLI installed and configured on your system or you can run it on Cloud Shell.
  • The jq utility for processing JSON.

Setup

  1. Clone the repository or download the script to your local machine.
  2. Ensure that the gcloud CLI is authorized and configured with your GCP account. You can do this by running:
Bash
gcloud auth login

3. 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.

Plain text
mdiloreto@cloudshell:~/script $ ./gcp_service_accounts_permissions_exporter.sh

Output

In the same directory of the script you will get two files:

  1. gcp_service_accounts_permissions.csv - Contains service account permissions for each project.
  2. 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.