Api Gateway Backend Security And Rate Limiting

google/skills/skills/cloud/api-gateway-backend-security-and-rate-limiting

作者 googlee96f2070b42c11e184e662cfe1fda8b20c9e2140無授權條款24K 個星標收錄於 2026年11月19日更新於 2026年11月19日儲存庫今天更新

Secures and rate limits backend services behind Google Cloud API Gateway across OpenAPI 2.0 and OpenAPI 3.0.x API configurations, including MCP-enabled gateways. Use when configuring API key or JWT authentication, locking down Cloud Run or Cloud Run functions backends with a gateway service account, reading authenticated claims from X-Apigateway-Api-Userinfo, setting per-project request quotas, or diagnosing HTTP 429 and HTTP 500 Service Control Quota errors. Do not use for Apigee, GKE Gateway API, Cloud Load Balancing custom domain setup, or LLM model routing.

精選包含腳本安全DevOps 與雲端
AI 產生的概覽

為 Google Cloud API Gateway 設定用戶端驗證、後端鎖定與依專案配額,並附配額指標檢查腳本。

功能
引導代理強化 Google Cloud API Gateway 後端:在 OpenAPI 2.0 與 3.0.x 規格中設定 API 金鑰與 JWT 用戶端驗證,建立專用閘道服務帳戶並授予呼叫者角色以阻擋直接存取後端,並透過 x-google-quota 擴充設定依專案的請求配額。也涵蓋保護 MCP 的 tools-list 與 tools-call 流量,以及從 X-Apigateway-Api-Userinfo 讀取已驗證的使用者宣告。隨附的 Python 腳本會在建立 API 設定前,將候選規格與現有閘道設定比對,檢查配額指標是否退化。
適用情境
適用於強化 Google Cloud API Gateway 部署、加入 API 金鑰或 JWT 驗證、鎖定 Cloud Run 或 Cloud Run functions 後端、設定請求配額,或診斷 HTTP 429 與 Service Control Quota 的 HTTP 500 錯誤。不適用於 Apigee、GKE Gateway API、負載平衡器自訂網域或 LLM 模型路由。
執行需求
需要已驗證的 Google Cloud CLI(gcloud)或應用程式預設憑證、Python 3.10+ 與 PyYAML 6.x(供隨附檢查腳本使用)、已啟用帳單的 Google Cloud 專案、已啟用的 API Gateway、Service Management、Service Control、IAM 與 API Keys API,以及 apigateway.admin、serviceAccountAdmin、run.invoker 等 IAM 角色。除使用基準規格離線執行腳本外,需要連線至 Google Cloud 的網路。

API Gateway Backend Security and Rate Limiting

This skill configures client authentication, gateway-to-backend authentication, and per-project rate limits (quotas) for Google Cloud API Gateway across OpenAPI 2.0 (swagger: "2.0") and OpenAPI 3.0.x (openapi: 3.0.4), including gateways with Model Context Protocol (MCP) enabled (Public Preview).

Attribution

MUST READ: Before executing any gcloud, REST API, or MCP command, you MUST read Usage Attribution [blocked]. For commands executed directly in the terminal (never in generated scripts, Terraform, or user code), prefix gcloud with CLOUDSDK_METRICS_REQUEST_ATTRIBUTION="agent-skills/1.0 (skill:api-gateway-backend-security-and-rate-limiting)" and append agent-skills/1.0 (skill:api-gateway-backend-security-and-rate-limiting) to User-Agent headers.

