Google Cloud Authentication and IAM Guide

Google Cloud Authentication and IAM Guide

Advises on authenticating and authorizing to Google Cloud with IAM, service identities and Application Default Credentials, asking clarifying questions before recommending a secure setup.

Skill files2 files

references/

SKILL.md270 lines
1
---
2
name: google-cloud-recipe-auth
3
metadata:
4
version: "1.0.3"
5
category: GettingStarted
6
description: Provides expert guidance on Identity and Access Management (IAM) and authenticating and authorizing to Google Cloud services and APIs, covering human users, service identities, Application Default Credentials (ADC), and best practices for secure access.
7
---
8
9
# Authenticating and Authorizing to Google Cloud (IAM)
10
11
[Authentication](https://docs.cloud.google.com/docs/authentication.md.txt)
12
is the process of proving **who you are**. In Google Cloud, you represent a
13
**Principal** (an identity like a user or a service). This is the first step
14
before [Authorization](https://docs.cloud.google.com/iam/docs/overview.md.txt)
15
(determining **what you can do**).
16
17
## Attribution
18
19
**MUST READ**: Before executing any `gcloud`, REST API, or MCP command, you
20
MUST read [Usage Attribution](references/attribution.md). For commands executed
21
directly in the terminal (never in generated scripts, Terraform, or user code),
22
prefix `gcloud` with
23
`CLOUDSDK_METRICS_REQUEST_ATTRIBUTION="agent-skills/1.0 (skill:google-cloud-recipe-auth)"`
24
and append `agent-skills/1.0 (skill:google-cloud-recipe-auth)` to `User-Agent`
25
headers.
26
27
## Authentication
28
29
### Clarifying Questions for the Agent
30
31
Before providing a specific solution, clarify the following with the user:
32
33
1. **Who or what is authenticating?** (A human developer, a local script, or an
34
application running in production?)
35
2. **Where is the code running?** (Local laptop, [Compute
36
Engine](https://docs.cloud.google.com/compute/docs.md.txt),
37
[GKE](https://docs.cloud.google.com/kubernetes-engine/docs.md.txt), [Cloud
38
Run](https://docs.cloud.google.com/run/docs.md.txt), or another cloud like
39
AWS/Azure?)
40
3. **What is the target?** (A Google Cloud API like Storage/BigQuery, or a
41
custom application you built?)
42
4. **Are you using a high-level client library?** (e.g., Python, Go, Node.js
43
libraries usually handle ADC automatically.)
44
45
---
46
47
## Human Authentication
48
49
For users to access Google Cloud, they need an identity that Google Cloud can
50
recognize.
51
52
### Types of User Identities
53
54
Google Cloud supports several ways to configure identities for your internal
55
workforce (developers, administrators, employees):
56
57
* **[Google-Managed
58
Accounts](https://docs.cloud.google.com/iam/docs/user-identities.md.txt)**:
59
You can use Cloud Identity or Google Workspace to create managed user
60
accounts. These are called managed accounts because your organization
61
controls their lifecycle and configuration.
62
* **[Federation using Cloud Identity or Google
63
Workspace](https://docs.cloud.google.com/iam/docs/user-identities.md.txt)**:
64
You can federate identities to allow users to use their existing identity
65
and credentials to sign in to Google services. Users authenticate against an
66
external identity provider (IdP), but you must keep accounts synchronized
67
into Google Cloud using tools like Google Cloud Directory Sync (GCDS) or an
68
external authoritative source like Active Directory or Microsoft Entra ID.
69
* **[Workforce Identity
70
Federation](https://docs.cloud.google.com/iam/docs/user-identities.md.txt)**:
71
This lets you use an external IdP to authenticate and authorize a workforce
72
using IAM directly. Unlike standard federation, you do not need to
73
synchronize user identities from your existing IdP to Google Cloud
74
identities. It supports syncless, attribute-based single sign-on.
75
76
### Methods of Access for Developers and Administrators
77
78
Used for interacting with Google Cloud resources and APIs during development and
79
management.
80
81
* **[Google Cloud Console](https://console.cloud.google.com/)**: The primary
82
web interface. You authenticate using your Google Account (Gmail or [Google
83
Workspace](https://workspace.google.com/)).
84
* **[gcloud CLI](https://docs.cloud.google.com/sdk/docs/install-sdk.md.txt)**
85
(`gcloud auth login`): Used to authenticate the CLI itself so you can run
86
management commands (e.g., `gcloud compute instances list`). It uses a
87
**Credential** (like an OAuth 2.0 refresh token) stored locally.
88
* **Local Development with [App Default Credentials
89
(ADC)](https://docs.cloud.google.com/docs/authentication/application-default-credentials.md.txt)
90
(`gcloud auth application-default login`)**: This is different from CLI
91
auth. It creates a local JSON file that Google Cloud **Client Libraries**
92
(Python, Java, etc.) use to act as "you" when you run code on your laptop.
93
* **[Service Account
94
Impersonation](https://docs.cloud.google.com/docs/authentication/use-service-account-impersonation.md.txt)**:
95
For security reasons, developers should avoid downloading Service Account
96
keys entirely. Instead, they should authenticate as humans (`gcloud auth
97
login`) and use Service Account Impersonation to run CLI commands or
98
generate short-lived credentials. This is a critical best practice for local
99
development and troubleshooting.
100
101
### For End-Users and Customers
102
103
Used when a human (who is not a developer) needs to access a web application
104
you've deployed on Google Cloud. Note: These are distinct from workforce
105
identities.
106
107
* **[Identity-Aware Proxy (IAP)](https://docs.cloud.google.com/iap/docs.md.txt)**:
108
Acts as a central authorization layer for web applications. It intercepts
109
web requests and verifies the user's identity (via Google Workspace, Cloud
110
Identity, or external providers) before letting them reach the application.
111
It's often used to protect internal apps without a VPN, or secure customer
112
portals.
113
* **[Identity
114
Platform](https://docs.cloud.google.com/identity-platform/docs.md.txt)**: A
115
Customer Identity and Access Management (CIAM) solution for adding consumer
116
sign-in (email/password, phone, social) directly into the code of your
117
custom-built applications.
118
119
---
120
121
## Service-to-Service Authentication
122
123
When code runs in production, it should use a **Service Account** rather than a
124
human user account.
125
126
### Service Accounts and Service Agents
127
128
* **[Service
129
Account](https://docs.cloud.google.com/iam/docs/service-account-overview.md.txt)**:
130
A special identity intended for non-human users. It's like a "robot
131
identity" with its own email address.
132
* **[Service Agent](https://docs.cloud.google.com/iam/docs/service-agents.md.txt)**:
133
A service account managed by Google that allows a service (like Pub/Sub) to
134
access your resources on your behalf.
135
136
### Best Practice: Attaching Service Accounts
137
138
Instead of using **Service Account Keys** (dangerous JSON files), you should
139
**attach** a custom service account to the Google Cloud resource. The resource's
140
environment then provides a **Token** (a short-lived digital object) via a local
141
metadata server.
142
143
* **[Compute
144
Engine](https://docs.cloud.google.com/compute/docs/access/create-enable-service-accounts-for-instances.md.txt)**:
145
Assign a service account during VM creation.
146
* **[Cloud
147
Run](https://docs.cloud.google.com/run/docs/securing/service-identity.md.txt)**:
148
Assign a service account in the service configuration.
149
150
### Special Cases & Advanced Topics
151
152
#### Kubernetes Engine (GKE)
153
154
Use **[Workload Identity Federation for
155
GKE](https://docs.cloud.google.com/kubernetes-engine/docs/how-to/workload-identity.md.txt)**
156
to map Kubernetes identities to IAM principal identifiers. This grants specific
157
Kubernetes workloads access to specific Google Cloud APIs. [Learn more
158
here.](https://docs.cloud.google.com/kubernetes-engine/docs/how-to/workload-identity.md.txt)
159
160
#### External Workloads ([Workload Identity Federation](https://docs.cloud.google.com/iam/docs/workload-identity-federation.md.txt))
161
162
For code running **outside** Google Cloud (e.g., AWS, Azure, or on-prem), do not
163
use keys. Instead, use Workload Identity Federation to exchange an external
164
token (like an AWS IAM role) for a short-lived Google Cloud access token.
165
166
#### [API Keys](https://docs.cloud.google.com/docs/authentication/api-keys.md.txt)
167
168
API keys are encrypted strings used for public data (e.g., Google Maps) or
169
simplified access like **[Gemini platform express
170
mode](https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/start/express-mode/overview.md.txt)**,
171
which allows fast testing of Gemini models without complex setup. Both humans
172
and services (e.g., Cloud Run-based AI agent) can use API keys, for the services
173
that support it.
174
175
Note: API keys should be
176
[restricted](https://docs.cloud.google.com/api-keys/docs/add-restrictions-api-keys.md.txt)
177
to specific APIs and projects to minimize security risks. Store API keys in a
178
secrets manager like [Secret
179
Manager](https://docs.cloud.google.com/secret-manager/docs.md.txt) to prevent
180
accidental exposure.
181
182
#### OAuth 2.0 Access Scopes
183
184
While IAM is the modern way to handle authorization, legacy Compute Engine VMs
185
and GKE node pools still rely on **Access Scopes** alongside IAM. If a VM's
186
scope is restricted, the attached service account will fail to make API calls
187
even if it has the correct IAM permissions. Check this first if attached service
188
accounts are failing unexpectedly.
189
190
#### Short-Lived Credentials
191
192
The underlying mechanism for impersonation and secure service-to-service
193
communication is the **IAM Service Account Credentials API**. This API generates
194
short-lived access tokens, OpenID Connect (OIDC) ID tokens, or self-signed JSON
195
Web Tokens (JWTs) dynamically, removing the need for static credentials.
196
197
---
198
199
## Authorization
200
201
After Authentication, Google Cloud uses **[Identity and Access Management
202
(IAM)](https://docs.cloud.google.com/iam/docs/overview.md.txt)** to determine
203
what the authenticated principal can do.
204
205
* **Allow Policy**: A record that binds a **Principal** to a **Role** on a
206
**Resource**.
207
* **[Predefined
208
Roles](https://docs.cloud.google.com/iam/docs/roles-permissions)**:
209
Prebuilt roles like `roles/storage.objectViewer` or
210
`roles/bigquery.dataEditor`. **Always try to use these first.**
211
* **[Custom
212
Roles](https://docs.cloud.google.com/iam/docs/creating-custom-roles.md.txt)**:
213
User-defined collections of specific permissions if predefined roles are too
214
broad.
215
216
---
217
218
## Examples
219
220
### Human-to-Service (Local Python Development)
221
222
1. **Authn**: Run `gcloud auth application-default login` to create local
223
credentials (ADC).
224
2. **Authz**: Grant your email the `roles/storage.objectViewer` role on a
225
bucket.
226
3. **Code**: Use the Python `storage.Client()`. It automatically finds your
227
local credentials via ADC. *Note: ADC searches in a specific order—first
228
checking the `GOOGLE_APPLICATION_CREDENTIALS` environment variable, then the
229
local gcloud JSON file, and finally the attached service account metadata
230
server.*
231
232
### Service-to-Service (Cloud Run to Cloud SQL)
233
234
1. **Authn**: Attach a custom Service Account to your Cloud Run service.
235
2. **Authz**: Grant that Service Account the `roles/cloudsql.client` role on
236
the project.
237
3. **Code**: The Cloud Run environment provides the token automatically to the
238
connection driver.
239
240
### Calling a Custom Application ([OIDC](https://docs.cloud.google.com/docs/authentication/get-id-token.md.txt))
241
242
When calling a private Cloud Run service from another service, the caller
243
generates a Google-signed **OpenID Connect (OIDC) ID Token** and passes it in
244
the `Authorization: Bearer <TOKEN>` header.
245
246
---
247
248
## Validation Checklist
249
250
- [ ] Is the user running code locally? Suggest `gcloud auth
251
application-default login` or **Service Account Impersonation**.
252
- [ ] Is the user attempting to use Service Account keys locally? Strongly
253
discourage this and recommend impersonation.
254
- [ ] Is the user running in production? Recommend attaching a custom,
255
least-privilege service account, NOT using keys.
256
- [ ] Is the user relying on the Compute Engine Default Service Account?
257
Recommend creating a custom service account instead.
258
- [ ] Is the user running on another cloud? Recommend Workload Identity
259
Federation.
260
- [ ] Is the user calling a custom app? Recommend OIDC ID Tokens.
261
- [ ] Has the user restricted their API Keys? Check for appropriate [API Key
262
Restrictions](https://docs.cloud.google.com/docs/authentication/api-keys.md.txt).
263
264
## References
265
266
- [Authentication Overview](https://docs.cloud.google.com/docs/authentication.md.txt)
267
- [User Identities](https://docs.cloud.google.com/iam/docs/user-identities.md.txt)
268
- [Application Default Credentials](https://docs.cloud.google.com/docs/authentication/provide-credentials-adc.md.txt)
269
- [Service Account Best Practices](https://docs.cloud.google.com/iam/docs/best-practices-service-accounts.md.txt)
270

© Google LLC · Apache 2.0 · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 14,621 prompts

More by @OpenSourceBuffGuy

See all
Sea turtle carrying floating film studio island prompt image

Sea turtle carrying floating film studio island

Draws a giant sea turtle carrying a floating island film studio through a starry sky, where fairies polish rough film strips into golden film.

image-prompt, nano-banana, 3d-render, cartoon, fantasy-world, creatures, animals, whimsical, dreamy, cinematic, dramatic-lighting, vibrant-colors, bokeh, miniature, widescreen, night-scene, illustration, concept-art, surreal, storytelling, clay-3d

Desert buggy escapes drones past industrial outpost prompt image

Desert buggy escapes drones past industrial outpost

Builds a 15-second desert chase video where a racer in an armored buggy escapes drones past an industrial outpost, using five reference images with set jobs.

video-prompt, hailuo, action, multi-shot, timed-beats, camera-movement, reference-image, character-consistency, negative-prompt, cinematic, sci-fi, cars, landscape, aerial-view, tracking-shot, photorealistic, sound-effects, structured-output, long-form, widescreen

1 view
Cozy Pixel Art Farm Island Village prompt image

Cozy Pixel Art Farm Island Village

Draws a cozy 16-bit pixel-art farming village across six small islands joined by bridges, with exact counts for buildings, animals and crops.

image-prompt, gpt-image, pixel-art, isometric, game-assets, concept-art, maps, worldbuilding, cozy, vibrant-colors, ocean, landscape, wide-shot, widescreen, game-development, video-games, structured-output, whimsical, summer, high-angle, aerial-view, animals, negative-prompt, cityscape

1 view

Reviews

Loading reviews…

Discussion

Loading comments…