jpskill.com
🛠️ 開発・MCP コミュニティ

gcp-auth

Google Cloudの認証認可設定を、ローカル開発からCloud Run/Functions/GKEへのデプロイ、API呼び出しまで、様々な環境に合わせて簡素化し、安全なアクセスを実現するSkill。

📜 元の英語説明(参考)

Configure authentication and authorization for Google Cloud — Application Default Credentials (ADC), service account impersonation, Workload Identity Federation for GKE and external clouds, and OIDC ID tokens for service-to- service auth. Use when setting up local dev access, deploying to Cloud Run / Cloud Functions / GKE, or calling private Google Cloud APIs.

🇯🇵 日本人クリエイター向け解説

一言でいうと

Google Cloudの認証認可設定を、ローカル開発からCloud Run/Functions/GKEへのデプロイ、API呼び出しまで、様々な環境に合わせて簡素化し、安全なアクセスを実現するSkill。

※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。

⚡ おすすめ: コマンド1行でインストール(60秒)

下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。

🍎 Mac / 🐧 Linux
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o gcp-auth.zip https://jpskill.com/download/14925.zip && unzip -o gcp-auth.zip && rm gcp-auth.zip
🪟 Windows (PowerShell)
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/14925.zip -OutFile "$d\gcp-auth.zip"; Expand-Archive "$d\gcp-auth.zip" -DestinationPath $d -Force; ri "$d\gcp-auth.zip"

完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。

💾 手動でダウンロードしたい(コマンドが難しい人向け)
  1. 1. 下の青いボタンを押して gcp-auth.zip をダウンロード
  2. 2. ZIPファイルをダブルクリックで解凍 → gcp-auth フォルダができる
  3. 3. そのフォルダを C:\Users\あなたの名前\.claude\skills\(Win)または ~/.claude/skills/(Mac)へ移動
  4. 4. Claude Code を再起動

⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。

🎯 このSkillでできること

下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。

📦 インストール方法 (3ステップ)

  1. 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
  2. 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
  3. 3. 展開してできたフォルダを、ホームフォルダの .claude/skills/ に置く
    • · macOS / Linux: ~/.claude/skills/
    • · Windows: %USERPROFILE%\.claude\skills\

Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。

詳しい使い方ガイドを見る →
最終更新
2026-05-18
取得日時
2026-05-18
同梱ファイル
1

📖 Skill本文(日本語訳)

※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。

GCP 認証と認可

概要

認証は「あなたは誰ですか?」に答えます。認可 (IAM) は「あなたは何ができますか?」に答えます。Google Cloud は、プリンシパル(ユーザーアカウント、サービスアカウント、フェデレーション ID)、メタデータサーバーまたは Service Account Credentials API によって発行される有効期間の短いトークン、およびプリンシパルをリソース上の事前定義またはカスタムロールにバインドする IAM 許可ポリシーという 3 つの概念を組み合わせて使用します。

適切なパターンは、コードがどこで実行されるか、誰が呼び出しているかによって異なります。

手順

意思決定マトリックス

シナリオ 使用方法
ローカル開発、Google API の呼び出し gcloud auth application-default login (ADC)
ローカル開発、ただしサービスアカウントとして動作する必要がある サービスアカウントの偽装 (キーではない)
Compute Engine / Cloud Run / Cloud Functions 上のコード アタッチされたサービスアカウント + メタデータサーバー
GKE 上のコード GKE 向け Workload Identity Federation
AWS / Azure / オンプレミス上のコード Workload Identity Federation (外部 IdP を使用)
プライベートな Cloud Run / Cloud Functions サービスの呼び出し Authorization: Bearer の OIDC ID トークン
パブリック / クォータ制限付き API (Maps, Vertex AI Express) 制限付きの API キー

ダウンロード可能なサービスアカウントキー (JSON) がこれらのいずれかの回答に表示される場合、その回答は誤りです。

Application Default Credentials を使用したローカル開発

# 1 回限りの設定: あなたの人間アカウントとしてログイン
gcloud auth login

# ローカル ADC の作成 (これはクライアントライブラリが使用するもの)
gcloud auth application-default login

