How to Let an AI QA Agent Change Only the Dev Resources You Choose. Limit an AI QA agent's writes to named dev resources: its own Keycloak client, deny by default, owner-named resources, a token per test session and a 403 test.
To restrict an AI QA agent’s write access to specific API resources in a dev environment, give the agent its own client, deny writes by default, and have each resource owner name the exact dev resources the agent may change. Then issue a short-lived token per test session that carries only the resources handed to that run, enforce that list at the API, and check that a write to an unnamed resource returns 403. In Keycloak, all of this fits in Authorization Services: its default Enforcing mode means “requests are denied by default even when there is no policy associated with a given resource” (Keycloak Authorization Services Guide, Resource server settings).
This post walks through that setup for an orders API in a dev realm: the agent’s client, the resources and permissions its owners name, the token the agent gets for one test session, the check in the API, a rejected out-of-scope write as proof, and the events that tie every change back to a session.
Why are OAuth scopes not enough for a QA agent?
A scope such as orders:write says what kind of action a token allows, but not which objects. If your QA agent holds that scope, it can update every order in the dev database, including the fixtures another team relies on for its own tests, and nothing in the token tells your API the difference. An AI agent makes this worse than a scripted test, because its next request depends on what the model decides, so you cannot read the test file to know which records it will touch.
The OAuth standards have been narrowing tokens for a while. Resource indicators let a client name “the target service or resource to which access is being requested” (RFC 8707, section 2), which narrows the token to one API but still not to one record inside it. Rich authorization requests go further with an authorization_details parameter, an array of JSON objects that each “specify the authorization requirements for a certain type of resource” (RFC 9396, section 2). Keycloak gives you the same object-level control today through Authorization Services, where you register each resource and ask for permissions on it by name (Keycloak Authorization Services Guide, Obtaining permissions). If the model is new to you, our posts on fine-grained authorization in Keycloak and on Authorization Services policy types cover the concepts.
What does the setup look like?
The whole setup has five pieces, and each answers one of the questions you would ask after a QA agent changed something it should not have.
| Piece | In Keycloak | What it answers |
|---|---|---|
| Agent identity | A confidential client qa-agent with a service account |
Who made the call |
| Default | The orders-api-dev client with authorization on, Enforcing mode |
What happens to anything nobody named |
| Owner-named resources | One resource per fixture, a write scope, and one permission per owning team |
Who allowed this write |
| Session token | A requesting party token (RPT) asked for with only the run’s resources | Which run could make the change, and until when |
| Proof and audit | A 403 test, admin events, client login events and API logs | Does the limit work, and what changed |
The agent asks Keycloak for write on order-1042, the resource named for its session (1), Keycloak returns a short-lived token listing order-1042 with the write scope (2), the agent’s write to order-1042 succeeds with 200 (3), and its write to order-2001, which nobody named, is refused with 403 (4).
Give the QA agent its own client
Create a confidential client for the agent, for example qa-agent, turn on its service account and turn off the standard browser flow. Each OIDC client “has a built-in service account”, and the guide says to “use this service account to obtain an access token” (Keycloak Server Administration Guide, Using a service account), so the agent can get its tokens from its own client instead of borrowing a person’s session. Our post on non-human identities for AI agents in Keycloak explains in more depth why an agent should have an identity of its own.
Keep this client separate from whoever started the test run. If you need the token to name both the agent and the engineer who launched it, use token exchange: the act claim in RFC 8693 expresses “that delegation has occurred” and identifies “the acting party to whom authority has been delegated” (RFC 8693, section 4.1). Our Keycloak 26.8 token exchange delegation post walks through delegation for agents in Keycloak. For most QA runs, a plain service account plus the run’s ID in your own logs is enough, and the AI agent authentication guide covers both patterns.
Finally, shorten the agent’s tokens. In the client’s Advanced tab, Access Token Lifespan “overrides the realm option with same name” (Keycloak Server Administration Guide, Advanced settings), so you can give the agent tokens that last a few minutes without touching your other clients.
Deny writes by default
Open the client that represents your dev API, here orders-api-dev, and turn on Authorization (Keycloak Authorization Services Guide, Enabling authorization services). On the Authorization tab’s Settings page, leave Policy Enforcement Mode on Enforcing, which is the default, and do not switch it to Permissive, where “requests are allowed even when there is no policy associated with a given resource” (Keycloak Authorization Services Guide, Resource server settings).
Set the Decision Strategy to Unanimous, which means “all permissions must evaluate to a positive decision in order for the final decision to be also positive” (Keycloak Authorization Services Guide, Resource server settings). You can also turn off Remote Resource Management, so resources “can be managed only from the administration console” (Keycloak Authorization Services Guide, Resource server settings) and the API cannot quietly register new ones.
Then open the Resources, Policies and Permissions tabs and remove any entry that grants more than your owners named, such as a resource that covers every URI of the API with a policy that grants everyone. An entry like that would let any client with a valid token reach every URL of the API. Once nothing like it is left, a write the agent was never granted is refused, whatever the agent decides to try. Reads are a separate choice: if the agent should read everything in dev, add a permission for a read scope on all order resources, and if not, leave it out and reads are denied too.
Let each owner name the resources the agent may change
Create one resource per dev object the agent may write, give it the URI the API serves it at and a write scope, and record who owns it. Resources “also have an owner”, and by default “resources are owned by the resource server” (Keycloak Authorization Services Guide, Resource owners), so for dev fixtures a resource attribute naming the owning team is often simpler. A shared resource type lets you group them, since “the type field of a resource can be used to group different resources together” (Keycloak Authorization Services Guide, Typed resources).
Next, create one client policy that matches only the agent. A client-based policy defines “conditions for your permissions where a set of one or more clients is permitted to access an object” (Keycloak Authorization Services Guide, Client-based policy). Each owning team then gets one scope-based permission that ties the write scope on its chosen resources to that policy. Naming a resource means adding it to your team’s permission, and taking it back means removing it. If a team wants the grant to expire, it can add a time-based policy to the same permission and set its Expire time, which “defines the time after which access must not be granted” (Keycloak Authorization Services Guide, Time-based policy).
This is what that looks like in the client’s exported authorization settings, trimmed to one resource and one team:
{
"policyEnforcementMode": "ENFORCING",
"decisionStrategy": "UNANIMOUS",
"allowRemoteResourceManagement": false,
"resources": [
{
"name": "order-1042",
"type": "urn:orders-api-dev:resources:order",
"uris": ["/orders/1042"],
"attributes": { "owner_team": ["checkout"] },
"scopes": [{ "name": "write" }]
}
],
"policies": [
{
"name": "Only qa-agent",
"type": "client",
"logic": "POSITIVE",
"decisionStrategy": "UNANIMOUS",
"config": { "clients": "[\"qa-agent\"]" }
},
{
"name": "Checkout: QA may write",
"description": "Fixtures the checkout team lets the QA agent change",
"type": "scope",
"logic": "POSITIVE",
"decisionStrategy": "UNANIMOUS",
"config": {
"resources": "[\"order-1042\"]",
"scopes": "[\"write\"]",
"applyPolicies": "[\"Only qa-agent\"]"
}
}
],
"scopes": [{ "name": "write" }, { "name": "read" }]
}
The export and import feature is described in the guide (Keycloak Authorization Services Guide, Export and import authorization configuration). Keeping this file in version control also lets owners name resources through a pull request instead of the admin console.
Issue one token per test session
At the start of a run, the test harness gets the agent’s access token with the client credentials grant and exchanges it for an RPT that asks only for the resources handed to that session. The guide’s examples call Keycloak’s token endpoint with the UMA grant type and a permission parameter in the form resource#scope, repeated for each resource (Keycloak Authorization Services Guide, Obtaining permissions):
# 1. The agent's own token, from its service account
AGENT_TOKEN=$(curl -s -X POST \
"$KC/realms/dev/protocol/openid-connect/token" \
-d grant_type=client_credentials \
-d client_id=qa-agent \
-d client_secret="$QA_AGENT_SECRET" | jq -r .access_token)
# 2. An RPT for this session's resources only
RPT=$(curl -s -X POST \
"$KC/realms/dev/protocol/openid-connect/token" \
-H "Authorization: Bearer $AGENT_TOKEN" \
-d grant_type=urn:ietf:params:oauth:grant-type:uma-ticket \
-d audience=orders-api-dev \
-d "permission=order-1042#write" | jq -r .access_token)
The RPT carries the permissions Keycloak granted, which the API can read from the token itself or from the introspection endpoint (Keycloak Authorization Services Guide, Introspecting a requesting party token), and the guide says to check its validity “based on its exp, iat, and aud claims” (Keycloak Authorization Services Guide, Do I need to invoke the server every time I want to introspect an RPT?), so a check that honours exp stops accepting it on its own. The harness passes it to the agent for that run only and drops it when the run ends, so a later run starts from nothing and has to ask again for whatever it was handed. This is how the writes stay scoped to a session: the owner decides which resources can ever be named, and the session token decides which of those this run can actually change.
Patched on the day, not on your next maintenance window
Keycloak 26.7.2 fixed CVE-2026-18963, an account takeover through password reset. Skycloak had it available for auto-upgrade and told customers the same day upstream shipped it. Self-hosted teams schedule that work themselves.
Enforce the list at the API
Your API must accept a write only when the RPT lists that resource with the write scope. Keycloak “provides all the necessary means to implement PEPs”, or policy enforcement points, for this (Keycloak Authorization Services Guide, Policy enforcers), and in other stacks a small check works once you have verified the token’s signature, issuer, expiry and audience as described in how to verify a Keycloak access token on the backend. Here is the check as Express middleware, where req.token holds the verified claims:
// Allow a write only if the RPT names this order with the write scope.
function requireWrite(req, res, next) {
const resource = `order-${req.params.id}`;
const permissions = req.token?.authorization?.permissions ?? [];
const allowed = permissions.some(
(p) => p.rsname === resource && (p.scopes ?? []).includes("write")
);
if (!allowed) {
return res.status(403).json({ error: "forbidden", resource });
}
next();
}
app.put("/orders/:id", requireWrite, updateOrder);
The check denies by default as well: an empty or missing permission list means 403, so a plain access token without an RPT cannot write anything.
Prove the limit works with a rejected write
Run one test session and make the agent try a resource nobody named, for example order-2001. You should see two refusals. First, if the harness asks Keycloak for it, Keycloak answers 403 Forbidden with "error": "access_denied" and "error_description": "request_denied" (Keycloak Authorization Services Guide, Obtaining permissions). You can also ask Keycloak for a yes or no answer with response_mode=decision, and “if the authorization request does not map to any permission, a 403 HTTP status code is returned” (Keycloak Authorization Services Guide, Obtaining permissions).
curl -s -o /dev/null -w "%{http_code}\n" -X POST \
"$KC/realms/dev/protocol/openid-connect/token" \
-H "Authorization: Bearer $AGENT_TOKEN" \
-d grant_type=urn:ietf:params:oauth:grant-type:uma-ticket \
-d audience=orders-api-dev \
-d "permission=order-2001#write" \
-d response_mode=decision
# 403
Second, if the agent sends the session’s RPT to PUT /orders/2001, your API answers 403 because order-2001 is not in the token. Keep both checks as automated tests next to your QA suite, so a change to a permission or to the middleware that opens up an unnamed resource fails the build instead of passing unnoticed.
Keep an audit trail of which session changed what
You want to answer two questions later: who named a resource, and which session changed it. For the first, turn on admin events with Include Representation, which “includes JSON documents sent through the admin REST API so you can view the administrators actions” (Keycloak Server Administration Guide, Auditing admin events). Every edit to a team’s permission then shows who added or removed a resource.
For the second, save user events for the dev realm, and use Add saved types for any further event types you want kept (Keycloak Server Administration Guide, Auditing user events), and make your API log the run’s ID, the azp client, the resource and the result for every write. Joining those logs on the run’s ID tells you exactly which session changed which resource. Our Keycloak auditing and event logging guide covers retention and where to ship the events.
What about staging and production?
Keep the QA agent out of them entirely. Create the qa-agent client only in the dev realm, so it has no credentials that any staging or production realm would accept, and give each environment its own realm and its own API client. The named-resource setup above then applies only where test data lives.
Frequently asked questions
Can I restrict an AI agent to specific records with OAuth scopes alone?
Not reliably. A scope names a kind of action across the whole API, so orders:write covers every order. You need a per-resource check, such as Keycloak Authorization Services resources and permissions, enforced by the API.
What happens to dev resources nobody named?
With Enforcing mode and no catch-all permission left, Keycloak grants nothing for them, and the API middleware refuses any write the token does not list. Whether the agent may still read them depends only on whether you add a read permission.
How do I know which test session changed a resource?
Give each run its own RPT, log the run’s ID with the client, resource and result of every write in your API, and keep Keycloak’s user and admin events on. The run’s ID links the change to the token and the token to the agent.
Do I need token exchange for a QA agent?
Only if the token must also name the person who started the run. Otherwise the agent’s own service account, plus the run’s ID in your logs, separates it from human users.
Next steps
Create a dedicated qa-agent client in your dev realm, turn on authorization for your dev API in Enforcing mode, have each owning team name the resources the agent may change, and run one test session to confirm that a write outside the session’s list is rejected with 403. Add that rejected write to your test suite so it keeps proving the limit.
If you would rather not run the Keycloak server for your dev, staging and production realms yourself, you can set up the same configuration on managed Keycloak hosting with Skycloak.


