# Grants

> Let another user or a group read one secret, for as long as you choose, and take it back at any time.

A grant opens one secret to someone who doesn't own it: a colleague who needs the database password, or a team that
needs the deploy key. You choose who, how much they may see, and when it ends. Everything else in your vault stays
closed to them.

Grants are how secrets are shared day to day. For a secret someone needs only once, send a
[one-time share](/secrets-store/sharing/one-time-shares/) instead; for a secret a whole team owns, keep it in the
group's vault ([Vaults](/secrets-store/vaults/)).

What's missing today: a grant on a group's or the realm's secret can pause when the user who last wrote the secret is
deleted or has their key replaced. See [Good to know](#good-to-know).

## Before you begin

- **Who:** the secret's owner gives and lists grants: you for your own vault, any member for a group's vault, a realm
  administrator for the realm's vault. A realm administrator may also list the grants on a group's secret, and take
  back any grant. No privilege is needed for your own vault.
- **Where:** the API. There are no screens yet.
- **First:** the grantee has used the store once, so they have a key to seal their copy to; for a group, a member has
  opened the group's vault once.

## How it works

A grant names one user or one group, an access level, and optionally an end time.

| Access | They can | They can't |
|---|---|---|
| `view` | Read every version, its metadata and subkeys; see it when they list your vault; send it on as a one-time share | Write, delete, or grant it to anyone else |
| `use` | Read the latest version | Everything else, including seeing it in a listing |

Give `use` when someone needs the value, such as a service reading a password at start-up. Give `view` when they need
to manage it with you.

**The grantee gets their own sealed copy.** When you give a grant, the realm seals a copy of the secret to the
grantee's own key, which opens only when they sign in. A copy for a group is sealed to the group's key and opens for
any member. Each time the secret is written, the copy is sealed again, so the grantee reads the current versions. A
realm administrator acting as themselves can't open that copy.

**A grant to a group follows its membership.** Someone who joins the group can read the secret; someone who leaves
can't, from their next request. Where a user holds several grants on one secret, the widest one counts.

## Set it up

As the secret's owner ([Call the API](/develop/api/) shows how to get `$CVYUH_SESSION`):

1. Give the grant. Here `maya` gives `ben` `view` on `db/staging` in her own vault, until a time she chooses:

   ```sh
   curl https://foobar.cvyuh.com/api/v0/vault/managed/v1/me/grants/db/staging \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "user": "ben", "access": "view", "validTo": "<RFC 3339 time>" }'
   ```

   Send `"group": "engineering"` in place of `user` to grant a group. Leave out `validTo` for a grant with no end. The
   realm answers `201` with the grant, including its `edgeId`.

2. The grantee finds it with a `GET` on `https://foobar.cvyuh.com/api/v0/vault/managed/v1/shared`, which lists every
   secret granted to them or their groups, with its `vault`, `path` and `access`.

3. They read it in the owner's vault:

   ```sh
   curl https://foobar.cvyuh.com/api/v0/vault/managed/v1/user/maya/data/db/staging \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
   ```

To see a secret's grants, `GET` `https://foobar.cvyuh.com/api/v0/vault/managed/v1/me/grants/db/staging`: each shows
`edgeId`, who it names, `access`, `validTo`, `grantedBy` and `live`. To take one back:

```sh
curl -X DELETE https://foobar.cvyuh.com/api/v0/vault/managed/v1/me/grants/db/staging/<edgeId> \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
```

The realm answers `204`, and their copy is gone.

## What the grantee sees

No message is sent: tell the grantee the path. The secret appears in their `GET …/shared` list from the moment the
grant is given, and leaves it when the grant is taken back. After its end time, reading it is refused: `404` in a
personal vault, `403` in a group's or the realm's.

## When it fails

| Answer | When | What to do |
|---|---|---|
| `404` naming the grantee | The user or group doesn't exist, or hasn't used the store yet | Check the id; the grantee uses the store once, or a group member opens its vault once |
| `404` on the path | The secret isn't there | Write it first; a grant needs an existing secret |
| `400`, "a grant names one user or one group" | The body named both, or neither | Send one of `user` or `group` |
| `400`, the grant would expire before it starts | `validTo` is already past | Send a time in the future |
| `400` on `validTo` | Not an RFC 3339 time | Send a time such as `2026-12-31T00:00:00Z` |
| `403` | The caller isn't the owner of the vault | Ask the owner, a member of the group, or a realm administrator |
| `404` taking a grant back | The `edgeId` isn't a grant on that secret | List the grants and use an `edgeId` from the list |

## Limits and defaults

| Limit | Value |
|---|---|
| Grants per secret | no fixed limit |
| Grant lifetime | no end unless you set `validTo`; any time in the future |
| Access levels | `view`, `use` |
| Who can be granted | users and groups of the same realm |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| A grantee's read answers `404` though the grant shows `"live": true` | The user who last wrote the secret was deleted, or had their key replaced after emergency access once they no longer had the vault's right | Someone with the vault's right writes the secret again; every grant's copy is sealed again |
| A grant to a new user answers `404` | They haven't used the store yet | They make any call to the store once, such as `GET …/config` |
| A grant shows `"live": false` | It reached its `validTo` | Take it back, or give a new one |

## Audit record

Giving or taking back a grant is an `UPDATED` change record on the secret, under who did it. Each grantee read is an
`ACCESS` record under the grantee. See [Secrets audit trail](/secrets-store/keys-and-protection/audit-trail/).

## Good to know

- **Only the owner gives a grant.** For your own vault that's you; for a group's vault, any member; for the realm's
  vault, its administrators. A realm administrator acting as themselves can't grant anyone access to your personal
  secrets.
- **A grant can pause when its secret's last writer goes.** Each copy opens from the key of the user who last wrote
  the secret. In a group's or the realm's vault, if that user is deleted, or has their key replaced after emergency
  access once they have left the group or stopped being an administrator, grants on that secret stop working until
  someone writes it again. Write it again after removing a member.
- **Anyone who may view a secret can share it.** A `view` grantee can send it on as a one-time share to anyone in the
  realm. Give `use` where that matters.
- **End times are checked on every request.** After its end time a grant gives nothing. It stays in the secret's
  list, marked not live, until you take it away.
- **Offboarding takes everything back at once.** A realm administrator can remove every grant to a user, in every
  vault, without reading any of the secrets. See [Offboard someone](/secrets-store/guides/offboard-someone/).
- **Taking a grant back can't take back what was read.** Change the secret if the grantee shouldn't keep the value.
- **Someone with no grant sees nothing.** Your secret reads as not there (`404`), so they can't even learn that the
  path exists.

## Reference

| Field | What it sets |
|---|---|
| `user` or `group` | Who the grant is for, by user id or group id. One of the two |
| `access` | `view` or `use` |
| `validTo` | When it ends, as an RFC 3339 time. Leave it out for no end. A time already past is refused |

## Related

- [Give someone access to a secret](/secrets-store/guides/give-someone-access/): the task, start to finish.
- [Share secrets with a team](/secrets-store/guides/team-secrets/): when a group's vault fits better than grants.
- [One-time shares](/secrets-store/sharing/one-time-shares/): send one version that can be read once.
- [Who can read a secret](/secrets-store/guides/who-can-read-a-secret/): every way a secret can be reached.
