GCP Workload Identity Federation Token Generation and Resource Retrieval
This document outlines the process of generating a Workload Identity Federation token using Google Cloud Shell, using the token to retrieve Compute Engine instances in a specified project, and configuring the Cloud Builder role for organization-wide scanning in GCP, AWS, and Azure. The process was completed on June 17, 2025, at 11:00 PM IST, with updates for the Cloud Builder role and simplified token generation added on July 1, 2025.Purpose
To enable secure access to Google Cloud resources using Workload Identity Federation and to configure the Cloud Builder role to collect audit logs, cost data, and resource information across GCP, AWS, and Azure at the project, organization, or folder level.Scope
This document covers:- Setting up Workload Identity Federation in Google Cloud to generate and use a federated token for accessing Compute Engine instances via service account impersonation.
- Creating and assigning the Cloud Builder role in GCP, AWS, and Azure for Steampipe scanning, with options to scope permissions to project, organization, or folder levels.
- Applicable to users managing cloud resources in a single project or across an organization.
Assumptions
- The user has administrative access to the Google Cloud project, AWS account, and Azure subscription.
- The
gcloud,aws, andazCLIs are installed and authenticated with sufficient permissions. - The user has basic knowledge of IAM roles and cloud resource management.
- A service account with appropriate permissions is available for impersonation.
Best Practices for Workload Identity Federation
- Use Short-Lived Tokens: Generate tokens with a short expiration time (e.g., 1 hour) to minimize security risks.
- Least Privilege: Assign only the necessary permissions to the service account or role to reduce the attack surface.
- Secure Service Account Access: Restrict access to service accounts used for impersonation.
- Audit and Monitor: Regularly audit IAM policies and monitor token usage to detect unauthorized access.
- Scope Permissions Appropriately: Assign the Cloud Builder role at the organization level for broad scanning, or at the project/folder level for granular control.
- Use Managed Authentication: Prefer
gcloud authcommands for token generation to simplify the process and reduce manual key management.
Prerequisites
- Google Cloud Shell environment or equivalent terminal setup.
gcloudCLI installed and authenticated with a user or service account with sufficient permissions (e.g.,roles/iam.admin,roles/compute.viewer).awsCLI installed and authenticated for AWS operations.azCLI installed and authenticated for Azure operations.- Google Cloud Project:
your-project-name(Project Number:your-project-number). - AWS Account:
your-aws-account-id. - Azure Subscription:
your-azure-subscription-id. - Service Account:
your-service-account@your-project-name.iam.gserviceaccount.com.
Step-by-Step Guide to Workload Identity Token Creation
The following steps outline the process to set up Workload Identity Federation, generate a token via service account impersonation, and use it to access resources, followed by instructions to configure the Cloud Builder role.Step 1: Set Up a New Workload Identity Pool
Create a new workload identity pool (your-identity-pool).
1.1 Create the Workload Identity Pool
Outcome:
- Pool created successfully.
Step 2: Create and Assign Cloud Builder Roles
The Cloud Builder role provides read-only access to collect cost, audit, and resource data. Below are instructions for creating and assigning this role in GCP, AWS, and Azure, with options to scope permissions to project, organization, or folder levels.2.1 GCP: Create and Assign the Cloud Builder Role
Create the Custom Role
Assign the Role
- Organization Level:
- Folder Level:
- Project Level:
Outcome:
- The Cloud Builder role is created in GCP and assigned to the service account
your-steampipe-svc@your-project-name.iam.gserviceaccount.comat the desired scope (organization, folder, or project).
2.2 AWS: Create and Assign the Cloud Builder Role
Create the IAM Role
cloud-builder-trust-policy.json:
Create and Attach the Policy
cloud-builder-policy.json:
Assign the Role to aAgency
- Assign the role to the Steampipe service by updating the IAM Roles Anywhere profile:
—role-arns “arn:aws:iam::your-aws-account-id:role/CloudBuilder”
—region your-region
Create the Custom Role
cloud-builder-role.json:
Assign the Role
- Subscription Level:
- Management Group Level:
- Resource Group Level:
Outcome:
- The Cloud Builder role is created in Azure and assigned to the service principal
your-steampipe-svc@your-tenant.onmicrosoft.comat the desired scope (subscription, management group, or resource group).
Step 3: Create a Service Account and Grant Access
Create a service account (your-service-account) and grant it the necessary permissions.
3.1 Create the Service Account
Outcome:
- Service account created successfully.
3.2 Grant Workload Identity User Role
Outcome:
- Permissions granted successfully.
Step 4: Generate an Access Token via Service Account Impersonation
Usegcloud auth to authenticate and impersonate the service account to obtain an access token.
4.1 Authenticate with gcloud
4.2 Generate the Access Token
Outcome:
- Generated an access token, e.g.:
- This token was used as
YOUR_ACCESS_TOKEN.
Summary
- Workload Identity Pool:
your-identity-pool - Service Account:
your-service-account@your-project-name.iam.gserviceaccount.com - Access Token:
ya29.c.Kp0B...(example) - Resource Retrieved: Compute Engine instances in
your-zonezone. - Cloud Builder Role: Created and assigned in GCP, AWS, and Azure for Steampipe scanning, with permissions scoped to organization, folder/project, or subscription/resource group levels as needed.