# Scoped credentials

URL: https://skywake.dev/docs/security/scoped-credentials

Short-lived store credentials that reach only one disk's folder in the bucket.

Agents and machines that mount a disk read and write the bucket directly. Scoped credentials let them do that without ever holding the bucket's own keys. Each set reaches only one disk's folder, and it expires on its own.

```bash norun
export SKYWAKE_STORE=skywake://app
export SKYWAKE_API_URL=https://api.example.com SKYWAKE_API_KEY=sw_...
skywake fork app agent-1
```

When the store is `skywake://NAME`, the CLI's local commands and [`skywaked`](../filesystem/mount.mdx) ask the API for credentials that reach only that disk, and renew them before they expire. No key for the whole bucket ever leaves the server. `NAME` is the disk's full name: `app` in the `default` environment, `prod/app` in `prod`.

By default they ask for `write` credentials. Set `SKYWAKE_STORE_ACCESS=read` to get read-only ones.

<Callout type="info">
  The `skywake://` store still reads `SKYWAKE_API_URL`, not `SKYWAKE_URL`, and the SDKs have no
  `credentials()` call yet. Until they do, ask for credentials over `/v1`, which goes away one
  release after v2.
</Callout>

## Ask for them directly [#ask-for-them-directly]

```bash norun
curl -X POST "$SKYWAKE_URL/v1/namespaces/prod%2Fapp/credentials" \
  -H "authorization: Bearer $SKYWAKE_API_KEY" -H "content-type: application/json" \
  -d '{"access": "write"}'
```

`access` is `read` or `write`. For `write`, you need a [key](./api-keys.mdx#roles) with the `write` role. The response holds `store` (as `s3://bucket/prefix`), `region`, `endpoint`, `accessKeyId`, `secretAccessKey`, `sessionToken` and `expiresAt`. When the API keeps branch heads on cache servers (below), it also holds `heads`: the servers, the disk's folder and a pass for them.

## What they can do [#what-they-can-do]

- They can read and write objects in the disk's folder, and nothing outside it.
- They can never delete. Destroying a branch writes an empty head, and garbage collection runs with the server's own keys.
- They can never write the head of a [protected branch](../filesystem/protection.mdx).
- They can never take garbage collection's lock.
- They can still overwrite other branches' heads, content and `deleted/` records on that disk. Content is checked against its hash when it is read, so any damage is noticed. With heads on cache servers, they can't write any head in the bucket: every head changes through the servers, which check the pass.

## Set it up [#set-it-up]

Give the API an IAM role that it may assume. The role needs read, write and list on the bucket. Each credential the API hands out gets a session policy that narrows it to one disk.

| API setting                | Meaning                                                              |
| -------------------------- | -------------------------------------------------------------------- |
| `SKYWAKE_API_STS_ROLE_ARN` | The role. Without it, the route fails with `501`.                    |
| `SKYWAKE_API_STS_ENDPOINT` | The STS endpoint of an S3-compatible store, such as MinIO or RustFS. |
| `SKYWAKE_API_STS_SECONDS`  | How long credentials last, 900 to 3600. `900` by default.            |

### Heads on cache servers [#heads-on-cache-servers]

Branch heads can live on the cache servers instead of the bucket, so forking or committing takes a few milliseconds instead of several bucket writes. Each set of credentials then comes with a pass that names its disk, its access and the protected branches, signed with the cache token. The servers check it without asking the API, and clients renew it on their own.

| API setting           | Meaning                                                       |
| --------------------- | ------------------------------------------------------------- |
| `SKYWAKE_HEADS`       | `tier` keeps heads on the cache servers. `bucket` by default. |
| `SKYWAKE_CACHE_NODES` | The cache servers, or one address that reaches them.          |
| `SKYWAKE_CACHE_TOKEN` | The cache servers' token, which also signs passes.            |

This applies to disks in the default region.

<Callout type="warn">
  Credentials already handed out keep working until they expire. If you revoke a key or protect a
  branch, those credentials are affected only once they expire.
</Callout>

Each request is recorded in the [audit log](./audit-log.mdx) as `createCredentials`, with the access that was asked for.

## Next [#next]

<Cards>
  <Card href="./ip-allowlist.mdx" />
  <Card href="../agents/cli.mdx" />
  <Card href="/reference/api/namespaces/createCredentials" />
</Cards>