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

# Key rotation

> Rotate the vault key pair with tenet-pii-enterprise rotate-key, then roll the services so new writes use it. Old sessions keep restoring; old key pairs are never deleted. Plus how this differs from KMS key rotation.

**Vault key rotation**: `tenet-pii-enterprise rotate-key` creates a new vault key pair, wraps its secret halves under your CMK and makes it current → you roll out both services → new writes use the new pair, and everything written under older keys still restores, because every ciphertext records the id of the key that wrote it.

## When to use it

On your key-management schedule, after suspected exposure of a running host, or when your policy requires a new vault key per period. Two different things rotate:

| | Vault key pair | Your KMS CMK |
| - | - | - |
| What | A 32-byte lookup key (keys the dedupe digests and the audit `session_ref`) and an X25519 key pair whose public half seals each stored value | The KMS key that wraps the lookup and private keys |
| How | `rotate-key`, then a rollout | `aws kms enable-key-rotation --key-id <cmk>` (KMS-managed) |
| Effect on old data | Old key pairs stay in the keyring and keep restoring | Transparent — KMS keeps old backing key material |
| Downtime | None; one rolling restart | None |

## How rotate-key works

1. It generates the key pair in process and wraps the lookup key and the private key with `kms:Encrypt` under `TENET_KMS_KEY_ARN`, each under its own encryption context (`tenet:purpose` = `pii-vault-lookup-key` or `pii-vault-private-key`, plus `tenet:key_id`, `tenet:deployment` and `tenet:public_key`). It writes both wrapped blobs and the public key to the keyring, in the vault's metadata table, under a new id like `k20261106T091500Z-1a2b3c4d`.
2. Only then does it move the `current` pointer. Old key pairs are never deleted.
3. It prints `new current vault key pair: <key id>` and exits `0`. On any KMS or vault error it exits `2` with one line, `tenet-pii-enterprise: error: <reason>`, carrying the AWS error code — never key material.

It runs as the **detect** identity, with the detect grants: it needs `kms:Encrypt` for the two key purposes and never unwraps a private key, which the detect role is denied. It needs the detect service's environment: `TENET_KMS_KEY_ARN`, `TENET_VAULT_URL`, `TENET_DEPLOYMENT_ID`, `PGUSER`, `PGPASSWORD`, `AWS_REGION`.

**Running replicas don't pick up the new key.** Each process pins its current key at launch. The new pair takes effect for a replica when it restarts — so finish with a rollout of both services.

Rotate the vault key pair and roll both services, with checks before and after.

