Skip to content

gcp_token ​

gcp_token gets a Google Cloud access token through Workload Identity Federation with an X.509 client certificate (mTLS). If configured, it then impersonates a service account. The token is cached and refreshed before it expires.

Certificates and credentials are given as inline content, so the config does not need any file paths.

There are two modes:

  • endpoint (default): the middleware responds like an OAuth2 client_credentials token endpoint. Use this for clients such as Redpanda Console or librdkafka (sasl.oauthbearer.token.endpoint.url).
  • inject: the middleware sets the token on the request and calls the next middleware, for example a service proxy.
yaml
server:
  entrypoints:
    token:
      address: 127.0.0.1:14293
  http:
    middlewares:
      gcp:
        gcp_token:
          format: raw            # or managed_kafka
          client_id: console
          client_secret: change-me
          credentials: |
            {
              "type": "external_account",
              "audience": "//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/pool/providers/provider",
              "subject_token_type": "urn:ietf:params:oauth:token-type:mtls",
              "token_url": "https://sts.mtls.googleapis.com/v1/token",
              "credential_source": { "certificate": { "use_default_certificate_config": true } },
              "service_account_impersonation_url": "https://iamcredentials.mtls.googleapis.com/v1/projects/-/serviceAccounts/sa@project.iam.gserviceaccount.com:generateAccessToken"
            }
          cert: |
            -----BEGIN CERTIFICATE-----
            ...
            -----END CERTIFICATE-----
          key: |
            -----BEGIN PRIVATE KEY-----
            ...
            -----END PRIVATE KEY-----
    routers:
      token:
        entrypoints: [token]
        path: /*
        middlewares: [gcp]

Fields ​

Using file paths instead of inline content:

yaml
gcp_token:
  credentials_file: /opt/certs/gcp/wif-credentials.json
  cert_file: /opt/certs/kafka/certs/kafkaextended-cert.pem
  key_file: /opt/certs/kafka/private/kafkaextended-key.pem
FieldDefaultDescription
credentialsRaw external_account JSON. credential_source is ignored; cert/key are used instead.
audiencefrom credentialsWorkload identity pool provider audience.
token_urlhttps://sts.mtls.googleapis.com/v1/tokenSTS token endpoint.
service_account_impersonation_urlfrom credentialsIf empty, the federated STS token is returned directly.
certPEM client certificate. It may contain the chain, with the leaf first.
keyPEM private key.
casystem rootsPEM root CAs for the Google endpoints.
credentials_file / cert_file / key_file / ca_fileFile path alternatives of the inline fields. A field and its _file form cannot both be set. Cert, key and CA files are re-read on each token refresh, so rotated certificates are picked up without a restart.
scopes[https://www.googleapis.com/auth/cloud-platform]Requested scopes.
lifetime1hLifetime of the impersonated token.
refresh_before5mRefresh the cached token this long before it expires.
timeout30sTimeout for upstream calls.
proxyproxy env varshttp/https proxy URL used for the STS and impersonation calls, e.g. http://user:pass@proxy.local:3128. mTLS goes through the proxy via CONNECT. If empty, HTTP_PROXY/HTTPS_PROXY/NO_PROXY are used.
formatrawraw returns the Google access token. managed_kafka wraps it in the JWT-like GOOG_OAUTH2_TOKEN format used by Google Managed Service for Apache Kafka.
modeendpointendpoint or inject.
headerAuthorizationHeader used in inject mode. Authorization gets a Bearer prefix.
client_id / client_secretProtect the endpoint. Accepted as basic auth or as form values.

If a refresh fails, the cached token is still served until it actually expires.