# ダウンストリームコマンドのプロジェクトを設定
gcloud config set project my-project
gcloud auth application-default set-quota-project my-project
# コードにクレデンシャルは不要 — ADC が自動的に取得される
from google.cloud import storage

client = storage.Client()  # uses ~/.config/gcloud/application_default_credentials.json
for blob in client.list_blobs("my-bucket"):
    print(blob.name)

ADC の検索順序:

  1. GOOGLE_APPLICATION_CREDENTIALS 環境変数 (JSON へのパス)
  2. gcloud auth application-default login ファイル
  3. ランタイムにアタッチされたサービスアカウント (GCE, Cloud Run, GKE など)

サービスアカウントの偽装 (ローカル開発、本番環境のような環境)

ローカルテスト用にサービスアカウントキーをダウンロードしないでください。代わりに偽装してください。

# ターゲットサービスアカウントに対する Token Creator ロールをユーザーに付与
gcloud iam service-accounts add-iam-policy-binding \
  prod-deploy@my-project.iam.gserviceaccount.com \
  --member="user:alice@example.com" \
  --role="roles/iam.serviceAccountTokenCreator"

# 1 つのコマンドで偽装を使用
gcloud storage buckets list \
  --impersonate-service-account=prod-deploy@my-project.iam.gserviceaccount.com

# または、ADC を設定して永続的に偽装を使用
gcloud auth application-default login \
  --impersonate-service-account=prod-deploy@my-project.iam.gserviceaccount.com

本番環境: アタッチされたサービスアカウント

# Cloud Run
gcloud run deploy api \
  --image=us-central1-docker.pkg.dev/my-project/repo/api:v1 \
  --region=us-central1 \
  --service-account=api-runtime@my-project.iam.gserviceaccount.com

# Compute Engine
gcloud compute instances create web-1 \
  --zone=us-central1-a \
  --service-account=web-runtime@my-project.iam.gserviceaccount.com \
  --scopes=cloud-platform

# Cloud Functions (第 2 世代)
gcloud functions deploy process-events \
  --gen2 --region=us-central1 \
  --runtime=python311 \
  --trigger-topic=events \
  --service-account=events-runtime@my-project.iam.gserviceaccount.com

ランタイムはローカルメタデータサーバーから有効期間の短い OAuth トークンを取得します。アプリケーションコードは google.auth.default() を呼び出すだけで動作します。

GKE 向け Workload Identity Federation

# クラスターで有効にする
gcloud container clusters update prod-cluster \
  --region=us-central1 \
  --workload-pool=my-project.svc.id.goog

# Kubernetes ServiceAccount を Google Service Account にバインド
gcloud iam service-accounts add-iam-policy-binding \
  orders-api@my-project.iam.gserviceaccount.com \
  --role="roles/iam.workloadIdentityUser" \
  --member="serviceAccount:my-project.svc.id.goog[default/orders-api]"
apiVersion: v1
kind: ServiceAccount
metadata:
  name: orders-api
  namespace: default
  annotations:
    iam.gke.io/gcp-service-account: orders-api@my-project.iam.gserviceaccount.com

AWS / Azure / オンプレミス向け Workload Identity Federation

# Workload Identity Pool を作成
gcloud iam workload-identity-pools create aws-pool \
  --location=global --display-name="AWS Pool"

# AWS プロバイダーを追加
gcloud iam workload-identity-pools providers create-aws aws \
  --location=global \
  --workload-identity-pool=aws-pool \
  --account-id=123456789012

# AWS IAM ロールを GSA にバインド
gcloud iam service-accounts add-iam-policy-binding \
  cross-cloud-reader@my-project.iam.gserviceaccount.com \
  --role="roles/iam.workloadIdentityUser" \
  --member="principalSet://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/aws-pool/attribute.aws_role/arn:aws:sts::123456789012:assumed-role/AppRole"

サービス間呼び出し用の OIDC ID トークン

# サービス A (Cloud Run) がサービス B (プライベート Cloud Run) を呼び出す
import google.auth.transport.requests
import google.oauth2.id_token
import requests

