Service Account Key vs Workload Identity Federation for least‑privilege VM access to a single Cloud Storage bucket
0 reputation · 10 May 2021, 12:19 UTC
0 reputation · 10 May 2021, 12:19 UTC
The goal is to grant a Compute Engine VM read‑only access to a single Cloud Storage bucket while adhering to least‑privilege principles. Two documented approaches are available: (1) create a service account, download its JSON key file, and attach the key to the VM; (2) configure Workload Identity Federation so the VM obtains short‑lived OAuth tokens from an external identity provider without storing any long‑lived secret.
The key‑file method is simple to set up but introduces a long‑lived credential that must be rotated manually, raising the risk of stale keys if rotation lapses. The federation method issues tokens that expire after a short period and are bound to the workload identity, yet the exact inactivity window and revocation propagation delay for OAuth refresh tokens and impersonation tokens are not uniformly documented across Google APIs, leaving operators uncertain how quickly access is cut off after a credential is revoked.
Which approach better satisfies the requirement to avoid storing long‑lived secrets while ensuring timely revocation of access? What is the maximum acceptable delay for revocation propagation when using workload identity federation? How does the absence of built‑in expiry for service account keys affect operational overhead compared with federation?
35275 reputation · 10 May 2021, 14:49 UTC
For a Compute Engine VM that needs read-only access to a single Cloud Storage bucket, Workload Identity Federation is the approach that avoids storing long-lived secrets. A service account key attached to the VM is a long-lived credential that must be managed manually. Federation issues short-lived OAuth access tokens bound to the workload identity, so no JSON key is stored on the VM.
Confirmed facts:
Likely explanation, marked for review:
Service account key:
Workload Identity Federation:
[contact removed].roles/storage.objectViewer on the specific bucket, not project-wide.roles/iam.workloadIdentityUser on the service account to the pool principal. Remove any existing key files from the VM.Missing diagnostic detail that changes the recommendation: Does the VM have outbound HTTPS egress to the federation token endpoint and the Google OAuth token endpoint? If egress is blocked, federation cannot obtain tokens and a key-based approach would be required. Confirm network egress before committing to federation.
Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 10 May 2021, 21:22 UTC
It is important to clarify a distinction between the two methods mentioned. If the VM is running within Google Compute Engine, using an Instance Service Account is the native GCP alternative to JSON keys. This allows the VM to fetch short-lived tokens from the internal metadata server without needing an external Identity Provider (IdP) or the complex setup of Workload Identity Federation.
Workload Identity Federation is primarily designed for workloads outside of GCP (e.g., AWS, Azure, or on-premises) to avoid long-lived keys. For an internal VM, the operational overhead is lowest when using the attached service account, as it provides the same benefit of avoiding stored secrets while simplifying the trust chain.
To ensure true least-privilege, verify that the IAM binding is applied at the bucket level rather than the project level. You can verify this using:
gcloud storage buckets get-iam-policy gs://[BUCKET_NAME]