# Test your policy in action

> Check a surface policy's decisions with a dry run and a template's examples before you apply it, then watch its live input and decisions on real requests and confirm them on the Audit page.

This guide tests a surface policy twice: in the policy editor before you apply it, and against real requests once it is on a surface.

Before you apply the policy, you run it against sample requests with the editor’s Test (dry-run) panel and its Template examples card. After you apply it, policy input capture shows the input and the decision of each real request on the surface’s Capture view, and the Audit page keeps each decision with its deny reason. For background on how the gateway evaluates policies, see [OPA policies](/products/affinidi-trust-fabric/agent-gateway/concepts/opa-policies.md).

A policy that compiles can still decide the wrong way. A rule can read a field missing from the request, or another allow rule can let a request through. Without testing, you find out from callers refused with 403, or from requests that reached your managed agent by mistake.

Use this guide when:

- You are writing a surface policy and want to check its decisions before any caller meets it.

- A policy denies requests you expected it to allow, and you need the input it actually received to debug it.

- You are changing a policy that is already on a surface and want to test the change against real requests.

This guide uses one example throughout: a policy that lets only callers with the admin role reach a surface. To create and attach policies in general, see [Apply OPA policies to your gateway and surfaces](/products/affinidi-trust-fabric/agent-gateway/how-to-guides/policies/apply-opa-policies.md).

## Prerequisites

- Agent Gateway 1.0.10 or later. Upgrade the appliance from Affinidi Portal if it runs an earlier version.

- The Administrator role on the gateway. Creating policies, editing surfaces and listening to policy input all need it.

- A saved A2A surface whose Caller Context uses JWT Bearer, with at least one value in Accepted Audiences, and no Identity element that requires the agent identity extension. See [Validate bearer tokens on a surface](/products/affinidi-trust-fabric/agent-gateway/how-to-guides/access/validate-bearer-tokens-on-a-surface.md).

- Two JWTs from your identity provider that the surface’s JWT Bearer method accepts: one whose payload has "role": "admin", and one with another role, such as "role": "developer".

- Enable VP Auditing switched on in Settings › Security, with Policy Decision selected under Events to keep, for the Audit page check in the Confirm section.

## Steps

### Test before you apply

Create a policy that denies by default

- In the sidebar, select Policies, then the Agent Surfaces tab.

- Select Define Agent Surface Policy.

- Under Start from, select Deny by default, then Use this template.

- In Details, enter support-desk-access as the Name.

- Select Create.

The page opens as Edit Policy: support-desk-access. The Test (dry-run) panel appears below the editor once the policy is saved.

Add the rules you want to test

- In the Policy section, select Add rules from a template.

- In the Add rules from a template panel, select Allow by JWT claim or role.

- Leave Claim as role, and enter admin in Allowed values.

- Select Add to policy.

The Rules view lists the new rules under “From template: Allow by JWT claim or role”.

Check the template examples

Find the Template examples card below the editor. It runs each of the template’s example requests through the dry run against your draft and compares the decision with the one the template expects.

Each row should show Matches. A row that shows Differs carries a short explanation, such as another allow rule letting the request through, or the policy allowing every request by default. Select Load in Test on a row to copy its input into Test (dry-run).

Add a deny reason

Select Rego, and add this line at the end of Policy Content (Rego):

```rego
deny_reason := "Only callers with the admin role can use this surface"
```

The gateway reads deny_reason only when the policy denies. The dry run, the Policy Input card and the Audit page then show this reason with each deny. Without it, a live deny carries the reason “Policy denied request”, and the dry run shows none. Wait for the green Valid indicator above the editor.

Save the policy

Select Save. The editor stays open and adds the change to the policy’s version history.

Run a dry run with a request the policy should deny

- On the Role viewer row of Template examples, select Load in Test. Its input replaces the content of Sample input JSON.

- Select Run against the draft above.

The result shows DENY with your deny reason, followed by the query, draft and the content hash it evaluated. Change "viewer" to "admin" in Sample input JSON and run it again to see ALLOW. Check Auto-run to run the dry run again after every edit.

The dry run evaluates the Rego in the editor, saved or not, and changes nothing on live traffic.

