| new file mode 100644 |
| index 000000000..d061000d9 |
| --- /dev/null |
| +++ b/docs/superpowers/specs/2026-11-25-store-write-credential-design.md |
| @@ -0,0 +1,326 @@ |
| +# Store write credential: design |
| + |
| +Status: draft for review, 2026-11-25. Asked for by Paul on 2026-11-25. |
| +Branch `store-write-credential`. |
| + |
| +## 1. What and why |
| + |
| +Today an app has exactly one store credential (`CARLOS_STORE_ACCESS_KEY` |
| +and `_SECRET_KEY`). It can read, overwrite and delete every object in the |
| +store. An app that hands its store to a third party, such as LiveKit Cloud |
| +Egress uploading a recording, has to hand over that whole credential |
| +(`docs/site/live-streams-bring-your-own-livekit.md`, "Not yet supported"). |
| + |
| +This adds a second kind of credential, minted on request: |
| + |
| +- **write only**: `s3:PutObject` (which also authorises |
| + CreateMultipartUpload, UploadPart and CompleteMultipartUpload), |
| + `s3:AbortMultipartUpload` and `s3:ListMultipartUploadParts`, nothing else; |
| +- **under one key prefix** of the app's own store, named by the app; |
| +- **temporary**: a lifetime the app chooses, 15 minutes to 24 hours; |
| +- printed as access key, secret, session token, expiry, bucket and region, |
| + the fields LiveKit's `S3Upload` takes (`access_key`, `secret`, |
| + `session_token`, `region`, `bucket`). |
| + |
| +No read, list, delete, tagging, ACL or bucket call. Every Deny the store's |
| +own credential carries still applies: the platform tag keys, the storage |
| +grace write Deny, the history Denys. |
| + |
| +## 2. Mechanism: GetFederationToken as the store's own IAM user |
| + |
| +The console mints the credential with STS `GetFederationToken`, called as |
| +**the store's own IAM user** (`carlos-store-<acct>-<app>[-<env>]`), with an |
| +inline session policy built by the core (section 3). |
| + |
| +AWS's rule for the result: "the session permissions are the intersection of |
| +the IAM user policies and the session policies that you pass", and the |
| +user's permissions boundary limits it too ("the intersection of all three |
| +policy types"). So the minted credential can never do more than: |
| + |
| +1. the store user boundary (`infra/modules/region-host`, store prefix only), |
| +2. the store user's inline policy (`storePolicyJSON`: one bucket, every |
| + Deny), and |
| +3. the session policy (one prefix, write only). |
| + |
| +The ceiling is the store's own principal. A bug in the session policy can |
| +at worst give the third party what the app's own key already has, in that |
| +one store. It can never reach another store or a deployment bucket, because |
| +layers 1 and 2 do not name them. |
| + |
| +### 2.1 Who holds the minting authority |
| + |
| +`GetFederationToken` must be called with an IAM user's long-term keys; role |
| +credentials are not accepted. The console runs as an EC2 role and does not |
| +hold the app's key (the secret's only landing place is the app's sealed |
| +config, walk.go §7). |
| + |
| +So each mint uses a **single-use access key**: the console, which already |
| +holds `iam:CreateAccessKey`/`DeleteAccessKey`/`ListAccessKeys` on the store |
| +user path (`host_store`), creates a key for the store user, calls |
| +`GetFederationToken` with it, and deletes it. The key's secret exists only |
| +in the console process for those seconds and is never logged, recorded or |
| +returned. |
| + |
| +### 2.2 Alternatives rejected |
| + |
| +- **AssumeRole of a per-store write role.** The console's credentials are |
| + themselves a role session, and role chaining caps a session at one hour |
| + ("Role chaining limits ... to a maximum of one hour"). A LiveKit file |
| + egress runs up to 3 hours and uploads at the end, so this cannot cover a |
| + stream. It would also need `iam:CreateRole` on the console, a new |
| + escalation surface. |
| +- **One deployment-wide minting user.** Its ceiling would be every store, |
| + so the session policy alone would separate tenants, and the per-store |
| + Denys (storage grace, scan enforcement) would have to be copied into |
| + every session policy. It also needs a long-lived secret on the console. |
| +- **The app mints its own token with its delivered key.** Possible once the |
| + user may call `GetFederationToken` (2.3), but then each app writes IAM |
| + policy itself. The scope decision belongs in the core, behind one door. |
| +- **Presigned URLs.** LiveKit's `S3Upload` takes keys and a session token, |
| + not URLs. |
| + |
| +### 2.3 What the store user gains, and how that stays safe |
| + |
| +To call `GetFederationToken` the store user needs `sts:GetFederationToken` |
| +in its inline policy and in its boundary. Both are added only where the |
| +feature is used: |
| + |
| +- the boundary statement only with the module switch |
| + `store_write_credentials = true` (section 6); off, the boundary renders |
| + byte for byte as today; |
| +- the inline statement only for a store whose record carries a |
| + `write_credentials` block, which the first mint writes. Until then that |
| + store's policy is byte for byte today's. |
| + |
| +Resource: `arn:<partition>:sts::<account>:federated-user/carlos-write-*`, |
| +the account and partition taken from the record's `user_arn`. |
| + |
| +That statement is also reachable with the app's own delivered key, because |
| +IAM cannot tell one access key of a user from another. With it the app can |
| +mint itself federation sessions no wider than its own key. That is no new |
| +power over the store, with one exception, which this design closes: |
| +**persistence past a rotation**. AWS does not document that deleting the |
| +access key that minted a federation token revokes the token; it documents |
| +only that permissions "are evaluated each time they are used" against the |
| +user's policies. A thief holding a leaked app key could mint 36-hour |
| +sessions before the member rotates, and they would survive |
| +`carlos store rotate --finish`. |
| + |
| +So for a store with `write_credentials`: |
| + |
| +- the inline policy carries a revocation Deny once `revoked_before` is set: |
| + `Deny s3:*` on the bucket and its objects when |
| + `DateLessThan aws:TokenIssueTime <revoked_before>`. `aws:TokenIssueTime` |
| + is present only for temporary credentials, so the app's long-term key is |
| + never matched; |
| +- `RotateFinish` sets `revoked_before` and converges the policy, so a |
| + finished rotation kills every federation session minted before it, the |
| + console's and any the app minted itself; |
| +- `carlos store credential --revoke` does the same without rotating, for a |
| + leaked write credential. |
| + |
| +`revoked_before` is the console's clock plus one minute, to cover skew |
| +against AWS's clock (the Deny compares AWS's issue time). Mints are refused |
| +until that minute has passed, so a fresh credential is never born revoked. |
| +AWS notes that "it might take a few minutes for policy updates to take |
| +effect", and the CLI says revocation can take a few minutes. |
| + |
| +The federated session itself can call no IAM operation and no STS operation |
| +except `GetCallerIdentity` (AWS: GetFederationToken), so a minted |
| +credential cannot mint another. |
| + |
| +One residual, stated rather than closed: a store torn down and created |
| +again for the same app reuses the user and bucket names. AWS ties a role's |
| +sessions to the role's unique id; it does not say the same for a federated |
| +user's parent. If it went by name, a write credential from the old store |
| +could write into the new one under its prefix until it expires (24 hours at |
| +most). The new record has no `write_credentials` block, so nothing in the |
| +new user's policy refers to it. |
| + |
| +## 3. The scope: the core's session policy |
| + |
| +Built in `internal/objstore` (provider-neutral, no SDK), from the bucket in |
| +the store's record and the validated prefix: |
| + |
| +```json |
| +{"Version": "2012-10-17", "Statement": [ |
| + {"Effect": "Allow", |
| + "Action": ["s3:PutObject", "s3:AbortMultipartUpload", "s3:ListMultipartUploadParts"], |
| + "Resource": "arn:aws:s3:::<bucket>/<prefix>*"}, |
| + {"Effect": "Deny", "Action": ["s3:PutObject"], |
| + "Resource": "arn:aws:s3:::<bucket>/<prefix>*", |
| + "Condition": {"Null": {"s3:RequestObjectTagKeys": "false"}}} |
| +]} |
| +``` |
| + |
| +The Deny refuses any upload that carries tags at all (`x-amz-tagging`), |
| +beside the user policy's Deny on the platform's own tag keys: the lesson in |
| +AGENTS.md ("absence is not the property") applied to the new credential. |
| + |
| +The prefix is validated before it goes near a policy: |
| + |
| +- 1 to 200 bytes, ending in `/`; |
| +- segments of `A-Z a-z 0-9 . _ -`, no empty, `.` or `..` segment, no |
| + leading `/`; this keeps IAM wildcards (`*`, `?`) and policy variables |
| + (`${...}`) out of the ARN; |
| +- not `derived/` or under it: media conversion writes its outputs there, |
| + and a third party must not be able to plant files that look |
| + platform-made. |
| + |
| +It can overwrite objects under its prefix (that is what `PutObject` is). |
| +The docs say to give each third party a prefix of its own. |
| + |
| +## 4. Lifetime |
| + |
| +`ttl` is required, 15 minutes (STS's floor) to 24 hours. LiveKit Cloud's |
| +longest egress is 12 hours (track or segments; a file output is 3 hours) |
| +and it uploads a file when the recording stops, so 24 hours covers the |
| +longest recording plus its upload. STS itself allows up to 36 hours; the |
| +lower cap shortens a leak. |
| + |
| +## 5. The key slot and failure handling |
| + |
| +IAM allows two access keys per user. The mint runs under the same locks as |
| +the walks (`release.WithAppMutation`, the console's account lock and store |
| +walk lock), so it never overlaps a grant, rotation or teardown of the same |
| +store. |
| + |
| +1. `ListAccessKeys`: two keys already (a rotation not yet finished, or a |
| + stray key) is a 409 telling the member to finish the rotation first. |
| +2. `CreateAccessKey`. |
| +3. `GetFederationToken`, retried with backoff for up to 30 seconds while |
| + STS answers `InvalidClientTokenId` or `AccessDenied` (a new key and a |
| + just-written policy both take seconds to propagate). |
| +4. `DeleteAccessKey`, always, also after 3 fails, on a context detached |
| + from the request's cancellation and bounded to 15 seconds. |
| + |
| +The mint never reaps keys it did not create: the rotation walk's |
| +"delivered but not yet recorded" gap means an unnamed key can be one the |
| +app is using. A key a crashed mint leaves behind has no holder, and the |
| +next `carlos store rotate` reaps it. |
| + |
| +## 6. Provider boundary |
| + |
| +- **Core** (`internal/objstore/writecred.go`): the prefix and lifetime |
| + rules, the session policy, the record block, the policy statements, the |
| + walk. No SDK. |
| +- **AWS adapter** (`internal/objstore/aws_writecred.go`): the single-use |
| + key and `GetFederationToken`. STS client from `internal/rootaws`. |
| +- **S3-compatible stores** (a record with an `endpoint`): refused with |
| + 409 `write_credentials_unsupported`, saying a write credential needs AWS |
| + S3 and STS. The console also refuses to boot with both |
| + `CARLOS_STORE_WRITE_CREDENTIALS=1` and `CARLOS_STORE_ENDPOINT`, as store |
| + versioning does. In this build IAM is always AWS's, so AWS STS sessions |
| + would not be accepted at another endpoint. Nothing ever falls back to the |
| + full credential. |
| +- **MinIO contract**: MinIO's STS `AssumeRole`, signed with a MinIO user's |
| + key, accepts a session policy and grants the intersection of the user's |
| + policy and it. The CI step proves the core's session policy, against a |
| + user carrying `storePolicyJSON`'s shape, on a real policy engine: a |
| + future MinIO provisioner implements the same `MintSession` seam with that |
| + call. |
| + |
| +The provisioner seam gains one method: |
| + |
| +```go |
| +// MintSession mints a temporary credential for store user `user`, narrowed by |
| +// the IAM session policy `policy`, lasting ttl. |
| +MintSession(ctx context.Context, user, policy string, ttl time.Duration) (Session, error) |
| +``` |
| + |
| +What a non-AWS deployment loses: write credentials. Its apps keep their |
| +full credential. |
| + |
| +## 7. Infra |
| + |
| +`infra/modules/region-host`: |
| + |
| +- variable `store_write_credentials` (bool, default false), precondition: |
| + needs `store_bucket_prefix`; |
| +- when true, one statement appended to the store user boundary: |
| + `Allow sts:GetFederationToken` on |
| + `arn:aws:sts::<account>:federated-user/carlos-write-*`. Managed fleet |
| + users share the boundary but their inline policies do not grant it, so |
| + they gain nothing; |
| +- no change to the console role: `host_store` already grants every IAM |
| + call the mint makes, and the STS call is made as the store user. |
| + |
| +## 8. Surface |
| + |
| +Console door (registered through `routedecls.go`, refused to linked |
| +accounts, refused on a host-claim console): |
| + |
| +``` |
| +POST /api/cli/apps/{account}/{app}/store/credentials |
| +{"access": "write", "prefix": "recordings/", "ttl_seconds": 14400, "environment": ""} |
| +-> 200 {"access_key", "secret", "session_token", "region", "bucket", |
| + "prefix", "expires_at"} Cache-Control: no-store |
| + |
| +POST /api/cli/apps/{account}/{app}/store/credentials/revoke |
| +-> 200 {"revoked_before"} |
| +``` |
| + |
| +Roles: `svcrole.Allow(svcrole.Service)`, so the app's server can mint one |
| +per recording with the same Service credential media conversion uses (and |
| +Admin). A member's CLI token reaches it through membership as usual. |
| + |
| +CLI, named like `store rotate`: |
| + |
| +``` |
| +carlos store credential --write --prefix recordings/ --ttl 4h --app shop [--environment e] [--json] |
| +carlos store credential --revoke --app shop [--environment e] |
| +``` |
| + |
| +`--write` is required, so a later kind has room. The default output leads |
| +with the expiry and the scope, then the five values; `--json` prints the |
| +door's answer, whose names match LiveKit's `S3Upload`. The secret and token |
| +go to stdout only; nothing logs them, console or CLI. |
| + |
| +The door needs `CARLOS_STORE_WRITE_CREDENTIALS=1` on the console (the |
| +operator applies the module switch first); without it, 503 |
| +`write_credentials_not_configured`. |
| + |
| +## 9. Record |
| + |
| +Additive block on the store record: |
| + |
| +```json |
| +"write_credentials": {"at": "...", "by": "...", "revoked_before": "..."} |
| +``` |
| + |
| +An older console that rewrites the record drops it; the store's policy then |
| +loses the STS statement at its next convergence and the next mint writes |
| +it back. An older console's `RotateFinish` does not revoke: roll consoles |
| +forward before turning the switch on. |
| + |
| +## 10. Tests |
| + |
| +- Unit, test-first: prefix and ttl validation; the session policy; the |
| + inline policy with and without the block and `revoked_before` (and byte |
| + identical without); the walk on the in-memory provisioner (refusals, the |
| + two-key 409, the key always deleted, the retry, revocation's minute); |
| + the AWS adapter against stub IAM and STS clients; the door and the CLI. |
| +- Policy evaluation: a small IAM evaluator in the tests, run over the |
| + three layers (boundary, inline, session), proving the minted scope |
| + cannot read, list, delete, tag, write outside the prefix, write another |
| + store or a deployment bucket, and that `revoked_before` cuts it off while |
| + leaving the app's long-term key alone. |
| +- MinIO contract (`.amadan/ci.d/30-minio-contract`): a real session from |
| + MinIO's STS narrowed by the core's session policy, against a user with |
| + the store policy: put and multipart under the prefix succeed; read, |
| + head, list, delete, tag, put outside the prefix, put tagged and put to |
| + another bucket are denied. |
| +- `tofu test`: the boundary byte for byte without the switch, the |
| + statement with it, the precondition. |
| + |
| +## 11. For Paul |
| + |
| +- Service credentials gain this door. An app's server needs it to mint per |
| + recording, but every existing Service credential can then mint write |
| + credentials for its app's store. |
| +- A store that has used write credentials lets its own delivered key call |
| + `GetFederationToken` (2.3). Revocation on `rotate --finish` closes the |
| + rotation gap; the cost is that finishing a rotation also ends every live |
| + write credential, which the LiveKit page already warns about for the old |
| + key. |
| +- A mint while a rotation is unfinished is refused (two-key ceiling). |