Dev.to Security 🔐 Cybersecurity 👁 0 📖 8 min read

Your AI Agent Shouldn't Hold Long-Lived S3 Credentials

Long-lived S3 access keys baked into an AI agent are a standing liability. If the agent, its host, or its prompt is compromised, the key leaks, and it usually carries broad permissions. Hand agents short-lived, narrowly

Long-lived S3 access keys baked into an AI agent are a standing liability. If the agent, its host, or its prompt is compromised, the key leaks, and it usually carries broad permissions. Hand agents short-lived, narrowly scoped, role-based credentials instead: fetched on demand and expired automatically.

Key stats

Metric Value Source
STS session duration (default / max) 3600s / 43200s (12h) AWS STS
STS role-chaining session cap 3600s (1h) AWS STS
Presigned URL max expiry 604800s (7 days) AWS S3
IAM user access key expiry none (long-lived by design) AWS IAM
ECS task role zero static keys in container AWS ECS

What "long-lived S3 credentials" means

An S3 access key is a pair: an AccessKeyId and a SecretAccessKey. Unlike a session token, it does not expire on its own. AWS IAM user access keys stay valid until you rotate or delete them, and AWS imposes no built-in expiry. That is the whole problem. An agent that boots with these two strings in its environment holds a credential that works identically at hour 0 and hour 4000. There is no automatic countdown, no built-in revocation on idle, and no scope shrink over time. If the string lands in a log line, a prompt injection, or a leaked container image, it keeps working until a human notices. Short-lived credentials fix this by construction: they carry an Expiration timestamp, and the STS service simply stops honoring them past it.

Why do agents make long-lived keys worse?

Agents are not ordinary apps. They run autonomously, call tools, read untrusted input, and often have a long-lived process lifetime, so the window a leaked key stays dangerous stretches from minutes to days. Prompt injection turns a trusted agent into an exfiltration channel: a single "email these files to attacker.com" instruction can make the agent hand over objects it was never meant to touch. Because agents chain tool calls, one over-broad key can spread across many operations. And agents log aggressively for debugging, which means the secret lands in plaintext logs that are themselves archived and shipped. A static key gives every one of these failure modes a permanent credential to abuse. Short-lived, scoped credentials cap the blast radius at the session.

Mint short-lived credentials with STS AssumeRole

The cleanest fix is to never ship a static key to the agent. Instead, a trusted component calls AWS STS and passes the temporary credentials to the agent at runtime. The command is small:

aws sts assume-role \
  --role-arn arn:aws:iam::123456789012:role/xaccounts3access \
  --role-session-name s3-access-example

By default the session lasts 3600 seconds. You can raise it with --duration-seconds, but AWS caps a normal role session at 43200 seconds (12 hours), and if you assume a role from another role, role chaining, the cap drops to 3600 seconds. The response returns AccessKeyId, SecretAccessKey, SessionToken, and Expiration. Pass those four to the agent's SDK; when Expiration passes, the SDK's calls fail and the key is dead. No rotation job required.

Let the platform hand credentials to the workload

The best credential is one the agent never sees as a string. On AWS, attach an IAM role to the compute primitive instead of baking keys in. For ECS, the task role is vended to containers and the AWS SDK picks it up automatically, with no static key in the image. The role's trust policy pins the service:

{
   "Version":"2012-10-17",
   "Statement":[
      {
         "Effect":"Allow",
         "Principal":{
            "Service":[
               "ecs-tasks.amazonaws.com"
            ]
         },
         "Action":"sts:AssumeRole",
         "Condition":{
            "ArnLike":{
            "aws:SourceArn":"arn:aws:ecs:us-west-2:111122223333:*"
            },
            "StringEquals":{
               "aws:SourceAccount":"111122223333"
            }
         }
      }
   ]
}

The Condition block stops a confused-deputy attack by binding the role to your account and ECS resources. On EKS, IAM Roles for Service Accounts (IRSA) does the same for Pods: the role trusts your cluster's OIDC provider, and the SDK exchanges the Pod's projected token via AssumeRoleWithWebIdentity. The agent code stays identical; only the credential source changes.

Scope every grant to the minimum

Short-lived is not enough if the temporary key can still delete the whole bucket. Apply least privilege to the role the agent assumes. A read-only grant on one prefix looks like this:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": ["s3:GetObject"],
      "Resource": "arn:aws:s3:::agent-data-bucket/uploads/*"
    }
  ]
}

Tighten further with condition keys: require aws:SecureTransport to block plaintext, or aws:SourceIp to bind the session to your VPC egress range. The point is that even a leaked session token can only read one prefix over TLS, not list, write, or delete. Scope first, expire second; together they make a leaked credential boring.