### Test after you apply

Attach the policy to the surface

- In the sidebar, select Surfaces, then open your surface.

- Drag Policy, under Security & Policy in the palette, onto the request side of the Access Point → Managed Agent edge.

- Select the Policy element, and choose support-desk-access in Policy Definition.

- Select the save icon in the toolbar (tooltip: Save changes).

For the other positions a Policy element can take, see [Use JWT claims to control access to a surface](/products/affinidi-trust-fabric/agent-gateway/how-to-guides/policies/use-jwt-claims-to-control-access-to-a-surface.md).

Turn on policy input capture

- In the sidebar, go to Settings › Admin › Feature Flags.

- Switch on the Status of policy_input_capture.

The change takes effect immediately. Policy input capture is for developing and testing policies, so you turn it off again at the end of this procedure.

Listen to the surface’s policy input

- Open your surface, and select the Monitoring tab.

- Select Capture.

- On the Policy Input card, select Listen to policy input.

The card shows “Listening”. Keep this view open: leaving it or reloading the page stops listening.

Send a request the policy should deny

From a terminal, send a request with the token whose role is developer. Replace <ENTRY_POINT_URL> with the Entry point URL shown at the top of the surface canvas:

```bash
curl -X POST "" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer " \
  -d '{"jsonrpc":"2.0","method":"message/send","id":"test-1","params":{"message":{"role":"user","messageId":"msg-001","parts":[{"kind":"text","text":"Where is my order?"}]}}}'
```

Send the same request again with the token whose role is admin, so an Allow row appears beside the Deny row.

A row appears on the Policy Input card with the time, the policy slot with support-desk-access and its version, and the decision Deny with your deny reason. Select Show input to read the input the policy received. Credentials in it are masked, and the gateway’s log redaction rules also apply, so values such as email addresses and phone numbers can show as [REDACTED-EMAIL] and [REDACTED-PHONE].

A request the closed Access Point refuses, such as one with a missing or expired token, gets 401 before any policy runs, so it adds no row.

Test the captured input in the editor

- On the row, select the copy button beside Show input.

- Open support-desk-access in Policies, and replace the content of Sample input JSON with the copied input.

- Select Run against the draft above.

The dry run gives the same DENY as the live request. Edit the Rego or the input here to try a change against a real request before you save it. The sample input is saved with the policy, and a captured input holds request data such as messages and claims, so replace it with a sample of your own before you select Save.

Stop listening and turn off policy input capture

- Listening stopped when you left the Capture view. If you went back to it, select Stop listening on the Policy Input card.

- In Settings › Admin › Feature Flags, switch off the Status of policy_input_capture.

While the flag is off, the gateway refuses to listen and stops any listen still running.

## Confirm

Replace <ENTRY_POINT_URL> in the commands below with the Entry point URL shown at the top of the surface canvas.

### Test 1: request with the developer role returns 403

```bash
curl -X POST "" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer " \
  -d '{"jsonrpc":"2.0","method":"message/send","id":"test-2","params":{"message":{"role":"user","messageId":"msg-002","parts":[{"kind":"text","text":"Where is my order?"}]}}}'
```

Expected: 403 Forbidden with this body. The response doesn’t carry the deny reason, which stays on the gateway:

```json
{"error":"Forbidden","message":"Agent trust policy denied the request"}
```

On the surface’s Monitoring tab, Channel Logs shows a warning that includes “Received error response with status 403 Forbidden”, without the deny reason.

### Test 2: request with the admin role returns the managed agent’s response

```bash
curl -X POST "" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer " \
  -d '{"jsonrpc":"2.0","method":"message/send","id":"test-3","params":{"message":{"role":"user","messageId":"msg-003","parts":[{"kind":"text","text":"Where is my order?"}]}}}'
```

Expected: the gateway forwards the request, and the response is your managed agent’s answer, usually 200 OK.

### Test 3: denied request shows on the Audit page with its deny reason

- In the sidebar, select Audit.

- Enter the surface’s name in Filter Audit Log…, and press Enter.

- Select Policy Decisions under Category, and Deny under Decision.

