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

+90
View File
@@ -0,0 +1,90 @@
using System.Globalization;
using Anubis.Core;
using Anubis.Platform;
namespace Anubis.Db;
/// <summary>A backup folder in /data/backups.</summary>
public sealed record BackupInfo(string Folder, string Name, DateTimeOffset TakenAt, string Reason, bool HasSqliteCopy);
/// <summary>
/// Backups (SPEC 5.4): the engine-independent archive, plus a consistent copy of the SQLite file made with the
/// backup API. Nightly at 03:30 Italian time, before every migration, and on request. The last 14 are kept.
/// </summary>
public sealed class Backups(DataDirectory data, IClock clock)
{
public const int Keep = 14;
public const string ArchiveFolder = "archivio";
public const string SqliteCopy = "anubis.sqlite3";
public async Task<BackupInfo> TakeAsync(Database db, string reason, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(db);
Directory.CreateDirectory(data.Backups);
var now = clock.UtcNow;
var name = now.ToString("yyyyMMdd-HHmmss", CultureInfo.InvariantCulture) + "-" + reason;
var folder = Path.Combine(data.Backups, name);
for (var n = 2; Directory.Exists(folder); n++)
{
folder = Path.Combine(data.Backups, name + "-" + n.ToString(CultureInfo.InvariantCulture));
}
// Written under a temporary name and renamed at the end: a half-written backup never looks complete.
var temp = folder + ".partial";
try
{
await Archive.ExportAsync(db, Path.Combine(temp, ArchiveFolder), clock, cancellationToken).ConfigureAwait(false);
if (db.Engine == DbEngine.Sqlite)
{
await db.BackupSqliteFileAsync(Path.Combine(temp, SqliteCopy), cancellationToken).ConfigureAwait(false);
}
Directory.Move(temp, folder);
}
catch
{
if (Directory.Exists(temp))
{
Directory.Delete(temp, recursive: true);
}
throw;
}
Prune();
return Describe(folder)!;
}
/// <summary>Backups from the newest, only complete ones.</summary>
public IReadOnlyList<BackupInfo> List() =>
!Directory.Exists(data.Backups)
? []
: Directory.EnumerateDirectories(data.Backups)
.Where(d => !d.EndsWith(".partial", StringComparison.Ordinal))
.Select(Describe)
.OfType<BackupInfo>()
.OrderByDescending(b => b.TakenAt)
.ThenByDescending(b => b.Name, StringComparer.Ordinal)
.ToList();
private void Prune()
{
foreach (var old in List().Skip(Keep))
{
Directory.Delete(old.Folder, recursive: true);
}
}
private static BackupInfo? Describe(string folder)
{
var name = Path.GetFileName(folder);
if (name.Length < 16 || !File.Exists(Path.Combine(folder, ArchiveFolder, Archive.IndexFile))
|| !DateTime.TryParseExact(name[..15], "yyyyMMdd-HHmmss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out var taken))
{
return null;
}
var reason = name.Length > 16 ? name[16..] : "";
return new BackupInfo(folder, name, new DateTimeOffset(taken, TimeSpan.Zero), reason, File.Exists(Path.Combine(folder, SqliteCopy)));
}
}