Prerequisites

  1. Install the Google Cloud CLI and Python 3.10+ YAML parser:
    • Install or update the Google Cloud SDK.
    • Install PyYAML for the quota metric validation script:
      bash
      pip install "PyYAML>=6.0,<7.0"
  2. Authenticate your environment:
    • Local CLI session: gcloud auth login
    • Application Default Credentials (SDKs): gcloud auth application-default login
    • Service account key file (if required by CI): set GOOGLE_APPLICATION_CREDENTIALS={key_file_path}
  3. Select or create a project, verify billing, and pick a supported gateway location:
    • Select an existing project (gcloud config set project {project_id}) or create one (gcloud projects create {project_id} as an escape hatch).
    • Verify that an active billing account is linked to {project_id} (required even when staying within the API Gateway pricing free tier).
    • Use us-central1 as the default --location for API Gateway commands. For the other supported regions, see API Gateway deployment model.
  4. Enable required Google Cloud APIs:
    bash
    gcloud services enable \  apigateway.googleapis.com \  servicemanagement.googleapis.com \  servicecontrol.googleapis.com \  iam.googleapis.com \  apikeys.googleapis.com \  --project={project_id}
    API Gateway does not need the Cloud Run API. If the backend runs on Cloud Run, that API is already enabled in the backend's project.
  5. Verify required IAM roles:
    • To manage API Gateway resources and enable services: roles/apigateway.admin and roles/serviceusage.serviceUsageAdmin
    • To create the gateway service account and attach it to an API config: roles/iam.serviceAccountAdmin and roles/iam.serviceAccountUser
    • To manage API keys in consumer projects: roles/serviceusage.apiKeysAdmin
    • For the gateway service account to invoke the backend: roles/run.invoker (Cloud Run or 2nd gen functions) or roles/cloudfunctions.invoker (1st gen functions)

Decision Matrix: Client Authentication, Backend Security, and Quotas

Select the layer that matches the security requirement:

GoalMechanism to ConfigureKey ConstraintDetailed Reference
Identify the calling consumer project for quotas and billingAPI key (x-api-key header convention; key query parameter escape hatch)NEVER rely on an API key alone to authenticate end-user identity. Rationale: API keys identify a Google Cloud project rather than a user, and clients can extract them.Client Authentication [blocked]
Authenticate end users or calling servicesJWT (oauth2 scheme with x-google-issuer / x-google-jwks_uri in OAS 2.0, or x-google-auth in OAS 3.x)Operation-level security overrides root security. Include api_key: [] and the JWT scheme in the same security requirement object when an endpoint needs both user identity and quotas.Client Authentication [blocked]
Block direct calls to the backend serviceDedicated gateway service account (--backend-auth-service-account) + roles/run.invoker + backend --no-allow-unauthenticatedRoll out the gateway config with --backend-auth-service-account BEFORE setting --no-allow-unauthenticated on a live backend to avoid 403 downtime.Backend Security [blocked]
Rate limit requests per consumer project (1/min/{project})x-google-management (OAS 2.0) or x-google-api-management (OAS 3.x) + x-google-quotaNEVER rename or remove a quota metric when creating a new API config while any gateway of the API runs an older config referencing that metric. Rationale: Creating the config immediately overwrites API-wide quotas and causes HTTP 500 Failed to call Service Control Quota on older gateways.Quotas and Rate Limiting [blocked]
Secure MCP tools/list and tools/call on /mcp (Public Preview)x-google-api-management.mcp.tools-list.security (exactly one JWT or API key scheme) + operation security and x-google-mcp-tool: falsex-google-mcp-tool: false hides an operation from MCP only; the underlying REST route remains live unless secured or removed.MCP Traffic Security [blocked]

Execution Workflow and Validation Loop

