Repository navigation
fix(keyvault): persist the platform secret, and prove arm64 natively in CI #173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
49281d5
2142ec8
acc91ea
8b01f40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,11 +79,31 @@ func main() { | |
| } | ||
| logger.Info("database migrations applied") | ||
|
|
||
| // Open the key vault. devMode allows an ephemeral DEK when no secret is set | ||
| // (handy for local development); production deployments must set | ||
| // CALNODE_ENCRYPTION_KEY or the vault will refuse to start. | ||
| // Resolve the platform secret BEFORE opening the vault. A missing | ||
| // CALNODE_ENCRYPTION_KEY no longer falls back to an ephemeral key chosen by | ||
| // guessing from the BASE_URL scheme: a secret is generated once and persisted | ||
| // beside the database, so a plain `docker run` survives a restart and a | ||
| // deployment behind a TLS-terminating proxy does not silently take the | ||
| // "development" path. An explicit CALNODE_ENCRYPTION_KEY still wins. | ||
| platformSecret, secretSource, secretPath, err := keyvault.ResolvePlatformSecret( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A generated secret is handed straight to Technical details# A generated secret must never be adopted as the legacy DEK
## Affected sites
- `cmd/calnode/main.go:88` — passes the resolved secret (possibly generated) into `Open`.
- `cmd/calnode/mcp.go:48` — same call, same exposure.
- `internal/keyvault/keyvault.go:116` / `migrateLegacy` (`:193`) — hex-decodes any 64-char secret and adopts it as the DEK when the keystore is empty and `*_enc` data exists.
## Evidence
Reproduced against a migrated in-memory DB seeded with `smtp_pass_enc` and no `crypto_keystore` row: `Open(db, <generated 64-hex>, "", false)` succeeds via `migrateLegacy`; a subsequent `Open` with the true legacy key fails with `failed to unwrap DEK — is CALNODE_ENCRYPTION_KEY correct? cipher: message authentication failed`.
## Required outcome
- A secret whose source is `SecretFromGenerated` must not be accepted as the legacy DEK. The same reasoning already applied to an empty key file applies here: a generated replacement silently destroys access to existing ciphertext.
- Fail with an actionable error telling the operator to supply the original `CALNODE_ENCRYPTION_KEY`; because the keystore stays empty, the data remains recoverable then.
- `Open`/`migrateLegacy` needs to know the secret was generated. Both call sites already hold `secretSource`, so pass that intent through instead of re-deriving it from the string. |
||
| cfg.EncryptionKey, db.FilePath(cfg.DatabaseURL)) | ||
| if err != nil { | ||
| logger.Error("keyvault: cannot determine the platform secret", "error", err) | ||
| os.Exit(1) | ||
| } | ||
| switch secretSource { | ||
| case keyvault.SecretFromGenerated: | ||
| logger.Warn("keyvault: CALNODE_ENCRYPTION_KEY is not set; generated a platform secret and persisted it", | ||
| "path", secretPath, | ||
| "note", "back it up together with the database: without it the encrypted columns cannot be read") | ||
| case keyvault.SecretFromFile: | ||
| logger.Info("keyvault: platform secret loaded from file", "path", secretPath) | ||
| } | ||
|
|
||
| // devMode still governs non-secret behaviour elsewhere; the vault no longer | ||
| // uses it to invent a key. | ||
| devMode := !strings.HasPrefix(cfg.BaseURL, "https://") | ||
| vault, err := keyvault.Open(database, cfg.EncryptionKey, cfg.RecoverySecret, devMode) | ||
| vault, err := keyvault.Open(database, platformSecret, cfg.RecoverySecret, devMode) | ||
| if err != nil { | ||
| logger.Error("keyvault: failed to open", "error", err) | ||
| os.Exit(1) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| // Package keyvault: platform-secret resolution. | ||
| // | ||
| // The vault needs one value from outside the database: the platform secret that | ||
| // derives the KEK. Until now that value had to come from the environment, and a | ||
| // missing value was handled by guessing intent from the BASE_URL scheme: | ||
| // | ||
| // BASE_URL http://... -> devMode -> ephemeral DEK, warned, secrets lost on restart | ||
| // BASE_URL https://... -> fatal error, server will not start | ||
| // | ||
| // Both arms are wrong for a plain `docker run`, which is the documented first | ||
| // install. Over http it silently generates an ephemeral DEK, so the SMTP | ||
| // password, Stripe keys and OAuth client secrets stored in the database become | ||
| // unreadable at the next restart. Over https it refuses to boot at all. And | ||
| // because the scheme is the only signal, a production deployment behind a TLS | ||
| // terminating proxy (BASE_URL http, TLS at the edge) takes the silent arm. | ||
| // | ||
| // Persisting a generated secret next to the database removes the guess. The key | ||
| // file lives beside the database it unlocks, which is also where backups already | ||
| // go, so the pair stays together. An explicit CALNODE_ENCRYPTION_KEY still wins, | ||
| // which is what the NS8 module and any externally managed deployment rely on. | ||
|
|
||
| //nolint:revive // file-level rationale. | ||
| package keyvault | ||
|
|
||
| import ( | ||
| "crypto/rand" | ||
| "encoding/hex" | ||
| "errors" | ||
| "fmt" | ||
| "io/fs" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| ) | ||
|
|
||
| // KeyFileName is the persisted platform secret written next to the database. | ||
| // | ||
| // #nosec G101 -- this is a file NAME, not a credential. The secret it names is | ||
| // generated at runtime and never appears in source. | ||
| const KeyFileName = "calnode.key" | ||
|
|
||
| // Where the platform secret came from, for logging and for tests. | ||
| const ( | ||
| // #nosec G101 -- the NAME of an environment variable, not a credential. | ||
| SecretFromEnv = "CALNODE_ENCRYPTION_KEY" | ||
| SecretFromFile = "file" | ||
| SecretFromGenerated = "generated" | ||
| ) | ||
|
|
||
| // ResolvePlatformSecret returns the platform secret that unlocks the vault. | ||
| // | ||
| // Precedence: | ||
| // 1. explicit (CALNODE_ENCRYPTION_KEY) - used verbatim, nothing is written. | ||
| // 2. an existing key file beside the database - read and reused. | ||
| // 3. no key at all - a new random secret is generated, written 0600 beside the | ||
| // database, and returned. This is the plain `docker run` case. | ||
| // | ||
| // dbPath is the on-disk database file; the key is placed in the same directory. | ||
| // The purpose is that a restart, an update, or a restored volume finds the same | ||
| // secret and the ciphertext in the database stays readable. | ||
| func ResolvePlatformSecret(explicit, dbPath string) (secret, source, path string, err error) { | ||
| if explicit != "" { | ||
| return explicit, SecretFromEnv, "", nil | ||
| } | ||
| if strings.TrimSpace(dbPath) == "" { | ||
| return "", "", "", errors.New("keyvault: no database path to place " + KeyFileName + " beside") | ||
| } | ||
|
|
||
| path = filepath.Join(filepath.Dir(dbPath), KeyFileName) | ||
|
|
||
| // An unreadable-but-present file must not silently fall through to | ||
| // generating a new secret: that would replace a working key with one that | ||
| // cannot decrypt the existing database. | ||
| // #nosec G304 -- path is derived from the operator's DATABASE_URL, not from | ||
| // request data. Where the key lives is deployment configuration. | ||
| if raw, readErr := os.ReadFile(path); readErr == nil { | ||
| existing := strings.TrimSpace(string(raw)) | ||
| if existing == "" { | ||
| return "", "", "", fmt.Errorf( | ||
| "keyvault: %s exists but is empty; delete it to generate a new key, "+ | ||
| "or restore it together with the database", path) | ||
| } | ||
| return existing, SecretFromFile, path, nil | ||
| } else if !errors.Is(readErr, fs.ErrNotExist) { | ||
| return "", "", "", fmt.Errorf("keyvault: read %s: %w", path, readErr) | ||
| } | ||
|
|
||
| if mkErr := os.MkdirAll(filepath.Dir(path), 0o700); mkErr != nil { | ||
| return "", "", "", fmt.Errorf("keyvault: create key directory: %w", mkErr) | ||
| } | ||
|
|
||
| var raw [32]byte | ||
| if _, randErr := rand.Read(raw[:]); randErr != nil { | ||
| return "", "", "", fmt.Errorf("keyvault: generate platform secret: %w", randErr) | ||
| } | ||
| generated := hex.EncodeToString(raw[:]) | ||
|
|
||
| // O_EXCL so two processes starting at once cannot overwrite each other; the | ||
| // loser re-reads the winner's file rather than proceeding with its own. | ||
| // #nosec G304 -- same operator-controlled path as above. | ||
| f, createErr := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600) | ||
| if errors.Is(createErr, fs.ErrExist) { | ||
| // #nosec G304 -- same operator-controlled path. | ||
| if existing, readErr := os.ReadFile(path); readErr == nil { | ||
| if s := strings.TrimSpace(string(existing)); s != "" { | ||
| return s, SecretFromFile, path, nil | ||
| } | ||
| } | ||
| return "", "", "", fmt.Errorf("keyvault: %s appeared but could not be read", path) | ||
| } | ||
| if createErr != nil { | ||
| return "", "", "", fmt.Errorf("keyvault: create %s: %w", path, createErr) | ||
| } | ||
| if _, writeErr := f.WriteString(generated + "\n"); writeErr != nil { | ||
| // Join rather than discard: a close or cleanup failure is worth reporting | ||
| // alongside the write failure, and a half-written key file must not be | ||
| // left behind looking usable. | ||
| writeErr = errors.Join(writeErr, f.Close()) | ||
| if rmErr := os.Remove(path); rmErr != nil && !errors.Is(rmErr, fs.ErrNotExist) { | ||
| writeErr = errors.Join(writeErr, rmErr) | ||
| } | ||
| return "", "", "", fmt.Errorf("keyvault: write %s: %w", path, writeErr) | ||
| } | ||
| if closeErr := f.Close(); closeErr != nil { | ||
| if rmErr := os.Remove(path); rmErr != nil && !errors.Is(rmErr, fs.ErrNotExist) { | ||
| closeErr = errors.Join(closeErr, rmErr) | ||
| } | ||
| return "", "", "", fmt.Errorf("keyvault: close %s: %w", path, closeErr) | ||
| } | ||
|
|
||
| return generated, SecretFromGenerated, path, nil | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This step only prints
Architecture/Os; it never asserts the value, so "Confirm the image is arm64" can succeed on an amd64 image. The later nativedocker runonly catches that by accident — because the runner has no binfmt/QEMU — which is an implicit dependency rather than the check the step claims to be. Add atestagainst the inspected value.