Skip to content
Latest (currently 2026.10.2) has the newest features, bug fixes, and CVE patches of Solo Enterprise for agentgateway.

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

Page as Markdown

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:

  1. Set up an agentgateway proxy.
  2. Set up an LLM provider, such as OpenAI.
  3. Set up an MCP backend such as mcp-website-fetcher in the agentgateway-system namespace. 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"],
      }
    }
EOF

Review 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.
FieldDescription
allowRequired. true to forward the request; false to reject it.
http_statusThe HTTP status code returned to the client. Set on both allow (200) and deny (403) so the response status is explicit.
bodyThe response body returned to the client on deny.
headersHeaders 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_addHeaders added to the response sent to the client. Useful for surfacing the OPA decision to the caller or to downstream filters.
request_headers_to_removeHeaders 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.

  1. 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
    EOF
  2. Verify that the OPA server pod is running.

    kubectl get pods -n agentgateway-system -l app=opa

    Example 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
EOF

Review the following table to understand this configuration.

SettingDescription
opaServerAuthConfigures the external auth service to call out to a standalone OPA server over HTTP. Use this option when you administer your own OPA server.
serverAddrThe 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.
packageThe Rego package name that contains the rule you want to enforce. The example queries the llm_authz package from the config map.
ruleNameThe 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:

Accepted

Protect 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.

  1. Create the OpenAI AgentgatewayBackend and an HTTPRoute that exposes it on the /openai-byo path. Replace the Authorization data 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
    EOF
  2. Create an EnterpriseAgentgatewayPolicy that 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
    EOF
  3. Configure how to reach the agentgateway-proxy from 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:80

    Keep this command running in its own terminal session. Use another terminal for the curl commands in the following steps, and choose the Local host tab each time.

  4. Send a request to the OpenAI route without the api-key header. Verify that the request is denied with a 403 response, the custom body, and the x-opa-decision: denied response 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 key
  5. Send the same request with the expected api-key header. Verify that the request is allowed, that the x-opa-decision: allowed response header is added, and that OpenAI returns a chat completion. Inside the gateway, the api-key header is stripped and the x-validated-by: opa-security-checkpoint header 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.

  1. Create an AgentgatewayBackend for an MCP server such as mcp-website-fetcher, and an HTTPRoute that exposes it on the /mcp-byo path.

    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
    EOF
  2. Attach 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
    EOF
  3. Send an initialize request to the MCP route without the api-key header. Verify that the request is denied with a 403 response.

    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 key
  4. Send the initialize request with the expected api-key header. Verify that the request is allowed, an mcp-session-id response header is returned, and the MCP server reports its capabilities. Save the mcp-session-id for 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')
  5. Send the notifications/initialized notification, then list the tools that the MCP server exposes. Both requests must include the api-key and Mcp-Session-Id headers.

    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