audience = "https://orders-api-xyz-uc.a.run.app"
auth_req = google.auth.transport.requests.Request()
id_token = google.oauth2.id_token.fetch_id_token(auth_req, audience)

response = requests.get(
    f"{audience}/orders/123",
    headers={"Authorization": f"Bearer {id_token}"},
)
print(response.json())
# サービス B の Cloud Run 呼び出し元をサービス A のサービスアカウントに許可
gcloud run services add-iam-policy-binding orders-api \
  --region=us-central1 \
  --member="serviceAccount:caller-runtime@my-project.iam.gserviceaccount.com" \
  --role="roles/run.invoker"

API キー (限定的なユースケース)

# 特定の API と HTTP リファラーに制限された API キーを作成
gcloud alpha services api-keys c
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開

GCP Authentication & Authorization

Overview

Authentication answers "who are you?" — authorization (IAM) answers "what can you do?" Google Cloud uses three concepts together: Principals (user accounts, service accounts, federated identities), short-lived tokens issued by metadata servers or the Service Account Credentials API, and IAM Allow Policies that bind principals to predefined or custom roles on resources.

The right pattern depends on where the code runs and who is calling.

Instructions

Decision Matrix

Scenario Use
Local development, calling Google APIs gcloud auth application-default login (ADC)
Local dev, but you need to act as a service account Service account impersonation (NOT keys)
Code on Compute Engine / Cloud Run / Cloud Functions Attached service account + metadata server
Code on GKE Workload Identity Federation for GKE
Code on AWS / Azure / on-prem Workload Identity Federation (with external IdP)
Calling a private Cloud Run / Cloud Functions service OIDC ID token in Authorization: Bearer
Public / quota-limited APIs (Maps, Vertex AI Express) API key with restrictions

If a downloadable service account key (JSON) appears in the answer for any of these, the answer is wrong.

Local Development with Application Default Credentials

# One-time setup: log in as your human account
gcloud auth login

# Create local ADC (this is what client libraries use)
gcloud auth application-default login

# Set the project for downstream commands
gcloud config set project my-project
gcloud auth application-default set-quota-project my-project
# No credentials in code — ADC is picked up automatically
from google.cloud import storage

client = storage.Client()  # uses ~/.config/gcloud/application_default_credentials.json
for blob in client.list_blobs("my-bucket"):
    print(blob.name)

ADC search order:

  1. GOOGLE_APPLICATION_CREDENTIALS env var (path to JSON)
  2. gcloud auth application-default login file
  3. Attached service account on the runtime (GCE, Cloud Run, GKE, etc.)

Service Account Impersonation (Local Dev, Production-Like)

Never download service account keys for local testing. Impersonate instead.

# Grant your user the Token Creator role on the target service account
gcloud iam service-accounts add-iam-policy-binding \
  prod-deploy@my-project.iam.gserviceaccount.com \
  --member="user:alice@example.com" \
  --role="roles/iam.serviceAccountTokenCreator"

# Use impersonation for one command
gcloud storage buckets list \
  --impersonate-service-account=prod-deploy@my-project.iam.gserviceaccount.com

# Or set ADC to use impersonation persistently
gcloud auth application-default login \
  --impersonate-service-account=prod-deploy@my-project.iam.gserviceaccount.com

Production: Attached Service Accounts

# Cloud Run
gcloud run deploy api \
  --image=us-central1-docker.pkg.dev/my-project/repo/api:v1 \
  --region=us-central1 \
  --service-account=api-runtime@my-project.iam.gserviceaccount.com

# Compute Engine
gcloud compute instances create web-1 \
  --zone=us-central1-a \
  --service-account=web-runtime@my-project.iam.gserviceaccount.com \
  --scopes=cloud-platform

# Cloud Functions (2nd gen)
gcloud functions deploy process-events \
  --gen2 --region=us-central1 \
  --runtime=python311 \
  --trigger-topic=events \
  --service-account=events-runtime@my-project.iam.gserviceaccount.com

The runtime fetches a short-lived OAuth token from the local metadata server — application code calls google.auth.default() and just works.

Workload Identity Federation for GKE

