feat(core): platform layer, portable database, archives and access (refs #7 #8 #9 #10 #11)

Phase 1 foundations in the Anubis library:
- Platform: startup configuration from environment variables (Docker) or
  config.toml (desktop, in-house TOML subset), data folder layout, secret key
  custody (ANUBIS_SECRET_KEY, DPAPI on Windows, restricted file elsewhere),
  installation identity and database fingerprint.
- Db: ADO.NET with Npgsql and Microsoft.Data.Sqlite, money as NUMERIC(38,18)
  or canonical text rounded to 18 decimals, UTC moments, UUIDv7 ids; numbered
  SQL migrations with type tokens and a version table; engine-independent
  export/import with count and accounting-total checks; backups keeping 14.
- Core: injectable clock, channel event bus, scheduler with fixed, interval
  and market-relative times, settings with change log, AES-GCM secrets, log
  redaction, Italian formats.
- Access: PBKDF2-SHA512 hashes, RFC 6238 TOTP, hashed session tokens,
  sign-in attempt limit.

SQLitePCLRaw (Apache-2.0) arrives with Microsoft.Data.Sqlite and is declared
in DependencyPolicyTests and API-NOTES.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-10 11:09:42 +01:00
1 parent ffb67c9135
commit 53ca01b7c3
44 files changed
+5644 -12

No files matched your search

+116 -1
View File
@@ -82,7 +82,7 @@ internal static class TestDatabase
/// <summary>Host and port of the PostgreSQL used by the tests, from <c>ANUBIS_TEST_PG_HOST</c> and <c>ANUBIS_TEST_PG_PORT</c>.</summary>
public static (string Host, int Port) Postgres { get; } = (
Environment.GetEnvironmentVariable("ANUBIS_TEST_PG_HOST") ?? "localhost",
Environment.GetEnvironmentVariable("ANUBIS_TEST_PG_HOST") ?? "127.0.0.1",
int.TryParse(Environment.GetEnvironmentVariable("ANUBIS_TEST_PG_PORT"), out var port) ? port : 55432);
public static async Task<bool> PostgresReachableAsync(TimeSpan timeout)
@@ -100,3 +100,118 @@ internal static class TestDatabase
}
}
}
/// <summary>
/// A fresh, empty database for one test: a SQLite file in a temporary folder, or a new PostgreSQL database
/// created on the test server and dropped afterwards. Never the developer's or the user's database.
/// </summary>
internal sealed class TestDb : IAsyncDisposable
{
private readonly string? _folder;
private readonly string? _postgresName;
private TestDb(Anubis.Platform.DbSettings settings, string? folder, string? postgresName)
{
Settings = settings;
_folder = folder;
_postgresName = postgresName;
Db = Anubis.Db.Database.Open(settings);
}
public Anubis.Platform.DbSettings Settings { get; }
public Anubis.Db.Database Db { get; }
public static string PostgresPassword { get; } = Environment.GetEnvironmentVariable("ANUBIS_TEST_PG_PASSWORD") ?? "anubis-dev";
/// <summary>A database on the engine of this run (ANUBIS_TEST_DB).</summary>
public static Task<TestDb> CreateAsync() =>
CreateAsync(TestDatabase.Engine == "postgres" ? Anubis.Platform.DbEngine.Postgres : Anubis.Platform.DbEngine.Sqlite);
public static async Task<TestDb> CreateAsync(Anubis.Platform.DbEngine engine)
{
if (engine == Anubis.Platform.DbEngine.Sqlite)
{
var folder = TempFolder.Create("db");
return new TestDb(new() { Engine = engine, SqlitePath = Path.Combine(folder, "test.sqlite3") }, folder, null);
}
var name = "anubis_t_" + Guid.NewGuid().ToString("N");
await using (var admin = Anubis.Db.Database.Open(AdminSettings()))
{
await using var connection = await admin.OpenConnectionAsync(CancellationToken.None).ConfigureAwait(false);
await using var command = connection.CreateCommand();
command.CommandText = $"CREATE DATABASE {name}";
await command.ExecuteNonQueryAsync().ConfigureAwait(false);
}
return new TestDb(AdminSettings() with { Database = name }, null, name);
}
/// <summary>True when a PostgreSQL server for tests answers: cross-engine tests run only then.</summary>
public static Task<bool> PostgresAvailableAsync() => TestDatabase.PostgresReachableAsync(TimeSpan.FromSeconds(5));
private static Anubis.Platform.DbSettings AdminSettings() => new()
{
Engine = Anubis.Platform.DbEngine.Postgres,
Host = TestDatabase.Postgres.Host,
Port = TestDatabase.Postgres.Port,
Database = "anubis",
User = "anubis",
Password = PostgresPassword,
};
public async ValueTask DisposeAsync()
{
await Db.DisposeAsync().ConfigureAwait(false);
if (_postgresName is not null)
{
await using var admin = Anubis.Db.Database.Open(AdminSettings());
await using var connection = await admin.OpenConnectionAsync(CancellationToken.None).ConfigureAwait(false);
await using var command = connection.CreateCommand();
command.CommandText = $"DROP DATABASE IF EXISTS {_postgresName} WITH (FORCE)";
await command.ExecuteNonQueryAsync().ConfigureAwait(false);
}
if (_folder is not null)
{
TempFolder.Delete(_folder);
}
}
}
/// <summary>Temporary folders for tests, under the system temp directory.</summary>
internal static class TempFolder
{
public static string Create(string purpose)
{
var path = Path.Combine(Path.GetTempPath(), "anubis-tests", purpose + "-" + Guid.NewGuid().ToString("N")[..12]);
Directory.CreateDirectory(path);
return path;
}
public static void Delete(string path)
{
Microsoft.Data.Sqlite.SqliteConnection.ClearAllPools();
for (var attempt = 0; attempt < 5; attempt++)
{
try
{
if (Directory.Exists(path))
{
Directory.Delete(path, recursive: true);
}
return;
}
catch (IOException) when (attempt < 4)
{
Thread.Sleep(100);
}
catch (UnauthorizedAccessException) when (attempt < 4)
{
Thread.Sleep(100);
}
}
}
}