Sub topic 8
Limiting the damage with scoped, short-lived credentials
This chapter assumes you can use Git and a shell, know what an API key is, and can set environment variables. The previous chapter moved secrets out of source code. Here you assume that one will still leak, and you shrink what it can do.
Why scope and lifetime matter
A credential has two properties that set the damage after a leak: what it is allowed to do (scope) and how long it works (lifetime). A long-lived admin key pasted into a prompt is the worst case. A read-only token that expires in one hour is a minor incident.
| Property | Risky choice | Safer choice |
|---|---|---|
| Scope | Account-wide admin key | Key for one service and one action |
| Lifetime | Never expires | Expires in minutes or hours |
| Environment | One key for dev and prod | Separate key per environment |
| Owner | Shared team login | One credential per person or tool |
Give the assistant its own credential
Do not let an AI tool or agent run with your personal credentials. Create an identity just for it, such as a dedicated service account, a separate API key, or a role. Then you can revoke it without breaking your own access, and logs show which actions the tool performed.
Give it only what the task needs. If the agent only reads a test database, give it a read-only user on a test database, not on production.
Prefer short-lived credentials
Many platforms can issue temporary credentials. On AWS, you assume a role and receive a key, secret, and session token that expire. The command below requests credentials valid for 15 minutes.
aws sts assume-role \
--role-arn arn:aws:iam::123456789012:role/ai-agent-readonly \
--role-session-name agent-session \
--duration-seconds 900The response includes AccessKeyId, SecretAccessKey, SessionToken, and an Expiration time. After that time, a copy of the values found in a prompt or log is useless.
The role itself carries the scope. This policy allows reading objects from one bucket prefix and nothing else.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:GetObject"],
"Resource": "arn:aws:s3:::example-reports/public/*"
}
]
}Other providers offer similar features, such as fine-grained personal access tokens with an expiry date, or OIDC federation, which lets a CI job trade its identity for a temporary token so no stored secret exists.
How the pieces fit
Common errors
- Reusing one key everywhere. Revoking it then breaks every system at once, so people delay revoking it.
- Long expiry "for convenience." A 90-day token behaves like a permanent one during an incident.
- Wildcard permissions.
"Action": "*"or"Resource": "*"gives back the scope you meant to remove. - Forgetting to test the limits. Try an action the credential should not allow and confirm it fails.
Check your understanding
Exercise
An AI agent needs to read log files from one storage bucket during a debugging session. Describe the credential you would give it, covering scope, lifetime, and identity.
Show answer
Create a dedicated role or service account for the agent, not your personal login. Allow only read access (for example s3:GetObject) on that one bucket or prefix. Issue temporary credentials that expire in minutes or hours, covering the length of the session. Then test that a write or a read from another bucket is denied.