项目文件夹

文件
wehub-resource-sync bb5c75ce05
Component Security Validation / Security Audit (push) Has been cancelled
Deploy to Cloudflare Pages / deploy (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:38:58 +08:00

1 行
12 KiB
JSON

{"content": "---\nname: google-cloud-auth\ndescription: Provides expert guidance on authenticating and authorizing to Google Cloud services and APIs, covering human users, service identities, Application Default Credentials (ADC), and best practices for secure access.\nsource: google/skills (Apache 2.0)\n---\n\n# Authenticating to Google Cloud\n\n[Authentication](https://docs.cloud.google.com/docs/authentication) is the\nprocess of proving **who you are**. In Google Cloud, you represent a\n**Principal** (an identity like a user or a service). This is the first step\nbefore [Authorization](https://docs.cloud.google.com/iam/docs/overview)\n(determining **what you can do**).\n\n## Authentication\n\n### Clarifying Questions for the Agent\n\nBefore providing a specific solution, clarify the following with the user:\n\n1. **Who or what is authenticating?** (A human developer, a local script, or an\n application running in production?)\n2. **Where is the code running?** (Local laptop, [Compute\n Engine](https://docs.cloud.google.com/compute/docs),\n [GKE](https://docs.cloud.google.com/kubernetes-engine/docs), [Cloud\n Run](https://docs.cloud.google.com/run/docs), or another cloud like\n AWS/Azure?)\n3. **What is the target?** (A Google Cloud API like Storage/BigQuery, or a\n custom application you built?)\n4. **Are you using a high-level client library?** (e.g., Python, Go, Node.js\n libraries usually handle ADC automatically.)\n\n---\n\n## Human Authentication\n\nFor users to access Google Cloud, they need an identity that Google Cloud can\nrecognize.\n\n### Types of User Identities\n\nGoogle Cloud supports several ways to configure identities for your internal\nworkforce (developers, administrators, employees):\n\n* **[Google-Managed\n Accounts](https://docs.cloud.google.com/iam/docs/user-identities#google-accounts)**:\n You can use Cloud Identity or Google Workspace to create managed user\n accounts. These are called managed accounts because your organization\n controls their lifecycle and configuration.\n* **[Federation using Cloud Identity or Google\n Workspace](https://docs.cloud.google.com/iam/docs/user-identities#synced-federation)**:\n You can federate identities to allow users to use their existing identity\n and credentials to sign in to Google services. Users authenticate against an\n external identity provider (IdP), but you must keep accounts synchronized\n into Google Cloud using tools like Google Cloud Directory Sync (GCDS) or an\n external authoritative source like Active Directory or Microsoft Entra ID.\n* **[Workforce Identity\n Federation](https://docs.cloud.google.com/iam/docs/user-identities#workforce)**:\n This lets you use an external IdP to authenticate and authorize a workforce\n using IAM directly. Unlike standard federation, you do not need to\n synchronize user identities from your existing IdP to Google Cloud\n identities. It supports syncless, attribute-based single sign-on.\n\n### Methods of Access for Developers and Administrators\n\nUsed for interacting with Google Cloud resources and APIs during development and\nmanagement.\n\n* **[Google Cloud Console](https://console.cloud.google.com/)**: The primary\n web interface. You authenticate using your Google Account (Gmail or [Google\n Workspace](https://workspace.google.com/)).\n* **[gcloud CLI](https://docs.cloud.google.com/sdk/docs/install-sdk) (`gcloud\n auth login`)**: Used to authenticate the CLI itself so you can run\n management commands (e.g., `gcloud compute instances list`). It uses a\n **Credential** (like an OAuth 2.0 refresh token) stored locally.\n* **Local Development with [App Default Credentials\n (ADC)](https://docs.cloud.google.com/docs/authentication/application-default-credentials)\n (`gcloud auth application-default login`)**: This is different from CLI\n auth. It creates a local JSON file that Google Cloud **Client Libraries**\n (Python, Java, etc.) use to act as \"you\" when you run code on your laptop.\n* **[Service Account\n Impersonation](https://docs.cloud.google.com/docs/authentication/use-service-account-impersonation)**:\n For security reasons, developers should avoid downloading Service Account\n keys entirely. Instead, they should authenticate as humans (`gcloud auth\n login`) and use Service Account Impersonation to run CLI commands or\n generate short-lived credentials. This is a critical best practice for local\n development and troubleshooting.\n\n### For End-Users and Customers\n\nUsed when a human (who is not a developer) needs to access a web application\nyou've deployed on Google Cloud. Note: These are distinct from workforce\nidentities.\n\n* **[Identity-Aware Proxy (IAP)](https://docs.cloud.google.com/iap/docs)**:\n Acts as a central authorization layer for web applications. It intercepts\n web requests and verifies the user's identity (via Google Workspace, Cloud\n Identity, or external providers) before letting them reach the application.\n It's often used to protect internal apps without a VPN, or secure customer\n portals.\n* **[Identity\n Platform](https://docs.cloud.google.com/identity-platform/docs)**: A\n Customer Identity and Access Management (CIAM) solution for adding consumer\n sign-in (email/password, phone, social) directly into the code of your\n custom-built applications.\n\n---\n\n## Service-to-Service Authentication\n\nWhen code runs in production, it should use a **Service Account** rather than a\nhuman user account.\n\n### Service Accounts and Service Agents\n\n* **[Service\n Account](https://docs.cloud.google.com/iam/docs/service-account-overview)**:\n A special identity intended for non-human users. It's like a \"robot\n identity\" with its own email address.\n* **[Service Agent](https://docs.cloud.google.com/iam/docs/service-agents)**:\n A service account managed by Google that allows a service (like Pub/Sub) to\n access your resources on your behalf.\n\n### Best Practice: Attaching Service Accounts\n\nInstead of using **Service Account Keys** (dangerous JSON files), you should\n**attach** a custom service account to the Google Cloud resource. The resource's\nenvironment then provides a **Token** (a short-lived digital object) via a local\nmetadata server.\n\n* **[Compute\n Engine](https://docs.cloud.google.com/compute/docs/access/create-enable-service-accounts-for-instances)**:\n Assign a service account during VM creation.\n* **[Cloud\n Run](https://docs.cloud.google.com/run/docs/securing/service-identity)**:\n Assign a service account in the service configuration.\n\n### Special Cases & Advanced Topics\n\n#### Kubernetes Engine (GKE)\n\nUse **[Workload Identity Federation for\nGKE](https://docs.cloud.google.com/kubernetes-engine/docs/how-to/workload-identity)**\nto map Kubernetes identities to IAM principal identifiers. This grants specific\nKubernetes workloads access to specific Google Cloud APIs. [Learn more\nhere.](https://docs.cloud.google.com/kubernetes-engine/docs/how-to/workload-identity#configure-authz-principals)\n\n#### External Workloads ([Workload Identity Federation](https://docs.cloud.google.com/iam/docs/workload-identity-federation))\n\nFor code running **outside** Google Cloud (e.g., AWS, Azure, or on-prem), do not\nuse keys. Instead, use Workload Identity Federation to exchange an external\ntoken (like an AWS IAM role) for a short-lived Google Cloud access token.\n\n#### [API Keys](https://docs.cloud.google.com/docs/authentication/api-keys)\n\nAPI keys are encrypted strings used for public data (e.g., Google Maps) or\nsimplified access like **[Vertex AI Express\nMode](https://docs.cloud.google.com/vertex-ai/generative-ai/docs/start/express-mode/overview)**,\nwhich allows fast testing of Gemini models without complex setup. Both humans\nand services (e.g., Cloud Run-based AI agent) can use API keys, for the services\nthat support it.\n\nNote: API keys should be\n[restricted](https://docs.cloud.google.com/api-keys/docs/add-restrictions-api-keys)\nto specific APIs and projects to minimize security risks. Store API keys in a\nsecrets manager like [Secret\nManager](https://docs.cloud.google.com/secret-manager/docs) to prevent\naccidental exposure.\n\n#### OAuth 2.0 Access Scopes\n\nWhile IAM is the modern way to handle authorization, legacy Compute Engine VMs\nand GKE node pools still rely on **Access Scopes** alongside IAM. If a VM's\nscope is restricted, the attached service account will fail to make API calls\neven if it has the correct IAM permissions. Check this first if attached service\naccounts are failing unexpectedly.\n\n#### Short-Lived Credentials\n\nThe underlying mechanism for impersonation and secure service-to-service\ncommunication is the **IAM Service Account Credentials API**. This API generates\nshort-lived access tokens, OpenID Connect (OIDC) ID tokens, or self-signed JSON\nWeb Tokens (JWTs) dynamically, removing the need for static credentials.\n\n---\n\n## Authorization\n\nAfter Authentication, Google Cloud uses **[Identity and Access Management\n(IAM)](https://docs.cloud.google.com/iam/docs/overview)** to determine what the\nauthenticated principal can do.\n\n* **Allow Policy**: A record that binds a **Principal** to a **Role** on a\n **Resource**.\n* **[Predefined\n Roles](https://docs.cloud.google.com/iam/docs/understanding-roles)**:\n Prebuilt roles like `roles/storage.objectViewer` or\n `roles/bigquery.dataEditor`. **Always try to use these first.**\n* **[Custom\n Roles](https://docs.cloud.google.com/iam/docs/creating-custom-roles)**:\n User-defined collections of specific permissions if predefined roles are too\n broad.\n\n---\n\n## Examples\n\n### Human-to-Service (Local Python Development)\n\n1. **Authn**: Run `gcloud auth application-default login` to create local\n credentials (ADC).\n2. **Authz**: Grant your email the `roles/storage.objectViewer` role on a\n bucket.\n3. **Code**: Use the Python `storage.Client()`. It automatically finds your\n local credentials via ADC. *Note: ADC searches in a specific order—first\n checking the `GOOGLE_APPLICATION_CREDENTIALS` environment variable, then the\n local gcloud JSON file, and finally the attached service account metadata\n server.*\n\n### Service-to-Service (Cloud Run to Cloud SQL)\n\n1. **Authn**: Attach a custom Service Account to your Cloud Run service.\n2. **Authz**: Grant that Service Account the `roles/cloudsql.client` role on\n the project.\n3. **Code**: The Cloud Run environment provides the token automatically to the\n connection driver.\n\n### Calling a Custom Application ([OIDC](https://docs.cloud.google.com/docs/authentication/get-id-token))\n\nWhen calling a private Cloud Run service from another service, the caller\ngenerates a Google-signed **OpenID Connect (OIDC) ID Token** and passes it in\nthe `Authorization: Bearer <TOKEN>` header.\n\n---\n\n## Validation Checklist\n\n- [ ] Is the user running code locally? Suggest `gcloud auth\n application-default login` or **Service Account Impersonation**.\n- [ ] Is the user attempting to use Service Account keys locally? Strongly\n discourage this and recommend impersonation.\n- [ ] Is the user running in production? Recommend attaching a custom,\n least-privilege service account, NOT using keys.\n- [ ] Is the user relying on the Compute Engine Default Service Account?\n Recommend creating a custom service account instead.\n- [ ] Is the user running on another cloud? Recommend Workload Identity\n Federation.\n- [ ] Is the user calling a custom app? Recommend OIDC ID Tokens.\n- [ ] Has the user restricted their API Keys? Check for appropriate [API Key\n Restrictions](https://docs.cloud.google.com/docs/authentication/api-keys#adding-application-restrictions).\n\n## References\n\n- [Authentication Overview](https://docs.cloud.google.com/docs/authentication)\n- [User Identities](https://docs.cloud.google.com/iam/docs/user-identities)\n- [Application Default Credentials](https://docs.cloud.google.com/docs/authentication/provide-credentials-adc)\n- [Service Account Best Practices](https://docs.cloud.google.com/iam/docs/best-practices-service-accounts)\n"}