Google Cloud Authentication
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
ExternalNameservice that points at the Google API host. The Kubernetes CRD provider must allowExternalNameservices (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
- Middleware
- Secret
- Route
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
apiVersion: v1
kind: Secret
type: Opaque
metadata:
name: gcp-creds
stringData:
service-account.json: |
{"type": "service_account", "project_id": "my-project", ...}
cache-encryption-key: "0123456789abcdef" # 16, 24, or 32 bytes
apiVersion: v1
kind: Service
metadata:
name: google-api
spec:
type: ExternalName
externalName: LOCATION-aiplatform.googleapis.com # Replace with the service endpoint that matches your project and location
ports:
- port: 443
---
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: google-api-path
spec:
replacePath:
path: /v1/projects/PROJECT_ID/locations/LOCATION/reasoningEngines # Replace PROJECT_ID and LOCATION with your values
---
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
name: google-api
spec:
entryPoints:
- websecure
routes:
- kind: Rule
match: Host(`api.example.com`) && PathPrefix(`/agents`)
middlewares:
- name: google-cloud-auth
- name: google-api-path
services:
- name: google-api
port: 443
scheme: https
passHostHeader: false
With this route, a GET request to https://api.example.com/agents lists the agents deployed in your project and location.
Replace PROJECT_ID and LOCATION with your own setup,
and use the Google service endpoint that matches LOCATION as the externalName.
Set passHostHeader: false so that Traefik Hub sends the Google host, not the client's host, as the Host header.
Configuration options
| Field | Description | Default | Required |
|---|---|---|---|
allowedCredentialTypes | Google 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 |
authToken | Static 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 |
credentials | URN 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.keyPrefix | Defines the prefix of the Redis keys that hold the cached access tokens. | "" | No |
store.redis.cluster | Enables Redis Cluster mode. Set to {} to enable it. | No | |
store.redis.database | Redis database number. | 0 | No |
store.redis.endpoints | Endpoints of the Redis instances to connect to, for example redis.traefik-hub.svc.cluster.local:6379. | Yes, when store is set | |
store.redis.password | Password Traefik Hub uses to connect to Redis. Supports URN references, for example urn:k8s:secret:redis:password. | "" | No |
store.redis.sentinel | Redis Sentinel configuration (masterSet, username, password). | No | |
store.redis.timeout | Connection timeout, for example 5s. | No | |
store.redis.tls | TLS configuration for secure Redis connections. | No | |
store.redis.username | Username Traefik Hub uses to connect to Redis. | "" | No |
store.secret | URN 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 lengthstore.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:
- Single instance mode
- Redis Cluster
- Redis Sentinel
For more information about Redis, we recommend the official Redis documentation.
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
Authorizationheader 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 twogoogle-cloud-authmiddlewares, orgoogle-cloud-authwithgoogle-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.
Related content
- Follow the Google Cloud Pattern to publish an agent on Google Agent Runtime as an API
- Learn the other upstream authentication patterns for API Management
- Authenticate to Google Cloud for AI traffic with the Google Agent Platform middleware
- Get tokens from an authorization server with the OAuth 2.1 Client Credentials middleware
- Publish a route as an API with API
