BYO ext auth service
Verified : Automated tests passed for: Kubernetes resources: The Kubernetes resources on this page were checked against their API schemas. End-to-end run: Every step ran in a fresh test environment and the results were checked.Integrate your own external authorization service with the gateway.
Bring your own external authorization service to protect requests that go through your Gateway.
About external auth
Agentgateway lets you integrate your own external authorization service to your Gateway. Then, this external authorization service makes authorization decisions for requests that go through the Gateway, as shown in the following diagram.
Review the following diagram to understand the flow of a request:
sequenceDiagram
participant C as Client / Agent
participant AGW as Agentgateway Proxy
participant BYO as Your Ext Auth Service<br/>(gRPC)
participant Backend as Backend<br/>(LLM / MCP / Agent / HTTP)
C->>AGW: Request to protected route
AGW->>BYO: gRPC Authorization Request<br/>(headers, path, method)
BYO->>BYO: Custom authorization logic<br/>(check headers, tokens,<br/>database lookups, etc.)
alt Authorized
BYO-->>AGW: ALLOW<br/>(optional: inject headers)
AGW->>Backend: Forward request
Backend-->>AGW: Response
AGW-->>C: 200 OK + Response
else Not Authorized
BYO-->>AGW: DENY<br/>(status code, message)
AGW-->>C: 403 Forbidden<br/>"denied by ext_authz"
end
Before you begin
Note
Note that this guide assumes that you want to apply the policy to the OpenAI LLM provider. You can use other LLM providers or apply external auth to an MCP server, agents, and Kubernetes services. Make sure to adjust these steps to apply to your AgentgatewayBackend type or Kubernetes service.
Deploy your own external authorization service
Deploy your own external authorization service as a backend service that is accessible to your agentgateway proxy.
Note
Keep in mind that your external authorization service must conform to the Envoy external auth proto. The external authorization service also configures the conditions for authorizing a request, such as the inclusion of a certain header or other credentials.
Deploy your external authorization service. The following example uses the Istio external authorization service for quick testing purposes. This service is configured to allow requests with the
x-ext-authz: allowheader.kubectl apply -f - <<EOF apiVersion: apps/v1 kind: Deployment metadata: namespace: agentgateway-system name: ext-authz labels: app: ext-authz spec: replicas: 1 selector: matchLabels: app: ext-authz template: metadata: labels: app: ext-authz app.kubernetes.io/name: ext-authz spec: containers: - image: gcr.io/istio-testing/ext-authz:1.25-dev name: ext-authz ports: - containerPort: 9000 EOFCreate a Service for the Deployment that your proxy can access.
kubectl apply -f - <<EOF apiVersion: v1 kind: Service metadata: namespace: agentgateway-system name: ext-authz labels: app: ext-authz spec: ports: - port: 4444 targetPort: 9000 protocol: TCP appProtocol: kubernetes.io/h2c selector: app: ext-authz EOF
Create external auth policy
You can attach an external authorization policy to a Gateway, HTTPRoute, or backend (an AgentgatewayBackend or a Kubernetes Service). If you attach policies at multiple levels, the request must pass each one to be authorized.
Gateway and HTTPRoute targets use the traffic.extAuth section so that authorization runs before the proxy selects a backend. Backend targets use the backend.extAuth section so that authorization runs after backend selection, which is useful when the authorization service shapes the outgoing request, for example by inserting a token, or when a route load-balances or fails over across multiple backends.
To check every request on a listener before route selection, you can also use a listener ExternalAuth filter.
For gRPC external authorization, agentgateway waits 2 seconds for the authorization service to respond by default. To use a different timeout, apply a second AgentgatewayPolicy whose targetRefs names the authorization service, either as a Kubernetes Service or as an AgentgatewayBackend, and set backend.http.requestTimeout on it.
Send a test request to the OpenAI backend. Verify that you get back a 200 HTTP response code and that no authorization is required.
curl -v "${INGRESS_GW_ADDRESS}/openai" -H content-type:application/json -d '{ "model": "gpt-5.4-nano", "messages": [ { "role": "system", "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair." }, { "role": "user", "content": "Write 5 sentences." } ] }'Example output:
HTTP/1.1 200 OK ...Create an AgentgatewayPolicy that references the external authorization service. Choose the tab for the target you want to attach the policy to. The Gateway and HTTPRoute tabs apply external authorization before backend selection. The AgentgatewayBackend tab applies it after backend selection.
kubectl apply -f - <<EOF apiVersion: agentgateway.dev/v1alpha1 kind: AgentgatewayPolicy metadata: namespace: agentgateway-system name: gateway-ext-auth-policy labels: app: ext-authz spec: targetRefs: - group: gateway.networking.k8s.io kind: Gateway name: agentgateway-proxy traffic: extAuth: backendRef: name: ext-authz namespace: agentgateway-system port: 4444 cache: key: - request.path - request.headers["authorization"] ttl: 5m maxEntries: 10000 grpc: {} failureMode: FailClosed EOFThe failureMode field sets what the proxy does when it cannot complete the call to the authorization service, such as when the service is unreachable or does not answer in time. The examples set the field to
FailClosed, which is also the behavior if you omit the field. If the authorization service is unavailable, the proxy rejects the request with a 403 response.With
FailOpen, the proxy forwards the request to the backend when the check fails, such as when the authorization service is unavailable. The proxy does not reject the request in such cases. While the authorization service is down,FailOpenlets unauthenticated requests reach your app. If the authorization service answers and denies the request, the proxy still rejects it. What counts as no answer depends on the protocol.- gRPC. The proxy treats a gRPC error from the call as no answer. For example, if the
backendRefpoints to a gRPC service that is not an authorization server, the service returnsUNIMPLEMENTED. WithFailOpen, the proxy then forwards every request, so a wrongbackendRefturns off authorization without an error. - HTTP. The proxy treats any HTTP response from the service as an answer, even an error such as
500. The proxy rejects the request and returns that response to the client, regardless of the failure mode. Only a missing response, such as a connection failure or a timeout, counts as no answer.
- gRPC. The proxy treats a gRPC error from the call as no answer. For example, if the
Repeat your request to the OpenAI backend and verify that the request is denied.
curl -v "${INGRESS_GW_ADDRESS}/openai" -H content-type:application/json -d '{ "model": "gpt-5.4-nano", "messages": [ { "role": "system", "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair." }, { "role": "user", "content": "Write 5 sentences." } ] }'Example output: Note the 403 Forbidden response, along with the special
x-ext-authz*headers that the Istio external authorization service adds to the request to explain the decision.* upload completely sent off: 268 bytes < HTTP/1.1 403 Forbidden < content-type: text/plain < content-length: 29 < * Connection #0 to host localhost left intact denied by ext_authz for not found header `x-ext-authz: allow` in the request%Send another request, this time with the
x-ext-authz: allowheader. The Istio external authorization service is configured to allow requests with this header. Therefore, the request succeeds.curl -v "${INGRESS_GW_ADDRESS}/openai" -H content-type:application/json \ -H "x-ext-authz: allow" \ -H "x-ai-api-key:N2YwMDIxZTEtNGUzNS1jNzgzLTRkYjAtYjE2YzRkZGVmNjcy" \ -d '{ "model": "gpt-5.4-nano", "messages": [ { "role": "system", "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair." }, { "role": "user", "content": "Write 5 sentences." } ] }'
Listener ExternalAuth filter
Instead of a policy, you can add an external authorization filter to a Gateway listener. The Gateway API calls this a pre-routing filter. The proxy sends every request that the listener accepts to your authorization service before it selects a route. The proxy denies an unauthorized request even if no route matches it.
Note
Listener filters require the experimental channel of Gateway API version 1.7 or later. For the install command, see Experimental features in Gateway API.
Add the
ExternalAuthfilter to thefilters.requestsfield of a listener. The following example uses theext-authzservice that you deployed earlier. This command replaces theagentgateway-proxyGateway. If you changed that Gateway, add thefiltersfield to its listener instead of applying this manifest.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: Gateway metadata: name: agentgateway-proxy namespace: agentgateway-system spec: gatewayClassName: agentgateway listeners: - name: http port: 80 protocol: HTTP allowedRoutes: namespaces: from: All filters: requests: - type: ExternalAuth externalAuth: protocol: GRPC grpc: {} backendRef: name: ext-authz port: 4444 EOFReview the following table to understand this configuration.
Field Description filters.requests[].typeThe type of the filter. Agentgateway supports only ExternalAuth.externalAuth.protocolThe protocol that the authorization service speaks, either GRPCorHTTP. ForGRPC, setgrpc. ForHTTP, sethttp.externalAuth.backendRefThe Service of the authorization service. If the Service is in another namespace, create a ReferenceGrant from the Gateway to the Service. Verify that the controller accepted the listener and resolved the filter.
kubectl get gateway agentgateway-proxy -n agentgateway-system \ -o jsonpath='{range .status.listeners[*].conditions[*]}{.type}={.status} {.reason}{"\n"}{end}'Example output:
Accepted=True Accepted Conflicted=False NoConflicts Programmed=True Programmed ResolvedRefs=True ResolvedRefsSend a request to a path that no route matches, without the
x-ext-authzheader. Because the filter runs before route selection, the proxy returns a403response from the authorization service instead of a404response.curl -i "${INGRESS_GW_ADDRESS}/no-such-route"Example output:
HTTP/1.1 403 Forbidden x-ext-authz-check-result: denied ... denied by ext_authz for not found header `x-ext-authz: allow` in the requestRepeat the request with the
x-ext-authz: allowheader. The authorization service allows the request, and the proxy then finds no matching route.curl -i "${INGRESS_GW_ADDRESS}/no-such-route" -H "x-ext-authz: allow"Example output:
HTTP/1.1 404 Not Found content-type: text/plain ... route not found
Review the following limits for listener filters.
- One filter only: A listener can have only one
ExternalAuthfilter. If you add a second one, theAcceptedcondition of the listener isFalsewith theInvalidListenerFilterreason. - Type: Other filter types, such as
ExtensionRef, are not supported. TheAcceptedcondition of the listener isFalsewith theUnsupportedValuereason. - References: If the
backendRefcannot be resolved, theResolvedRefscondition isFalsewith theInvalidListenerFilterRefreason. If the Service is in another namespace and no ReferenceGrant allows the Gateway to reference it, the reason isRefNotPermitted.
Listener filters work the same way on listeners of a ListenerSet.
Cleanup
You can remove the resources that you created in this guide.If you added the listener filter, remove it from the Gateway before you delete the ext-authz Service. Otherwise, the filter’s backendRef no longer resolves, the listener is no longer programmed, and the Gateway stops serving traffic.
kubectl patch gateway agentgateway-proxy -n agentgateway-system --type=json \
-p='[{"op":"remove","path":"/spec/listeners/0/filters"}]'Example output:
gateway.gateway.networking.k8s.io/agentgateway-proxy patchedkubectl delete AgentgatewayPolicy gateway-ext-auth-policy route-ext-auth-policy backend-ext-auth-policy -n agentgateway-system --ignore-not-found
kubectl delete deployment ext-authz -n agentgateway-system
kubectl delete service ext-authz -n agentgateway-system