- Select the newest entry, whose summary reads Surface policy “support-desk-access” on the Access Point leg. Its title, such as Denied POST /, gives the request’s method and path.

Expected: the entry’s details show Policy support-desk-access, Scope Surface, Flow Access Point, Decision deny and Deny reason “Only callers with the admin role can use this surface”. Select Allow under Decision to see the allowed request from Test 2, with the decision allow.

## Troubleshooting

| Symptom | Likely cause | Fix |
| No Template examples card after Add to policy. | The rules were added before the policy was first created, or the card was closed. | Save the policy once, then add the template’s rules on the Edit Policy page. |
| Result is empty, with a note that the examples assume the template’s default settings. | A template setting differs from the value the examples use, so a different decision is expected. | Read the Expected and This policy columns yourself, or load a row in Test to check it. |
| Role viewer shows Differs: the policy allows every request by default. | The policy started from an allow-all policy, such as Create custom policy. | Select Deny by default in the warning in the Rules view, and save. |
| Run against the draft above stays disabled. | The policy doesn’t compile, or Sample input JSON isn’t valid JSON. | Fix the error shown above the editor or under the sample input. The button’s tooltip says which one blocks the run. |
| The dry run allows a request that the live policy denies. | The sample input doesn’t match what the gateway sends, for example a claim at a different path. | Copy the captured input from the Policy Input card and run the dry run on it. |
| The Policy Input card says policy input capture is turned off. | The policy_input_capture feature flag is off. | Switch it on in Settings › Admin › Feature Flags. |
| Listen to policy input is disabled, with “Needs permission to edit surfaces and policies”. | Your role can’t edit surfaces and policies. | Ask an Administrator to listen, or to change your role. |
| The card says the surface has no policy yet. | The Policy element isn’t attached, or the surface wasn’t saved after attaching it. | Attach the policy and select Save changes. |
| Listening shows no rows after a request. | The closed Access Point refused the request with 401 before policy ran, or another node of the appliance served it. | Send a valid token. On an appliance with several nodes, only requests served by the node the dashboard is connected to show. |
| The card shows “Waiting for the dashboard connection”. | The dashboard lost its live connection to the gateway. | Keep the view open until the connection returns. Nothing is captured until then. |
| A response policy’s dry run doesn’t decide as expected. | Response policies read input.response, input.caller and input.surface, not input.source_auth. | Write the sample input in that shape. See [Response policy input](/products/affinidi-trust-fabric/agent-gateway/reference/surfaces/opa-policies.md#response-policy-input). |
| MCP tool policy decisions don’t appear on the card. | Policy input capture leaves out MCP tool policies. | Test a tool policy with Test (dry-run) on the policy. |
| No Policy Decisions entry on the Audit page. | VP Auditing is off, or Policy Decision isn’t selected under Events to keep. | Switch on Enable VP Auditing in Settings › Security, select Policy Decision, save, and send the request again. |

## Next steps

- [Use JWT claims to control access to a surface](/products/affinidi-trust-fabric/agent-gateway/how-to-guides/policies/use-jwt-claims-to-control-access-to-a-surface.md): Place Policy elements on each side of a surface’s edges.

- [Control MCP tool access with per-tool policies](/products/affinidi-trust-fabric/agent-gateway/how-to-guides/policies/control-mcp-tool-access-with-per-tool-policies.md): Gate individual MCP tools behind their own policies.

## Related

- [OPA policies](/products/affinidi-trust-fabric/agent-gateway/concepts/opa-policies.md): How the gateway evaluates gateway and surface policies.

- [Dry-run testing](/products/affinidi-trust-fabric/agent-gateway/reference/surfaces/opa-policies.md#dry-run-testing-test-panel): Every field of the Test (dry-run) panel.

- [Policy input capture](/products/affinidi-trust-fabric/agent-gateway/reference/surfaces/opa-policies.md#policy-input-capture): The Policy Input card’s columns, limits and masking.

- [Policy templates](/products/affinidi-trust-fabric/agent-gateway/reference/surfaces/policy-templates.md): What each built-in template decides, including its examples.

- [Audit log](/products/affinidi-trust-fabric/agent-gateway/concepts/audit-log.md): What the Audit page records and how to filter it.