Follow this zero-downtime sequence when authoring or updating an API Gateway security or quota configuration:

  1. Create the gateway service account and grant backend invoker permissions:
    • Follow Steps 1–3 of Backend Security [blocked] to create {gateway_sa_name} and request human approval before granting roles/run.invoker on {backend_service}.
    • Update backend handler code to decode verified user claims from X-Apigateway-Api-Userinfo (base64url-encoded JSON) instead of Authorization.
  2. Author the OpenAPI authentication and quota definitions:
    • Use the OpenAPI 2.0 or OpenAPI 3.0.x templates and provider tables in Client Authentication [blocked] and Quotas and Rate Limiting [blocked].
    • If the specification enables MCP (x-google-api-management.mcp), configure exactly one JWT or API key scheme on tools-list (also list a JWT scheme in the root security list), and review operation opt-outs using MCP Traffic Security [blocked].
  3. Run the quota metric validation loop BEFORE running api-configs create:
    • If neither the active specs nor {candidate_openapi_spec} define quota metrics (x-google-management.metrics or x-google-api-management.metrics), skip this step.
    • Request human approval before creating an API config that defines or modifies quotas, because api-configs create immediately replaces quota definitions across all gateways of the API.
    • By default, run scripts/check_quota_metrics.py against all active gateways deployed for {api_id} in {project_id}:
      bash
      python3 scripts/check_quota_metrics.py \  --spec={candidate_openapi_spec} \  --project={project_id} \  --api={api_id}
    • Escape hatch for offline environments without gcloud access (pass --baseline-spec for every config currently served by a gateway of the API):
      bash
      python3 scripts/check_quota_metrics.py \  --spec={candidate_openapi_spec} \  --baseline-spec={previous_openapi_spec} \  --dry-run
    • If scripts/check_quota_metrics.py exits with code 1 (status: "FAIL"), add the missing metrics and limits back to {candidate_openapi_spec}, move each x-google-quota listed in ignored_quota_extensions into the operations it applies to, and re-run the script until it exits with code 0 (status: "PASS"). If the user wants to remove a limit on purpose, confirm the removal with the user, because it removes that limit from every gateway of the API. If skipped_configs is not empty, tell the user that the script did not inspect those API configs (for example, gRPC configs), and check their quota metrics and limits manually. If it exits with code 2, fix the problem printed to stderr: the CLI arguments, gcloud authentication, or an API config that the script cannot inspect.
  4. Create the API config, update the gateway, enable the managed service, and lock down the backend:
    • Follow Steps 4–5 of Backend Security [blocked] to run gcloud api-gateway api-configs create and gcloud api-gateway gateways update (request human approval before updating a production gateway), enable the API's managedService in consumer projects (Client Authentication [blocked]), and then restrict {backend_service} with --no-allow-unauthenticated.
    • Verify that direct calls to the backend *.run.app URL return 403, unauthenticated calls to the gateway defaultHostname return 401, and authenticated calls return 200.

Pre-Deployment Checklist

  • Dedicated {gateway_sa_name} is granted roles/run.invoker (or roles/cloudfunctions.invoker), and --no-allow-unauthenticated is applied after the gateway config is active.
  • Backend code decodes X-Apigateway-Api-Userinfo for user claims.
  • Security enforcement does not depend solely on a load balancer or Google Cloud Armor in front of the gateway, because the gateway's defaultHostname (*.gateway.dev) cannot be disabled.
  • Every quota-enforced operation requires an API key (api_key: []), and the API's managedService is enabled and allowed on the API key in each consumer project.
  • python3 scripts/check_quota_metrics.py returns status: "PASS" (exit code 0) against active gateway configs or all baseline specs before api-configs create.
  • If MCP is enabled, x-google-api-management.mcp.tools-list.security names exactly one JWT or API key scheme (a JWT scheme is also in the root security list), and administrative endpoints are secured on REST in addition to x-google-mcp-tool: false.

Resource Cleanup

To avoid ongoing charges when tearing down a test or staging environment, request human approval and delete the gateway, all API configs, the API, and the service account in dependency order:

bash
gcloud api-gateway gateways delete {gateway_id} --location={region} --project={project_id}gcloud api-gateway api-configs delete {config_id} --api={api_id} --project={project_id}gcloud api-gateway apis delete {api_id} --project={project_id}gcloud iam service-accounts delete {gateway_sa_name}@{project_id}.iam.gserviceaccount.com --project={project_id}

來源與署名

來源:google/skills位於skills/cloud/api-gateway-backend-security-and-rate-limiting提交e96f207

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

在 README 中新增徽章

向讀者展示該技能已收錄於 SourceWeft,徽章連結到本頁。

「Listed on SourceWeft」徽章預覽
[![Listed on SourceWeft](https://sourceweft.com/badge/skills/gh-google-skills-api-gateway-backend-security-and-rate-limiting-skills-cloud-api-gateway-backend-security-and-rate-limiting-7034b61b666b9d91.svg)](https://sourceweft.com/skills/gh-google-skills-api-gateway-backend-security-and-rate-limiting-skills-cloud-api-gateway-backend-security-and-rate-limiting-7034b61b666b9d91?utm_source=readme&utm_medium=badge)

檢舉或申請下架