How do you narrow an export worker with s3:* and db:admin?
Question
A nightly billing-export worker reads closed rows from the invoices table and uploads CSV files to arn:aws:s3:::acme-exports/billing/*. Right now it uses one static access key granting s3:* on * plus a db:admin database login.
- What extra blast radius does this setup create if the worker is compromised?
- How do you scope its storage and database identities to least privilege?
- How should the worker authenticate instead of static long-lived keys?
# Current overly broad worker config (synthetic) WORKER_POLICY = { "storage": [{"actions": ["s3:*"], "resources": ["*"]}], "database": {"role": "db:admin", "auth": "static_env_key"}, }
Show hints (3)
- "MATCH VERBS AND PATHS"
The worker only writes billing CSVs and reads
invoices. Why does it hold delete, cross-bucket read, and schema-drop permissions? - "SPLIT READ AND WRITE"
Give the export worker its own read-only database identity, separate from the billing API that creates or updates invoices.
- "SHORT LIVED IDENTITY"
Use platform workload identity (an IAM role, Kubernetes service account token, or OIDC federation) so credentials expire automatically.
Show checked explanation
Wildcard s3:* on * and db:admin let a compromised worker read every bucket, delete backups, or alter database tables and users. Scope it to s3:PutObject on acme-exports/billing/* and a dedicated read-only database role limited to invoices. Replace static keys with short-lived workload identity tokens issued by the runtime.
from fnmatch import fnmatchcase POLICY = [ ({"s3:PutObject"}, "arn:aws:s3:::acme-exports/billing/*"), ({"db:Select"}, "table:invoices"), ] def allowed(act, res): return any(act in acts and fnmatchcase(res, p) for acts, p in POLICY) reqs = [("s3:PutObject", "arn:aws:s3:::acme-exports/billing/dec.csv"), ("s3:GetObject", "arn:aws:s3:::acme-backups/prod.sql"), ("s3:DeleteObject", "arn:aws:s3:::acme-exports/billing/dec.csv"), ("db:Select", "table:invoices"), ("db:DropTable", "table:invoices")] for act, res in reqs: print(act, "->", allowed(act, res))
s3:PutObject -> True s3:GetObject -> False s3:DeleteObject -> False db:Select -> True db:DropTable -> False
Code run on CPython 3.12.10 against synthetic policy rules comparing wildcard grants with scoped ARN and verb allowlists; official AWS, Google Cloud, Kubernetes, and NIST links checked. Checked by an editor other than the author.
Walkthrough
Least privilege means each service identity gets only the exact actions and resources its job needs, with everything else denied by default. Today, a bug in the nightly export job exposes unrelated S3 buckets such as backups and payroll. It also grants full database control, including DROP TABLE, creating superusers, and reading password hashes.
| control | current worker | least-privilege target |
|---|---|---|
| Storage verbs and prefix | s3:* on * | s3:PutObject on arn:aws:s3:::acme-exports/billing/* |
| Database identity | Shared db:admin | Dedicated read-only role (SELECT on invoices only) |
| Read vs write separation | One shared account | Separate read-only export identity from write services |
| Credentials | Static long-lived key | Short-lived workload identity token (IAM role / OIDC) |
Scope both the verb and the resource. Listing s3:PutObject alone is not enough if the resource stays *, because the worker could still overwrite files in other buckets. Naming both the action (s3:PutObject) and the exact ARN prefix (arn:aws:s3:::acme-exports/billing/*) blocks reads on acme-backups and deletes inside acme-exports.
Separate read-only and write identities. Give the nightly export job a dedicated read-only database role restricted to invoices (or a sanitized export view). Keep INSERT, UPDATE, and schema migrations on separate identities used only by the billing API and migration jobs.
Drop static keys for short-lived tokens. Attach an IAM role, GCP service account via Workload Identity, or Kubernetes projected token directly to the workload. The platform mints short-lived credentials automatically, so no static secret sits in .env, container images, or CI variables waiting to leak.
Common mistakes
- Narrowing the action to
s3:PutObjectwhile keepingResource: "*". The worker can still overwrite files in unrelated buckets. - Sharing one service account across the web API, migrations, and batch jobs. Every workload then inherits the widest combined rights.
- Storing static cloud access keys in environment variables or container layers instead of using an IAM role or workload identity.
- Granting
SELECTon the whole database instead of theinvoicestable or view. The worker could still read token or user tables.
Follow-up questions
- "What if the export worker runs outside your cloud, such as in GitHub Actions or on-premises?" Use OIDC workload identity federation instead of exporting static JSON or IAM keys. The external platform issues a short-lived signed token scoped to one repo or workload, and your cloud exchanges it for temporary credentials.
- "How do you tighten an existing service account with
s3:*without breaking the nightly job?" Inspect cloud audit logs (such as CloudTrail) or IAM Access Analyzer across a full billing cycle to list every action and resource actually used. Attach a scoped role in staging, roll it out, and watch forAccessDeniedevents before removing the old grant. - "If the bucket requires KMS encryption on upload, what KMS permission should the worker get?" Grant only
kms:GenerateDataKey(andkms:Encryptif needed) scoped to that single export KMS key ARN, neverkms:*on*. Omitkms:Decryptif this worker never reads exports back.
What a strong answer covers
Maps the blast radius of s3:* and db:admin, and narrows permissions by both verb and resource ARN or table. Separates read-only export from write identities, and replaces static keys with short-lived workload identity tokens.
Open in interactive practice deck →