fix(config-db): encrypt deployment configuration JSON at rest (CDB-1, WP1 task 1.8)

The flattened config embeds each bound data connection's decrypted
protocol JSON, and PendingDeployments / DeployedConfigSnapshots stored
it verbatim - the credentials dp:protocol:v1 protects on DataConnections
sat in plaintext in two more tables and every backup.

- DeploymentConfigSecretConverter (own DP purpose, dp:config:v1: prefix)
  on both ConfigurationJson columns: every write protected, reads accept
  legacy plaintext so un-migrated rows keep working. Schema-only
  contexts refuse the write.
- DeploymentConfigSecretUpgrade: one-time startup re-protect from
  MigrationHelper (after the protocol upgrade). A startup backfill, not
  an EF migration, because the Data Protection key ring is not reachable
  from a SQL migration script. Plaintext-detecting (NOT LIKE prefix),
  paged by Id, binary compare-and-swap (both centrals may run it),
  idempotent, and not a startup gate (logged + retried next start).
- No EF model change (has-pending-model-changes: none); no migration.

Tests: SQLite round-trip (raw column has no secret, EF read equals
original) for both tables, legacy plaintext readable then re-protected
idempotently, schema-only write refused, and the upgrade on real SQL
Server. PendingDeploymentRepositoryIntegrationTests now supplies a key
ring.

Deploy gate: upgrade both central nodes together (old binaries cannot
read protected rows).
This commit is contained in:
Joseph Doherty
2026-12-03 10:28:39 -05:00
parent 5a052c6327
commit d4e13d3aa4
7 changed files with 474 additions and 1 deletions
@@ -507,4 +507,30 @@ persisted key ring. Keep a coordinated pre-upgrade database/key-ring backup. Dow
while protected protocol rows exist. The existing same-database key-ring storage remains
defence in depth; this change does not introduce external key management.
### Deployment configuration encryption at rest (code review 2026-12-02 CDB-1)
The flattened deployment configuration embeds each bound data connection's **decrypted**
protocol JSON (endpoints and any inline credentials — `FlattenedConfiguration.Connections`), so
`PendingDeployments.ConfigurationJson` and `DeployedConfigSnapshots.ConfigurationJson` are now
protected in full by `DeploymentConfigSecretConverter` (its own Data Protection purpose, versioned
`dp:config:v1:` prefix). Before this the credentials the `dp:protocol:v1:` converter protects on
`DataConnections` sat in plaintext in both tables and every backup. Both columns were already
`nvarchar(max)`; no schema migration. Every write is protected; schema-only contexts reject writes
(`GuardSecretWritesHaveAKeyRing`). Every consumer reads through EF (the notify-and-fetch endpoint
that serves the site, the deployment diff), so all of them see plaintext transparently.
Unlike the protocol upgrade, **reads accept legacy plaintext** (a JSON document never starts with
the prefix), so un-migrated rows keep working. `DeploymentConfigSecretUpgrade` re-protects them at
startup from `MigrationHelper`, after `DataConnectionSecretUpgrade`: a **startup backfill, not an
EF migration**, because protecting needs the Data Protection key ring, which a SQL migration script
cannot reach. It reads only rows whose value lacks the prefix (`NOT LIKE 'dp:config:v1:%'`), pages
by primary key (64 rows), and updates with a binary-collation compare-and-swap on the old value, so
both central nodes may run it at once and a rerun is a no-op. It is **not a startup gate**: a
failure is logged at Warning and retried at the next start. Upgrade both central nodes together —
old binaries cannot read protected rows (they would hand the site ciphertext). Check after the first
start: `SELECT COUNT(*) FROM DeployedConfigSnapshots WHERE ConfigurationJson NOT LIKE 'dp:config:v1:%'`
(and the same for `PendingDeployments`) returns 0. The deployment comparison diff still shows the
decrypted connection configuration to an authorized operator; that is a display concern, not
storage.
Trace replay reads keyset pages of eight rows, so EF retry buffering cannot materialize a whole trace before the consumer applies its byte budget. SQL omits an oversized DetailsJson value before transfer; all other projected strings have schema length limits. Canonical serialized UTF-8 size, including escaping and metadata, determines the final 1 MiB limit.