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
@@ -137,11 +137,17 @@ public class PendingDeploymentRepositoryIntegrationTests : IClassFixture<MsSqlMi
return instance.Id;
}
// CDB-1: PendingDeployments.ConfigurationJson is encrypted at rest, so a write-capable
// context needs a key ring; one shared provider lets every context in a test read the
// others' writes.
private static readonly Microsoft.AspNetCore.DataProtection.IDataProtectionProvider KeyRing =
new Microsoft.AspNetCore.DataProtection.EphemeralDataProtectionProvider();
private ScadaBridgeDbContext CreateContext()
{
var options = new DbContextOptionsBuilder<ScadaBridgeDbContext>()
.UseSqlServer(_fixture.ConnectionString)
.Options;
return new ScadaBridgeDbContext(options);
return new ScadaBridgeDbContext(options, KeyRing);
}
}