> ## Documentation Index
> Fetch the complete documentation index at: https://docs.certgov.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Keep Private Keys in Your Vault

> Store certificate private keys in your own HashiCorp Vault or OpenBao instead of CertGov, and deliver certificates with an agent that reads the key from your vault.

## Overview

With **key custody**, CertGov writes each new certificate's private key to your HashiCorp Vault or OpenBao (KV version 2) at issuance and deletes its own copy. When a certificate is delivered, the [connector agent](https://docs.certgov.app/guides/connector-agent.md) on your network reads the key from your vault itself. CertGov never holds a login that can read your keys.

```
Issuance:  CertGov ──(write-only token)──▶ your vault           CertGov deletes its copy
Delivery:  CertGov ──(cert + key location)──▶ agent ──(read token)──▶ your vault
                                              agent ──▶ nginx / Apache / …
```

| Who | Holds | Can |
| - | - | - |
| CertGov | A write-only token on the key path | Write new keys. Cannot read them back |
| Your agent | A read-only token, on the agent's host | Read keys to install certificates |
| You | Your vault, its policies and tokens | Revoke either token at any time |

Key custody is set per [trust profile](https://docs.certgov.app/guides/trust-profiles.md). On **Trust Profiles**, the **Keys** column shows where each profile's keys live: *CertGov (encrypted)*, or your key store's name.

> [!NOTE]
>
> Using Azure Key Vault instead? See [Azure Key Vault](https://docs.certgov.app/guides/azure-key-vault.md), Path B. Delivering certificates whose key is in Azure Key Vault isn't supported yet.

## Requirements

* HashiCorp Vault or OpenBao with a **KV version 2** secrets engine (the examples use the default `secret/` mount)
* A [connector agent](https://docs.certgov.app/guides/connector-agent.md), **v0.1.11 or later**, on a host that can reach your vault and your servers
* Your vault reachable from the agent. In **Agent** access mode (recommended), CertGov never connects to your vault directly

## 1. Create the vault policies

CertGov writes keys under `<mount>/<path prefix>/<certificate ID>`, by default `secret/certgov/keys/<id>`.

**Write-only policy, for CertGov:**

```hcl theme={null}
# certgov-key-write.hcl
path "secret/data/certgov/keys/*" {
  capabilities = ["create", "update"]
}

# The key store's connection test writes and deletes this one entry
path "secret/data/certgov/_probe" {
  capabilities = ["create", "update", "delete"]
}
```

**Read-only policy, for your agent:**

```hcl theme={null}
# certgov-agent-key-read.hcl
path "secret/data/certgov/keys/*" {
  capabilities = ["read"]
}
```

```bash theme={null}
vault policy write certgov-key-write certgov-key-write.hcl
vault policy write certgov-agent-key-read certgov-agent-key-read.hcl
```

The write policy lets CertGov store keys without being able to read them. If you use a different mount or path prefix, change the paths to match.

## 2. Add the key store host

In CertGov, go to **Hosts → Add Host** and set:

| Field | Value |
| - | - |
| Host type | **Key Store** |
| Store Type | HashiCorp Vault (also for OpenBao) |
| Access Mode | **Agent** (recommended). The agent runs the write; your vault doesn't have to be reachable from CertGov |
| Vault Address | e.g. `https://vault.internal:8200` |
| KV Mount | `secret` |
| KV Path Prefix | `certgov/keys` |
| Namespace | Vault Enterprise / HCP only |
| Token | A token with the **write-only** policy |

Create the token:

```bash theme={null}
vault token create -policy=certgov-key-write -period=768h -orphan -display-name=certgov-key-write
```

## 3. Turn on key custody in the trust profile

Edit the trust profile, and under **Advanced → Key Custody** choose the host as the **Key Store Host**. From then on, every certificate issued under the profile has its key written to your vault, and CertGov deletes its copy.

Certificates issued before you turned it on keep their keys in CertGov. To stop, set **Key Store Host** back to *none*; new certificates then keep their keys in CertGov again.

## 4. Give the agent its read token

On the agent's host, create a read-only token and store it in a file only the agent's service user can read (`certgov` if you followed the [agent guide](https://docs.certgov.app/guides/connector-agent.md); use `root` if the agent runs as root):

```bash theme={null}
vault token create -policy=certgov-agent-key-read -period=768h -orphan \
  -display-name=certgov-agent-$(hostname) -field=token \
  | sudo install -m 600 -o certgov -g certgov /dev/stdin /etc/certgov-agent/vault-token
```

Add a `vault` section to `/etc/certgov-agent/agent.yaml`:

```yaml theme={null}
vault:
  addr: https://vault.internal:8200
  token_file: /etc/certgov-agent/vault-token
  # namespace: admin/team-a          # Vault Enterprise / HCP only
  # ca_cert_file: /etc/certgov-agent/vault-ca.pem   # if your vault uses a private CA
```

Then restart the agent:

```bash theme={null}
sudo systemctl restart certgov-agent
```

The agent can also take the token from the `VAULT_TOKEN` environment variable instead of `token_file`.

## 5. Deliver

Use **Deliver** or **Test** on the service's targets as usual. For a certificate whose key is in your vault, CertGov sends the agent the certificate and the key's location, not the key. The agent reads the key, then runs the delivery template with it, exactly as if CertGov had sent the key. Templates need no changes.

The agent that runs the job does the read, including for SSH targets: it reads the key, then installs it on the target over SSH. The read token only needs to be on the agent's host, not on every server.

## Keeping the agent's token valid

The read token belongs to you: you create it, choose its policy, and can revoke it at any time. A periodic token (`-period=768h`) stays valid as long as it's renewed at least once per period.

* **Renew it on a schedule**, for example a daily `vault token renew` using the token file, or
* **Let Vault Agent manage it**: point Vault Agent's token sink at `/etc/certgov-agent/vault-token`. The connector agent rereads the file for every delivery, so a rotated token is picked up without a restart.

If the token expires, deliveries of vault-held keys fail with a 403 from your vault until you replace the file.

## Troubleshooting

| Message | Fix |
| - | - |
| *agent … (v0.1.10) can't read it from there; update it to v0.1.11 or later* | Update the connector agent |
| *no vault login on this agent* | Add `vault.token_file` to `agent.yaml` (or set `VAULT_TOKEN`) and restart the agent |
| *vault refused this agent's token … (HTTP 403)* | The token expired, was revoked, or its policy doesn't cover the key path |
| *no key in vault at … (HTTP 404)* | The key isn't at that path. Check the key store's mount and path prefix |
| *deliver it with a certgov-agent that can read the vault (not a direct API target)* | Direct API targets run inside CertGov, which can't read your vault. Deliver through an agent |
| *the move didn't finish* | The key wasn't confirmed written to your vault at issuance. Request a new certificate |

## Security notes

* CertGov stores the key store's write token. With the write-only policy above, that token can't read keys back.
* At issuance the key is saved in CertGov encrypted (AES-256-GCM) until the write to your vault succeeds, then deleted. If the write fails, CertGov keeps the encrypted key and the certificate works as one without key custody.
* Downloading the private key from **Inventory** isn't available for these certificates. Read it from your vault instead.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.