The MeshOPA policy authorizes requests with Open Policy Agent. The proxy asks an OPA agent running beside it whether to allow each request, and the agent evaluates Rego policies against the request’s attributes.

MeshTrafficPermission decides whether one workload can talk to another, based on the identity in the TLS handshake. MeshOPA decides per request, using the method, path, headers, and optionally the body, so it can enforce rules based on what’s being asked, not only on who’s asking.

The check runs on the destination’s inbound HTTP traffic, including HTTP/2 and gRPC. It doesn’t inspect plain TCP traffic or traffic the proxy can’t decrypt. An OPA allow doesn’t override a MeshTrafficPermission deny: traffic must pass every authorization check.

Proxies that no MeshOPA selects get no OPA check. Before relying on a policy, confirm the following:

  • The data plane has its embedded OPA agent enabled. The --opa-enabled option defaults to true, but can be turned off.
  • The inbound listener contains envoy.filters.http.ext_authz.

If the agent is disabled, no check is installed, and onAgentFailure: Deny doesn’t change that. It only applies when an installed check’s agent fails.

Allow requests carrying a bearer token

The following example makes proxies labeled app: backend deny requests unless their authorization header starts with Bearer . Test it on a non-production workload whose caller already passes the required traffic permissions:

targetRef selects the backend proxies that enforce the check: Mesh, or Dataplane with labels. sectionName isn’t supported, so the check applies to every inbound of the selected proxies. Everything else goes under default. There’s no to or rules, because the proxy handling the request makes the decision.

The example only checks the header shape. It doesn’t validate the token’s signature, expiry, audience, or other claims, so a production policy must implement the checks your application needs.

Provide Rego and agent configuration

appendPolicies[].rego and agentConfig both take a secure data source: a type that says where the data comes from, and a matching field that holds it (secretRef, file, envVar, or insecureInline). type accepts the following values:

type

Where the data comes from

Secret A secret in the policy’s mesh, resolved by the control plane. Set it with secretRef: {kind: Secret, name: SECRET_NAME}.
File A file the control plane can read, set with file.path. It isn’t a path in the application or sidecar container. The path must be absolute and can’t contain ..; a violation fails when the control plane generates the configuration, not when the policy is applied.
EnvVar An environment variable of the control plane process, set with envVar.name.
InsecureInline Plain text in the policy, set with insecureInline.value.

Inline content is readable by anyone who can read the policy. Inline Rego is usually fine, but put an agentConfig that carries a management server token in a Secret.

Inline Rego is compiled when the resource is applied, so a syntax error is rejected with the OPA parse error. Modules use Rego v0 syntax, as in the examples on this page. To write OPA 1.x syntax, such as allow if { ... }, add import rego.v1 to the module. Rego from Secret, File, or EnvVar is only compiled later, when the control plane generates the agent configuration, so missing sources or incompatible modules fail then. A successfully created resource doesn’t prove that the agent has a working policy.

appendPolicies[].ignoreDecision keeps a policy loaded on the agent, but leaves its result out of the decision. ignoreDecision is useful for shared helper modules. It doesn’t evaluate an ignored rule on its own or record what it would have decided, so test new rules explicitly instead of treating them as shadow checks.

Configure the request check

default.authConfig controls the exchange between the proxy and the agent, and accepts the following fields:

Field

Effect

onAgentFailure What to do when the proxy can’t reach the agent or the policy fails to run: Allow or Deny. Defaults to Deny, which rejects requests instead of passing them through unauthorized.
statusOnError The HTTP status returned when the proxy can’t reach the agent, from 100 to 599. 0 leaves it to Envoy.
timeout How long the proxy waits for one decision. Defaults to 500ms, and 0 also uses the default.
requestBody.maxSize How much of the request body to send to the agent. Larger bodies are truncated and marked with x-envoy-auth-partial-body: true. If unset or 0, no body is sent.
requestBody.sendRawBody Sends the body as raw bytes instead of UTF-8. Defaults to false. Setting it to true without a maxSize above 0 is rejected.

Allow only applies to agent communication or execution failures, not to a deny decision. Likewise, statusOnError only sets the error response, not the deny response from a working agent. Keep the timeout within the caller’s overall request budget, because authorization adds latency before the application sees the request.

The following example sends up to 8192 bytes of the request body to the agent, and reads its Rego from a secret. Before applying it, create backend-authz-policy in the policy’s mesh, with a Rego module that defines allow:

Confirm that the control plane can resolve the secret and that the agent loads the generated configuration.

A request larger than maxSize isn’t denied automatically: OPA gets partial input. If a decision needs the full body, make the policy reject partial input. Also test empty and malformed bodies. Sending raw bytes changes the input format, but doesn’t guarantee a parsed JSON body. For more information, see the OPA request input reference.

Configure the OPA agent

default.agentConfig is the OPA agent’s own bootstrap configuration, in OPA’s format. Use it to point the agent at a bundle server or a decision log sink. Overrides passed to kuma-dp with --opa-set (KMESH_OPA_CONFIG_OVERRIDES) are applied on top of the configuration generated from agentConfig.

The following example enables console decision logs. Its Rego allows every request that reaches the OPA check, so only use it on test workloads, or keep your real authorization modules when adding decision logging:

Decision logs can contain request input, including headers and body data. Restrict access to them, and set up masking before sending them to shared log storage. Don’t put bundle server credentials in an inline agentConfig.

Overlapping policies

When several MeshOPA policies apply to the same proxy, their appendPolicies lists are combined. A workload-specific policy adds its entries to the mesh-wide ones instead of replacing them.

A request is allowed only if every combined entry allows it: each entry’s Rego package must set allow to true and must not set deny to true. If a package doesn’t define allow, or sets it to false, the request is denied. Entries with ignoreDecision: true are left out of this check.

For example, a mesh-wide token check and a backend-specific method check both apply to backend requests, and adding an allow-all policy for backend doesn’t override the token check. Use separate Rego packages for independent checks, because modules in the same package share rules and data.

Conflicting authConfig fields and the selected agentConfig follow policy precedence, so review the combined configuration before changing failure behavior for some workloads. To remove an inherited check, change the broader policy’s scope or content. An empty appendPolicies list in a narrower policy doesn’t remove it.

Validating the authorization decision

  1. For the first example, send one request without an Authorization header and one with Authorization: Bearer test. Confirm that the first is rejected and the second passes the OPA check. Whether it then succeeds depends on the destination and other authorization policies.
  2. On an isolated workload with the authorization filter already installed, stop or disconnect the OPA agent and repeat the request. With the default onAgentFailure: Deny, confirm that the request is rejected. If you set onAgentFailure: Allow, document that fail-open choice and test it deliberately. Disabling OPA before startup isn’t an equivalent test.
  3. For body-based decisions, send requests smaller and larger than maxSize.
  4. For overlapping policies, send a request that passes each check alone but fails one of the combined requirements.
  5. When the test ends, remove diagnostic allow-all policies and restore your logging and failure settings.

If the result is unexpected, check the following:

Observation

Check

Requests bypass the expected check Destination selection, HTTP handling, whether the agent is enabled, and whether envoy.filters.http.ext_authz is active.
Every request is denied Inspect the combined modules and the decision input. Every participating package needs a true allow and no true deny. Review traffic permissions separately.
The configured error status appears Agent availability, evaluation errors, and the decision timeout. Rego didn’t necessarily deny the request.
A policy is stored but not active Source loading and compilation errors on the control plane, then the configuration the agent actually loaded.

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!