# Change a secret safely

> When several users or tools write one secret, make each write name the version it replaces, so no change is silently lost.

The `engineering` team shares a deploy key, and a scheduled job rotates it too. If two of them write at once, one
change can wipe out the other unseen. With [check-and-set](/secrets-store/secrets/check-and-set/), each write names the
version it was based on, and a write based on an old version fails instead.

**Before you start:** you are a member of `engineering`, signed in with your session in `$CVYUH_SESSION`
([Call the API](/develop/api/) shows how).

## Write with check-and-set

<Steps>

1. Read the secret and note its version, shown as `data.metadata.version`. Say it is `3`:

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

2. Write, naming that version:

   ```sh
   curl https://foobar.cvyuh.com/api/v0/vault/managed/v1/group/engineering/data/deploy/key \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "options": { "cas": 3 }, "data": { "key": "<new deploy key>" } }'
   ```

3. If no one wrote in between, your write is version 4. If someone did, the realm answers `400` and says what the
   latest version is. Read again, decide, and write again with the new version.

</Steps>

To create a path only if it doesn't exist yet, write with `"cas": 0`.

## Make it required

So no one can forget, require check-and-set for the path:

```sh
curl https://foobar.cvyuh.com/api/v0/vault/managed/v1/group/engineering/metadata/deploy/key \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "Content-Type: application/json" \
  -d '{ "cas_required": true }'
```

For the whole realm, a realm administrator sets `managed.casRequired` to `true`: see
[Set the realm's defaults](/secrets-store/guides/set-the-realms-defaults/). A write without `cas` then answers `400`.

## Check it worked

A `GET` on `https://foobar.cvyuh.com/api/v0/vault/managed/v1/group/engineering/metadata/deploy/key` shows
`cas_required: true` and the `current_version` your write made.

## Undo it

Lift the path's requirement by sending `{ "cas_required": false }` to the same address. A path can't lift the realm's.

## Related

- [Check-and-set](/secrets-store/secrets/check-and-set/): how it works.
- [Versions](/secrets-store/secrets/versions/): what each write makes.
