Sub topic 8
Scoped, Short-Lived, Per-User Credentials
This chapter assumes you know how API tokens work, can read short Python and JSON snippets, and understand roles and permissions. Earlier chapters showed how to enforce authorization in code. Here the question is what the tool can reach even when your code has a bug.
Why credentials are the last line of defense
Allowlists and argument checks run in your runtime. If one has a gap, the credential decides how much damage a bad call can do. A tool that holds an admin key can do admin things, whatever the prompt says. A tool that holds a narrow key can only do narrow things.
Three properties make a credential safe to give a tool:
- Scoped: it allows only the actions and resources the task needs.
- Short-lived: it expires in minutes, so a leaked copy is useless soon.
- Per-user: it carries the identity of the person the agent acts for, not a shared service identity.
Scope the credential to the task
Start from the tool's job and write the smallest policy that lets it work. An agent that answers questions from published reports needs to read objects under one prefix of one bucket. It does not need to list other buckets, write objects, or read private data.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::example-reports-bucket/public-summaries/*"
}
]
}This policy names one action and one resource pattern. If an injected instruction tells the agent to fetch s3://example-reports-bucket/payroll/2026.csv, the request fails at the cloud provider, even if your own checks missed it.
Give each tool its own credential. If the file reader and the email sender share one key, compromising either gives the attacker both abilities.
Make credentials short-lived
Long-lived keys stored in environment variables or config files get copied into logs, prompts, and error messages. Issue temporary credentials instead, and request them when a task starts. On AWS, AWS Security Token Service (STS) does this. You can also pass an inline session policy that narrows the role further for this one task.
import json
import boto3
def credentials_for_task(user_id: str, prefix: str) -> dict:
sts = boto3.client("sts")
session_policy = {
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": f"arn:aws:s3:::example-reports-bucket/{prefix}/*",
}],
}
response = sts.assume_role(
RoleArn="arn:aws:iam::123456789012:role/agent-report-reader",
RoleSessionName=f"agent-{user_id}",
Policy=json.dumps(session_policy),
DurationSeconds=900,
)
return response["Credentials"]The session gets the intersection of the role's policy and the session policy, so it can never exceed the role. It expires after 900 seconds, which is the shortest STS allows. The session name includes the user ID, so audit logs show who the agent acted for.
Keep the returned credentials in the tool's runtime. Never place them in the model's context.
Use per-user identity
A shared service account makes the agent a confused deputy: it can read anything any user could read, and it will do so for whoever sends the request. Instead, pass the user's identity through to the tool, for example through an OAuth token exchange, so the downstream system applies the user's own permissions.
A user who cannot open a record directly then cannot get the agent to open it either.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| One admin key shared by all tools | Any injected call has full access | One narrow credential per tool |
| Static keys in environment variables | Leaks persist until someone rotates them | Temporary credentials per task |
| Wildcards such as `s3:*` on `*` | The policy allows far more than the task needs | Name actions and resources |
| Credentials passed in prompts | The model can repeat them in output | Keep them in the runtime only |
Exercise
An agent that summarizes a customer's own support tickets uses one shared service account with read access to all tickets. Name two changes that apply the ideas in this chapter.
Show answer
First, issue a per-user, short-lived credential (or token exchange) so ticket reads run with the requesting customer's permissions and expire in minutes. Second, narrow the policy to read-only access on the ticket resource, with no access to other customers' tickets or write actions. Each tool should also get its own credential.