Rotate the vault key pair of my Tenet PII Engine (Enterprise) deployment, following [https://docs.tenetlabs.com/pii-engine/operate/key-rotation](https://docs.tenetlabs.com/pii-engine/operate/key-rotation.md).

Ask me for: platform (EKS namespace and Helm release, or ECS cluster/stack name and NamePrefix), Region, whether this is production, a time window (the rollout briefly reduces detect capacity), the detect and rehydrate endpoints, and a host or workload that runs as a role allowed to call both (detect and rehydrate invoker lists) with the tenet-pii-lattice client installed.

1. Before: confirm both services are ready (ECS: running = desired and every Lattice target HEALTHY; EKS: `kubectl exec deploy/<release>-tenet-pii-enterprise-detect -- tenet-pii-enterprise healthcheck --ready` exits 0, same for rehydrate). Create one test session with `python -m tenet_pii_lattice <detect endpoint> deidentify` on synthetic text and keep its text and session\_id.
2. Run rotate-key once as the detect identity, exactly as shown on the page for my platform (EKS: a one-off Job built from the detect Deployment's pod spec with args \["rotate-key"] and different labels; ECS: `aws ecs run-task` with the detect task definition and a container command override of \["rotate-key"]). STOP and ask for my approval before running it in production.
3. Confirm the job/task exited 0 and its log contains "new current vault key pair: k…". If it exited 2, show me the `tenet-pii-enterprise: error:` line and stop.
4. Roll out both services (EKS: `kubectl rollout restart` detect then rehydrate and wait for `rollout status`; ECS: `aws ecs update-service --force-new-deployment` for detect then rehydrate and wait for services-stable). Ask before doing this in production.
5. After: both services ready again; `python -m tenet_pii_lattice <rehydrate endpoint> reidentify <session_id>` with the step-1 text still restores it; a new deidentify → reidentify round trip works.
   Report the new key id and the results. Never print the original text of a real document.

## Run it

> [tab] EKS
>
> A one-off Job from the detect Deployment's own pod spec — same image, ServiceAccount (IRSA role) and environment — with different labels so no Lattice route or Service ever selects it:
>
> ```bash theme={null}
> NS=tenet-pii; DEPLOY=tenet-tenet-pii-enterprise-detect
> kubectl -n "$NS" get deploy "$DEPLOY" -o json | jq '{
>   apiVersion: "batch/v1", kind: "Job",
>   metadata: {name: "tenet-rotate-key-\(now | floor)"},
>   spec: {backoffLimit: 0, ttlSecondsAfterFinished: 3600, template: {
>     metadata: {labels: {"app.kubernetes.io/name": "tenet-pii-enterprise", "app.kubernetes.io/component": "rotate-key"}},
>     spec: (.spec.template.spec
>       | .restartPolicy = "Never"
>       | del(.topologySpreadConstraints)
>       | .containers[0].args = ["rotate-key"]
>       | del(.containers[0].startupProbe, .containers[0].livenessProbe, .containers[0].readinessProbe)
>       | .containers[0].resources = {requests: {cpu: "250m", memory: "512Mi"}, limits: {cpu: "1", memory: "2Gi"}})}}}' \
>   | kubectl apply -f -
> kubectl -n "$NS" logs -f job/$(kubectl -n "$NS" get jobs -o name | grep rotate-key | tail -1 | cut -d/ -f2)
> # → new current vault key pair: k20261106T091500Z-1a2b3c4d
>
> kubectl -n "$NS" rollout restart deploy/tenet-tenet-pii-enterprise-detect
> kubectl -n "$NS" rollout status  deploy/tenet-tenet-pii-enterprise-detect
> kubectl -n "$NS" rollout restart deploy/tenet-tenet-pii-enterprise-rehydrate
> kubectl -n "$NS" rollout status  deploy/tenet-tenet-pii-enterprise-rehydrate
> ```
>
> The image's entrypoint is `tenet-pii-enterprise`, so `args: ["rotate-key"]` runs the subcommand. The Job pod isn't selected by the chart's NetworkPolicies. If your cluster has a default-deny egress policy, allow it DNS, the AWS endpoint CIDRs on 443 and the database.

> [tab] ECS
>
> A one-off task from the detect task definition with the command overridden. It runs as the detect task role with the detect secrets injected.
>
> ```bash theme={null}
> CLUSTER=$(aws cloudformation describe-stacks --stack-name tenet-pii \
>   --query "Stacks[0].Outputs[?OutputKey=='ClusterName'].OutputValue" --output text)
> SG=$(aws ecs describe-services --cluster "$CLUSTER" --services detect \
>   --query 'services[0].networkConfiguration.awsvpcConfiguration.securityGroups' --output text | tr '\t' ',')
> SUBNETS=$(aws ecs describe-services --cluster "$CLUSTER" --services detect \
>   --query 'services[0].networkConfiguration.awsvpcConfiguration.subnets' --output text | tr '\t' ',')
> CP=$(aws ecs describe-services --cluster "$CLUSTER" --services detect \
>   --query 'services[0].capacityProviderStrategy[0].capacityProvider' --output text)
>
> TASK=$(aws ecs run-task --cluster "$CLUSTER" --task-definition tenet-pii-detect \
>   --capacity-provider-strategy capacityProvider="$CP",weight=1 \
>   --network-configuration "awsvpcConfiguration={subnets=[$SUBNETS],securityGroups=[$SG],assignPublicIp=DISABLED}" \
>   --overrides '{"containerOverrides":[{"name":"detect","command":["rotate-key"],"cpu":512,"memoryReservation":512,"memory":2048}]}' \
>   --query 'tasks[0].taskArn' --output text)
> aws ecs wait tasks-stopped --cluster "$CLUSTER" --tasks "$TASK"
> aws ecs describe-tasks --cluster "$CLUSTER" --tasks "$TASK" --query 'tasks[0].containers[0].exitCode'   # → 0
> # The key id is in the /<NamePrefix>/detect log group.
>
> aws ecs update-service --cluster "$CLUSTER" --service detect --force-new-deployment
> aws ecs wait services-stable --cluster "$CLUSTER" --services detect
> aws ecs update-service --cluster "$CLUSTER" --service rehydrate --force-new-deployment
> aws ecs wait services-stable --cluster "$CLUSTER" --services rehydrate
> ```
>
> The task definition family is `<NamePrefix>-detect` (`tenet-pii-detect` by default) and the container is `detect`. Detect tasks fill their instances, so the capacity provider may launch an instance for the one-off task; it terminates when idle.

## What changes, and what doesn't

* **Old sessions keep restoring.** The rehydrate service unwraps the private key of whichever key pair sealed a value; old pairs stay in the keyring. Nothing is re-encrypted.
* **Recurring values still dedupe.** Lookups try the lookup key of every key the vault has used, so a value first seen under the old pair keeps its placeholder.
* **During the rolling restart**, a replica that started before the rotation may issue a second placeholder for a value that a new replica already saw. Both placeholders restore correctly.
* **`session_ref` changes.** Audit records key `session_ref` under the current lookup key, so one session's records stop correlating across the rotation.

## Gotchas

* **Rehydrate never creates keys.** At startup it waits up to two minutes for the detect service to create the first key pair, then refuses to start: ``vault keyring is empty: start the detect role first or run `tenet-pii-enterprise rotate-key` ``.
* **The deployment id is checked on every rotation.** A `TENET_DEPLOYMENT_ID` that differs from the one stamped in the vault is refused.
* **`kms:Encrypt` denied?** The detect role needs `kms:Encrypt` for `pii-vault-lookup-key` and `pii-vault-private-key` with your deployment id pinned. [Troubleshooting](https://docs.tenetlabs.com/pii-engine/troubleshooting.md#kms-accessdeniedexception-at-startup).
* **Don't disable or schedule deletion of the CMK** to "retire" an old key pair. Every key pair ever used is wrapped by it; losing it loses the vault and the audit objects.

## Links

* [Architecture — KMS design](https://docs.tenetlabs.com/pii-engine/concepts/architecture.md#kms-design)
* [Upgrades](https://docs.tenetlabs.com/pii-engine/operate/upgrades.md)
* [Troubleshooting — KMS AccessDenied](https://docs.tenetlabs.com/pii-engine/troubleshooting.md#kms-accessdeniedexception-at-startup)


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