CI / Build and test (SQLite) (pull_request) Successful in 45s
CI / PostgreSQL 16 integration (pull_request) Skipped
CI / PostgreSQL 17 integration (pull_request) Skipped
CI / YAML check (pull_request) Successful in 3s
CI / Docker image (pull_request) Successful in 51s
Every later milestone stores its data through this layer, so it has to behave the same on both engines and be safe by construction: - Khaemhat.Data: explicit entity maps, typed queries over a safe expression subset, SqlFragment where every interpolated value becomes a parameter, unit of work with transactions, repositories with audit columns and optimistic concurrency on row_version, retries on transient errors. - SQLite provider (STRICT tables, WAL, exact decimals as integers scaled by 10^6, Unicode case-insensitive LIKE, serialised writes) and PostgreSQL 16+ provider (numeric(19,6), timestamptz, jsonb, advisory locks, database creation and server info). Databases are configured from postgres:// and sqlite:// URLs whose password never appears in messages. - C# migrations with system and company catalogues, schema_version with checksums, one transaction per migration under a lock, refusal of unknown applied versions; first system tables sys_companies and sys_settings. - CompanyRegistry stores each company's database URL encrypted with the instance key (AES-256-GCM, HKDF-derived key, purpose as associated data), so a copied row cannot be read for another company. - tools/Khaemhat.DataModelTool, scripts/gen-datamodel.sh and scripts/new-migration.sh: the current schema section of docs/DATA-MODEL.md is generated from the migrations and a test fails when it is stale. - C# 14 binds array.Contains to MemoryExtensions over spans; the query translator unwraps the span conversion. - Tests run every data scenario on SQLite and PostgreSQL (PostgreSQL in CI); ADR 0018 records where the contracts differ from SPEC 5.1. Docs, wiki, changelog and status updated. Closes #7, #8, #9, #10, #11, #12, #13, #14 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
134 lines
4.9 KiB
C#
134 lines
4.9 KiB
C#
using Khaemhat.Core.Security;
|
|
using Khaemhat.Infrastructure.Security;
|
|
|
|
namespace Khaemhat.Infrastructure.Tests;
|
|
|
|
/// <summary>Encryption of secrets at rest (SPEC 12.9, issue M1 #12).</summary>
|
|
[TestClass]
|
|
public sealed class SecretProtectorTests
|
|
{
|
|
private const string Purpose = "company:ACME:database";
|
|
private const string Secret = "postgres://acme:S3cret!@db/khaemhat_acme";
|
|
|
|
[TestMethod]
|
|
public void Protect_ThenUnprotect_RoundTrips()
|
|
{
|
|
var protector = NewProtector();
|
|
|
|
var protectedValue = protector.Protect(Secret, Purpose);
|
|
|
|
StringAssert.StartsWith(protectedValue, "khs1." + protector.Key.Id + ".");
|
|
Assert.DoesNotContain("S3cret", protectedValue);
|
|
Assert.AreEqual(Secret, protector.Unprotect(protectedValue, Purpose));
|
|
Assert.AreEqual(string.Empty, protector.Unprotect(protector.Protect(string.Empty, Purpose), Purpose));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void TheSameSecret_IsEncryptedDifferentlyEachTime()
|
|
{
|
|
var protector = NewProtector();
|
|
|
|
Assert.AreNotEqual(protector.Protect(Secret, Purpose), protector.Protect(Secret, Purpose));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void KeyId_IsStable_ForTheSameKey()
|
|
{
|
|
var key = SecretKey.GenerateBase64();
|
|
|
|
Assert.AreEqual(SecretKey.FromBase64(key).Id, SecretKey.FromBase64(key).Id);
|
|
Assert.AreEqual(8, SecretKey.FromBase64(key).Id.Length);
|
|
Assert.AreNotEqual(SecretKey.FromBase64(key).Id, SecretKey.FromBase64(SecretKey.GenerateBase64()).Id);
|
|
Assert.AreEqual("SecretKey " + SecretKey.FromBase64(key).Id, SecretKey.FromBase64(key).ToString());
|
|
}
|
|
|
|
[TestMethod]
|
|
public void AnotherKey_IsRefused_NamingBothKeyIds()
|
|
{
|
|
var protectedValue = NewProtector().Protect(Secret, Purpose);
|
|
var other = NewProtector();
|
|
|
|
var error = Assert.ThrowsExactly<SecretProtectionException>(() => other.Unprotect(protectedValue, Purpose));
|
|
|
|
StringAssert.Contains(error.Message, SecretProtector.KeyIdOf(protectedValue)!);
|
|
StringAssert.Contains(error.Message, other.Key.Id);
|
|
Assert.DoesNotContain("S3cret", error.ToString());
|
|
}
|
|
|
|
[TestMethod]
|
|
public void AnotherPurpose_IsRefused()
|
|
{
|
|
var protector = NewProtector();
|
|
var protectedValue = protector.Protect(Secret, Purpose);
|
|
|
|
Assert.ThrowsExactly<SecretProtectionException>(() => protector.Unprotect(protectedValue, "company:EVIL:database"));
|
|
}
|
|
|
|
[TestMethod]
|
|
[DataRow("")]
|
|
[DataRow("plain text")]
|
|
[DataRow("khs1.abc")]
|
|
[DataRow("khs2.00000000.AAAA")]
|
|
public void NotAProtectedValue_IsRefused(string value)
|
|
{
|
|
Assert.ThrowsExactly<SecretProtectionException>(() => NewProtector().Unprotect(value, Purpose));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void DamagedValues_AreRefused()
|
|
{
|
|
var protector = NewProtector();
|
|
var protectedValue = protector.Protect(Secret, Purpose);
|
|
var payload = protectedValue[(protectedValue.LastIndexOf('.') + 1)..];
|
|
var prefix = protectedValue[..(protectedValue.LastIndexOf('.') + 1)];
|
|
var damaged = payload[..20] + (payload[20] == 'A' ? 'B' : 'A') + payload[21..];
|
|
|
|
Assert.ThrowsExactly<SecretProtectionException>(() => protector.Unprotect(prefix + damaged, Purpose));
|
|
Assert.ThrowsExactly<SecretProtectionException>(() => protector.Unprotect(prefix + payload[..10], Purpose));
|
|
Assert.ThrowsExactly<SecretProtectionException>(() => protector.Unprotect(prefix + "not*base64", Purpose));
|
|
}
|
|
|
|
[TestMethod]
|
|
[DataRow("")]
|
|
[DataRow(" ")]
|
|
[DataRow("not base64!")]
|
|
[DataRow("c2hvcnQ=")]
|
|
public void WeakOrInvalidKeys_AreRefused_WithoutEchoingThem(string value)
|
|
{
|
|
var error = Assert.ThrowsExactly<FormatException>(() => SecretKey.FromBase64(value));
|
|
|
|
StringAssert.Contains(error.Message, "KHAEMHAT_SECRET_KEY");
|
|
if (value.Trim().Length > 0)
|
|
{
|
|
Assert.DoesNotContain(value, error.Message);
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Rotation_ReencryptsWithTheNewKey()
|
|
{
|
|
var current = NewProtector();
|
|
var next = NewProtector();
|
|
var rotation = new SecretRotation(current, next);
|
|
var old = current.Protect(Secret, Purpose);
|
|
|
|
Assert.IsTrue(rotation.NeedsRotation(old));
|
|
var rotated = rotation.Rotate(old, Purpose);
|
|
|
|
Assert.IsFalse(rotation.NeedsRotation(rotated));
|
|
Assert.AreEqual(next.Key.Id, SecretProtector.KeyIdOf(rotated));
|
|
Assert.AreEqual(Secret, next.Unprotect(rotated, Purpose));
|
|
Assert.ThrowsExactly<SecretProtectionException>(() => current.Unprotect(rotated, Purpose));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void KeyIdOf_ReadsOnlyProtectedValues()
|
|
{
|
|
Assert.IsNull(SecretProtector.KeyIdOf(null));
|
|
Assert.IsNull(SecretProtector.KeyIdOf("plain"));
|
|
Assert.AreEqual("0a1b2c3d", SecretProtector.KeyIdOf("khs1.0a1b2c3d.AAAA"));
|
|
}
|
|
|
|
private static SecretProtector NewProtector() => new(SecretKey.FromBase64(SecretKey.GenerateBase64()));
|
|
}
|