Keys and secrets
The master key and how to rotate it, what the engine encrypts at rest, API clients, and how to offboard people.
On this page
The engine holds credentials for the systems it connects to. It encrypts them with one master key, ENCRYPTION_MASTER_KEY. This page covers that key, how to rotate it, what else you rotate, how API clients work and what to do when someone leaves.
What is encrypted at rest
The engine encrypts these values with AES-256-GCM before it writes them to PostgreSQL. Each value gets its own salt and IV, and the key comes from ENCRYPTION_MASTER_KEY through PBKDF2 with 100,000 iterations. Every encrypted value records the id of the key that sealed it, a non-secret fingerprint, so the engine knows which key to use.
| Value | Where it lives |
|---|---|
| Adapter credentials (API keys, secrets, private keys, passwords) | adapter profiles |
| Webhook trigger secrets (the HMAC secret and expected header values) | triggers and every saved workflow version |
| Site-to-site VPN pre-shared keys | tunnels |
| Authenticator (TOTP) secrets | engine users |
| Alert targets | alert rules |
| Saved sample messages | workflows |
| Saved test cases (inputs and expected outputs) | workflow test cases |
| Mirth import sessions and their results | migration sessions |
| Log export credentials (Datadog API key, AWS keys, Splunk HEC token) | log export settings |
| Secret workspace variables | workspace variables |
| OIDC client secrets of identity providers added under Settings → Single sign-on | single sign-on providers |
| The Mirth or Rhapsody API login of a parallel run connection | incumbent connections |
Some secrets are never stored in a form the engine can read back:
- Passwords are hashed with scrypt.
- API client keys are stored as a SHA-256 hash.
- Emailed sign-in codes are stored as an HMAC.
- medhook.dev stores only a hash of each signed license.
Message content is encrypted only for workflows that ask for it, see Stored message encryption. Otherwise it is protected by your database disk encryption and by retention. The engine auth secret, DATABASE_URL and REDIS_URL live in your secrets manager, not in the database.
Stored message encryption
A workflow with Encrypt stored messages on (the Mirth channel setting "Encrypt message content") stores its messages encrypted with AES-256-GCM. That covers the received message, every step's output, a JavaScript step's console lines and channel map, retry and replay input, and queued deliveries, including dead letters. An admin turns it on in the workflow's Settings tab, under Data (Encrypt stored messages), or with PUT /api/workflows/{id}/message-encryption. Each change is audited as workflow.message_encryption_changed. Set ENCRYPT_STORED_MESSAGES=true to turn it on for every workflow.
- Key.
MESSAGE_ENCRYPTION_KEY, at least 32 characters (openssl rand -hex 32). When it is unset, the engine usesENCRYPTION_MASTER_KEY. The engine derives the key once at startup, not once per message. - Key id and row. Each stored message records the fingerprint of the key that sealed it and is bound to its own execution and step, so a value copied to another row or column does not decrypt. To change the key, move the old one to
MESSAGE_ENCRYPTION_KEY_PREVIOUS(a comma-separated list) and set the new one. New messages use the new key, and older messages still read. The master keys (ENCRYPTION_MASTER_KEYandENCRYPTION_MASTER_KEY_PREVIOUS) can also decrypt.rotate-master-keydoes not re-encrypt stored messages. Keep an old message key until retention has deleted the last message it sealed. - What still works. The execution detail view, the Messages page with its search and its Needs action and Queues tabs, replay, workflow retries and queued delivery all decrypt the content for people whose role lets them see messages. Search decrypts each row it scans, within the usual scan limit. It does not search in the database.
- What stays readable in the database. Ids, statuses, times, error text, and the few bookkeeping values the engine queries in SQL: the retry parent and schedule, the no-retry reason code, the queue enqueue time and the replay link. Each must look like an id, a time or a code, or it is sealed too. Everything else in the retry, queue and replay markers, including earlier destinations' responses, is encrypted.
- When encryption applies. Turning the setting on applies to messages stored after it, within 30 seconds on every engine replica. A message stored encrypted stays encrypted on every engine replica: turning the setting off only affects new messages. A message whose workflow has been deleted, or that the engine cannot place, is encrypted. Old plaintext rows still read.
- Moving workflows. The setting travels with workflow export and import, promotion between workspaces, marketplace templates and Mirth import and export. An import can turn it on for an existing workflow, never off.
- A missing or wrong key. Lists keep working. A message that cannot be decrypted shows the reason in place of its content, and the engine logs an error that names the key id. Replay refuses it with
UNDECRYPTABLE, and a queued or retried delivery fails with the reason instead of sending anything. The stored ciphertext is never overwritten. Restore the key, then replay.
Escrow the master key
The master key decrypts every stored credential. If you lose it, none of them can be recovered. Each healthcare organization has to re-key its tunnel, and every connection has to be set up again. Terraform refuses to destroy the key, but a single copy in one project is still a single point of failure.
Right after install, have your security team keep an offline copy outside Google Cloud, for example in a sealed envelope or an offline vault held by two named people. Read it from a terminal, never from chat or a ticket.
gcloud secrets versions access latest --secret=medhook-encryption-master-keyRecord where the copy is and who holds it. Repeat after every rotation.
Rotate the master key
Rotate when you suspect exposure, or when your policy says so. The engine reads two keys during a rotation, ENCRYPTION_MASTER_KEY to encrypt and ENCRYPTION_MASTER_KEY_PREVIOUS to decrypt, so the engine keeps serving throughout.
-
Keep the current key as the previous key, and generate a new one.
bashgcloud secrets versions access latest --secret=medhook-encryption-master-key \ | gcloud secrets versions add medhook-encryption-master-key-previous --data-file=- openssl rand -hex 32 | tr -d '\n' | gcloud secrets versions add medhook-encryption-master-key --data-file=- -
Deploy. See Install. The engine now encrypts with the new key and still decrypts with the old one.
-
Re-encrypt everything stored under the old key, then check. Saved test cases are re-encrypted from
1.0.0-rc.16. On an older engine, keepENCRYPTION_MASTER_KEY_PREVIOUSset, or save the cases again, before you disable the previous key.bashkubectl exec -n medhook deploy/medhook-engine -- rotate-master-key kubectl exec -n medhook deploy/medhook-engine -- rotate-master-key --check -
Put the new key in escrow. Then disable the previous key's secret version and deploy again.
-
Keep the old key in escrow until the last database backup taken before the rotation has expired. Those backups need it.
On Docker Compose, set the two variables in .env, restart the engine and run the same commands with docker compose exec engine.
How rotate-master-key works
It decrypts every value in memory first. If any value cannot be opened by a configured key, it stops before it writes anything. The writes and the audit event then go in one database transaction. Each write only lands if the row still holds the value the rotation read. If someone changes a row in the meantime, such as a credential edit, the transaction rolls back and the rotation plans again from fresh reads, so a live change is never reverted. After 3 such conflicts it stops, changes nothing and asks you to run it again.
The rotation writes an encryption.key_rotated audit event. It carries key ids and counts only, never key material.
Run with no flag, it does everything. The flags narrow it or make it read-only.
| Flag | What it does |
|---|---|
--check | Reports only and writes nothing. Prints a count per category. |
--seal-only | Handles only webhook trigger secrets: seals plaintext ones and moves old-key ones to the current key. Quick, and never reads executions. Every deploy runs it. |
--headers-only | Masks credential headers stored with older webhook executions and node results. It reads every execution, so run it by hand, off-peak, once after upgrading. |
--batch-size N | Rows per batch for the header masking. The default is 500. |
--throttle-ms N | A pause after each header batch. The default is 0. |
--seal-only and --headers-only start with 1.0.0-rc.14. --seal-only and --headers-only cannot be combined. --batch-size and --throttle-ms apply to header masking only. The header masking prints progress after every batch and is safe to stop and run again. It records an execution.headers_masked audit event with counts only.
--check follows the same flags and exits 0 only when every value uses the current key, no webhook trigger secret is plaintext and no stored headers need masking. Otherwise it prints counts such as these and exits 3.
Current key id: 3f9c0a1b2c3d4e5f
workflowTrigger.config: 4 current, 1 on an older key, 2 plaintext
execution.triggerData: 40 with unmasked credential headersExit codes
| Code | Meaning |
|---|---|
0 | Done. With --check, nothing is left to do. |
1 | Failed. The key and secret transaction changed nothing. Header masking keeps the rows it finished, so run it again to resume. |
2 | Bad arguments, or DATABASE_URL or ENCRYPTION_MASTER_KEY is missing, or a key is malformed. |
3 | --check only: some values use an older key, some trigger secrets are plaintext, or some stored headers need masking. |
A freshly upgraded engine returns 3 from --check until --seal-only and --headers-only have run.
Other secrets
Rotate each of these without touching the master key.
| Secret | How to rotate |
|---|---|
| License key | Add a new version of medhook-license-key, then deploy. |
| Engine auth secret | Add a new version of medhook-engine-auth-secret (openssl rand -hex 64), then deploy. This signs everyone out and ends every bearer token. API client keys keep working. |
| Database password | Set it with gcloud sql users set-password, add the same value as a new version of medhook-postgres-password, and deploy straight away. |
| Google sign-in secret | Create a new client secret in your OAuth client, add it as a new version of medhook-engine-google-client-secret, deploy, then delete the old one. |
| Email API key | Create a new key with your email provider, add it as a new version of medhook-engine-email-api-key, deploy, then revoke the old one. |
| API client key | create-engine-api-client NAME --rotate. See below. |
| Tunnel pre-shared key | Rotate it from the tunnel's row under Tunnels. |
Engine API clients
Scripts, CI jobs and other systems call the engine as named API clients, never with the engine auth secret. Create one client per caller, with the least role it needs.
kubectl exec -n medhook deploy/medhook-engine -- create-engine-api-client nightly-sync --role operator --expires 90dIt prints the client's key once. Store it in the caller's secrets manager. The engine keeps only a SHA-256 hash. The caller exchanges the key for a bearer token that lasts 2 hours, or until the client expires if that comes first. See Authentication.
| Command | What it does |
|---|---|
create-engine-api-client NAME --role ROLE --expires 90d | Creates a client with viewer, operator or admin access and an expiry. |
create-engine-api-client NAME --rotate | Prints a new key. The old key and every token minted from it stop working within seconds. |
create-engine-api-client NAME --revoke | Stops the key and every token minted from it within seconds. No one else is affected. |
create-engine-api-client --list | Shows each client's role, status, expiry and last use. It never shows a key. |
- Roles. A client gets what a signed-in person with the same role gets, except for workspaces: a client created with
--workspace <slug>works only in that workspace (another is 403WORKSPACE_FORBIDDEN), and one created without it reaches every workspace at its role. Aviewerclient never sees message content.viewerclients read,operatorclients change workflows, and onlyadminclients reach admin routes. No API client can approve a deploy or skip the deploy checklist: approval needs a signed-in person other than the requester. - Audit. Every change a client makes is recorded with the actor
api-client:NAME. Creating, rotating and revoking a client are recorded asapi_client.*events. - Leaked key. Revoke first, then rotate when the caller is ready.
Root-secret tokens, which send the engine auth secret instead of an API key, are anonymous admin access. The engine refuses them in production unless ENGINE_ROOT_TOKEN_ENABLED=true. Leave that off.
Offboard an engine user
Disable the account:
kubectl exec -n medhook deploy/medhook-engine -- create-engine-user person@example.test --disableIn one transaction this stops them signing in by password, Google or single sign-on, ends every open session, and invalidates open sign-in codes and set-password links. It records operator.disabled. The account and its audit history stay. Running the command again without --enable never re-admits them. --enable does, and records operator.enabled.
Then close what the account could not cover:
- Remove them from your Google Workspace domain or your identity provider, if they signed in with Google or single sign-on.
- Remove their workspace memberships under Settings → Users & roles → Members, so a later re-enable starts with no access.
- Revoke or rotate any API client whose key they held.
- If they were an admin who could reveal a tunnel pre-shared key, rotate the keys of the tunnels they could see. Reveals are audited.
- If they knew the engine auth secret or the master key, rotate those.
The engine marks an account created by a first Google sign-in with ENGINE_DEFAULT_ROLE, operator by default. Single sign-on accounts get their role from the identity provider's groups instead (see Single sign-on). Set it to viewer if new accounts should start read-only. They belong to no workspace until an admin adds them under Settings → Users & roles → Members.
Checklist
- An offline copy of the master key is in escrow, and you know who holds it.
- Each integration has its own API client with an expiry and the least role.
- No one uses root-secret tokens.
- After any rotation,
rotate-master-key --checkexits0. - Offboarding includes the account, its API client keys and any secrets it could read.