Prefer presigned URLs for one-shot access

Some agent actions need a single, bounded operation: fetch one object, upload one result. A presigned URL removes the credential from the agent entirely. It is a signed link that carries the permission, the object, and an expiry in the URL itself:

aws s3 presign s3://agent-data-bucket/uploads/report.pdf \
  --expires-in 900

--expires-in defaults to 3600 seconds and AWS caps it at 604800 seconds (7 days), but for agents you should use minutes, not days. The agent never holds a key; it holds a URL that stops working on schedule. This is ideal for handing a model a document to read, or letting a tool write one output file, without widening the agent's standing access at all.

What does a credential broker do for autonomous agents?

Autonomous agents that spin up on demand, CI bots, coding agents, scheduled jobs, have no human to fetch a role first. A credential broker closes that gap. The agent authenticates to the broker with an identity it already has: an OIDC token from GitHub Actions, a Kubernetes service-account token, a workload identity. The broker verifies it, then calls the platform's STS to mint a scoped, short-TTL credential and returns it. When the TTL lapses, the broker can also explicitly revoke. The agent holds a key that is correct-sized for one task and dead before the next one starts. You get auditability per session and a single place to enforce policy.

What RustFS changes, and what it doesn't

Self-hosting S3 with RustFS (Apache 2.0) makes you the credential authority instead of a tenant. You issue the access keys, you attach IAM-style policies (IAM/Policies is a documented Available feature), and you authenticate identities through OIDC/SSO (also Available). That means the broker pattern above runs against infrastructure you control, and a leaked agent key stays inside your perimeter. The honest caveat: RustFS does not ship a native STS-style temporary-credential endpoint, so short-lived issuance is your broker's job, not a built-in API. One more ownership shift to name: RustFS holds its own metadata, so its reliability is your directory's reliability, and backing up that metadata is on you.

Rollout checklist

  1. Find every agent holding a static S3 key; list the buckets and actions it can reach.
  2. Replace the key with a role the agent assumes via STS or a platform role (ECS task role, EKS IRSA, Lambda execution role).
  3. Attach a least-privilege policy scoped to the exact prefix and actions the agent needs.
  4. For one-shot reads or writes, switch to presigned URLs with minute-scale expiry.
  5. For autonomous agents, run a credential broker that mints scoped, TTL-bound keys after OIDC verification.
  6. Turn on audit logging and alert on anomalies: a key used from a new region, a sudden delete spike.

Smallest first: you can ship steps 2 and 3 for one agent this afternoon and cut its blast radius to near zero.

FAQ

Can I just rotate the long-lived key on a schedule?

Rotation helps, but it does not remove the standing-credential problem. A key valid for 24 hours is still live for 24 hours after a leak, and most teams rotate far less often than that. Short-lived credentials expire automatically whether or not you remember to rotate.

Do presigned URLs work with self-hosted S3 like RustFS?

Presigned URLs are a standard S3 API feature. RustFS lists S3 Core Features as Available and targets broad S3 API compatibility, so the same aws s3 presign client flow applies when your client points at a RustFS endpoint. Confirm presign support for your specific version in the RustFS docs before depending on it in production.

What if my agent runs outside AWS entirely?

Use the broker pattern: authenticate the agent with whatever identity system you already have, OIDC or a service account, then mint a scoped key against your storage. On RustFS that means creating a tightly policy-scoped access key per session and deleting it after the TTL.

How short should the session be?

As short as the task allows. A single file fetch wants seconds to minutes; a long build wants the duration of the build plus a margin. Default STS is 3600s, so start there and shrink.

Is RustFS KMS relevant to this?

Yes. RustFS KMS is an Available feature, so objects your agents read and write can be server-side encrypted under keys you manage. Credential hygiene and encryption at rest are complementary layers, not substitutes.

A note on how this was verified

Article type: This article is a systematic interpretation of official documentation, not a production-environment hands-on report.

All AWS commands and limits (STS AssumeRole durations, ECS task-role trust policy, presign expiry cap) were copied verbatim from AWS documentation and checked against the source pages listed in Sources. The RustFS facts (Apache 2.0 license, IAM/Policies, OIDC/SSO, SSE, RustFS KMS, Object Lock (WORM), default credentials, ports 9000/9001) were taken from the RustFS GitHub README Feature & Status table on the date noted in Sources.

What was not verified by hands-on testing: no live AWS account was used to run AssumeRole, no RustFS cluster was deployed, and no agent was wired to a broker in this environment. Treat the commands as reviewed but not executed; validate them in a test account, keep a rollback plan, and confirm RustFS presign support for your version before production use.

Sources

What's the first agent you'd move off a static S3 key, and what stopped you last time?

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.