Credential Encryption
All credentials in Crewship are encrypted at rest using AES-256-GCM. The implementation ininternal/encryption/encryption.go is designed for cross-compatibility between Go and TypeScript.
Encryption Scheme
Wire Format
Encrypted values are stored as versioned base64 strings:Byte Layout
IV || AuthTag || Ciphertext), not the standard Go GCM output format (Ciphertext || AuthTag). The reordering is necessary for compatibility with the TypeScript implementation in lib/encryption.ts.
Encryption Flow
Decryption Flow
Key Versioning
Envelopes are prefixed with a version identifier (e.g.,v1:) to support key rotation. The version NEW envelopes are minted with defaults to v1 and is switched with the CREWSHIP_ENCRYPTION_KEY_VERSION environment variable during a rotation. Decryption always honours the per-envelope prefix, so any generation whose key is still in the environment remains readable.
Key Resolution
On decrypt, if the version-specific env var is not set, resolution falls back to
ENCRYPTION_KEY (legacy compatibility). On encrypt, the resolution is strict: minting vN envelopes requires ENCRYPTION_KEY_VN to be present — a silent fallback would stamp envelopes with a version whose key they were not encrypted with. crewship start validates the active version’s key at boot and refuses to start on a misconfigured rotation.
Key Source Visibility
On first bootcrewship start auto-generates ENCRYPTION_KEY and persists it to <dataDir>/secrets.env (mode 0600) — which on default installs is the same volume as the SQLite database. A copied disk or volume backup therefore carries both the ciphertext and the key. Crewship surfaces this:
- A startup
WARNlog fires on every boot while the key is auto-generated/colocated. GET /api/v1/admin/health(CLI:crewship system health) reportsencryption_key_source:"external"(operator-injected env var),"generated"(auto-bootstrapped, colocated), or"unknown".
ENCRYPTION_KEY from an external secret store (systemd EnvironmentFile=, Kubernetes secret, Vault) and remove the entry from secrets.env.
No key configured (fail-closed)
Secret writes fail closed. If no usable encryption key resolves for the active key version, Crewship refuses to store the secret rather than silently writing it to the database in plaintext. This closes a compatibility shim that shipped with the webhook-secret work (#1072/#1029) and was never given a sunset: before this change, a deployment with noENCRYPTION_KEY stored webhook signing secrets as plaintext and the only trace was a log line nobody was reading.
What an operator sees
The write fails and the API returns500. For the webhook-secret endpoints (agent webhook-secret rotate, pipeline-webhook create) the response body itself is actionable — it names ENCRYPTION_KEY and the opt-out flag — and the server log carries the full reason, naming the env var that is missing or malformed:
How to fix it
Generate a key and put it in the environment, then restart:crewship start bootstraps ENCRYPTION_KEY on first boot (persisting it to <dataDir>/secrets.env) and then calls VerifyCurrentKey() — it refuses to start if the active key version’s env var is missing or malformed. So a key-less deployment is told at boot, not at first secret write. The fail-closed encrypt path is the backstop for embedded/alternate entry points that skip that bootstrap, and for a mid-rotation CREWSHIP_ENCRYPTION_KEY_VERSION pointing at a key that has not been supplied yet.
Reading already-stored plaintext
Fail-closed applies to writes only. Values already stored in plaintext by an older build (or under the opt-out) continue to be read back unchanged, with or without a key present and regardless of the flag — the read path discriminates on thevN: envelope prefix. Turning fail-closed on therefore cannot brick an existing install’s stored secrets.
The migration backfill waits for a key
Upgrading to the release that introduced webhook-secret encryption runs a one-time migration (v140) that encrypts any webhook/signing secrets previously stored in plaintext. If no usableENCRYPTION_KEY is configured when that migration runs, it skips instead of failing — a migration must never break boot — and logs a WARN that the secrets are left plaintext at rest. The backfill is effectively deferred: the migration does not run again later, so after configuring a key, run:
Rotating the Master Key
crewship admin reencrypt re-encrypts secret envelopes to the current version.
It does not re-sign journal or instance-audit history. Keep the original
ENCRYPTION_KEY (v1) string byte for byte even with no v1 envelopes: those
audit MACs still depend on this independent root.
Runbook (rotating from v1 to v2):
<data-dir>/audit-keyring.json) from the old master-key version to the new
one, with the same signing keys; the boot log fails loudly if the old key is
missing. So keep every earlier ENCRYPTION_KEY_V<N> configured until a server
has started once on the new version. Changing the ENCRYPTION_KEY value
itself in place (instead of adding a version) makes the keyring — and every
v1 envelope and legacy audit MAC — unreadable; never do that. An operator-managed
keyring at CREWSHIP_AUDIT_KEYRING_PATH outside the data directory is not
re-wrapped; it is yours to protect.
The operation is idempotent — envelopes already at the current version are
skipped, so an interrupted run can be repeated. Undecryptable values are
counted under failed and left untouched. The CLI exits non-zero so failed
conversion is not mistaken for completed conversion. Even Failed: 0 does
not authorize retiring the v1 journal/audit root.
The walk covers every envelope-bearing column: credentials.encrypted_value, credentials.encrypted_refresh_token, credentials.oauth_client_secret_enc, credentials.oauth_refresh_token_enc, credential_rotations.old_value, notification_channels.secret_enc, composio_settings.encrypted_api_key, oauth_states.code_verifier, and the two webhook-secret columns agents.webhook_secret and pipeline_webhooks.signing_secret.
Webhook secrets are encrypted at rest, fail-closed.
agents.webhook_secret and pipeline_webhooks.signing_secret are encrypted whenever a usable key resolves; with no key the write is rejected rather than downgraded to plaintext (see No key configured below). Rows written in plaintext by older builds keep working — they are read back as-is. During master-key rotation such a bare (non-enveloped) webhook value is reported as skipped, not failed, so envelope-conversion counts stay accurate; this does not authorize removing the v1 audit root. Migration v140 backfills existing plaintext webhook secrets to envelopes when a key is present.escalations.resolution is not in the walk. It used to be, for CREDENTIAL escalations, when a human’s typed answer was encrypted into it. Since the answer to a credential ask is a grant into the vault rather than a value (see Credentials → What a CREDENTIAL escalation never carries), that column only ever holds plaintext — the historical marker [credential submitted], or the prose an expiry, cancel or auto-resolve leaves. Walking it could only ever produce failed rows, which made the failed=0 criterion unreachable on any database that had answered a credential ask; it was removed from the inventory in #2408.beforeunload prompt) while any draft’s newest edit is not stored yet. A
tab that is killed without that prompt (for example by a mobile browser)
can lose the last edit.
Instance recovery kits retain a configured v1 root even with no v1 envelopes.
Use the kit’s exact original key string. Workspace and crew backups exclude
the instance audit trail; normal and fork restores retain the destination
instance’s own trail and key. A missing recovery key, DB-only copy or tampered
tail is not repaired by automatically re-signing history. Recover the trusted
original key before audit-protected administration can continue.
Legacy Format Support
The decryptor accepts both:- Versioned:
v1:base64data(current format) - Legacy:
base64data(no version prefix, treated as v1)
Cross-Language Compatibility
The encryption format is shared between:- Go:
internal/encryption/encryption.go - TypeScript:
lib/encryption.ts
- IV size: 16 bytes
- Byte order: IV || AuthTag || Ciphertext
- Base64 encoding: standard or raw
- Key derivation: direct hex decode (no KDF)
Security Properties
Generating an Encryption Key
The encryption key must be exactly 64 hex characters (32 bytes). A shorter or longer key will cause
hex.DecodeString to fail or produce the wrong key length for AES-256.