# Enable on the cluster
gcloud container clusters update prod-cluster \
  --region=us-central1 \
  --workload-pool=my-project.svc.id.goog

# Bind a Kubernetes ServiceAccount to a Google Service Account
gcloud iam service-accounts add-iam-policy-binding \
  orders-api@my-project.iam.gserviceaccount.com \
  --role="roles/iam.workloadIdentityUser" \
  --member="serviceAccount:my-project.svc.id.goog[default/orders-api]"
apiVersion: v1
kind: ServiceAccount
metadata:
  name: orders-api
  namespace: default
  annotations:
    iam.gke.io/gcp-service-account: orders-api@my-project.iam.gserviceaccount.com

Workload Identity Federation for AWS / Azure / On-Prem

# Create a workload identity pool
gcloud iam workload-identity-pools create aws-pool \
  --location=global --display-name="AWS Pool"

# Add an AWS provider
gcloud iam workload-identity-pools providers create-aws aws \
  --location=global \
  --workload-identity-pool=aws-pool \
  --account-id=123456789012

# Bind an AWS IAM role to a GSA
gcloud iam service-accounts add-iam-policy-binding \
  cross-cloud-reader@my-project.iam.gserviceaccount.com \
  --role="roles/iam.workloadIdentityUser" \
  --member="principalSet://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/aws-pool/attribute.aws_role/arn:aws:sts::123456789012:assumed-role/AppRole"

OIDC ID Tokens for Service-to-Service Calls

# Service A (Cloud Run) calling Service B (private Cloud Run)
import google.auth.transport.requests
import google.oauth2.id_token
import requests

audience = "https://orders-api-xyz-uc.a.run.app"
auth_req = google.auth.transport.requests.Request()
id_token = google.oauth2.id_token.fetch_id_token(auth_req, audience)

response = requests.get(
    f"{audience}/orders/123",
    headers={"Authorization": f"Bearer {id_token}"},
)
print(response.json())
# Grant Cloud Run invoker on Service B to Service A's service account
gcloud run services add-iam-policy-binding orders-api \
  --region=us-central1 \
  --member="serviceAccount:caller-runtime@my-project.iam.gserviceaccount.com" \
  --role="roles/run.invoker"

API Keys (Limited Use Cases)

# Create an API key restricted to a specific API and HTTP referrer
gcloud alpha services api-keys create \
  --display-name="maps-frontend" \
  --api-target=service=maps-backend.googleapis.com \
  --allowed-referrers="https://app.example.com/*"

Store the key in Secret Manager, never commit to git, and always restrict it.

Examples

Example 1 — Get a developer set up to call BigQuery

User clones the repo and needs to run integration tests locally. Walk them through gcloud auth application-default login, gcloud config set project my-project, and gcloud auth application-default set-quota-project my-project. Confirm the Python BigQuery client picks up ADC with no code changes. If they need to act as a service account, set up impersonation rather than handing them a JSON key.

Example 2 — Migrate a GKE workload off a JSON key Secret

User's deployment mounts a service account JSON key as a Kubernetes Secret. Enable Workload Identity on the cluster, create a Kubernetes ServiceAccount with the iam.gke.io/gcp-service-account annotation, bind it to the existing GSA via roles/iam.workloadIdentityUser, redeploy without the Secret mount, then delete both the Secret and the underlying GSA key.

Guidelines

  • Never download service account keys — use impersonation, attached SAs, or Workload Identity Federation
  • Never use the Compute Engine default service account in production — create a dedicated, least-privilege SA per workload
  • For local dev, ADC + impersonation is the only acceptable pattern
  • Use Workload Identity for GKE — JSON keys mounted as Secrets are a security anti-pattern
  • For service-to-service calls, use OIDC ID tokens, not API keys or shared secrets
  • Restrict API keys by API, IP, and HTTP referrer; store them in Secret Manager
  • Compute Engine VMs and GKE node pools have OAuth access scopes that override IAM — check these first if attached SA calls fail unexpectedly
  • Prefer predefined roles over custom roles; use custom only when predefined are too broad
  • Audit service account usage via Cloud Audit Logs and the Recommender's "unused service accounts" insight