For the complete documentation index, see llms.txt. Markdown versions of all docs pages are available by appending .md to any docs URL.
Bring your own OPA server
Bring your own OPA server to enforce OPA policies.
By administering your own Open Policy Agent (OPA) server, you can make use of extended OPA use cases. For example, your Rego rules can live as a signed bundle in an external, central location, such as an AWS S3 bucket, to meet your internal security requirements. Bringing your own OPA server increases the administrative complexity, but works better at scale and provides more OPA-native support for teams familiar with administering an OPA server. It also lets you take advantage of your existing OPA server configuration and enterprise OPA license.
Architecture
The following diagram shows how a standalone OPA server works with the Solo Enterprise for agentgateway external auth service.
sequenceDiagram
participant OPA as OPA server (separate pod)
Note over OPA: On startup: load Rego rules<br/>from mounted ConfigMap<br/>(or external bundle server)
participant Client
participant GW as agentgateway proxy
participant EA as ext-auth-service
participant UP as Upstream
Client->>GW: 1. Request matches OPA-protected route
GW->>EA: 2. Authorization request
EA->>OPA: 3. Evaluate policy (opa.agentgateway-system.svc:8181)
OPA->>OPA: 4. Check request against loaded rules
OPA-->>EA: 5. Authorization decision
EA-->>GW: 6. Forward decision
alt Authorized (allow = true)
GW->>UP: 7. Forward request to upstream
UP-->>GW: Response
GW-->>Client: 200 OK
else Not Authorized (allow = false)
GW-->>Client: 403 Forbidden
end
Before you begin
Follow the quick starts or the following guides to:
- Set up an agentgateway proxy.
- Set up an LLM provider, such as OpenAI.
- Set up an MCP backend such as
mcp-website-fetcherin theagentgateway-systemnamespace. The MCP example reuses this backend.
Create the Rego rules
Create a Rego policy and store it in a Kubernetes config map that the OPA server loads at startup.
The following policy denies all requests by default and allows requests that include a specific api-key header. Both branches return a structured result object that controls the HTTP response. The result object name (result) is the rule that the AuthConfig queries in a later step.
kubectl apply -f- <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
name: opa-config-byo
namespace: agentgateway-system
data:
policy.rego: |-
package llm_authz
import future.keywords.if
# Default: deny with 403 + custom body + response header
default result := {
"allow": false,
"response_headers_to_add": {"x-opa-decision": "denied"},
"request_headers_to_remove": ["api-key"],
"body": "Access denied: missing or invalid API key",
"http_status": 403,
}
# Allow when api-key matches; inject upstream header, strip api-key
result := r if {
input.http_request.headers["api-key"] == "authorized-user-key"
r := {
"allow": true,
"http_status": 200,
"headers": {"x-validated-by": "opa-security-checkpoint"},
"response_headers_to_add": {"x-opa-decision": "allowed"},
"request_headers_to_remove": ["api-key"],
}
}
EOFReview the following table to understand the OPA result-object fields used in this policy. The external auth service interprets these field names directly.
Review the following table to understand this configuration.| Field | Description |
|---|---|
allow | Required. true to forward the request; false to reject it. |
http_status | The HTTP status code returned to the client. Set on both allow (200) and deny (403) so the response status is explicit. |
body | The response body returned to the client on deny. |
headers | Headers added to the upstream request on allow. Use this to inject context for the backend, such as a proof-of-authorization header. |
response_headers_to_add | Headers added to the response sent to the client. Useful for surfacing the OPA decision to the caller or to downstream filters. |
request_headers_to_remove | Headers stripped from the request before it is forwarded upstream. Use this to remove credentials such as the api-key so they do not leak to the upstream provider. |
Note
If you modify this policy, keep the package llm_authz declaration. The package field in the AuthConfig that you create later must match the package name in your Rego policy.
Set up the OPA server
Deploy an OPA server as a Kubernetes Deployment and Service that the external auth service can reach.
Deploy OPA with the Rego policy config map mounted as a volume.
kubectl apply -f- <<EOF apiVersion: apps/v1 kind: Deployment metadata: name: opa namespace: agentgateway-system labels: app: opa spec: replicas: 1 selector: matchLabels: app: opa template: metadata: labels: app: opa spec: containers: - name: opa image: openpolicyagent/opa:0.70.0 args: - run - --server - --addr=0.0.0.0:8181 - --log-level=info - --disable-telemetry - /policies/policy.rego ports: - name: http containerPort: 8181 volumeMounts: - name: opa-policy mountPath: /policies readOnly: true volumes: - name: opa-policy configMap: name: opa-config-byo --- apiVersion: v1 kind: Service metadata: name: opa namespace: agentgateway-system labels: app: opa spec: selector: app: opa ports: - name: http port: 8181 targetPort: 8181 EOFVerify that the OPA server pod is running.
kubectl get pods -n agentgateway-system -l app=opaExample output:
NAME READY STATUS RESTARTS AGE opa-84cf58c56f-x8dlk 1/1 Running 0 30s
Create the AuthConfig
Create an AuthConfig resource that points the external auth service at your OPA server.
kubectl apply -f- <<EOF
apiVersion: extauth.solo.io/v1
kind: AuthConfig
metadata:
name: llm-opa-byo
namespace: agentgateway-system
spec:
configs:
- opaServerAuth:
serverAddr: http://opa.agentgateway-system.svc.cluster.local:8181
package: llm_authz
ruleName: result
EOFReview the following table to understand this configuration.
| Setting | Description |
|---|---|
opaServerAuth | Configures the external auth service to call out to a standalone OPA server over HTTP. Use this option when you administer your own OPA server. |
serverAddr | The address of your OPA server. For in-cluster servers, use the full Kubernetes service DNS name so the external auth service can resolve the OPA server across namespaces, in the format http://SERVICE.NAMESPACE.svc.cluster.local:PORT. |
package | The Rego package name that contains the rule you want to enforce. The example queries the llm_authz package from the config map. |
ruleName | The Rego rule within the package that returns the authorization result object. The external auth service queries OPA at /v1/data/<package>/<ruleName>. The example queries the result rule, which returns the structured object with allow, http_status, body, headers, response_headers_to_add, and request_headers_to_remove fields. For more information about rule names, see the OPA Data API docs. |
Caution
Do not set spec.configs[].name on a single-config AuthConfig. The Solo Enterprise for agentgateway external auth service compiles a named config into a boolean chain expression, and a single bare name fails the parser with an invalid binary expression error. Set name only when you chain multiple configs together with a boolean_expr. The example AuthConfig in this guide omits name and works with a single config.
Verify that the AuthConfig is in the Accepted state.
kubectl get authconfig llm-opa-byo -n agentgateway-system -o jsonpath='{.status.state}'Example output:
AcceptedProtect an LLM route
This example attaches the AuthConfig to an HTTPRoute that fronts an OpenAI AgentgatewayBackend. Requests without the expected api-key header are denied with a 403 response. Requests with the correct header are forwarded to OpenAI with an x-validated-by header injected upstream and the api-key header stripped.
Create the OpenAI
AgentgatewayBackendand an HTTPRoute that exposes it on the/openai-byopath. Replace theAuthorizationdata value your LLM provider’s API key.kubectl apply -f- <<EOF apiVersion: v1 kind: Secret metadata: name: openai-secret-byo namespace: agentgateway-system type: Opaque stringData: Authorization: "Bearer $OPENAI_API_KEY" --- apiVersion: agentgateway.dev/v1alpha1 kind: AgentgatewayBackend metadata: name: openai-byo namespace: agentgateway-system spec: ai: provider: openai: model: gpt-4o-mini policies: auth: secretRef: name: openai-secret-byo --- apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: openai-byo namespace: agentgateway-system spec: parentRefs: - name: agentgateway-proxy namespace: agentgateway-system rules: - matches: - path: type: PathPrefix value: /openai-byo backendRefs: - name: openai-byo namespace: agentgateway-system group: agentgateway.dev kind: AgentgatewayBackend EOFCreate an
EnterpriseAgentgatewayPolicythat attaches the AuthConfig to the OpenAI HTTPRoute.kubectl apply -f- <<EOF apiVersion: enterpriseagentgateway.solo.io/v1alpha1 kind: EnterpriseAgentgatewayPolicy metadata: name: llm-opa-byo-policy namespace: agentgateway-system spec: targetRefs: - group: gateway.networking.k8s.io kind: HTTPRoute name: openai-byo traffic: entExtAuth: authConfigRef: name: llm-opa-byo namespace: agentgateway-system EOFConfigure how to reach the
agentgateway-proxyfrom your workstation.export INGRESS_GW_ADDRESS=$(kubectl get gateway agentgateway-proxy \ -n agentgateway-system -o jsonpath='{.status.addresses[0].value}')kubectl port-forward deployment/agentgateway-proxy -n agentgateway-system 8080:80Keep this command running in its own terminal session. Use another terminal for the
curlcommands in the following steps, and choose the Local host tab each time.Send a request to the OpenAI route without the
api-keyheader. Verify that the request is denied with a403response, the custom body, and thex-opa-decision: deniedresponse header.curl -i $INGRESS_GW_ADDRESS/openai-byo \ -H "Content-Type: application/json" \ -d '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"hi"}],"max_tokens":5}'curl -i http://localhost:8080/openai-byo \ -H "Content-Type: application/json" \ -d '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"hi"}],"max_tokens":5}'Example output:
HTTP/1.1 403 Forbidden x-opa-decision: denied Access denied: missing or invalid API keySend the same request with the expected
api-keyheader. Verify that the request is allowed, that thex-opa-decision: allowedresponse header is added, and that OpenAI returns a chat completion. Inside the gateway, theapi-keyheader is stripped and thex-validated-by: opa-security-checkpointheader is injected before the request is forwarded to OpenAI.curl -i $INGRESS_GW_ADDRESS/openai-byo \ -H "Content-Type: application/json" \ -H "api-key: authorized-user-key" \ -d '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"Say hi"}],"max_tokens":5}'curl -i http://localhost:8080/openai-byo \ -H "Content-Type: application/json" \ -H "api-key: authorized-user-key" \ -d '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"Say hi"}],"max_tokens":5}'Example output:
HTTP/1.1 200 OK content-type: application/json x-opa-decision: allowed {"choices":[{"message":{"role":"assistant","content":"Hello!"}, ...}], ...}
Protect an MCP route
The same AuthConfig can protect MCP routes. This example reuses the llm-opa-byo AuthConfig and creates a separate EnterpriseAgentgatewayPolicy that targets an MCP HTTPRoute.
Create an
AgentgatewayBackendfor an MCP server such asmcp-website-fetcher, and an HTTPRoute that exposes it on the/mcp-byopath.kubectl apply -f- <<EOF apiVersion: agentgateway.dev/v1alpha1 kind: AgentgatewayBackend metadata: name: mcp-byo namespace: agentgateway-system spec: mcp: targets: - name: mcp-website-fetcher static: host: mcp-website-fetcher.agentgateway-system.svc.cluster.local port: 80 protocol: SSE --- apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: mcp-byo namespace: agentgateway-system spec: parentRefs: - name: agentgateway-proxy namespace: agentgateway-system rules: - matches: - path: type: PathPrefix value: /mcp-byo backendRefs: - name: mcp-byo namespace: agentgateway-system group: agentgateway.dev kind: AgentgatewayBackend EOFAttach the same AuthConfig to the MCP HTTPRoute.
kubectl apply -f- <<EOF apiVersion: enterpriseagentgateway.solo.io/v1alpha1 kind: EnterpriseAgentgatewayPolicy metadata: name: mcp-opa-byo-policy namespace: agentgateway-system spec: targetRefs: - group: gateway.networking.k8s.io kind: HTTPRoute name: mcp-byo traffic: entExtAuth: authConfigRef: name: llm-opa-byo namespace: agentgateway-system EOFSend an
initializerequest to the MCP route without theapi-keyheader. Verify that the request is denied with a403response.curl -i $INGRESS_GW_ADDRESS/mcp-byo \ -H "Content-Type: application/json" \ -H "Accept: application/json, text/event-stream" \ -d '{"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}},"id":1}'curl -i http://localhost:8080/mcp-byo \ -H "Content-Type: application/json" \ -H "Accept: application/json, text/event-stream" \ -d '{"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}},"id":1}'Example output:
HTTP/1.1 403 Forbidden x-opa-decision: denied Access denied: missing or invalid API keySend the
initializerequest with the expectedapi-keyheader. Verify that the request is allowed, anmcp-session-idresponse header is returned, and the MCP server reports its capabilities. Save themcp-session-idfor subsequent requests.HEADERS=$(mktemp) curl -D "$HEADERS" $INGRESS_GW_ADDRESS/mcp-byo \ -H "Content-Type: application/json" \ -H "Accept: application/json, text/event-stream" \ -H "api-key: authorized-user-key" \ -d '{"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}},"id":1}' SESSION=$(grep -i "mcp-session-id" "$HEADERS" | awk '{print $2}' | tr -d '\r')HEADERS=$(mktemp) curl -D "$HEADERS" http://localhost:8080/mcp-byo \ -H "Content-Type: application/json" \ -H "Accept: application/json, text/event-stream" \ -H "api-key: authorized-user-key" \ -d '{"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}},"id":1}' SESSION=$(grep -i "mcp-session-id" "$HEADERS" | awk '{print $2}' | tr -d '\r')Send the
notifications/initializednotification, then list the tools that the MCP server exposes. Both requests must include theapi-keyandMcp-Session-Idheaders.curl $INGRESS_GW_ADDRESS/mcp-byo \ -H "Content-Type: application/json" \ -H "Accept: application/json, text/event-stream" \ -H "Mcp-Session-Id: $SESSION" \ -H "api-key: authorized-user-key" \ -d '{"jsonrpc":"2.0","method":"notifications/initialized"}' curl $INGRESS_GW_ADDRESS/mcp-byo \ -H "Content-Type: application/json" \ -H "Accept: application/json, text/event-stream" \ -H "Mcp-Session-Id: $SESSION" \ -H "api-key: authorized-user-key" \ -d '{"jsonrpc":"2.0","method":"tools/list","id":2}'curl http://localhost:8080/mcp-byo \ -H "Content-Type: application/json" \ -H "Accept: application/json, text/event-stream" \ -H "Mcp-Session-Id: $SESSION" \ -H "api-key: authorized-user-key" \ -d '{"jsonrpc":"2.0","method":"notifications/initialized"}' curl http://localhost:8080/mcp-byo \ -H "Content-Type: application/json" \ -H "Accept: application/json, text/event-stream" \ -H "Mcp-Session-Id: $SESSION" \ -H "api-key: authorized-user-key" \ -d '{"jsonrpc":"2.0","method":"tools/list","id":2}'Example output:
data: {"jsonrpc":"2.0","id":2,"result":{"tools":[{"name":"fetch","description":"Fetches a website and returns its content", ...}]}}
Cleanup
You can optionally remove the resources that you set up as part of this guide.kubectl delete -n agentgateway-system EnterpriseAgentgatewayPolicy llm-opa-byo-policy mcp-opa-byo-policy
kubectl delete -n agentgateway-system AuthConfig llm-opa-byo
kubectl delete -n agentgateway-system Deployment opa
kubectl delete -n agentgateway-system Service opa
kubectl delete -n agentgateway-system ConfigMap opa-config-byo
kubectl delete -n agentgateway-system HTTPRoute openai-byo mcp-byo
kubectl delete -n agentgateway-system AgentgatewayBackend openai-byo mcp-byo
kubectl delete -n agentgateway-system Secret openai-secret-byo