# Get started with Secrets Store

> Turn the secrets store on for your realm, store a secret in your own vault, and give a colleague access to it, with every call.

In this walkthrough you turn the store on for the realm `foobar`, then `maya` keeps the staging database password in
her own vault and gives her colleague `ben` read access to it for two weeks. At the end, `ben` reads the password
under his own sign-in, and nothing else in `maya`'s vault is open to him.

## Before you begin

- You are a realm administrator of `foobar`: a member of a group holding `RealmOwner`. The realm has the users `maya`
  and `ben`, who need no privilege of their own.
- Everything is done through the API; there are no screens yet.
- Each step says who makes the call. Each person signs in and keeps their own session in `$CVYUH_SESSION`:
  [Call the API](/develop/api/) shows how.
- Every Secrets Store call goes to `https://foobar.cvyuh.com/api/v0/vault/managed/v1`.

## Store a secret and share it

<Steps>

1. **Turn the store on.** As a realm administrator:

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

   The realm writes its [Realm settings](/secrets-store/realm-settings/) at their defaults and answers `201`
   (`200` if the store was already on):

   ```json
   { "data": { "enabled": true } }
   ```

   Until this call, every other call to the store answers `409`. Anyone who isn't a realm administrator is refused
   (`403`).

2. **Ben opens the store once.** Signed in as `ben`, read the store's settings:

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

   ```json
   {
     "data": { "max_versions": 10, "cas_required": false, "delete_version_after": "0s" }
   }
   ```

   A user's own key is made the first time they use the store. A grant is sealed to the grantee's key, so `ben` needs
   one before `maya` can grant him anything. Skip this, and step 4 answers `404`.

3. **Maya stores the password.** Signed in as `maya`, write it into her own vault (`me`) under a path she chooses:

   ```sh
   curl https://foobar.cvyuh.com/api/v0/vault/managed/v1/me/data/db/staging \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "data": { "user": "app", "password": "<password>" } }'
   ```

   The realm answers `200` with version 1's metadata:

   ```json
   {
     "data": {
       "created_time": "<RFC 3339 time>",
       "custom_metadata": null,
       "deletion_time": "",
       "destroyed": false,
       "version": 1
     }
   }
   ```

4. **Maya gives Ben access.** A `use` grant lets him read the current value and nothing else; `validTo` ends it by
   itself:

   ```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": "use", "validTo": "<RFC 3339 time>" }'
   ```

   The realm seals a copy of the secret to `ben`'s key and answers `201`:

   ```json
   {
     "data": {
       "edgeId": "<grant id>",
       "user": "ben",
       "access": "use",
       "validTo": "<RFC 3339 time>",
       "grantedBy": "maya",
       "live": true
     }
   }
   ```

5. **Ben finds it and reads it.** Signed in as `ben`, list what has been shared with him:

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

   ```json
   { "data": { "keys": [ { "vault": "user/maya", "path": "db/staging", "access": "use" } ] } }
   ```

   Then read the secret in `maya`'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"
   ```

   ```json
   {
     "data": {
       "data": { "user": "app", "password": "<password>" },
       "metadata": { "version": 1, "created_time": "<RFC 3339 time>", "…": "…" }
     }
   }
   ```

</Steps>

## Check it worked

- As `ben`, read any other path in `maya`'s vault: the realm answers `404`, as if it weren't there.
- As `maya`, list the grants on the secret with
  `curl https://foobar.cvyuh.com/api/v0/vault/managed/v1/me/grants/db/staging -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"`:
  the grant to `ben` shows `"live": true` until its `validTo`.

<Aside type="tip" title="Take it back early">
  `maya` ends the grant before its time with
  `curl -X DELETE https://foobar.cvyuh.com/api/v0/vault/managed/v1/me/grants/db/staging/<grant id> -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"`.
  The realm answers `204`, and `ben`'s copy is gone at once.
</Aside>

## If something goes wrong

| Symptom | Cause | Fix |
|---|---|---|
| Any call answers `409` | The store isn't on | Step 1, as a realm administrator |
| Step 1 answers `403` | The caller isn't a realm administrator | Sign in as a member of a group holding `RealmOwner` |
| Step 4 answers `404` | `ben` hasn't used the store yet | Step 2, signed in as `ben`, then step 4 again |
| `ben`'s read answers `404` | The grant has ended, or the path is wrong | List the grants as `maya`; check `"live"` and the path |

Every error is in [Secrets Store API](/secrets-store/api/#errors).

## Next steps

<CardGrid>
  <LinkCard title="Share secrets with a team" description="A group's vault, where access follows the group's membership." href="/secrets-store/guides/team-secrets/" />
  <LinkCard title="Set the realm's defaults" description="History, check-and-set, share lifetimes and the emergency delay." href="/secrets-store/guides/set-the-realms-defaults/" />
  <LinkCard title="Who can read a secret" description="Every way a secret can be reached, and what no one can do." href="/secrets-store/guides/who-can-read-a-secret/" />
</CardGrid>
