# Realm settings

> Turn the secrets store on, and set the defaults every vault follows for history, check-and-set, shares and emergency access.

One settings object per realm, `vaultSettings`, holds what every vault takes when a secret sets nothing of its own:
how much history is kept, whether writes must name the version they replace, how long one-time shares last and how
long an emergency nominee waits. A realm administrator writes it once, by turning the store on, and changes it
whenever the realm's policy changes.

## Before you begin

- **Who:** a realm administrator. Turning the store on needs `RealmOwner`, or a privilege of your own that allows
  `MODIFY` on secrets; changing the settings needs `MODIFY` on the realm's settings, which `RealmOwner` gives. Reading
  them needs `READ`, which `RealmViewer` gives too. See
  [Delegated administration](/platform/realms/delegated-administration/).
- **Where:** the API. There are no screens yet.
- **First:** nothing. The store is off until you turn it on.

## How it works

The managed vaults' settings sit in the `managed` section. The one-time shares' sit at the top, since they span both
kinds of vault. The sealed vaults' sit in `sealed`: see [Sealed vault settings](/secrets-store/sealed-vaults/settings/).

| Setting | What it does | Default | Range |
|---|---|---|---|
| `managed.maxVersions` | Versions a path keeps | 10 | 1 to 100 |
| `managed.casRequired` | Every write must use [check-and-set](/secrets-store/secrets/check-and-set/) | `false` | |
| `managed.deleteVersionAfter` | Seconds after which a version reads as deleted; `0` keeps them | 0 | 0 to ten years |
| `managed.emergencyDelayDefault` | Seconds a nominee waits after asking, when the grantor names no delay | 604800 (a week) | 1 second to ten years |
| `managed.tokenReuse` | How long a sign-in keeps its access to its own keys open between requests | `session`, 600 seconds | see below |
| `shareDefaultTtl` | Seconds a [one-time share](/secrets-store/sharing/one-time-shares/) waits when its maker names no lifetime | 600 (ten minutes) | 1 second to 30 days |
| `shareMaxTtl` | The longest a one-time share may wait, in seconds | 604800 (a week) | 1 second to 30 days |

`managed.tokenReuse` is `{ "mode": "session", "ttl": 600 }` by default: a sign-in keeps its access to its own keys
open between requests for `ttl` seconds (10 to 3600), never past the session's end. Set `mode` to `request` to reopen
that access on every request instead: stricter, and slower.

## Set it up

As a realm administrator ([Call the API](/develop/api/) shows how to get `$CVYUH_SESSION`):

1. Turn the store on:

   ```sh
   curl -X POST https://foobar.cvyuh.com/api/v0/vault/managed/v1/enable \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
   ```

   The realm writes `vaultSettings` with the defaults above, and answers `201` the first time and `200` after. Until
   then every other call to the store answers `409`.

2. Read the settings, and note their `rev`:

   ```sh
   curl https://foobar.cvyuh.com/api/v0/am/objects/v1/vaultSettings/vaultSettings \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
   ```

   A `404` means the store isn't on yet.

3. Change them, sending only the fields to change, each in its section:

   ```sh
   curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/vaultSettings/vaultSettings \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -H 'If-Match: "<rev>"' \
     -d '{ "managed": { "maxVersions": 5 } }'
   ```

Anyone signed in to the realm reads the first three at
`https://foobar.cvyuh.com/api/v0/vault/managed/v1/config`, as `max_versions`, `cas_required` and
`delete_version_after`.

## What users see

A change applies to every vault at once, to the next request: a lower `managed.maxVersions` at each path's next write,
a new `managed.deleteVersionAfter` to versions written after it, new share lifetimes to shares made after it. Nothing
already written is changed. No one is told of the change.

## When it fails

| Answer | When | What to do |
|---|---|---|
| `403` turning the store on | The caller isn't a realm administrator | Ask someone holding `RealmOwner` |
| `404` reading the settings | The store isn't on yet | Turn it on first |
| `400` | A field the settings don't name, or a value out of range | Send only the fields in the table, each in its section |
| `412` | The settings changed since you read their `rev` | Read again, and send your change with the new `rev` |
| `409` from any vault call | The store isn't on | Turn it on |

## Limits and defaults

Every default and range is in the table above. None can be raised past its range.

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| A `PATCH` answers `400` though the field exists | It was sent at the top instead of in its section | Put `maxVersions` and the other `managed` fields under `managed` |
| A path keeps more versions than the realm's limit | The path sets its own `max_versions` | A path's own setting wins; see [Metadata](/secrets-store/secrets/metadata/) |
| Writes start failing with `400` after a change | `managed.casRequired` is now `true` | Writers send `options.cas`; see [Check-and-set](/secrets-store/secrets/check-and-set/) |

## Audit record

Turning the store on is a `CREATED` change record on `vaultSettings`, and each change an `UPDATED` or `PATCHED` one,
with what changed, before and after. Read it in
[Change history](/audit-reports/audit-trail/change-history/) at
`https://foobar.cvyuh.com/api/v0/am/objects/v1/vaultSettings/vaultSettings/history`.

## Good to know

- **Only a realm administrator turns the store on.** Anyone else is refused (`403`).
- **A path's own setting wins** over the realm's, where it sets one. Check-and-set is the exception: a path can require
  it, but can't lift the realm's `managed.casRequired`.
- **A field the settings don't name is refused** (`400`), at the top or in a section, so a change in an old shape fails
  rather than setting nothing.
- **A guarded change.** With `If-Match` and the `rev` you read, a change made since then makes yours fail rather than
  overwrite it.
- **The realm sets only how long shares last.** Anyone who may view a secret can grant or share it to anyone in the
  realm; no setting turns links or grants off or limits who may receive them yet. See
  [What's next for sharing](/secrets-store/sharing/whats-next/#sharing-policy).
- **There is no call to turn the store off.**

## Coming next

A setting for whether a user's first vault is sealed or managed (`personalDefault`). It can be set, but nothing reads it
yet: see [What's next for vaults](/secrets-store/vaults/whats-next/#a-users-first-vault-kind).

## Related

- [Set the realm's defaults](/secrets-store/guides/set-the-realms-defaults/): the task, start to finish.
- [Metadata](/secrets-store/secrets/metadata/): a path's own settings.
