Skip to main content

Google Cloud Authentication

note

Available starting v3.22.0-rc.1.

The Google Cloud Authentication middleware (google-cloud-auth) authenticates the requests that Traefik Hub forwards to a Google Cloud API. It gets a Google access token and sets it as the Authorization: Bearer header of the upstream request. Use it to publish a Google Cloud service, such as an agent deployed on Google Agent Runtime, as an API on Traefik Hub, with no extra component in front of it.

API consumers authenticate to Traefik Hub with the method of your API. Traefik Hub authenticates to Google with the credentials you configure on this middleware. The middleware only sets the Authorization header. It doesn't change the request path, host, or body.

To publish an agent on Google Agent Runtime as an API, follow the Google Cloud Pattern.


Requirements​

  • Enable API Management:

    helm upgrade traefik traefik/traefik -n traefik --wait \
    --reset-then-reuse-values \
    --set hub.apimanagement.enabled=true
  • Define a Kubernetes ExternalName service that points at the Google API host. The Kubernetes CRD provider must allow ExternalName services (providers.kubernetesCRD.allowExternalNameServices=true).

  • Provide Google credentials with permission to call the target API. With the default Google Cloud credential chain, these are the credentials of the workload that runs the gateway.

Configuration example​

Authenticate with a service account key and cache tokens in Redis
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: google-cloud-auth
spec:
plugin:
google-cloud-auth:
credentials: urn:k8s:secret:gcp-creds:service-account.json
store:
keyPrefix: gca-tokens
secret: urn:k8s:secret:gcp-creds:cache-encryption-key
redis:
endpoints:
- redis.traefik-hub.svc.cluster.local:6379

Configuration options​

FieldDescriptionDefaultRequired
allowedCredentialTypesGoogle credential types accepted when credentials holds a key, for example service_account, authorized_user, or external_account. See the Google credential types. This option doesn't apply to the default Google Cloud credential chain.[service_account]No
authTokenStatic access token, or a URN referencing a Kubernetes Secret that holds it. Traefik Hub sets it as the Authorization: Bearer header and doesn't contact Google for a token. Can't be used with credentials.""No
credentialsURN referencing a Kubernetes Secret that holds the Google credentials JSON, or the path of a credentials file mounted in the pod. When empty, Traefik Hub uses the default Google Cloud credential chain. Can't be used with authToken.""No
store.keyPrefixDefines the prefix of the Redis keys that hold the cached access tokens.""No
store.redis.clusterEnables Redis Cluster mode. Set to {} to enable it.No
store.redis.databaseRedis database number.0No
store.redis.endpointsEndpoints of the Redis instances to connect to, for example redis.traefik-hub.svc.cluster.local:6379.Yes, when store is set
store.redis.passwordPassword Traefik Hub uses to connect to Redis. Supports URN references, for example urn:k8s:secret:redis:password.""No
store.redis.sentinelRedis Sentinel configuration (masterSet, username, password).No
store.redis.timeoutConnection timeout, for example 5s.No
store.redis.tlsTLS configuration for secure Redis connections.No
store.redis.usernameUsername Traefik Hub uses to connect to Redis.""No
store.secretURN referencing a Kubernetes Secret that holds the AES key used to encrypt the cached tokens. The key must be 16, 24, or 32 bytes long. Generate a 16-byte key with, for example, openssl rand -hex 8.Yes, when store is set

Authentication methods​

authToken and credentials are mutually exclusive. Traefik Hub requests tokens with the scope https://www.googleapis.com/auth/cloud-platform.

Default Google Cloud credential chain​

Omit authToken and credentials. Traefik Hub resolves credentials from the default Google Cloud credential chain, for example GKE Workload Identity or the metadata server.

spec:
plugin:
google-cloud-auth: {}

Credentials file​

Set credentials to a URN referencing a Kubernetes Secret that holds the credentials JSON, or to the path of a file mounted in the pod. By default, Traefik Hub only accepts service_account keys. To accept another type, list it in allowedCredentialTypes:

spec:
plugin:
google-cloud-auth:
credentials: urn:k8s:secret:gcp-creds:authorized-user.json
allowedCredentialTypes:
- authorized_user

Static access token​

Set authToken to use a token you manage yourself. Traefik Hub sets it on every request and never refreshes it, so you must replace it before it expires.

spec:
plugin:
google-cloud-auth:
authToken: urn:k8s:secret:gcp-creds:access-token

Token caching​

Set store to cache access tokens in Redis. Traefik Hub encrypts each cached token with the key in store.secret, and the cached token expires 30 seconds before the token itself. Replicas of the gateway that use the same Redis and the same store.secret share cached tokens.

Caching applies when credentials is set. It has no effect with authToken, because Traefik Hub doesn't contact Google for a token in that case.

store.secret length

store.secret must be 16, 24, or 32 bytes long, which makes it an AES-128, AES-192, or AES-256 key. The middleware fails to start otherwise.

store.redis​

Connection parameters to your Redis server are attached to your Middleware deployment.

The following Redis modes are supported:

For more information about Redis, we recommend the official Redis documentation.

info

If you use Redis in single instance mode or Redis Sentinel, you can configure the database field. This value won't be taken into account if you use Redis Cluster (only database 0 is available).

In this case, a warning is displayed, and the value is ignored.

Request behavior​

  • The client's Authorization header is replaced. Traefik Hub always sends its own Google token upstream and never forwards the header a client sent.
  • A chain can authenticate to Google Cloud only once. If a request passes through multiple middlewares that authenticate to Google Cloud, the second one returns 500 Internal Server Error. Examples are two google-cloud-auth middlewares, or google-cloud-auth with google-agent-platform.
  • If Traefik Hub can't get a token, the request returns 500 Internal Server Error. The gateway logs the reason. See Troubleshooting.

Troubleshooting​

authorized_user credential type not allowed

Problem: Requests return 500 Internal Server Error, and the gateway logs accessing token: authorized_user credential type not allowed.

Solution: credentials holds a key type that allowedCredentialTypes doesn't list, and only service_account is accepted by default. Add the type named in the log to allowedCredentialTypes, or provide a service_account key.

could not find default credentials

Problem: Requests return 500 Internal Server Error, and the gateway logs finding default credentials: google: could not find default credentials.

Solution: Neither authToken nor credentials is set, and the gateway runs without a Google identity. Set credentials, or run the gateway with an identity that the default Google Cloud credential chain can find, such as GKE Workload Identity.

a middleware chain must authenticate to Google Cloud only once

Problem: Requests return 500 Internal Server Error, and the gateway logs a middleware chain must authenticate to Google Cloud only once.

Solution: Two middlewares that authenticate to Google Cloud run on the same route. Remove one of them, for example the google-cloud-auth middleware from a route that already uses google-agent-platform.

authToken and credentials can't be both defined

Problem: Requests to the route return 404 Not Found, and the gateway logs validating configuration: authToken and credentials can't be both defined.

Solution: Set either authToken or credentials, but not both. Traefik Hub doesn't build a middleware whose configuration is rejected, so the route that uses it isn't served.

store.secret must be 16, 24, or 32 bytes

Problem: Requests to the route return 404 Not Found, and the gateway logs validating configuration: store.secret must be 16, 24, or 32 bytes, got N.

Solution: Set store.secret to a key of exactly 16, 24, or 32 bytes. For example, generate 16 bytes with openssl rand -hex 8.