Google Cloud Platform ESPv2

repository·master·Indexed 43 days ago

https://github.com/googlecloudplatform/esp-v2

An L7 service proxy for API management of JSON/REST and gRPC services. Built on an Envoy-based data plane, ESPv2 provides authentication (API Keys, JWT), rate limiting, gRPC transcoding, and observability. It supports deployment in sidecar mode for local backends or reverse proxy mode for remote services like Cloud Run.

Tokens
13.3K
Snippets
37
Records
68
Agent score
55%

What's inside ESPv2

  1. Overview of Google Cloud Platform ESPv2

    master

    Google Cloud Platform ESPv2 is a general-purpose L7 service proxy designed for API management of JSON/REST or gRPC services. It integrates with Google Service Infrastructure to provide policy enforcement and telemetry.

    ESPv2 consists of two primary components:

    1. Config Manager (Control Plane): Configures the Envoy proxy dynamically via the Google Service Management API and producer-specified flags.
    2. Envoy (Data Plane): Uses custom filters to process API requests and responses.

    Key capabilities include:

    • Authentication: Supports API Keys and JWT-compliant platforms (Firebase, Auth0, Okta, Google ID tokens, etc.).
    • Traffic Control: Quotas and rate limiting.
    • gRPC Transcoding: Maps HTTP/JSON requests to gRPC methods.
    • Observability: API telemetry (metrics, error rates, latencies) and application observability (traces and logs).
    • Protocols: Supports HTTP/1.1, HTTP/2, web sockets, gRPC, and gRPC-web.
    • Security: Supports TLS and mTLS for both downstream and upstream connections.
    • Dynamic Routing: Acts as a sidecar for local backends or a reverse proxy for remote services (e.g., on Cloud Run).
  2. Overview of the ESPv2 business logic directory

    master

    The src/api_proxy directory contains the core business logic for ESPv2. This logic is designed to be invoked by HTTP Filters.

    Important Integration Rule: When using code from this directory, any logic that is specific to Envoy (the underlying proxy) should be abstracted away before calling the ESPv2 business logic. This ensures that the core business logic remains decoupled from the specific proxy implementation.

  3. Configure ESPv2 using OpenAPI and x-google-backend

    master

    ESPv2 requires an OpenAPI Specification v2.0 document to define the service surface and routing. To link the proxy to your backend, you must modify your OpenAPI document with two specific fields:

    1. host: Set this to the PROXY_SERVICE_URL (the URL of your ESPv2 Cloud Run service) without the protocol (e.g., my-proxy-service.a.run.app).
    2. x-google-backend: Add this object containing the address field, which must be the BACKEND_SERVICE_URL (the URL of your backend Cloud Run service) with the protocol (e.g., https://my-backend-service.a.run.app).
    {
      "host": "PROXY_SERVICE_URL",
      "x-google-backend": {
        "address": "https://BACKEND_SERVICE_URL"
      }
    }
  4. How Config Manager controls ESPv2 configuration

    master

    Config Manager acts as the control plane for ESPv2 by providing dynamic configuration to Envoy using the go-control-plane library. It automates the translation of Google Cloud Platform service configurations into Envoy xDS configurations.

    There are two primary configuration modes:

    1. ESPv2 Startup Configuration: Upon startup, Config Manager fetches the latest googleapis API service config from Google Service Management. It translates this into Envoy xDS configuration and caches it within go-control-plane to feed the Envoy proxy.

    2. Auto Service Configuration Update: When the --rollout_strategy flag is set to managed, Config Manager automatically manages configuration updates. It retrieves the latest rollout from Google Service Management, identifies the version ID with the maximum traffic percentage, and fetches the corresponding service config. It polls Google Service Management every 60 seconds to detect new rollouts and updates Envoy configurations automatically and silently.

  5. Understand the Service Control Filter

    master

    The Service Control Filter integrates with Google Service Infrastructure to manage API requests. It uses the Google Service Control REST API to perform the following functions for incoming API requests:

    • Authentication: Verifying the identity of the API consumer.
    • Rate-limiting: Enforcing quota and rate limits.
    • Metrics Reporting: Sending usage data to Google Service Control.
    • Logging: Creating audit logs for API requests.
  6. Use the Backend Auth Filter for proxy-to-service authorization

    master

    The Backend Auth Filter enables authorization when sending requests to backends via Dynamic Routing. When authentication is configured within a backend rule, this filter automatically overwrites the Authorization header with the appropriate identity token.

    Important Behavior: This is a pass-through filter. If the requested operation is not explicitly configured in the filter configuration, the request will pass through the proxy unmodified.

  7. Migrating from ESP to ESPv2

    master

    ESPv2 is the successor to the original ESP (which used an nginx-based data plane). ESPv2 uses Envoy and is designed to be mostly backwards-compatible.

    • Compatibility: API producers generally do not need to modify the Endpoints Service Configuration.
    • Exceptions: There are a few incompatible startup flags.
    • Recommendation:
      • For serverless reverse proxy deployments, use ESPv2 instead of ESP.
      • For sidecar deployments, ESPv2 is recommended as it receives more features.

    For detailed migration steps, consult the official migration guide.

  8. How the gRPC Metadata Scrubber Filter works

    master

    The gRPC Metadata Scrubber Filter is an Envoy filter designed to prevent the accidental dropping of gRPC trailers in specific HTTP/1.1 deployment scenarios.

    When a response has a content-type of application/grpc, the filter checks for the presence of a content-length header. If content-length is found, the filter removes it.

    This is necessary because Envoy's downstream HTTP/1 codec follows RFC 7230, which dictates that gRPC trailers are dropped if the response headers include a content-length. By scrubbing the content-length header, the filter allows gRPC trailers to be retained when using chunked encoding (often used by downstream clients to transport gRPC over HTTP/1).

  9. Configure Dynamic Routing (API Gateway)

    master

    When using ESPv2 as an API Gateway, the Google OpenAPI x-google-backend extension is automatically converted into the following Envoy components:

    • Route Configuration: Handles host rewrites to the remote backend.
    • Path Rewrite Filter: Handles path rewrites to the remote backend.
    • Backend Auth Filter: Handles authentication with the remote backend.
  10. Understand ESPv2 configuration versioning

    master

    ESPv2 configuration is versioned to ensure compatibility between the Config Generator and the Envoy binary running in the Cloud API Gateway.

    Versioning Structure

    • The current global version is tracked in api/VERSION.
    • Directory paths include the version, e.g., api/envoy/v12/http/backend_auth.
    • Proto package names include the version, e.g., espv2.api.envoy.v12.http.common.Pattern.

    Compatibility Rules

    Because the Cloud API Gateway may run an older Envoy binary with a newer Config Generator (or vice versa), you must follow these rules when modifying proto files:

    1. Avoid Breaking Changes: All changes should be backward compatible.
    2. Breaking Changes Require Version Increments: If a change is not backward compatible, you must increase the configuration version.

    Examples of Breaking Changes

    • Adding a new filter config: Old Envoys will fail to parse the new filter config proto type URL.
    • Removing a field: Older configurations stored in the gateway may cause functionality breaks when new Envoys are rolled out, as they will ignore the removed field.
    • Adding a new CLI flag to GCSRunner: Since this is baked into the data plane, it can change functionality when older configurations are used.

    Examples of Non-Breaking Changes

    • Adding a new field to an existing filter: Old Envoys will simply ignore the new field (feature disabled), and new Envoys using old configs will also have the feature disabled.