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:
1 parent
ffb67c9135
commit
53ca01b7c3
44 files changed
+5644
-12
No files matched your search
@@ -2,8 +2,87 @@
|
||||
"version": 2,
|
||||
"dependencies": {
|
||||
"net10.0-windows7.0": {
|
||||
"Microsoft.Data.Sqlite.Core": {
|
||||
"type": "Transitive",
|
||||
"resolved": "10.0.14",
|
||||
"contentHash": "1p9rO3Q2X/L/9YrYog3vaDzi8u+WZfJCOkDYoHqmofqYP0DLPLPyZcS2Rnh/79JXAodgbs1AgsaNwLpIvC3Xew==",
|
||||
"dependencies": {
|
||||
"SQLitePCLRaw.core": "2.1.13"
|
||||
}
|
||||
},
|
||||
"Microsoft.Extensions.DependencyInjection.Abstractions": {
|
||||
"type": "Transitive",
|
||||
"resolved": "10.0.0",
|
||||
"contentHash": "L3AdmZ1WOK4XXT5YFPEwyt0ep6l8lGIPs7F5OOBZc77Zqeo01Of7XXICy47628sdVl0v/owxYJTe86DTgFwKCA=="
|
||||
},
|
||||
"Microsoft.Extensions.Logging.Abstractions": {
|
||||
"type": "Transitive",
|
||||
"resolved": "10.0.0",
|
||||
"contentHash": "FU/IfjDfwaMuKr414SSQNTIti/69bHEMb+QKrskRb26oVqpx3lNFXMjs/RC9ZUuhBhcwDM2BwOgoMw+PZ+beqQ==",
|
||||
"dependencies": {
|
||||
"Microsoft.Extensions.DependencyInjection.Abstractions": "10.0.0"
|
||||
}
|
||||
},
|
||||
"SQLitePCLRaw.bundle_e_sqlite3": {
|
||||
"type": "Transitive",
|
||||
"resolved": "2.1.13",
|
||||
"contentHash": "NlDixlC7uHmo+ojXRgkNvN0tQtVGpLDZ3x2OpgNj+G07VHh5cXSszFDrAiC4BpUiPwkcaeuQkMV9WQ123xVtmQ==",
|
||||
"dependencies": {
|
||||
"SQLitePCLRaw.lib.e_sqlite3": "2.1.13",
|
||||
"SQLitePCLRaw.provider.e_sqlite3": "2.1.13"
|
||||
}
|
||||
},
|
||||
"SQLitePCLRaw.core": {
|
||||
"type": "Transitive",
|
||||
"resolved": "2.1.13",
|
||||
"contentHash": "2joofoinuHBFs0nMX3nKuXgxpXryRkCOdM2Ua6HBqQLLy6IW+5f28ZI+HAvMyF8vG7XfNz/lwfBfE6V4juru8Q=="
|
||||
},
|
||||
"SQLitePCLRaw.lib.e_sqlite3": {
|
||||
"type": "Transitive",
|
||||
"resolved": "2.1.13",
|
||||
"contentHash": "D2UAuiGxFYBqeRXjjjGgvJ5UQifzn6wefmSqeKU7XcNGKLbJRP6X7kcPZ6opOBJGdCIlgbSxTvDpBbLvsN594A=="
|
||||
},
|
||||
"SQLitePCLRaw.provider.e_sqlite3": {
|
||||
"type": "Transitive",
|
||||
"resolved": "2.1.13",
|
||||
"contentHash": "ZU60dSqoSNRe49KcQDUmOVgdRtjITOssVXFtiISs+zJXDI4D2Sndwpn2NOtzDQNPu0ZAvpoLPQ+GLKv6iEZn8Q==",
|
||||
"dependencies": {
|
||||
"SQLitePCLRaw.core": "2.1.13"
|
||||
}
|
||||
},
|
||||
"anubis": {
|
||||
"type": "Project"
|
||||
"type": "Project",
|
||||
"dependencies": {
|
||||
"Microsoft.Data.Sqlite": "[10.0.14, )",
|
||||
"Npgsql": "[10.0.3, )",
|
||||
"System.Security.Cryptography.ProtectedData": "[10.0.14, )"
|
||||
}
|
||||
},
|
||||
"Microsoft.Data.Sqlite": {
|
||||
"type": "CentralTransitive",
|
||||
"requested": "[10.0.14, )",
|
||||
"resolved": "10.0.14",
|
||||
"contentHash": "muyroan6rKrf0omZL8UF7Kw5uOGp/yBg1L/XWvkFN3WhKaNtK2IWqRZw93nIwVqjhTVDO0l4CEGhHIh+mb+sKg==",
|
||||
"dependencies": {
|
||||
"Microsoft.Data.Sqlite.Core": "10.0.14",
|
||||
"SQLitePCLRaw.bundle_e_sqlite3": "2.1.13",
|
||||
"SQLitePCLRaw.core": "2.1.13"
|
||||
}
|
||||
},
|
||||
"Npgsql": {
|
||||
"type": "CentralTransitive",
|
||||
"requested": "[10.0.3, )",
|
||||
"resolved": "10.0.3",
|
||||
"contentHash": "7nb5YzXuvWWJxB0J8DiyL3we+X4FOctZrt0fIBnucOIaIevFEEwGQVZKtiu9olXdlNAK1eNgqSral6r/jlhI4w==",
|
||||
"dependencies": {
|
||||
"Microsoft.Extensions.Logging.Abstractions": "10.0.0"
|
||||
}
|
||||
},
|
||||
"System.Security.Cryptography.ProtectedData": {
|
||||
"type": "CentralTransitive",
|
||||
"requested": "[10.0.14, )",
|
||||
"resolved": "10.0.14",
|
||||
"contentHash": "UuaBUHgmtc9h6CMxaQqFDDJxkS5JVy7XEB+DbtCvD68GFcVM9/AccuNwFpARx8nwCnvjaxzoOBjG3gPz32P1Eg=="
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,87 @@
|
||||
"version": 2,
|
||||
"dependencies": {
|
||||
"net10.0": {
|
||||
"Microsoft.Data.Sqlite.Core": {
|
||||
"type": "Transitive",
|
||||
"resolved": "10.0.14",
|
||||
"contentHash": "1p9rO3Q2X/L/9YrYog3vaDzi8u+WZfJCOkDYoHqmofqYP0DLPLPyZcS2Rnh/79JXAodgbs1AgsaNwLpIvC3Xew==",
|
||||
"dependencies": {
|
||||
"SQLitePCLRaw.core": "2.1.13"
|
||||
}
|
||||
},
|
||||
"Microsoft.Extensions.DependencyInjection.Abstractions": {
|
||||
"type": "Transitive",
|
||||
"resolved": "10.0.0",
|
||||
"contentHash": "L3AdmZ1WOK4XXT5YFPEwyt0ep6l8lGIPs7F5OOBZc77Zqeo01Of7XXICy47628sdVl0v/owxYJTe86DTgFwKCA=="
|
||||
},
|
||||
"Microsoft.Extensions.Logging.Abstractions": {
|
||||
"type": "Transitive",
|
||||
"resolved": "10.0.0",
|
||||
"contentHash": "FU/IfjDfwaMuKr414SSQNTIti/69bHEMb+QKrskRb26oVqpx3lNFXMjs/RC9ZUuhBhcwDM2BwOgoMw+PZ+beqQ==",
|
||||
"dependencies": {
|
||||
"Microsoft.Extensions.DependencyInjection.Abstractions": "10.0.0"
|
||||
}
|
||||
},
|
||||
"SQLitePCLRaw.bundle_e_sqlite3": {
|
||||
"type": "Transitive",
|
||||
"resolved": "2.1.13",
|
||||
"contentHash": "NlDixlC7uHmo+ojXRgkNvN0tQtVGpLDZ3x2OpgNj+G07VHh5cXSszFDrAiC4BpUiPwkcaeuQkMV9WQ123xVtmQ==",
|
||||
"dependencies": {
|
||||
"SQLitePCLRaw.lib.e_sqlite3": "2.1.13",
|
||||
"SQLitePCLRaw.provider.e_sqlite3": "2.1.13"
|
||||
}
|
||||
},
|
||||
"SQLitePCLRaw.core": {
|
||||
"type": "Transitive",
|
||||
"resolved": "2.1.13",
|
||||
"contentHash": "2joofoinuHBFs0nMX3nKuXgxpXryRkCOdM2Ua6HBqQLLy6IW+5f28ZI+HAvMyF8vG7XfNz/lwfBfE6V4juru8Q=="
|
||||
},
|
||||
"SQLitePCLRaw.lib.e_sqlite3": {
|
||||
"type": "Transitive",
|
||||
"resolved": "2.1.13",
|
||||
"contentHash": "D2UAuiGxFYBqeRXjjjGgvJ5UQifzn6wefmSqeKU7XcNGKLbJRP6X7kcPZ6opOBJGdCIlgbSxTvDpBbLvsN594A=="
|
||||
},
|
||||
"SQLitePCLRaw.provider.e_sqlite3": {
|
||||
"type": "Transitive",
|
||||
"resolved": "2.1.13",
|
||||
"contentHash": "ZU60dSqoSNRe49KcQDUmOVgdRtjITOssVXFtiISs+zJXDI4D2Sndwpn2NOtzDQNPu0ZAvpoLPQ+GLKv6iEZn8Q==",
|
||||
"dependencies": {
|
||||
"SQLitePCLRaw.core": "2.1.13"
|
||||
}
|
||||
},
|
||||
"anubis": {
|
||||
"type": "Project"
|
||||
"type": "Project",
|
||||
"dependencies": {
|
||||
"Microsoft.Data.Sqlite": "[10.0.14, )",
|
||||
"Npgsql": "[10.0.3, )",
|
||||
"System.Security.Cryptography.ProtectedData": "[10.0.14, )"
|
||||
}
|
||||
},
|
||||
"Microsoft.Data.Sqlite": {
|
||||
"type": "CentralTransitive",
|
||||
"requested": "[10.0.14, )",
|
||||
"resolved": "10.0.14",
|
||||
"contentHash": "muyroan6rKrf0omZL8UF7Kw5uOGp/yBg1L/XWvkFN3WhKaNtK2IWqRZw93nIwVqjhTVDO0l4CEGhHIh+mb+sKg==",
|
||||
"dependencies": {
|
||||
"Microsoft.Data.Sqlite.Core": "10.0.14",
|
||||
"SQLitePCLRaw.bundle_e_sqlite3": "2.1.13",
|
||||
"SQLitePCLRaw.core": "2.1.13"
|
||||
}
|
||||
},
|
||||
"Npgsql": {
|
||||
"type": "CentralTransitive",
|
||||
"requested": "[10.0.3, )",
|
||||
"resolved": "10.0.3",
|
||||
"contentHash": "7nb5YzXuvWWJxB0J8DiyL3we+X4FOctZrt0fIBnucOIaIevFEEwGQVZKtiu9olXdlNAK1eNgqSral6r/jlhI4w==",
|
||||
"dependencies": {
|
||||
"Microsoft.Extensions.Logging.Abstractions": "10.0.0"
|
||||
}
|
||||
},
|
||||
"System.Security.Cryptography.ProtectedData": {
|
||||
"type": "CentralTransitive",
|
||||
"requested": "[10.0.14, )",
|
||||
"resolved": "10.0.14",
|
||||
"contentHash": "UuaBUHgmtc9h6CMxaQqFDDJxkS5JVy7XEB+DbtCvD68GFcVM9/AccuNwFpARx8nwCnvjaxzoOBjG3gPz32P1Eg=="
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,71 @@
|
||||
"version": 2,
|
||||
"dependencies": {
|
||||
"net10.0": {
|
||||
"Microsoft.Data.Sqlite.Core": {
|
||||
"type": "Transitive",
|
||||
"resolved": "10.0.14",
|
||||
"contentHash": "1p9rO3Q2X/L/9YrYog3vaDzi8u+WZfJCOkDYoHqmofqYP0DLPLPyZcS2Rnh/79JXAodgbs1AgsaNwLpIvC3Xew==",
|
||||
"dependencies": {
|
||||
"SQLitePCLRaw.core": "2.1.13"
|
||||
}
|
||||
},
|
||||
"SQLitePCLRaw.bundle_e_sqlite3": {
|
||||
"type": "Transitive",
|
||||
"resolved": "2.1.13",
|
||||
"contentHash": "NlDixlC7uHmo+ojXRgkNvN0tQtVGpLDZ3x2OpgNj+G07VHh5cXSszFDrAiC4BpUiPwkcaeuQkMV9WQ123xVtmQ==",
|
||||
"dependencies": {
|
||||
"SQLitePCLRaw.lib.e_sqlite3": "2.1.13",
|
||||
"SQLitePCLRaw.provider.e_sqlite3": "2.1.13"
|
||||
}
|
||||
},
|
||||
"SQLitePCLRaw.core": {
|
||||
"type": "Transitive",
|
||||
"resolved": "2.1.13",
|
||||
"contentHash": "2joofoinuHBFs0nMX3nKuXgxpXryRkCOdM2Ua6HBqQLLy6IW+5f28ZI+HAvMyF8vG7XfNz/lwfBfE6V4juru8Q=="
|
||||
},
|
||||
"SQLitePCLRaw.lib.e_sqlite3": {
|
||||
"type": "Transitive",
|
||||
"resolved": "2.1.13",
|
||||
"contentHash": "D2UAuiGxFYBqeRXjjjGgvJ5UQifzn6wefmSqeKU7XcNGKLbJRP6X7kcPZ6opOBJGdCIlgbSxTvDpBbLvsN594A=="
|
||||
},
|
||||
"SQLitePCLRaw.provider.e_sqlite3": {
|
||||
"type": "Transitive",
|
||||
"resolved": "2.1.13",
|
||||
"contentHash": "ZU60dSqoSNRe49KcQDUmOVgdRtjITOssVXFtiISs+zJXDI4D2Sndwpn2NOtzDQNPu0ZAvpoLPQ+GLKv6iEZn8Q==",
|
||||
"dependencies": {
|
||||
"SQLitePCLRaw.core": "2.1.13"
|
||||
}
|
||||
},
|
||||
"anubis": {
|
||||
"type": "Project"
|
||||
"type": "Project",
|
||||
"dependencies": {
|
||||
"Microsoft.Data.Sqlite": "[10.0.14, )",
|
||||
"Npgsql": "[10.0.3, )",
|
||||
"System.Security.Cryptography.ProtectedData": "[10.0.14, )"
|
||||
}
|
||||
},
|
||||
"Microsoft.Data.Sqlite": {
|
||||
"type": "CentralTransitive",
|
||||
"requested": "[10.0.14, )",
|
||||
"resolved": "10.0.14",
|
||||
"contentHash": "muyroan6rKrf0omZL8UF7Kw5uOGp/yBg1L/XWvkFN3WhKaNtK2IWqRZw93nIwVqjhTVDO0l4CEGhHIh+mb+sKg==",
|
||||
"dependencies": {
|
||||
"Microsoft.Data.Sqlite.Core": "10.0.14",
|
||||
"SQLitePCLRaw.bundle_e_sqlite3": "2.1.13",
|
||||
"SQLitePCLRaw.core": "2.1.13"
|
||||
}
|
||||
},
|
||||
"Npgsql": {
|
||||
"type": "CentralTransitive",
|
||||
"requested": "[10.0.3, )",
|
||||
"resolved": "10.0.3",
|
||||
"contentHash": "7nb5YzXuvWWJxB0J8DiyL3we+X4FOctZrt0fIBnucOIaIevFEEwGQVZKtiu9olXdlNAK1eNgqSral6r/jlhI4w=="
|
||||
},
|
||||
"System.Security.Cryptography.ProtectedData": {
|
||||
"type": "CentralTransitive",
|
||||
"requested": "[10.0.14, )",
|
||||
"resolved": "10.0.14",
|
||||
"contentHash": "UuaBUHgmtc9h6CMxaQqFDDJxkS5JVy7XEB+DbtCvD68GFcVM9/AccuNwFpARx8nwCnvjaxzoOBjG3gPz32P1Eg=="
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,87 @@
|
||||
"version": 2,
|
||||
"dependencies": {
|
||||
"net10.0-windows7.0": {
|
||||
"Microsoft.Data.Sqlite.Core": {
|
||||
"type": "Transitive",
|
||||
"resolved": "10.0.14",
|
||||
"contentHash": "1p9rO3Q2X/L/9YrYog3vaDzi8u+WZfJCOkDYoHqmofqYP0DLPLPyZcS2Rnh/79JXAodgbs1AgsaNwLpIvC3Xew==",
|
||||
"dependencies": {
|
||||
"SQLitePCLRaw.core": "2.1.13"
|
||||
}
|
||||
},
|
||||
"Microsoft.Extensions.DependencyInjection.Abstractions": {
|
||||
"type": "Transitive",
|
||||
"resolved": "10.0.0",
|
||||
"contentHash": "L3AdmZ1WOK4XXT5YFPEwyt0ep6l8lGIPs7F5OOBZc77Zqeo01Of7XXICy47628sdVl0v/owxYJTe86DTgFwKCA=="
|
||||
},
|
||||
"Microsoft.Extensions.Logging.Abstractions": {
|
||||
"type": "Transitive",
|
||||
"resolved": "10.0.0",
|
||||
"contentHash": "FU/IfjDfwaMuKr414SSQNTIti/69bHEMb+QKrskRb26oVqpx3lNFXMjs/RC9ZUuhBhcwDM2BwOgoMw+PZ+beqQ==",
|
||||
"dependencies": {
|
||||
"Microsoft.Extensions.DependencyInjection.Abstractions": "10.0.0"
|
||||
}
|
||||
},
|
||||
"SQLitePCLRaw.bundle_e_sqlite3": {
|
||||
"type": "Transitive",
|
||||
"resolved": "2.1.13",
|
||||
"contentHash": "NlDixlC7uHmo+ojXRgkNvN0tQtVGpLDZ3x2OpgNj+G07VHh5cXSszFDrAiC4BpUiPwkcaeuQkMV9WQ123xVtmQ==",
|
||||
"dependencies": {
|
||||
"SQLitePCLRaw.lib.e_sqlite3": "2.1.13",
|
||||
"SQLitePCLRaw.provider.e_sqlite3": "2.1.13"
|
||||
}
|
||||
},
|
||||
"SQLitePCLRaw.core": {
|
||||
"type": "Transitive",
|
||||
"resolved": "2.1.13",
|
||||
"contentHash": "2joofoinuHBFs0nMX3nKuXgxpXryRkCOdM2Ua6HBqQLLy6IW+5f28ZI+HAvMyF8vG7XfNz/lwfBfE6V4juru8Q=="
|
||||
},
|
||||
"SQLitePCLRaw.lib.e_sqlite3": {
|
||||
"type": "Transitive",
|
||||
"resolved": "2.1.13",
|
||||
"contentHash": "D2UAuiGxFYBqeRXjjjGgvJ5UQifzn6wefmSqeKU7XcNGKLbJRP6X7kcPZ6opOBJGdCIlgbSxTvDpBbLvsN594A=="
|
||||
},
|
||||
"SQLitePCLRaw.provider.e_sqlite3": {
|
||||
"type": "Transitive",
|
||||
"resolved": "2.1.13",
|
||||
"contentHash": "ZU60dSqoSNRe49KcQDUmOVgdRtjITOssVXFtiISs+zJXDI4D2Sndwpn2NOtzDQNPu0ZAvpoLPQ+GLKv6iEZn8Q==",
|
||||
"dependencies": {
|
||||
"SQLitePCLRaw.core": "2.1.13"
|
||||
}
|
||||
},
|
||||
"anubis": {
|
||||
"type": "Project"
|
||||
"type": "Project",
|
||||
"dependencies": {
|
||||
"Microsoft.Data.Sqlite": "[10.0.14, )",
|
||||
"Npgsql": "[10.0.3, )",
|
||||
"System.Security.Cryptography.ProtectedData": "[10.0.14, )"
|
||||
}
|
||||
},
|
||||
"Microsoft.Data.Sqlite": {
|
||||
"type": "CentralTransitive",
|
||||
"requested": "[10.0.14, )",
|
||||
"resolved": "10.0.14",
|
||||
"contentHash": "muyroan6rKrf0omZL8UF7Kw5uOGp/yBg1L/XWvkFN3WhKaNtK2IWqRZw93nIwVqjhTVDO0l4CEGhHIh+mb+sKg==",
|
||||
"dependencies": {
|
||||
"Microsoft.Data.Sqlite.Core": "10.0.14",
|
||||
"SQLitePCLRaw.bundle_e_sqlite3": "2.1.13",
|
||||
"SQLitePCLRaw.core": "2.1.13"
|
||||
}
|
||||
},
|
||||
"Npgsql": {
|
||||
"type": "CentralTransitive",
|
||||
"requested": "[10.0.3, )",
|
||||
"resolved": "10.0.3",
|
||||
"contentHash": "7nb5YzXuvWWJxB0J8DiyL3we+X4FOctZrt0fIBnucOIaIevFEEwGQVZKtiu9olXdlNAK1eNgqSral6r/jlhI4w==",
|
||||
"dependencies": {
|
||||
"Microsoft.Extensions.Logging.Abstractions": "10.0.0"
|
||||
}
|
||||
},
|
||||
"System.Security.Cryptography.ProtectedData": {
|
||||
"type": "CentralTransitive",
|
||||
"requested": "[10.0.14, )",
|
||||
"resolved": "10.0.14",
|
||||
"contentHash": "UuaBUHgmtc9h6CMxaQqFDDJxkS5JVy7XEB+DbtCvD68GFcVM9/AccuNwFpARx8nwCnvjaxzoOBjG3gPz32P1Eg=="
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,268 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Anubis.Core;
|
||||
using Anubis.Db;
|
||||
|
||||
namespace Anubis.Access;
|
||||
|
||||
public sealed record User(Guid Id, string Username, bool MustChangePassword, bool HasSecondFactor);
|
||||
|
||||
public sealed record SessionInfo(Guid Id, User User, DateTimeOffset CreatedAt, DateTimeOffset ExpiresAt);
|
||||
|
||||
public enum LoginResult
|
||||
{
|
||||
Ok,
|
||||
WrongPassword,
|
||||
SecondFactorNeeded,
|
||||
WrongSecondFactor,
|
||||
Locked,
|
||||
NoUser,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Limit on sign-in attempts (SPEC 15.4): after 5 failures from the same address in 15 minutes that address
|
||||
/// waits 15 minutes; after 20 failures overall, everyone waits. Kept in memory: a restart clears it, which
|
||||
/// costs an attacker a restart they cannot cause.
|
||||
/// </summary>
|
||||
public sealed class LoginLimiter(IClock clock)
|
||||
{
|
||||
public const int PerAddress = 5;
|
||||
public const int Overall = 20;
|
||||
public static readonly TimeSpan Window = TimeSpan.FromMinutes(15);
|
||||
|
||||
private readonly Lock _gate = new();
|
||||
private readonly Dictionary<string, List<DateTimeOffset>> _failures = new(StringComparer.Ordinal);
|
||||
private readonly List<DateTimeOffset> _all = [];
|
||||
|
||||
/// <summary>How long <paramref name="address"/> must still wait, or zero.</summary>
|
||||
public TimeSpan Wait(string address)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
var now = clock.UtcNow;
|
||||
Trim(now);
|
||||
var mine = _failures.TryGetValue(address, out var list) ? list : [];
|
||||
var wait = TimeSpan.Zero;
|
||||
if (mine.Count >= PerAddress)
|
||||
{
|
||||
wait = mine[^PerAddress] + Window - now;
|
||||
}
|
||||
|
||||
if (_all.Count >= Overall)
|
||||
{
|
||||
var overall = _all[^Overall] + Window - now;
|
||||
wait = overall > wait ? overall : wait;
|
||||
}
|
||||
|
||||
return wait > TimeSpan.Zero ? wait : TimeSpan.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
public void Failed(string address)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
var now = clock.UtcNow;
|
||||
if (!_failures.TryGetValue(address, out var list))
|
||||
{
|
||||
_failures[address] = list = [];
|
||||
}
|
||||
|
||||
list.Add(now);
|
||||
_all.Add(now);
|
||||
}
|
||||
}
|
||||
|
||||
public void Succeeded(string address)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
_failures.Remove(address);
|
||||
}
|
||||
}
|
||||
|
||||
private void Trim(DateTimeOffset now)
|
||||
{
|
||||
_all.RemoveAll(t => t + Window <= now);
|
||||
foreach (var key in _failures.Keys.ToList())
|
||||
{
|
||||
_failures[key].RemoveAll(t => t + Window <= now);
|
||||
if (_failures[key].Count == 0)
|
||||
{
|
||||
_failures.Remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The single user of Anubis and their sessions (SPEC 15.4). Sessions are random tokens in an HttpOnly cookie;
|
||||
/// only their SHA-256 is stored, so a copy of the database does not let anyone in.
|
||||
/// </summary>
|
||||
public sealed class AccessStore(Database db, IClock clock, SecretStore secrets, int iterations = Passwords.DefaultIterations)
|
||||
{
|
||||
public const string AdminName = "admin";
|
||||
public static readonly TimeSpan IdleTimeout = TimeSpan.FromHours(12);
|
||||
public static readonly TimeSpan MaxLifetime = TimeSpan.FromDays(7);
|
||||
|
||||
public Task<bool> HasUserAsync(CancellationToken cancellationToken = default) =>
|
||||
db.ReadAsync(async s => await s.CountAsync("SELECT COUNT(*) FROM users").ConfigureAwait(false) > 0, cancellationToken);
|
||||
|
||||
/// <summary>Creates the user when there is none. In Docker the initial password must be changed at first sign-in.</summary>
|
||||
public async Task<bool> EnsureUserAsync(string password, bool mustChange, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var hash = Passwords.Hash(password, iterations);
|
||||
var now = clock.UtcNow;
|
||||
return await db.WriteAsync(async s =>
|
||||
{
|
||||
if (await s.CountAsync("SELECT COUNT(*) FROM users").ConfigureAwait(false) > 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
await s.ExecuteAsync("INSERT INTO users (id, username, password_hash, must_change_password, totp_secret, created_at, updated_at) VALUES (@id, @u, @h, @m, NULL, @t, @t)",
|
||||
cancellationToken, ("id", Ids.NewId(clock)), ("u", AdminName), ("h", hash), ("m", mustChange), ("t", now)).ConfigureAwait(false);
|
||||
return true;
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task<(LoginResult Result, string? Token)> SignInAsync(string password, string? code, string? userAgent, string? address, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var user = await db.ReadAsync(s => s.FirstOrDefaultAsync("SELECT id, password_hash, totp_secret FROM users WHERE username = @u",
|
||||
r => (Id: r.Id("id"), Hash: r.Text("password_hash"), Totp: r.BytesOrNull("totp_secret")), ("u", AdminName)), cancellationToken).ConfigureAwait(false);
|
||||
if (user == default)
|
||||
{
|
||||
return (LoginResult.NoUser, null);
|
||||
}
|
||||
|
||||
if (!Passwords.Verify(password, user.Hash))
|
||||
{
|
||||
return (LoginResult.WrongPassword, null);
|
||||
}
|
||||
|
||||
if (user.Totp is not null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(code))
|
||||
{
|
||||
return (LoginResult.SecondFactorNeeded, null);
|
||||
}
|
||||
|
||||
var secret = Convert.FromBase64String(secrets.Open("totp:" + user.Id.ToString("D"), user.Totp));
|
||||
if (!Totp.Verify(secret, code, clock.UtcNow))
|
||||
{
|
||||
return (LoginResult.WrongSecondFactor, null);
|
||||
}
|
||||
}
|
||||
|
||||
var token = Ids.NewToken();
|
||||
var now = clock.UtcNow;
|
||||
await db.WriteAsync(s => s.ExecuteAsync(
|
||||
"INSERT INTO sessions (id, user_id, token_hash, user_agent, address, created_at, last_seen_at, expires_at) VALUES (@id, @u, @h, @ua, @a, @t, @t, @e)",
|
||||
cancellationToken, ("id", Ids.NewId(clock)), ("u", user.Id), ("h", HashToken(token)), ("ua", Truncate(userAgent, 300)), ("a", Truncate(address, 100)),
|
||||
("t", now), ("e", now + MaxLifetime)), cancellationToken).ConfigureAwait(false);
|
||||
return (LoginResult.Ok, token);
|
||||
}
|
||||
|
||||
/// <summary>The session behind a cookie token, or null when unknown, idle too long or expired.</summary>
|
||||
public async Task<SessionInfo?> ValidateAsync(string? token, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (string.IsNullOrEmpty(token))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var now = clock.UtcNow;
|
||||
var found = await db.ReadAsync(s => s.FirstOrDefaultAsync(
|
||||
"SELECT s.id, s.created_at, s.last_seen_at, s.expires_at, u.id AS user_id, u.username, u.must_change_password, u.totp_secret FROM sessions s JOIN users u ON u.id = s.user_id WHERE s.token_hash = @h",
|
||||
r => new
|
||||
{
|
||||
Id = r.Id("id"),
|
||||
Created = r.Moment("created_at"),
|
||||
Seen = r.Moment("last_seen_at"),
|
||||
Expires = r.Moment("expires_at"),
|
||||
User = new User(r.Id("user_id"), r.Text("username"), r.Bool("must_change_password"), !r.IsNull("totp_secret")),
|
||||
}, ("h", HashToken(token))), cancellationToken).ConfigureAwait(false);
|
||||
if (found is null || now >= found.Expires || now - found.Seen >= IdleTimeout)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// Refresh the idle timer at most once a minute, to keep writes rare.
|
||||
if (now - found.Seen > TimeSpan.FromMinutes(1))
|
||||
{
|
||||
await db.WriteAsync(s => s.ExecuteAsync("UPDATE sessions SET last_seen_at = @t WHERE id = @id", cancellationToken, ("t", now), ("id", found.Id)), cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return new SessionInfo(found.Id, found.User, found.Created, found.Expires);
|
||||
}
|
||||
|
||||
public Task SignOutAsync(string token, CancellationToken cancellationToken = default) =>
|
||||
db.WriteAsync(s => s.ExecuteAsync("DELETE FROM sessions WHERE token_hash = @h", cancellationToken, ("h", HashToken(token))), cancellationToken);
|
||||
|
||||
/// <summary>Changes the password and ends every other session.</summary>
|
||||
public async Task<bool> ChangePasswordAsync(Guid userId, string current, string replacement, Guid keepSession, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var hash = await db.ReadAsync(s => s.FirstOrDefaultAsync("SELECT password_hash FROM users WHERE id = @id", r => r.Text("password_hash"), ("id", userId)), cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
if (hash is null || !Passwords.Verify(current, hash))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var replacementHash = Passwords.Hash(replacement, iterations);
|
||||
var now = clock.UtcNow;
|
||||
await db.WriteAsync(async s =>
|
||||
{
|
||||
await s.ExecuteAsync("UPDATE users SET password_hash = @h, must_change_password = @m, updated_at = @t WHERE id = @id", cancellationToken,
|
||||
("h", replacementHash), ("m", false), ("t", now), ("id", userId)).ConfigureAwait(false);
|
||||
await s.ExecuteAsync("DELETE FROM sessions WHERE user_id = @u AND id <> @keep", cancellationToken, ("u", userId), ("keep", keepSession)).ConfigureAwait(false);
|
||||
await LogAsync(s, "security.password", "cambiata", cancellationToken).ConfigureAwait(false);
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>Turns on the second factor after the user proved the app produces valid codes.</summary>
|
||||
public async Task<bool> EnableSecondFactorAsync(Guid userId, byte[] secret, string code, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(secret);
|
||||
if (!Totp.Verify(secret, code, clock.UtcNow))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var sealedSecret = secrets.Seal("totp:" + userId.ToString("D"), Convert.ToBase64String(secret));
|
||||
await db.WriteAsync(async s =>
|
||||
{
|
||||
await s.ExecuteAsync("UPDATE users SET totp_secret = @s, updated_at = @t WHERE id = @id", cancellationToken,
|
||||
("s", sealedSecret), ("t", clock.UtcNow), ("id", userId)).ConfigureAwait(false);
|
||||
await LogAsync(s, "security.second_factor", "attivato", cancellationToken).ConfigureAwait(false);
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> DisableSecondFactorAsync(Guid userId, string password, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var hash = await db.ReadAsync(s => s.FirstOrDefaultAsync("SELECT password_hash FROM users WHERE id = @id", r => r.Text("password_hash"), ("id", userId)), cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
if (hash is null || !Passwords.Verify(password, hash))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
await db.WriteAsync(async s =>
|
||||
{
|
||||
await s.ExecuteAsync("UPDATE users SET totp_secret = NULL, updated_at = @t WHERE id = @id", cancellationToken, ("t", clock.UtcNow), ("id", userId)).ConfigureAwait(false);
|
||||
await LogAsync(s, "security.second_factor", "disattivato", cancellationToken).ConfigureAwait(false);
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
private Task<int> LogAsync(DbSession s, string key, string what, CancellationToken cancellationToken) =>
|
||||
s.ExecuteAsync("INSERT INTO config_history (id, key, old_value, new_value, changed_by, created_at) VALUES (@id, @k, NULL, @n, @by, @t)", cancellationToken,
|
||||
("id", Ids.NewId(clock)), ("k", key), ("n", new DbJson(System.Text.Json.JsonSerializer.Serialize(what))), ("by", AdminName), ("t", clock.UtcNow));
|
||||
|
||||
private static string HashToken(string token) => Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(token)));
|
||||
|
||||
private static string? Truncate(string? value, int length) => value is null || value.Length <= length ? value : value[..length];
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
using System.Globalization;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace Anubis.Access;
|
||||
|
||||
/// <summary>
|
||||
/// Password hashes with PBKDF2-SHA512 (<see cref="Rfc2898DeriveBytes.Pbkdf2(string, byte[], int, HashAlgorithmName, int)"/>),
|
||||
/// in place of Argon2id (ADR 0003). Stored as <c>pbkdf2-sha512$iterations$salt$hash</c> so the cost can grow later.
|
||||
/// </summary>
|
||||
public static class Passwords
|
||||
{
|
||||
/// <summary>OWASP's figure for PBKDF2-HMAC-SHA512.</summary>
|
||||
public const int DefaultIterations = 210_000;
|
||||
|
||||
public const int MinimumLength = 10;
|
||||
|
||||
private const string Scheme = "pbkdf2-sha512";
|
||||
|
||||
public static string Hash(string password, int iterations = DefaultIterations)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(password);
|
||||
var salt = RandomNumberGenerator.GetBytes(16);
|
||||
var hash = Rfc2898DeriveBytes.Pbkdf2(Encoding.UTF8.GetBytes(password), salt, iterations, HashAlgorithmName.SHA512, 64);
|
||||
return string.Join('$', Scheme, iterations.ToString(CultureInfo.InvariantCulture), Convert.ToBase64String(salt), Convert.ToBase64String(hash));
|
||||
}
|
||||
|
||||
/// <summary>Checks a password in constant time. A malformed stored hash never verifies.</summary>
|
||||
public static bool Verify(string password, string stored)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(password);
|
||||
ArgumentNullException.ThrowIfNull(stored);
|
||||
var parts = stored.Split('$');
|
||||
if (parts.Length != 4 || parts[0] != Scheme || !int.TryParse(parts[1], NumberStyles.None, CultureInfo.InvariantCulture, out var iterations) || iterations < 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
byte[] salt;
|
||||
byte[] expected;
|
||||
try
|
||||
{
|
||||
salt = Convert.FromBase64String(parts[2]);
|
||||
expected = Convert.FromBase64String(parts[3]);
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var actual = Rfc2898DeriveBytes.Pbkdf2(Encoding.UTF8.GetBytes(password), salt, iterations, HashAlgorithmName.SHA512, expected.Length);
|
||||
return CryptographicOperations.FixedTimeEquals(actual, expected);
|
||||
}
|
||||
|
||||
/// <summary>What is wrong with a new password, in plain Italian, or null when it is acceptable.</summary>
|
||||
public static string? Problem(string? password, string? initialPassword = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(password) || password.Length < MinimumLength)
|
||||
{
|
||||
return $"La password deve avere almeno {MinimumLength} caratteri.";
|
||||
}
|
||||
|
||||
if (initialPassword is not null && password == initialPassword)
|
||||
{
|
||||
return "Scegli una password diversa da quella iniziale del container.";
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Base32 without padding (RFC 4648), the format authenticator apps expect for TOTP secrets.</summary>
|
||||
public static class Base32
|
||||
{
|
||||
private const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
||||
|
||||
public static string Encode(ReadOnlySpan<byte> data)
|
||||
{
|
||||
var text = new StringBuilder((data.Length * 8 + 4) / 5);
|
||||
int buffer = 0, bits = 0;
|
||||
foreach (var b in data)
|
||||
{
|
||||
buffer = (buffer << 8) | b;
|
||||
bits += 8;
|
||||
while (bits >= 5)
|
||||
{
|
||||
text.Append(Alphabet[(buffer >> (bits - 5)) & 31]);
|
||||
bits -= 5;
|
||||
}
|
||||
}
|
||||
|
||||
if (bits > 0)
|
||||
{
|
||||
text.Append(Alphabet[(buffer << (5 - bits)) & 31]);
|
||||
}
|
||||
|
||||
return text.ToString();
|
||||
}
|
||||
|
||||
public static byte[] Decode(string text)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(text);
|
||||
var clean = text.Replace(" ", "", StringComparison.Ordinal).Replace("-", "", StringComparison.Ordinal).TrimEnd('=').ToUpperInvariant();
|
||||
var result = new List<byte>(clean.Length * 5 / 8);
|
||||
int buffer = 0, bits = 0;
|
||||
foreach (var c in clean)
|
||||
{
|
||||
var value = Alphabet.IndexOf(c, StringComparison.Ordinal);
|
||||
if (value < 0)
|
||||
{
|
||||
throw new FormatException("Carattere non valido nel codice: " + c);
|
||||
}
|
||||
|
||||
buffer = (buffer << 5) | value;
|
||||
bits += 5;
|
||||
if (bits >= 8)
|
||||
{
|
||||
result.Add((byte)(buffer >> (bits - 8)));
|
||||
bits -= 8;
|
||||
}
|
||||
}
|
||||
|
||||
return [.. result];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Time-based one-time passwords (RFC 6238), written in house and checked against the RFC's test vectors.
|
||||
/// Defaults are the ones every authenticator app uses: HMAC-SHA1, 6 digits, 30-second steps.
|
||||
/// </summary>
|
||||
public static class Totp
|
||||
{
|
||||
public static byte[] NewSecret() => RandomNumberGenerator.GetBytes(20);
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "CA5350", Justification = "RFC 6238 and every authenticator app use HMAC-SHA1; HMAC keeps SHA-1 safe here.")]
|
||||
public static string Compute(ReadOnlySpan<byte> secret, DateTimeOffset at, int digits = 6, int stepSeconds = 30, string algorithm = "SHA1")
|
||||
{
|
||||
var counter = at.ToUnixTimeSeconds() / stepSeconds;
|
||||
Span<byte> message = stackalloc byte[8];
|
||||
for (var i = 7; i >= 0; i--)
|
||||
{
|
||||
message[i] = (byte)(counter & 0xff);
|
||||
counter >>= 8;
|
||||
}
|
||||
|
||||
byte[] hash = algorithm switch
|
||||
{
|
||||
"SHA1" => HMACSHA1.HashData(secret, message),
|
||||
"SHA256" => HMACSHA256.HashData(secret, message),
|
||||
"SHA512" => HMACSHA512.HashData(secret, message),
|
||||
_ => throw new ArgumentException("Unsupported algorithm " + algorithm, nameof(algorithm)),
|
||||
};
|
||||
var offset = hash[^1] & 0x0f;
|
||||
var binary = ((hash[offset] & 0x7f) << 24) | (hash[offset + 1] << 16) | (hash[offset + 2] << 8) | hash[offset + 3];
|
||||
var modulo = (int)Math.Pow(10, digits);
|
||||
return (binary % modulo).ToString(CultureInfo.InvariantCulture).PadLeft(digits, '0');
|
||||
}
|
||||
|
||||
/// <summary>Accepts the code of the current step and of one step before or after, for clocks a little apart.</summary>
|
||||
public static bool Verify(ReadOnlySpan<byte> secret, string? code, DateTimeOffset now)
|
||||
{
|
||||
if (code is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
code = code.Replace(" ", "", StringComparison.Ordinal);
|
||||
if (code.Length != 6 || !code.All(char.IsAsciiDigit))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var ok = false;
|
||||
for (var step = -1; step <= 1; step++)
|
||||
{
|
||||
var expected = Compute(secret, now.AddSeconds(step * 30));
|
||||
ok |= CryptographicOperations.FixedTimeEquals(Encoding.ASCII.GetBytes(expected), Encoding.ASCII.GetBytes(code));
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
/// <summary>The address authenticator apps read (otpauth://), for manual entry or a QR code.</summary>
|
||||
public static string Uri(string issuer, string account, ReadOnlySpan<byte> secret) =>
|
||||
$"otpauth://totp/{System.Uri.EscapeDataString(issuer)}:{System.Uri.EscapeDataString(account)}?secret={Base32.Encode(secret)}&issuer={System.Uri.EscapeDataString(issuer)}&algorithm=SHA1&digits=6&period=30";
|
||||
}
|
||||
@@ -2,6 +2,14 @@
|
||||
<PropertyGroup>
|
||||
<RootNamespace>Anubis</RootNamespace>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Npgsql" />
|
||||
<PackageReference Include="Microsoft.Data.Sqlite" />
|
||||
<PackageReference Include="System.Security.Cryptography.ProtectedData" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="Db\Migrations\*.sql" LogicalName="Anubis.Db.Migrations.%(Filename)%(Extension)" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="Anubis.Tests" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
namespace Anubis.Core;
|
||||
|
||||
/// <summary>
|
||||
/// The only source of time in Anubis (SPEC 16, trap 12): no code reads the system clock directly,
|
||||
/// so tests and the demo mode can run at any moment.
|
||||
/// </summary>
|
||||
public interface IClock
|
||||
{
|
||||
DateTimeOffset UtcNow { get; }
|
||||
|
||||
/// <summary>Waits until the clock reaches <paramref name="dueUtc"/>. A manual clock returns when it is moved past it.</summary>
|
||||
Task WaitUntilAsync(DateTimeOffset dueUtc, CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>The real clock of the machine.</summary>
|
||||
public sealed class SystemClock : IClock
|
||||
{
|
||||
public static SystemClock Instance { get; } = new();
|
||||
|
||||
public DateTimeOffset UtcNow => DateTimeOffset.UtcNow;
|
||||
|
||||
public async Task WaitUntilAsync(DateTimeOffset dueUtc, CancellationToken cancellationToken)
|
||||
{
|
||||
// Wake at least once a minute: after a sleep of the computer the remaining time is recomputed (trap 21).
|
||||
while (true)
|
||||
{
|
||||
var remaining = dueUtc - UtcNow;
|
||||
if (remaining <= TimeSpan.Zero)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await Task.Delay(remaining < TimeSpan.FromMinutes(1) ? remaining : TimeSpan.FromMinutes(1), cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A clock that moves only when told to: tests and the frozen demo clock (SPEC 13.1).</summary>
|
||||
public sealed class ManualClock(DateTimeOffset start) : IClock
|
||||
{
|
||||
private readonly Lock _gate = new();
|
||||
private readonly List<(DateTimeOffset Due, TaskCompletionSource Done)> _waiters = [];
|
||||
private DateTimeOffset _now = start.ToUniversalTime();
|
||||
|
||||
public DateTimeOffset UtcNow
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
return _now;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Set(DateTimeOffset value)
|
||||
{
|
||||
List<TaskCompletionSource> ready;
|
||||
lock (_gate)
|
||||
{
|
||||
_now = value.ToUniversalTime();
|
||||
ready = _waiters.Where(w => w.Due <= _now).Select(w => w.Done).ToList();
|
||||
_waiters.RemoveAll(w => w.Due <= _now);
|
||||
}
|
||||
|
||||
foreach (var done in ready)
|
||||
{
|
||||
done.TrySetResult();
|
||||
}
|
||||
}
|
||||
|
||||
public void Advance(TimeSpan by) => Set(UtcNow + by);
|
||||
|
||||
/// <summary>Number of callers waiting for a future moment; lets tests know a loop has gone to sleep.</summary>
|
||||
public int Waiting
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
return _waiters.Count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Task WaitUntilAsync(DateTimeOffset dueUtc, CancellationToken cancellationToken)
|
||||
{
|
||||
var done = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
lock (_gate)
|
||||
{
|
||||
if (dueUtc <= _now)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
_waiters.Add((dueUtc, done));
|
||||
}
|
||||
|
||||
cancellationToken.Register(() =>
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
_waiters.RemoveAll(w => w.Done == done);
|
||||
}
|
||||
|
||||
done.TrySetCanceled(cancellationToken);
|
||||
});
|
||||
return done.Task;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
using System.Threading.Channels;
|
||||
|
||||
namespace Anubis.Core;
|
||||
|
||||
/// <summary>
|
||||
/// In-process event bus (ADR 0003) on <see cref="System.Threading.Channels"/>. Every subscriber gets its own
|
||||
/// bounded queue: a slow reader loses its oldest events instead of blocking the publisher (prices and orders
|
||||
/// must never wait for the interface).
|
||||
/// </summary>
|
||||
public sealed class EventBus
|
||||
{
|
||||
private readonly Lock _gate = new();
|
||||
private readonly List<Subscription> _subscriptions = [];
|
||||
|
||||
/// <summary>Delivers <paramref name="message"/> to every current subscriber of <typeparamref name="T"/> (or a base type).</summary>
|
||||
public void Publish<T>(T message)
|
||||
where T : notnull
|
||||
{
|
||||
Subscription[] targets;
|
||||
lock (_gate)
|
||||
{
|
||||
targets = [.. _subscriptions];
|
||||
}
|
||||
|
||||
foreach (var subscription in targets)
|
||||
{
|
||||
if (subscription.Type.IsInstanceOfType(message))
|
||||
{
|
||||
subscription.Writer.TryWrite(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Starts receiving messages of type <typeparamref name="T"/>. Dispose the result to stop.</summary>
|
||||
public EventSubscription<T> Subscribe<T>(int capacity = 256)
|
||||
{
|
||||
var channel = Channel.CreateBounded<object>(new BoundedChannelOptions(capacity)
|
||||
{
|
||||
FullMode = BoundedChannelFullMode.DropOldest,
|
||||
SingleReader = true,
|
||||
});
|
||||
var subscription = new Subscription(typeof(T), channel.Writer);
|
||||
lock (_gate)
|
||||
{
|
||||
_subscriptions.Add(subscription);
|
||||
}
|
||||
|
||||
return new EventSubscription<T>(channel.Reader, () =>
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
_subscriptions.Remove(subscription);
|
||||
}
|
||||
|
||||
channel.Writer.TryComplete();
|
||||
});
|
||||
}
|
||||
|
||||
public int SubscriberCount
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
return _subscriptions.Count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private sealed record Subscription(Type Type, ChannelWriter<object> Writer);
|
||||
}
|
||||
|
||||
/// <summary>A live subscription to the <see cref="EventBus"/>.</summary>
|
||||
public sealed class EventSubscription<T>(ChannelReader<object> reader, Action unsubscribe) : IDisposable
|
||||
{
|
||||
public async IAsyncEnumerable<T> ReadAllAsync([System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
|
||||
{
|
||||
await foreach (var item in reader.ReadAllAsync(cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
yield return (T)item;
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryRead(out T? message)
|
||||
{
|
||||
if (reader.TryRead(out var item))
|
||||
{
|
||||
message = (T)item;
|
||||
return true;
|
||||
}
|
||||
|
||||
message = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Dispose() => unsubscribe();
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
|
||||
namespace Anubis.Core;
|
||||
|
||||
/// <summary>
|
||||
/// Every number and date the user reads, written the Italian way (SPEC 13.3): decimal comma, dot for
|
||||
/// thousands, currency symbol after a non-breaking space, typographic minus, plus sign on results.
|
||||
/// The rest of the code never formats user-facing numbers by itself.
|
||||
/// </summary>
|
||||
public static class Format
|
||||
{
|
||||
/// <summary>U+2212, the typographic minus.</summary>
|
||||
public const char Minus = '−';
|
||||
|
||||
/// <summary>U+00A0, the space between a number and its currency symbol.</summary>
|
||||
public const char NonBreakingSpace = ' ';
|
||||
|
||||
private static readonly NumberFormatInfo _italian = new()
|
||||
{
|
||||
NumberDecimalSeparator = ",",
|
||||
NumberGroupSeparator = ".",
|
||||
NumberGroupSizes = [3],
|
||||
NegativeSign = Minus.ToString(),
|
||||
PositiveSign = "+",
|
||||
};
|
||||
|
||||
private static readonly string[] _days = ["dom", "lun", "mar", "mer", "gio", "ven", "sab"];
|
||||
private static readonly string[] _daysLong = ["domenica", "lunedì", "martedì", "mercoledì", "giovedì", "venerdì", "sabato"];
|
||||
private static readonly string[] _months = ["gen", "feb", "mar", "apr", "mag", "giu", "lug", "ago", "set", "ott", "nov", "dic"];
|
||||
private static readonly string[] _monthsLong =
|
||||
["gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre"];
|
||||
|
||||
/// <summary>A number with Italian separators, rounded half away from zero: <c>10.412</c>, <c>−2,57</c>.</summary>
|
||||
public static string Number(decimal value, int decimals = 0, bool withSign = false)
|
||||
{
|
||||
var rounded = Math.Round(value, decimals, MidpointRounding.AwayFromZero);
|
||||
var text = Math.Abs(rounded).ToString("N" + decimals.ToString(CultureInfo.InvariantCulture), _italian);
|
||||
if (rounded < 0)
|
||||
{
|
||||
return Minus + text;
|
||||
}
|
||||
|
||||
return withSign && rounded > 0 ? "+" + text : text;
|
||||
}
|
||||
|
||||
/// <summary>An amount with its currency after a non-breaking space: <c>1.299,84 €</c>, <c>+18,40 €</c>, <c>−26,00 €</c>.</summary>
|
||||
public static string Money(decimal value, string currency = "EUR", bool withSign = false, int decimals = 2) =>
|
||||
Number(value, decimals, withSign) + NonBreakingSpace + CurrencySymbol(currency);
|
||||
|
||||
public static string CurrencySymbol(string currency) => currency.ToUpperInvariant() switch
|
||||
{
|
||||
"EUR" => "€",
|
||||
"USD" => "$",
|
||||
_ => currency.ToUpperInvariant(),
|
||||
};
|
||||
|
||||
/// <summary>A percentage given in percent units: <c>Percent(4.12m, 2, true)</c> is <c>+4,12%</c>.</summary>
|
||||
public static string Percent(decimal percent, int decimals = 1, bool withSign = false) =>
|
||||
Number(percent, decimals, withSign) + "%";
|
||||
|
||||
/// <summary>
|
||||
/// Shares of a whole in percent that always add up to exactly 100 (largest remainder method, SPEC 13.3).
|
||||
/// Negative or zero weights get 0; with no positive weight every share is 0.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<decimal> Shares(IReadOnlyList<decimal> weights, int decimals = 1)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(weights);
|
||||
var positive = weights.Select(w => w > 0 ? w : 0m).ToArray();
|
||||
var total = positive.Sum();
|
||||
var result = new decimal[positive.Length];
|
||||
if (total == 0)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
var unit = 1m;
|
||||
for (var i = 0; i < decimals; i++)
|
||||
{
|
||||
unit /= 10m;
|
||||
}
|
||||
|
||||
var units = (long)Math.Round(100m / unit);
|
||||
var exact = positive.Select(w => w / total * units).ToArray();
|
||||
var floors = exact.Select(Math.Floor).ToArray();
|
||||
var missing = units - (long)floors.Sum();
|
||||
var order = Enumerable.Range(0, exact.Length)
|
||||
.OrderByDescending(i => exact[i] - floors[i])
|
||||
.ThenByDescending(i => positive[i])
|
||||
.ThenBy(i => i)
|
||||
.ToList();
|
||||
for (var k = 0; k < missing; k++)
|
||||
{
|
||||
floors[order[k % order.Count]] += 1;
|
||||
}
|
||||
|
||||
for (var i = 0; i < floors.Length; i++)
|
||||
{
|
||||
result[i] = floors[i] * unit;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>Short spoken date in the user's time zone: <c>Oggi 06:10</c>, <c>Ieri 21:58</c>, <c>mar 6 ott</c>.</summary>
|
||||
public static string ShortDate(DateTimeOffset moment, DateTimeOffset now, TimeZoneInfo zone, bool withTime = true)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(zone);
|
||||
var local = TimeZoneInfo.ConvertTime(moment, zone);
|
||||
var today = TimeZoneInfo.ConvertTime(now, zone).Date;
|
||||
var time = local.ToString("HH:mm", CultureInfo.InvariantCulture);
|
||||
if (local.Date == today)
|
||||
{
|
||||
return withTime ? "Oggi " + time : "Oggi";
|
||||
}
|
||||
|
||||
if (local.Date == today.AddDays(-1))
|
||||
{
|
||||
return withTime ? "Ieri " + time : "Ieri";
|
||||
}
|
||||
|
||||
if (local.Date == today.AddDays(1))
|
||||
{
|
||||
return withTime ? "Domani " + time : "Domani";
|
||||
}
|
||||
|
||||
var text = $"{_days[(int)local.DayOfWeek]} {local.Day.ToString(CultureInfo.InvariantCulture)} {_months[local.Month - 1]}";
|
||||
if (local.Year != today.Year)
|
||||
{
|
||||
text += " " + local.Year.ToString(CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
/// <summary>Full date for the top bar: <c>Venerdì 9 ottobre 2026, ore 08:21</c>.</summary>
|
||||
public static string LongDateTime(DateTimeOffset moment, TimeZoneInfo zone)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(zone);
|
||||
var local = TimeZoneInfo.ConvertTime(moment, zone);
|
||||
var day = _daysLong[(int)local.DayOfWeek];
|
||||
return string.Create(CultureInfo.InvariantCulture,
|
||||
$"{char.ToUpperInvariant(day[0])}{day[1..]} {local.Day} {_monthsLong[local.Month - 1]} {local.Year}, ore {local:HH:mm}");
|
||||
}
|
||||
|
||||
/// <summary>A date without time: <c>12 agosto 2026</c>.</summary>
|
||||
public static string LongDate(DateOnly date) =>
|
||||
string.Create(CultureInfo.InvariantCulture, $"{date.Day} {_monthsLong[date.Month - 1]} {date.Year}");
|
||||
|
||||
/// <summary>How long ago, in words: <c>4 secondi fa</c>, <c>3 minuti fa</c>.</summary>
|
||||
public static string Ago(TimeSpan elapsed)
|
||||
{
|
||||
if (elapsed < TimeSpan.Zero)
|
||||
{
|
||||
elapsed = TimeSpan.Zero;
|
||||
}
|
||||
|
||||
static string Unit(long n, string one, string many) =>
|
||||
n.ToString(CultureInfo.InvariantCulture) + " " + (n == 1 ? one : many) + " fa";
|
||||
|
||||
return elapsed.TotalSeconds < 60 ? Unit((long)elapsed.TotalSeconds, "secondo", "secondi")
|
||||
: elapsed.TotalMinutes < 60 ? Unit((long)elapsed.TotalMinutes, "minuto", "minuti")
|
||||
: elapsed.TotalHours < 24 ? Unit((long)elapsed.TotalHours, "ora", "ore")
|
||||
: Unit((long)elapsed.TotalDays, "giorno", "giorni");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// CSV export (SPEC 13.3): semicolon separator, decimal comma, UTF-8 with BOM, Italian headers.
|
||||
/// Decimals are written without thousands separators so spreadsheets read them as numbers.
|
||||
/// </summary>
|
||||
public static byte[] Csv(IReadOnlyList<string> headers, IEnumerable<IReadOnlyList<object?>> rows)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(headers);
|
||||
ArgumentNullException.ThrowIfNull(rows);
|
||||
var text = new StringBuilder();
|
||||
text.AppendJoin(';', headers.Select(CsvField)).Append("\r\n");
|
||||
foreach (var row in rows)
|
||||
{
|
||||
text.AppendJoin(';', row.Select(cell => CsvField(CsvValue(cell)))).Append("\r\n");
|
||||
}
|
||||
|
||||
var preamble = Encoding.UTF8.GetPreamble();
|
||||
var body = Encoding.UTF8.GetBytes(text.ToString());
|
||||
return [.. preamble, .. body];
|
||||
}
|
||||
|
||||
private static string CsvValue(object? cell) => cell switch
|
||||
{
|
||||
null => "",
|
||||
decimal d => d.ToString(CultureInfo.InvariantCulture).Replace('.', ','),
|
||||
double f => f.ToString("R", CultureInfo.InvariantCulture).Replace('.', ','),
|
||||
int or long => Convert.ToString(cell, CultureInfo.InvariantCulture)!,
|
||||
DateOnly date => date.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
|
||||
DateTimeOffset moment => moment.ToString("dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture),
|
||||
_ => Convert.ToString(cell, CultureInfo.InvariantCulture) ?? "",
|
||||
};
|
||||
|
||||
private static string CsvField(string value) =>
|
||||
value.IndexOfAny([';', '"', '\r', '\n']) >= 0 ? "\"" + value.Replace("\"", "\"\"", StringComparison.Ordinal) + "\"" : value;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace Anubis.Core;
|
||||
|
||||
/// <summary>Surrogate keys (SPEC 5.4): UUID version 7, ordered by creation time.</summary>
|
||||
public static class Ids
|
||||
{
|
||||
/// <summary>A new UUIDv7 whose time part comes from the injected clock, so ids follow simulated time too.</summary>
|
||||
public static Guid NewId(IClock clock)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(clock);
|
||||
return Guid.CreateVersion7(clock.UtcNow);
|
||||
}
|
||||
|
||||
/// <summary>A random token for sessions and fingerprints, URL-safe base64 of 32 bytes.</summary>
|
||||
public static string NewToken(int bytes = 32)
|
||||
{
|
||||
var buffer = RandomNumberGenerator.GetBytes(bytes);
|
||||
return Convert.ToBase64String(buffer).TrimEnd('=').Replace('+', '-').Replace('/', '_');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Anubis.Core;
|
||||
|
||||
/// <summary>
|
||||
/// Removes keys, signatures, passwords and authentication headers from text before it is written to a log
|
||||
/// (trap 17, SPEC 15.4). Two layers: known secret values registered at runtime, and patterns for the
|
||||
/// shapes secrets take in headers, URLs and key=value pairs.
|
||||
/// </summary>
|
||||
public static partial class Redactor
|
||||
{
|
||||
public const string Mask = "***";
|
||||
|
||||
private static readonly Lock _gate = new();
|
||||
private static string[] _known = [];
|
||||
|
||||
/// <summary>Registers a secret value (a key read from the environment, a password) so it is masked wherever it appears.</summary>
|
||||
public static void Register(string? secret)
|
||||
{
|
||||
// Very short values would mask ordinary words; secrets that short are rejected elsewhere anyway.
|
||||
if (string.IsNullOrEmpty(secret) || secret.Length < 6)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
lock (_gate)
|
||||
{
|
||||
if (!_known.Contains(secret, StringComparer.Ordinal))
|
||||
{
|
||||
_known = [.. _known.Append(secret).OrderByDescending(s => s.Length)];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static string Redact(string? text)
|
||||
{
|
||||
if (string.IsNullOrEmpty(text))
|
||||
{
|
||||
return text ?? "";
|
||||
}
|
||||
|
||||
foreach (var secret in _known)
|
||||
{
|
||||
text = text.Replace(secret, Mask, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
text = SensitiveHeader().Replace(text, m => m.Groups["name"].Value + m.Groups["sep"].Value + Mask);
|
||||
text = Bearer().Replace(text, m => m.Groups["scheme"].Value + " " + Mask);
|
||||
text = SensitivePair().Replace(text, m => m.Groups["name"].Value + m.Groups["sep"].Value + Mask);
|
||||
text = UrlCredentials().Replace(text, m => m.Groups["scheme"].Value + Mask + "@");
|
||||
text = TelegramToken().Replace(text, Mask);
|
||||
return text;
|
||||
}
|
||||
|
||||
// Broker authentication headers (Alpaca, OKX) and the standard ones.
|
||||
[GeneratedRegex(@"(?<name>\b(?:APCA-API-KEY-ID|APCA-API-SECRET-KEY|OK-ACCESS-KEY|OK-ACCESS-SIGN|OK-ACCESS-PASSPHRASE|Authorization|Proxy-Authorization|Cookie|Set-Cookie|X-Api-Key)\b)(?<sep>\s*[:=]\s*)[^\r\n]+", RegexOptions.IgnoreCase)]
|
||||
private static partial Regex SensitiveHeader();
|
||||
|
||||
[GeneratedRegex(@"(?<scheme>\b(?:Bearer|Basic|token))\s+[A-Za-z0-9\-._~+/]{8,}=*", RegexOptions.IgnoreCase)]
|
||||
private static partial Regex Bearer();
|
||||
|
||||
// key=value, "key": "value", key: value for names that carry secrets.
|
||||
[GeneratedRegex(@"(?<name>[""']?[A-Za-z0-9_\-]*(?:password|passwd|secret|token|api[_-]?key|apikey|passphrase|signature|sign|private[_-]?key)[""']?)(?<sep>\s*[:=]\s*[""']?)[^\s""'&,;}]+", RegexOptions.IgnoreCase)]
|
||||
private static partial Regex SensitivePair();
|
||||
|
||||
// Credentials inside connection URLs: postgres://user:password@host.
|
||||
[GeneratedRegex(@"(?<scheme>\b[a-z][a-z0-9+.\-]*://)[^\s/@:]+:[^\s/@]+@", RegexOptions.IgnoreCase)]
|
||||
private static partial Regex UrlCredentials();
|
||||
|
||||
[GeneratedRegex(@"\d{8,10}:[A-Za-z0-9_\-]{35}\b")]
|
||||
private static partial Regex TelegramToken();
|
||||
}
|
||||
@@ -0,0 +1,258 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
namespace Anubis.Core;
|
||||
|
||||
/// <summary>A trading session as the broker's calendar gives it (SPEC 5.5): early closes included.</summary>
|
||||
public sealed record MarketSession(DateOnly Date, DateTimeOffset OpenUtc, DateTimeOffset CloseUtc);
|
||||
|
||||
/// <summary>
|
||||
/// Market calendar. Market times are never derived from Italian time: US and European daylight saving
|
||||
/// change in different weeks (trap 2). The real calendar comes from the broker in phase 2.
|
||||
/// </summary>
|
||||
public interface IMarketCalendar
|
||||
{
|
||||
/// <summary>The session of the exchange's local <paramref name="day"/>, or null when the market is closed.</summary>
|
||||
MarketSession? SessionOn(DateOnly day);
|
||||
}
|
||||
|
||||
public enum MarketAnchor
|
||||
{
|
||||
Open,
|
||||
Close,
|
||||
}
|
||||
|
||||
/// <summary>When a job runs. <see cref="NextAfter"/> is pure: it depends only on its argument.</summary>
|
||||
public abstract record Schedule
|
||||
{
|
||||
/// <summary>The first run strictly after <paramref name="afterUtc"/>, or null if there is none in sight.</summary>
|
||||
public abstract DateTimeOffset? NextAfter(DateTimeOffset afterUtc);
|
||||
|
||||
/// <summary>Every day at a wall-clock time in a time zone, e.g. 03:30 Europe/Rome or 00:05 UTC.</summary>
|
||||
public static Schedule Daily(TimeOnly time, TimeZoneInfo zone) => new DailySchedule(time, zone);
|
||||
|
||||
/// <summary>At fixed intervals from midnight UTC, e.g. every 5 minutes.</summary>
|
||||
public static Schedule Every(TimeSpan interval) => new IntervalSchedule(interval);
|
||||
|
||||
/// <summary>Relative to the open or close of each trading session, e.g. 5 minutes after the open.</summary>
|
||||
public static Schedule Market(MarketAnchor anchor, TimeSpan offset, IMarketCalendar calendar) =>
|
||||
new MarketSchedule(anchor, offset, calendar);
|
||||
}
|
||||
|
||||
internal sealed record DailySchedule(TimeOnly Time, TimeZoneInfo Zone) : Schedule
|
||||
{
|
||||
public override DateTimeOffset? NextAfter(DateTimeOffset afterUtc)
|
||||
{
|
||||
var startDay = DateOnly.FromDateTime(TimeZoneInfo.ConvertTime(afterUtc, Zone).DateTime).AddDays(-1);
|
||||
for (var day = startDay; day <= startDay.AddDays(3); day = day.AddDays(1))
|
||||
{
|
||||
var candidate = ToUtc(day.ToDateTime(Time));
|
||||
if (candidate > afterUtc)
|
||||
{
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// A time skipped by daylight saving moves forward by the gap; a repeated one takes its first occurrence.
|
||||
private DateTimeOffset ToUtc(DateTime local)
|
||||
{
|
||||
local = DateTime.SpecifyKind(local, DateTimeKind.Unspecified);
|
||||
while (Zone.IsInvalidTime(local))
|
||||
{
|
||||
local = local.AddMinutes(1);
|
||||
}
|
||||
|
||||
var offset = Zone.IsAmbiguousTime(local) ? Zone.GetAmbiguousTimeOffsets(local).Max() : Zone.GetUtcOffset(local);
|
||||
return new DateTimeOffset(local, offset).ToUniversalTime();
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed record IntervalSchedule(TimeSpan Interval) : Schedule
|
||||
{
|
||||
public override DateTimeOffset? NextAfter(DateTimeOffset afterUtc)
|
||||
{
|
||||
if (Interval <= TimeSpan.Zero)
|
||||
{
|
||||
throw new InvalidOperationException("The interval must be positive.");
|
||||
}
|
||||
|
||||
var ticks = afterUtc.UtcTicks;
|
||||
var next = (ticks / Interval.Ticks + 1) * Interval.Ticks;
|
||||
return new DateTimeOffset(next, TimeSpan.Zero);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed record MarketSchedule(MarketAnchor Anchor, TimeSpan Offset, IMarketCalendar Calendar) : Schedule
|
||||
{
|
||||
public override DateTimeOffset? NextAfter(DateTimeOffset afterUtc)
|
||||
{
|
||||
// Sessions are looked up by exchange date; two days of margin cover any offset and time zone.
|
||||
var first = DateOnly.FromDateTime(afterUtc.UtcDateTime).AddDays(-2);
|
||||
for (var day = first; day <= first.AddDays(16); day = day.AddDays(1))
|
||||
{
|
||||
var session = Calendar.SessionOn(day);
|
||||
if (session is null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var at = (Anchor == MarketAnchor.Open ? session.OpenUtc : session.CloseUtc) + Offset;
|
||||
if (at > afterUtc)
|
||||
{
|
||||
return at;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A job registered with the <see cref="Scheduler"/>.</summary>
|
||||
public sealed record ScheduledJob(string Name, string Description, Schedule Schedule, Func<CancellationToken, Task> Run)
|
||||
{
|
||||
/// <summary>
|
||||
/// How late a run may start. After a sleep of the computer or a long pause, a run later than this is
|
||||
/// skipped and reported instead of executed blindly (trap 21, SPEC 5.6).
|
||||
/// </summary>
|
||||
public TimeSpan MaxLateness { get; init; } = TimeSpan.FromMinutes(15);
|
||||
}
|
||||
|
||||
public enum JobOutcome
|
||||
{
|
||||
Completed,
|
||||
Failed,
|
||||
Skipped,
|
||||
}
|
||||
|
||||
/// <summary>Published on the bus after every run, skip or failure.</summary>
|
||||
public sealed record JobRun(string Name, DateTimeOffset DueUtc, DateTimeOffset StartedUtc, DateTimeOffset FinishedUtc, JobOutcome Outcome, string? Error);
|
||||
|
||||
/// <summary>A run coming up, for "Prossime mosse" and the jobs list.</summary>
|
||||
public sealed record UpcomingRun(string Name, string Description, DateTimeOffset DueUtc);
|
||||
|
||||
/// <summary>
|
||||
/// Runs jobs at fixed times, at intervals, or relative to market open and close (SPEC 5.5).
|
||||
/// All waiting goes through <see cref="IClock"/>, so tests drive it with a manual clock.
|
||||
/// </summary>
|
||||
public sealed partial class Scheduler(IClock clock, EventBus bus, ILogger<Scheduler>? logger = null)
|
||||
{
|
||||
private readonly ILogger _logger = logger ?? NullLogger<Scheduler>.Instance;
|
||||
private readonly Lock _gate = new();
|
||||
private readonly Dictionary<string, (ScheduledJob Job, DateTimeOffset? Due)> _jobs = new(StringComparer.Ordinal);
|
||||
private TaskCompletionSource _changed = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
|
||||
/// <summary>Moment of the last loop iteration; the health check uses it to see the loop is alive.</summary>
|
||||
public DateTimeOffset? LastBeatUtc { get; private set; }
|
||||
|
||||
public void Add(ScheduledJob job)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(job);
|
||||
lock (_gate)
|
||||
{
|
||||
_jobs[job.Name] = (job, job.Schedule.NextAfter(clock.UtcNow));
|
||||
_changed.TrySetResult();
|
||||
}
|
||||
}
|
||||
|
||||
public IReadOnlyList<UpcomingRun> Upcoming(int count = 10)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
return _jobs.Values.Where(j => j.Due is not null)
|
||||
.OrderBy(j => j.Due)
|
||||
.Take(count)
|
||||
.Select(j => new UpcomingRun(j.Job.Name, j.Job.Description, j.Due!.Value))
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RunAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
LastBeatUtc = clock.UtcNow;
|
||||
DateTimeOffset? next;
|
||||
Task changed;
|
||||
lock (_gate)
|
||||
{
|
||||
next = _jobs.Values.Where(j => j.Due is not null).Select(j => j.Due).Min();
|
||||
changed = _changed.Task;
|
||||
}
|
||||
|
||||
// Wake at the next due time, when a job is added, or at least every minute for the heartbeat.
|
||||
var wakeAt = next is { } due && due < clock.UtcNow.AddMinutes(1) ? due : clock.UtcNow.AddMinutes(1);
|
||||
using (var wakeCancel = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken))
|
||||
{
|
||||
var waiting = clock.WaitUntilAsync(wakeAt, wakeCancel.Token);
|
||||
await Task.WhenAny(waiting, changed).ConfigureAwait(false);
|
||||
await wakeCancel.CancelAsync().ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
await waiting.ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
// A new job arrived: recompute.
|
||||
}
|
||||
}
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
lock (_gate)
|
||||
{
|
||||
if (_changed.Task.IsCompleted)
|
||||
{
|
||||
_changed = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
}
|
||||
}
|
||||
|
||||
await RunDueAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Runs every job whose time has come, once, and schedules its next run.</summary>
|
||||
public async Task RunDueAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var now = clock.UtcNow;
|
||||
List<(ScheduledJob Job, DateTimeOffset Due)> due;
|
||||
lock (_gate)
|
||||
{
|
||||
due = _jobs.Values.Where(j => j.Due is { } d && d <= now).Select(j => (j.Job, j.Due!.Value)).OrderBy(j => j.Value).ToList();
|
||||
foreach (var (job, _) in due)
|
||||
{
|
||||
_jobs[job.Name] = (job, job.Schedule.NextAfter(now));
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var (job, dueAt) in due)
|
||||
{
|
||||
var started = clock.UtcNow;
|
||||
if (started - dueAt > job.MaxLateness)
|
||||
{
|
||||
LogSkipped(_logger, job.Name, dueAt);
|
||||
bus.Publish(new JobRun(job.Name, dueAt, started, started, JobOutcome.Skipped, "Orario superato di troppo: il lavoro non è stato eseguito."));
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await job.Run(cancellationToken).ConfigureAwait(false);
|
||||
bus.Publish(new JobRun(job.Name, dueAt, started, clock.UtcNow, JobOutcome.Completed, null));
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException || !cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
LogFailed(_logger, ex, job.Name);
|
||||
bus.Publish(new JobRun(job.Name, dueAt, started, clock.UtcNow, JobOutcome.Failed, ex.Message));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[LoggerMessage(Level = LogLevel.Warning, Message = "Job {Job} skipped: it was due at {Due} and is too late to run")]
|
||||
private static partial void LogSkipped(ILogger logger, string job, DateTimeOffset due);
|
||||
|
||||
[LoggerMessage(Level = LogLevel.Error, Message = "Job {Job} failed")]
|
||||
private static partial void LogFailed(ILogger logger, Exception exception, string job);
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Anubis.Db;
|
||||
|
||||
namespace Anubis.Core;
|
||||
|
||||
/// <summary>Whether a secret is set and where it comes from. Never the value (SPEC 13.5, 15.4).</summary>
|
||||
public sealed record SecretStatus(string Name, bool IsSet, bool FromEnvironment, DateTimeOffset? UpdatedAt);
|
||||
|
||||
/// <summary>
|
||||
/// Secrets encrypted in the database with AES-GCM under the installation's secret key (SPEC 15.4). The name is
|
||||
/// bound to the ciphertext as associated data, so a value cannot be moved under another name. Values never go
|
||||
/// to logs or API answers: the store registers them with the <see cref="Redactor"/> as soon as they are read.
|
||||
/// A key from the environment wins over the saved one (SPEC 15.1).
|
||||
/// </summary>
|
||||
public sealed class SecretStore(Database db, IClock clock, byte[] key, IReadOnlyDictionary<string, string>? environmentKeys = null)
|
||||
{
|
||||
private const int NonceSize = 12;
|
||||
private const int TagSize = 16;
|
||||
|
||||
private readonly IReadOnlyDictionary<string, string> _environment = environmentKeys ?? new Dictionary<string, string>();
|
||||
|
||||
public async Task SetAsync(string name, string value, string changedBy, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrEmpty(name);
|
||||
ArgumentException.ThrowIfNullOrEmpty(value);
|
||||
if (_environment.ContainsKey(name))
|
||||
{
|
||||
throw new ReadOnlySettingException("secret:" + name, "variabile d'ambiente " + name);
|
||||
}
|
||||
|
||||
Redactor.Register(value);
|
||||
var sealedValue = Seal(name, value);
|
||||
var now = clock.UtcNow;
|
||||
await db.WriteAsync(async s =>
|
||||
{
|
||||
var exists = await s.CountAsync("SELECT COUNT(*) FROM secrets WHERE name = @n", ("n", name)).ConfigureAwait(false) > 0;
|
||||
if (exists)
|
||||
{
|
||||
await s.ExecuteAsync("UPDATE secrets SET ciphertext = @c, updated_at = @t WHERE name = @n", cancellationToken, ("n", name), ("c", sealedValue), ("t", now)).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
await s.ExecuteAsync("INSERT INTO secrets (id, name, ciphertext, created_at, updated_at) VALUES (@id, @n, @c, @t, @t)", cancellationToken,
|
||||
("id", Ids.NewId(clock)), ("n", name), ("c", sealedValue), ("t", now)).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
await Log(s, name, exists ? "impostato" : "non impostato", "impostato", changedBy, now, cancellationToken).ConfigureAwait(false);
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task<bool> RemoveAsync(string name, string changedBy, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var now = clock.UtcNow;
|
||||
return await db.WriteAsync(async s =>
|
||||
{
|
||||
var removed = await s.ExecuteAsync("DELETE FROM secrets WHERE name = @n", cancellationToken, ("n", name)).ConfigureAwait(false) > 0;
|
||||
if (removed)
|
||||
{
|
||||
await Log(s, name, "impostato", "non impostato", changedBy, now, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return removed;
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>The plain value, for the code that needs it (adapters). Never pass it to the interface.</summary>
|
||||
public async Task<string?> RevealAsync(string name, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (_environment.TryGetValue(name, out var fromEnvironment))
|
||||
{
|
||||
return fromEnvironment;
|
||||
}
|
||||
|
||||
var stored = await db.ReadAsync(s => s.FirstOrDefaultAsync("SELECT ciphertext FROM secrets WHERE name = @n", r => r.Bytes("ciphertext"), ("n", name)), cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
if (stored is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var value = Open(name, stored);
|
||||
Redactor.Register(value);
|
||||
return value;
|
||||
}
|
||||
|
||||
public async Task<SecretStatus> StatusAsync(string name, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (_environment.ContainsKey(name))
|
||||
{
|
||||
return new SecretStatus(name, true, true, null);
|
||||
}
|
||||
|
||||
var updated = await db.ReadAsync(s => s.QueryAsync("SELECT updated_at FROM secrets WHERE name = @n", r => r.Moment("updated_at"), ("n", name)), cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
return new SecretStatus(name, updated.Count > 0, false, updated.Count > 0 ? updated[0] : null);
|
||||
}
|
||||
|
||||
internal byte[] Seal(string name, string value)
|
||||
{
|
||||
var nonce = RandomNumberGenerator.GetBytes(NonceSize);
|
||||
var plain = Encoding.UTF8.GetBytes(value);
|
||||
var cipher = new byte[plain.Length];
|
||||
var tag = new byte[TagSize];
|
||||
using var aes = new AesGcm(key, TagSize);
|
||||
aes.Encrypt(nonce, plain, cipher, tag, Encoding.UTF8.GetBytes(name));
|
||||
return [.. nonce, .. tag, .. cipher];
|
||||
}
|
||||
|
||||
internal string Open(string name, byte[] stored)
|
||||
{
|
||||
if (stored.Length < NonceSize + TagSize)
|
||||
{
|
||||
throw new CryptographicException("Stored secret is too short.");
|
||||
}
|
||||
|
||||
var plain = new byte[stored.Length - NonceSize - TagSize];
|
||||
using var aes = new AesGcm(key, TagSize);
|
||||
try
|
||||
{
|
||||
aes.Decrypt(stored.AsSpan(0, NonceSize), stored.AsSpan(NonceSize + TagSize), stored.AsSpan(NonceSize, TagSize), plain, Encoding.UTF8.GetBytes(name));
|
||||
}
|
||||
catch (AuthenticationTagMismatchException ex)
|
||||
{
|
||||
throw new CryptographicException("Il segreto non si decifra: la chiave dei segreti è cambiata. Reinserisci il valore dalle Impostazioni.", ex);
|
||||
}
|
||||
|
||||
return Encoding.UTF8.GetString(plain);
|
||||
}
|
||||
|
||||
private Task<int> Log(DbSession s, string name, string old, string updated, string changedBy, DateTimeOffset now, CancellationToken cancellationToken) =>
|
||||
s.ExecuteAsync("INSERT INTO config_history (id, key, old_value, new_value, changed_by, created_at) VALUES (@id, @k, @o, @n, @by, @t)", cancellationToken,
|
||||
("id", Ids.NewId(clock)), ("k", "secret:" + name), ("o", new DbJson(JsonSerializer.Serialize(old))), ("n", new DbJson(JsonSerializer.Serialize(updated))),
|
||||
("by", changedBy), ("t", now));
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
using System.Text.Json;
|
||||
using Anubis.Db;
|
||||
|
||||
namespace Anubis.Core;
|
||||
|
||||
/// <summary>A setting the interface may change: key, default and allowed values.</summary>
|
||||
public sealed record SettingDefinition(string Key, string Label, string Default, IReadOnlyList<string>? Allowed = null);
|
||||
|
||||
/// <summary>A value fixed by the startup configuration: it wins and is shown read-only with where to change it (SPEC 13.5).</summary>
|
||||
public sealed record StartupOverride(string Value, string Source);
|
||||
|
||||
public sealed record SettingValue(string Key, string Value, bool ReadOnly, string? Source);
|
||||
|
||||
public sealed record SettingChange(string Key, string? OldValue, string? NewValue, string ChangedBy, DateTimeOffset At);
|
||||
|
||||
/// <summary>Published on the bus after a change, so open pages update.</summary>
|
||||
public sealed record SettingChanged(string Key, string Value);
|
||||
|
||||
public sealed class ReadOnlySettingException(string key, string source)
|
||||
: InvalidOperationException($"Questa impostazione arriva dalla configurazione d'avvio e si cambia lì: {source}.")
|
||||
{
|
||||
public string Key { get; } = key;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Settings saved in the database with their change log (SPEC 13.5, 15.4): every change records when, the old
|
||||
/// and the new value, and who. Values of the startup configuration are read-only here.
|
||||
/// </summary>
|
||||
public sealed class SettingsStore(Database db, IClock clock, EventBus bus, IReadOnlyDictionary<string, StartupOverride>? overrides = null)
|
||||
{
|
||||
public static readonly SettingDefinition Theme = new("appearance.theme", "Tema", "auto", ["auto", "light", "dark"]);
|
||||
public static readonly SettingDefinition Currency = new("appearance.currency", "Valuta", "EUR", ["EUR", "USD"]);
|
||||
|
||||
/// <summary>Bot state behind the top bar buttons: active, paused (no new entries) or stopped (kill switch, SPEC 9.3).</summary>
|
||||
public static readonly SettingDefinition BotState = new("bot.state", "Stato del bot", "active", ["active", "paused", "stopped"]);
|
||||
|
||||
public static readonly IReadOnlyList<SettingDefinition> All = [Theme, Currency, BotState];
|
||||
|
||||
private readonly IReadOnlyDictionary<string, StartupOverride> _overrides = overrides ?? new Dictionary<string, StartupOverride>();
|
||||
|
||||
public static SettingDefinition Definition(string key) =>
|
||||
All.FirstOrDefault(d => d.Key == key) ?? throw new ArgumentException("Impostazione sconosciuta: " + key, nameof(key));
|
||||
|
||||
public async Task<SettingValue> GetAsync(string key, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var definition = Definition(key);
|
||||
if (_overrides.TryGetValue(key, out var fixedValue))
|
||||
{
|
||||
return new SettingValue(key, fixedValue.Value, true, fixedValue.Source);
|
||||
}
|
||||
|
||||
var saved = await db.ReadAsync(s => s.FirstOrDefaultAsync("SELECT value FROM config WHERE key = @k", r => r.Json("value").Text, ("k", key)), cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
return new SettingValue(key, saved is null ? definition.Default : JsonSerializer.Deserialize<string>(saved)!, false, null);
|
||||
}
|
||||
|
||||
public async Task SetAsync(string key, string value, string changedBy, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(value);
|
||||
var definition = Definition(key);
|
||||
if (_overrides.TryGetValue(key, out var fixedValue))
|
||||
{
|
||||
throw new ReadOnlySettingException(key, fixedValue.Source);
|
||||
}
|
||||
|
||||
if (definition.Allowed is { } allowed && !allowed.Contains(value, StringComparer.Ordinal))
|
||||
{
|
||||
throw new ArgumentException($"Valore non ammesso per {definition.Label}: {value}.", nameof(value));
|
||||
}
|
||||
|
||||
var now = clock.UtcNow;
|
||||
var json = new DbJson(JsonSerializer.Serialize(value));
|
||||
var changed = await db.WriteAsync(async s =>
|
||||
{
|
||||
var old = await s.FirstOrDefaultAsync("SELECT value FROM config WHERE key = @k", r => r.Json("value").Text, ("k", key)).ConfigureAwait(false);
|
||||
if (old == json.Text)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (old is null)
|
||||
{
|
||||
await s.ExecuteAsync("INSERT INTO config (id, key, value, created_at, updated_at) VALUES (@id, @k, @v, @t, @t)", cancellationToken,
|
||||
("id", Ids.NewId(clock)), ("k", key), ("v", json), ("t", now)).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
await s.ExecuteAsync("UPDATE config SET value = @v, updated_at = @t WHERE key = @k", cancellationToken,
|
||||
("k", key), ("v", json), ("t", now)).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var previous = old is null ? new DbJson(JsonSerializer.Serialize(definition.Default)) : new DbJson(old);
|
||||
await s.ExecuteAsync("INSERT INTO config_history (id, key, old_value, new_value, changed_by, created_at) VALUES (@id, @k, @o, @n, @by, @t)", cancellationToken,
|
||||
("id", Ids.NewId(clock)), ("k", key), ("o", previous), ("n", json), ("by", changedBy), ("t", now)).ConfigureAwait(false);
|
||||
return true;
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (changed)
|
||||
{
|
||||
bus.Publish(new SettingChanged(key, value));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The change log, newest first. Secret values never appear: their entries say only "impostato" or "tolto".</summary>
|
||||
public Task<List<SettingChange>> HistoryAsync(string? key = null, int limit = 100, CancellationToken cancellationToken = default) =>
|
||||
db.ReadAsync(s => s.QueryAsync(
|
||||
"SELECT key, old_value, new_value, changed_by, created_at FROM config_history" + (key is null ? "" : " WHERE key = @k") + " ORDER BY created_at DESC, id DESC LIMIT @n",
|
||||
r => new SettingChange(r.Text("key"), Decode(r.TextOrNull("old_value")), Decode(r.TextOrNull("new_value")), r.Text("changed_by"), r.Moment("created_at")),
|
||||
key is null ? [("n", limit)] : [("k", key), ("n", limit)]), cancellationToken);
|
||||
|
||||
private static string? Decode(string? json) => json is null ? null : JsonSerializer.Deserialize<string>(json);
|
||||
}
|
||||
@@ -0,0 +1,301 @@
|
||||
using System.Data.Common;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using Anubis.Core;
|
||||
using Anubis.Platform;
|
||||
|
||||
namespace Anubis.Db;
|
||||
|
||||
/// <summary>What an archive contains: written as index.json next to one JSONL file per table.</summary>
|
||||
public sealed record ArchiveIndex(
|
||||
string Format,
|
||||
int FormatVersion,
|
||||
int SchemaVersion,
|
||||
DateTimeOffset CreatedAt,
|
||||
string SourceEngine,
|
||||
IReadOnlyList<ArchiveTable> Tables,
|
||||
IReadOnlyDictionary<string, string> AccountingTotals);
|
||||
|
||||
public sealed record ArchiveTable(string Name, string File, long Count);
|
||||
|
||||
/// <summary>
|
||||
/// Engine-independent export and import (SPEC 5.4): <c>anubis db export</c> and <c>anubis db import</c>.
|
||||
/// Used by nightly backups, restores and moves between PostgreSQL and SQLite.
|
||||
/// </summary>
|
||||
public static class Archive
|
||||
{
|
||||
public const string FormatName = "anubis-archive";
|
||||
public const string IndexFile = "index.json";
|
||||
|
||||
private static readonly JsonSerializerOptions _json = new(JsonSerializerDefaults.Web) { WriteIndented = true };
|
||||
|
||||
/// <summary>
|
||||
/// Writes every table in a single read-only transaction, so the archive is a picture of one instant.
|
||||
/// Money is written as exact decimal text, moments as UTC ISO text, identifiers as text.
|
||||
/// </summary>
|
||||
public static async Task<ArchiveIndex> ExportAsync(Database db, string folder, IClock clock, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(db);
|
||||
ArgumentNullException.ThrowIfNull(clock);
|
||||
Directory.CreateDirectory(folder);
|
||||
if (Directory.EnumerateFileSystemEntries(folder).Any())
|
||||
{
|
||||
throw new IOException($"La cartella {folder} non è vuota: un archivio va scritto in una cartella nuova.");
|
||||
}
|
||||
|
||||
var index = await db.ReadAsync(async s =>
|
||||
{
|
||||
var version = await CurrentVersionAsync(s).ConfigureAwait(false);
|
||||
var tables = new List<ArchiveTable>();
|
||||
var totals = new Totals();
|
||||
|
||||
// The tables of the database's own version: a backup before a migration reads the old schema.
|
||||
foreach (var table in Schema.Parse(Migrations.All.Take(version)))
|
||||
{
|
||||
var file = table.Name + ".jsonl";
|
||||
long count = 0;
|
||||
await using var stream = new FileStream(Path.Combine(folder, file), FileMode.CreateNew, FileAccess.Write);
|
||||
await using var writer = new StreamWriter(stream, new UTF8Encoding(false)) { NewLine = "\n" };
|
||||
var columns = string.Join(", ", table.Columns.Select(c => c.Name));
|
||||
await s.ForEachAsync($"SELECT {columns} FROM {table.Name} ORDER BY id", async reader =>
|
||||
{
|
||||
var row = ReadRow(reader, table, s.Engine);
|
||||
totals.Add(table, row);
|
||||
await writer.WriteLineAsync(row.ToJsonString()).ConfigureAwait(false);
|
||||
count++;
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
tables.Add(new ArchiveTable(table.Name, file, count));
|
||||
}
|
||||
|
||||
return new ArchiveIndex(FormatName, 1, version, clock.UtcNow, s.Engine.ToString().ToLowerInvariant(), tables, totals.ToDictionary());
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
await File.WriteAllTextAsync(Path.Combine(folder, IndexFile), JsonSerializer.Serialize(index, _json), cancellationToken).ConfigureAwait(false);
|
||||
return index;
|
||||
}
|
||||
|
||||
public static async Task<ArchiveIndex> ReadIndexAsync(string folder, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var path = Path.Combine(folder, IndexFile);
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
throw new FileNotFoundException("Questa cartella non contiene un archivio di Anubis (manca index.json).", path);
|
||||
}
|
||||
|
||||
var index = JsonSerializer.Deserialize<ArchiveIndex>(await File.ReadAllTextAsync(path, cancellationToken).ConfigureAwait(false), _json)
|
||||
?? throw new InvalidDataException("index.json vuoto.");
|
||||
if (index.Format != FormatName || index.FormatVersion != 1)
|
||||
{
|
||||
throw new InvalidDataException("Formato d'archivio sconosciuto.");
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads an archive into an empty database: creates the schema of the archive's version, inserts every row,
|
||||
/// checks counts and accounting totals against the index, then brings the schema to the latest version.
|
||||
/// Anything that does not match aborts the import and leaves nothing behind.
|
||||
/// </summary>
|
||||
public static async Task<ArchiveIndex> ImportAsync(Database db, string folder, IClock clock, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(db);
|
||||
ArgumentNullException.ThrowIfNull(clock);
|
||||
var index = await ReadIndexAsync(folder, cancellationToken).ConfigureAwait(false);
|
||||
if (index.SchemaVersion > Migrations.Latest)
|
||||
{
|
||||
throw new SchemaException($"L'archivio è alla versione {index.SchemaVersion} dello schema, più recente di questa versione di Anubis.");
|
||||
}
|
||||
|
||||
var status = await Migrations.StatusAsync(db, cancellationToken).ConfigureAwait(false);
|
||||
if (!status.IsEmpty && !await IsEmptyAsync(db, cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
throw new InvalidOperationException("Il database di arrivo non è vuoto: l'importazione si fa solo in un database vuoto.");
|
||||
}
|
||||
|
||||
if (status.Current > index.SchemaVersion)
|
||||
{
|
||||
throw new InvalidOperationException("Il database di arrivo ha già uno schema più recente dell'archivio: usa un database vuoto.");
|
||||
}
|
||||
|
||||
await Migrations.MigrateAsync(db, clock, null, cancellationToken, upTo: index.SchemaVersion).ConfigureAwait(false);
|
||||
var tables = Schema.Parse(Migrations.All.Take(index.SchemaVersion));
|
||||
|
||||
await db.WriteAsync(async s =>
|
||||
{
|
||||
foreach (var entry in index.Tables)
|
||||
{
|
||||
var table = tables.FirstOrDefault(t => t.Name == entry.Name)
|
||||
?? throw new InvalidDataException($"L'archivio contiene la tabella sconosciuta {entry.Name}.");
|
||||
var sql = $"INSERT INTO {table.Name} ({string.Join(", ", table.Columns.Select(c => c.Name))}) VALUES ({string.Join(", ", table.Columns.Select(c => "@" + c.Name))})";
|
||||
long count = 0;
|
||||
using var reader = new StreamReader(Path.Combine(folder, entry.File), Encoding.UTF8);
|
||||
while (await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false) is { } line)
|
||||
{
|
||||
if (line.Length == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var row = JsonNode.Parse(line)!.AsObject();
|
||||
var args = table.Columns.Select(c => (c.Name, FromJson(row[c.Name], c))).ToArray();
|
||||
await s.ExecuteAsync(sql, cancellationToken, args).ConfigureAwait(false);
|
||||
count++;
|
||||
}
|
||||
|
||||
if (count != entry.Count)
|
||||
{
|
||||
throw new InvalidDataException($"La tabella {entry.Name} ha {count} righe nel file e {entry.Count} nell'indice: archivio incompleto.");
|
||||
}
|
||||
}
|
||||
|
||||
// Re-read what was written, in the same transaction, and compare with the index.
|
||||
var totals = new Totals();
|
||||
foreach (var table in tables)
|
||||
{
|
||||
var columns = string.Join(", ", table.Columns.Select(c => c.Name));
|
||||
long count = 0;
|
||||
await s.ForEachAsync($"SELECT {columns} FROM {table.Name}", reader =>
|
||||
{
|
||||
totals.Add(table, ReadRow(reader, table, s.Engine));
|
||||
count++;
|
||||
return Task.CompletedTask;
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
var expected = index.Tables.FirstOrDefault(t => t.Name == table.Name)?.Count ?? 0;
|
||||
if (count != expected)
|
||||
{
|
||||
throw new InvalidDataException($"Dopo l'importazione {table.Name} ha {count} righe invece di {expected}.");
|
||||
}
|
||||
}
|
||||
|
||||
var written = totals.ToDictionary();
|
||||
foreach (var (key, value) in index.AccountingTotals)
|
||||
{
|
||||
if (!written.TryGetValue(key, out var actual) || actual != value)
|
||||
{
|
||||
throw new InvalidDataException($"Il totale {key} non coincide: {value} nell'archivio, {actual ?? "nessuno"} nel database.");
|
||||
}
|
||||
}
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
await Migrations.MigrateAsync(db, clock, null, cancellationToken).ConfigureAwait(false);
|
||||
return index;
|
||||
}
|
||||
|
||||
/// <summary>Row counts of every table, for comparisons in tests and on the System page.</summary>
|
||||
public static async Task<Dictionary<string, long>> CountsAsync(Database db, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(db);
|
||||
return await db.ReadAsync(async s =>
|
||||
{
|
||||
var counts = new Dictionary<string, long>(StringComparer.Ordinal);
|
||||
foreach (var table in Schema.Tables)
|
||||
{
|
||||
counts[table.Name] = await s.CountAsync($"SELECT COUNT(*) FROM {table.Name}").ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return counts;
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>Accounting totals computed in C#, never in SQL, so both engines give the same result (SPEC 5.4).</summary>
|
||||
public static async Task<IReadOnlyDictionary<string, string>> AccountingTotalsAsync(Database db, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(db);
|
||||
return await db.ReadAsync(async s =>
|
||||
{
|
||||
var totals = new Totals();
|
||||
foreach (var table in Schema.Tables.Where(t => Schema.AccountingTables.Contains(t.Name)))
|
||||
{
|
||||
await s.ForEachAsync($"SELECT {string.Join(", ", table.Columns.Select(c => c.Name))} FROM {table.Name}", reader =>
|
||||
{
|
||||
totals.Add(table, ReadRow(reader, table, s.Engine));
|
||||
return Task.CompletedTask;
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return (IReadOnlyDictionary<string, string>)totals.ToDictionary();
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private static async Task<bool> IsEmptyAsync(Database db, CancellationToken cancellationToken)
|
||||
{
|
||||
var counts = await CountsAsync(db, cancellationToken).ConfigureAwait(false);
|
||||
return counts.Values.All(c => c == 0);
|
||||
}
|
||||
|
||||
private static async Task<int> CurrentVersionAsync(DbSession s) =>
|
||||
await Migrations.TableExistsAsync(s, Migrations.VersionTable).ConfigureAwait(false)
|
||||
? (int)await s.CountAsync($"SELECT COALESCE(MAX(version), 0) FROM {Migrations.VersionTable}").ConfigureAwait(false)
|
||||
: 0;
|
||||
|
||||
private static JsonObject ReadRow(DbDataReader reader, Table table, DbEngine engine)
|
||||
{
|
||||
var row = new DbRow(reader, engine);
|
||||
var json = new JsonObject();
|
||||
foreach (var column in table.Columns)
|
||||
{
|
||||
json[column.Name] = row.IsNull(column.Name) ? null : column.Kind switch
|
||||
{
|
||||
ColumnKind.Money => JsonValue.Create(DbValues.MoneyText(row.Money(column.Name))),
|
||||
ColumnKind.Uuid => JsonValue.Create(row.Id(column.Name).ToString("D")),
|
||||
ColumnKind.Moment => JsonValue.Create(DbValues.MomentText(row.Moment(column.Name))),
|
||||
ColumnKind.Json => JsonValue.Create(row.Json(column.Name).Text),
|
||||
ColumnKind.Bool => JsonValue.Create(row.Bool(column.Name)),
|
||||
ColumnKind.Bytes => JsonValue.Create(Convert.ToBase64String(row.Bytes(column.Name))),
|
||||
ColumnKind.Measure => JsonValue.Create(row.Measure(column.Name)),
|
||||
ColumnKind.Number => JsonValue.Create(row.Number(column.Name)),
|
||||
_ => JsonValue.Create(row.Text(column.Name)),
|
||||
};
|
||||
}
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
private static object? FromJson(JsonNode? node, Column column)
|
||||
{
|
||||
if (node is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return column.Kind switch
|
||||
{
|
||||
ColumnKind.Money => DbValues.ParseMoney(node.GetValue<string>()),
|
||||
ColumnKind.Uuid => Guid.Parse(node.GetValue<string>()),
|
||||
ColumnKind.Moment => DbValues.ParseMoment(node.GetValue<string>()),
|
||||
ColumnKind.Json => new DbJson(node.GetValue<string>()),
|
||||
ColumnKind.Bool => node.GetValue<bool>(),
|
||||
ColumnKind.Bytes => Convert.FromBase64String(node.GetValue<string>()),
|
||||
ColumnKind.Measure => node.GetValue<double>(),
|
||||
ColumnKind.Number => node.GetValue<long>(),
|
||||
_ => node.GetValue<string>(),
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>Sum of every money column of the accounting tables, as exact decimal text keyed "table.column".</summary>
|
||||
private sealed class Totals
|
||||
{
|
||||
private readonly SortedDictionary<string, decimal> _sums = new(StringComparer.Ordinal);
|
||||
|
||||
public void Add(Table table, JsonObject row)
|
||||
{
|
||||
if (!Schema.AccountingTables.Contains(table.Name))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var column in table.Columns.Where(c => c.Kind == ColumnKind.Money))
|
||||
{
|
||||
var key = table.Name + "." + column.Name;
|
||||
var value = row[column.Name] is { } node ? decimal.Parse(node.GetValue<string>(), NumberStyles.Number, CultureInfo.InvariantCulture) : 0m;
|
||||
_sums[key] = (_sums.TryGetValue(key, out var sum) ? sum : 0m) + value;
|
||||
}
|
||||
}
|
||||
|
||||
public Dictionary<string, string> ToDictionary() =>
|
||||
_sums.ToDictionary(kv => kv.Key, kv => DbValues.Normalize(kv.Value).ToString(CultureInfo.InvariantCulture), StringComparer.Ordinal);
|
||||
}
|
||||
}
|
||||
@@ -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)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,329 @@
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Globalization;
|
||||
using Anubis.Platform;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Npgsql;
|
||||
|
||||
namespace Anubis.Db;
|
||||
|
||||
/// <summary>
|
||||
/// Access to the configured database with plain ADO.NET (ADR 0003). The same SQL runs on PostgreSQL and
|
||||
/// SQLite; values pass through <see cref="DbValues"/>. On SQLite all writes go through one writer at a time
|
||||
/// (SPEC 5.4) while reads run in parallel thanks to WAL.
|
||||
/// </summary>
|
||||
public sealed class Database : IAsyncDisposable
|
||||
{
|
||||
private readonly SemaphoreSlim _writer = new(1, 1);
|
||||
private readonly string _connectionString;
|
||||
private readonly NpgsqlDataSource? _postgres;
|
||||
|
||||
private Database(DbEngine engine, string connectionString, NpgsqlDataSource? postgres, string description)
|
||||
{
|
||||
Engine = engine;
|
||||
_connectionString = connectionString;
|
||||
_postgres = postgres;
|
||||
Description = description;
|
||||
}
|
||||
|
||||
public DbEngine Engine { get; }
|
||||
|
||||
/// <summary>Engine and place, without the password.</summary>
|
||||
public string Description { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Opens the configured database. With SQLite, <paramref name="allowCreate"/> false refuses to create a missing
|
||||
/// file: a data folder that already belongs to an installation never gets a new empty database by itself (trap 20).
|
||||
/// PostgreSQL databases are never created by Anubis (SPEC 15.1).
|
||||
/// </summary>
|
||||
public static Database Open(DbSettings settings, bool allowCreate = true)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(settings);
|
||||
if (settings.Engine == DbEngine.Sqlite)
|
||||
{
|
||||
var path = settings.SqlitePath ?? throw new InvalidOperationException("SQLite path missing.");
|
||||
var builder = new SqliteConnectionStringBuilder
|
||||
{
|
||||
DataSource = path,
|
||||
Mode = allowCreate ? SqliteOpenMode.ReadWriteCreate : SqliteOpenMode.ReadWrite,
|
||||
Pooling = true,
|
||||
ForeignKeys = true,
|
||||
DefaultTimeout = 5,
|
||||
};
|
||||
if (allowCreate)
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(path))!);
|
||||
}
|
||||
|
||||
return new Database(DbEngine.Sqlite, builder.ToString(), null, settings.Describe());
|
||||
}
|
||||
|
||||
var pg = settings.Url is { } url ? new NpgsqlConnectionStringBuilder(ToKeyValue(url)) : new NpgsqlConnectionStringBuilder
|
||||
{
|
||||
Host = settings.Host,
|
||||
Port = settings.Port,
|
||||
Database = settings.Database,
|
||||
Username = settings.User,
|
||||
Password = settings.Password,
|
||||
};
|
||||
pg.Timeout = 5;
|
||||
pg.CommandTimeout = 60;
|
||||
pg.ApplicationName = "anubis";
|
||||
var dataSource = new NpgsqlDataSourceBuilder(pg.ToString()).Build();
|
||||
return new Database(DbEngine.Postgres, pg.ToString(), dataSource, settings.Describe());
|
||||
}
|
||||
|
||||
// ANUBIS_DB_URL may be a postgres:// URL or already a key=value connection string.
|
||||
private static string ToKeyValue(string url)
|
||||
{
|
||||
if (!url.StartsWith("postgres://", StringComparison.OrdinalIgnoreCase) && !url.StartsWith("postgresql://", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return url;
|
||||
}
|
||||
|
||||
var uri = new Uri(url);
|
||||
var builder = new NpgsqlConnectionStringBuilder
|
||||
{
|
||||
Host = uri.Host,
|
||||
Port = uri.Port > 0 ? uri.Port : 5432,
|
||||
Database = Uri.UnescapeDataString(uri.AbsolutePath.TrimStart('/')),
|
||||
};
|
||||
var userInfo = uri.UserInfo.Split(':', 2);
|
||||
builder.Username = Uri.UnescapeDataString(userInfo[0]);
|
||||
if (userInfo.Length > 1)
|
||||
{
|
||||
builder.Password = Uri.UnescapeDataString(userInfo[1]);
|
||||
}
|
||||
|
||||
foreach (var pair in uri.Query.TrimStart('?').Split('&', StringSplitOptions.RemoveEmptyEntries))
|
||||
{
|
||||
var kv = pair.Split('=', 2);
|
||||
builder[Uri.UnescapeDataString(kv[0])] = kv.Length > 1 ? Uri.UnescapeDataString(kv[1]) : "";
|
||||
}
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
public async Task<DbConnection> OpenConnectionAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (_postgres is not null)
|
||||
{
|
||||
return await _postgres.OpenConnectionAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var connection = new SqliteConnection(_connectionString);
|
||||
try
|
||||
{
|
||||
await connection.OpenAsync(cancellationToken).ConfigureAwait(false);
|
||||
await using var pragma = connection.CreateCommand();
|
||||
pragma.CommandText = "PRAGMA journal_mode=WAL; PRAGMA synchronous=FULL; PRAGMA foreign_keys=ON; PRAGMA busy_timeout=5000;";
|
||||
await pragma.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
|
||||
return connection;
|
||||
}
|
||||
catch
|
||||
{
|
||||
await connection.DisposeAsync().ConfigureAwait(false);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Checks the database answers. Throws when it does not.</summary>
|
||||
public async Task PingAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await using var connection = await OpenConnectionAsync(cancellationToken).ConfigureAwait(false);
|
||||
await using var command = connection.CreateCommand();
|
||||
command.CommandText = "SELECT 1";
|
||||
await command.ExecuteScalarAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>Runs <paramref name="work"/> in a read-only transaction that sees a single instant of the database.</summary>
|
||||
public async Task<T> ReadAsync<T>(Func<DbSession, Task<T>> work, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(work);
|
||||
await using var connection = await OpenConnectionAsync(cancellationToken).ConfigureAwait(false);
|
||||
await using var transaction = Engine == DbEngine.Postgres
|
||||
? await connection.BeginTransactionAsync(IsolationLevel.RepeatableRead, cancellationToken).ConfigureAwait(false)
|
||||
: ((SqliteConnection)connection).BeginTransaction(deferred: true);
|
||||
var session = new DbSession(Engine, connection, transaction);
|
||||
if (Engine == DbEngine.Postgres)
|
||||
{
|
||||
await session.ExecuteAsync("SET TRANSACTION READ ONLY", cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var result = await work(session).ConfigureAwait(false);
|
||||
await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>Runs <paramref name="work"/> in a write transaction, committed only if it completes.</summary>
|
||||
public async Task<T> WriteAsync<T>(Func<DbSession, Task<T>> work, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(work);
|
||||
await _writer.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
await using var connection = await OpenConnectionAsync(cancellationToken).ConfigureAwait(false);
|
||||
await using var transaction = Engine == DbEngine.Postgres
|
||||
? await connection.BeginTransactionAsync(IsolationLevel.ReadCommitted, cancellationToken).ConfigureAwait(false)
|
||||
: ((SqliteConnection)connection).BeginTransaction(deferred: false);
|
||||
var result = await work(new DbSession(Engine, connection, transaction)).ConfigureAwait(false);
|
||||
await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
|
||||
return result;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_writer.Release();
|
||||
}
|
||||
}
|
||||
|
||||
public Task WriteAsync(Func<DbSession, Task> work, CancellationToken cancellationToken = default) =>
|
||||
WriteAsync(async s =>
|
||||
{
|
||||
await work(s).ConfigureAwait(false);
|
||||
return true;
|
||||
}, cancellationToken);
|
||||
|
||||
/// <summary>A consistent copy of the SQLite file through the online backup API (SPEC 5.4). Not available on PostgreSQL.</summary>
|
||||
public async Task BackupSqliteFileAsync(string destination, CancellationToken cancellationToken)
|
||||
{
|
||||
if (Engine != DbEngine.Sqlite)
|
||||
{
|
||||
throw new InvalidOperationException("File copies exist only for SQLite.");
|
||||
}
|
||||
|
||||
await using var source = (SqliteConnection)await OpenConnectionAsync(cancellationToken).ConfigureAwait(false);
|
||||
await using var target = new SqliteConnection(new SqliteConnectionStringBuilder { DataSource = destination, Pooling = false }.ToString());
|
||||
await target.OpenAsync(cancellationToken).ConfigureAwait(false);
|
||||
source.BackupDatabase(target);
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_postgres is not null)
|
||||
{
|
||||
await _postgres.DisposeAsync().ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
SqliteConnection.ClearPool(new SqliteConnection(_connectionString));
|
||||
}
|
||||
|
||||
_writer.Dispose();
|
||||
}
|
||||
|
||||
public override string ToString() => Description + " (" + Engine.ToString().ToLower(CultureInfo.InvariantCulture) + ")";
|
||||
}
|
||||
|
||||
/// <summary>One open transaction. Parameters are named <c>@name</c> on both engines.</summary>
|
||||
public sealed class DbSession(DbEngine engine, DbConnection connection, DbTransaction transaction)
|
||||
{
|
||||
public DbEngine Engine => engine;
|
||||
|
||||
public DbConnection Connection => connection;
|
||||
|
||||
public async Task<int> ExecuteAsync(string sql, CancellationToken cancellationToken = default, params (string Name, object? Value)[] args)
|
||||
{
|
||||
await using var command = Command(sql, args);
|
||||
return await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public Task<int> ExecuteAsync(string sql, params (string Name, object? Value)[] args) => ExecuteAsync(sql, CancellationToken.None, args);
|
||||
|
||||
public async Task<List<T>> QueryAsync<T>(string sql, Func<DbRow, T> map, params (string Name, object? Value)[] args)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(map);
|
||||
await using var command = Command(sql, args);
|
||||
await using var reader = await command.ExecuteReaderAsync().ConfigureAwait(false);
|
||||
var result = new List<T>();
|
||||
var row = new DbRow(reader, engine);
|
||||
while (await reader.ReadAsync().ConfigureAwait(false))
|
||||
{
|
||||
result.Add(map(row));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<T?> FirstOrDefaultAsync<T>(string sql, Func<DbRow, T> map, params (string Name, object? Value)[] args) =>
|
||||
(await QueryAsync(sql, map, args).ConfigureAwait(false)).FirstOrDefault();
|
||||
|
||||
public async Task<long> CountAsync(string sql, params (string Name, object? Value)[] args)
|
||||
{
|
||||
await using var command = Command(sql, args);
|
||||
var value = await command.ExecuteScalarAsync().ConfigureAwait(false);
|
||||
return Convert.ToInt64(value, CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
/// <summary>Streams rows without loading them all, for exports.</summary>
|
||||
public async Task ForEachAsync(string sql, Func<DbDataReader, Task> onRow, CancellationToken cancellationToken)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(onRow);
|
||||
await using var command = Command(sql, []);
|
||||
await using var reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false);
|
||||
while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
await onRow(reader).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
private DbCommand Command(string sql, (string Name, object? Value)[] args)
|
||||
{
|
||||
var command = connection.CreateCommand();
|
||||
command.Transaction = transaction;
|
||||
command.CommandText = sql;
|
||||
foreach (var (name, value) in args)
|
||||
{
|
||||
var parameter = command.CreateParameter();
|
||||
parameter.ParameterName = name.StartsWith('@') ? name : "@" + name;
|
||||
parameter.Value = DbValues.ToProvider(value, engine);
|
||||
if (engine == DbEngine.Postgres && value is DbJson)
|
||||
{
|
||||
((NpgsqlParameter)parameter).NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Json;
|
||||
}
|
||||
|
||||
command.Parameters.Add(parameter);
|
||||
}
|
||||
|
||||
return command;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Typed reading of the current row, the mirror of <see cref="DbValues.ToProvider"/>.</summary>
|
||||
public readonly struct DbRow(DbDataReader reader, DbEngine engine)
|
||||
{
|
||||
private int Ordinal(string column) => reader.GetOrdinal(column);
|
||||
|
||||
public bool IsNull(string column) => reader.IsDBNull(Ordinal(column));
|
||||
|
||||
public string Text(string column) => reader.GetString(Ordinal(column));
|
||||
|
||||
public string? TextOrNull(string column) => IsNull(column) ? null : Text(column);
|
||||
|
||||
public long Number(string column) => Convert.ToInt64(reader.GetValue(Ordinal(column)), CultureInfo.InvariantCulture);
|
||||
|
||||
public bool Bool(string column) => engine == DbEngine.Sqlite ? Number(column) != 0 : reader.GetBoolean(Ordinal(column));
|
||||
|
||||
public decimal Money(string column) => engine == DbEngine.Sqlite
|
||||
? DbValues.ParseMoney(reader.GetString(Ordinal(column)))
|
||||
: DbValues.Normalize(reader.GetDecimal(Ordinal(column)));
|
||||
|
||||
public decimal? MoneyOrNull(string column) => IsNull(column) ? null : Money(column);
|
||||
|
||||
public Guid Id(string column) => engine == DbEngine.Sqlite ? Guid.Parse(reader.GetString(Ordinal(column))) : reader.GetGuid(Ordinal(column));
|
||||
|
||||
public Guid? IdOrNull(string column) => IsNull(column) ? null : Id(column);
|
||||
|
||||
public DateTimeOffset Moment(string column) => engine == DbEngine.Sqlite
|
||||
? DbValues.ParseMoment(reader.GetString(Ordinal(column)))
|
||||
: new DateTimeOffset(DateTime.SpecifyKind(reader.GetDateTime(Ordinal(column)), DateTimeKind.Utc));
|
||||
|
||||
public DateTimeOffset? MomentOrNull(string column) => IsNull(column) ? null : Moment(column);
|
||||
|
||||
public DbJson Json(string column) => new(reader.GetString(Ordinal(column)));
|
||||
|
||||
public byte[] Bytes(string column) => (byte[])reader.GetValue(Ordinal(column));
|
||||
|
||||
public byte[]? BytesOrNull(string column) => IsNull(column) ? null : Bytes(column);
|
||||
|
||||
public double Measure(string column) => Convert.ToDouble(reader.GetValue(Ordinal(column)), CultureInfo.InvariantCulture);
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
using System.Globalization;
|
||||
using Anubis.Platform;
|
||||
|
||||
namespace Anubis.Db;
|
||||
|
||||
/// <summary>A JSON document column (SPEC 5.4: generic JSON, no engine-specific operators). Stored as written.</summary>
|
||||
public readonly record struct DbJson(string Text);
|
||||
|
||||
/// <summary>
|
||||
/// The in-house column types of SPEC 5.4, the same on both engines (ADR 0007):
|
||||
/// <list type="bullet">
|
||||
/// <item>money, quantities and prices: <see cref="decimal"/>, <c>NUMERIC(38,18)</c> on PostgreSQL and canonical text on SQLite, rounded to 18 decimals before writing (trap 19);</item>
|
||||
/// <item>moments: <see cref="DateTimeOffset"/> in UTC, microsecond precision, ISO text on SQLite; a <see cref="DateTime"/> without time zone is refused;</item>
|
||||
/// <item>identifiers: <see cref="Guid"/> (UUIDv7), <c>UUID</c> on PostgreSQL and lowercase text on SQLite.</item>
|
||||
/// </list>
|
||||
/// </summary>
|
||||
public static class DbValues
|
||||
{
|
||||
/// <summary>NUMERIC(38,18) keeps 20 digits before the decimal point.</summary>
|
||||
public static readonly decimal MaxMagnitude = 99_999_999_999_999_999_999m;
|
||||
|
||||
public const int Scale = 18;
|
||||
|
||||
private const string TimestampFormat = "yyyy-MM-dd'T'HH:mm:ss.ffffff'Z'";
|
||||
|
||||
/// <summary>Rounds to 18 decimals (half away from zero, as PostgreSQL does) and refuses values the column cannot hold.</summary>
|
||||
public static decimal Money(decimal value)
|
||||
{
|
||||
if (Math.Abs(value) > MaxMagnitude)
|
||||
{
|
||||
throw new OverflowException($"Value {value.ToString(CultureInfo.InvariantCulture)} exceeds NUMERIC(38,18).");
|
||||
}
|
||||
|
||||
return Normalize(Math.Round(value, Scale, MidpointRounding.AwayFromZero));
|
||||
}
|
||||
|
||||
/// <summary>Removes trailing zeros so the same number always has the same text: 1.500 becomes 1.5.</summary>
|
||||
public static decimal Normalize(decimal value) => value / 1.000000000000000000000000000000000m;
|
||||
|
||||
public static string MoneyText(decimal value) => Money(value).ToString(CultureInfo.InvariantCulture);
|
||||
|
||||
public static decimal ParseMoney(string text) => Normalize(decimal.Parse(text, NumberStyles.Number, CultureInfo.InvariantCulture));
|
||||
|
||||
/// <summary>UTC, truncated to microseconds (the precision of PostgreSQL TIMESTAMPTZ).</summary>
|
||||
public static DateTimeOffset Moment(DateTimeOffset value)
|
||||
{
|
||||
var utc = value.ToUniversalTime();
|
||||
return new DateTimeOffset(utc.UtcTicks - (utc.UtcTicks % 10), TimeSpan.Zero);
|
||||
}
|
||||
|
||||
public static string MomentText(DateTimeOffset value) => Moment(value).ToString(TimestampFormat, CultureInfo.InvariantCulture);
|
||||
|
||||
public static DateTimeOffset ParseMoment(string text)
|
||||
{
|
||||
if (!text.EndsWith('Z'))
|
||||
{
|
||||
throw new FormatException("A stored moment has no time zone: " + text);
|
||||
}
|
||||
|
||||
return DateTimeOffset.Parse(text, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);
|
||||
}
|
||||
|
||||
/// <summary>Converts a .NET value to what the engine's driver should receive.</summary>
|
||||
public static object ToProvider(object? value, DbEngine engine) => value switch
|
||||
{
|
||||
null => DBNull.Value,
|
||||
decimal d => engine == DbEngine.Sqlite ? MoneyText(d) : Money(d),
|
||||
DateTimeOffset t => engine == DbEngine.Sqlite ? MomentText(t) : Moment(t),
|
||||
DateTime dt => dt.Kind == DateTimeKind.Utc
|
||||
? ToProvider(new DateTimeOffset(dt), engine)
|
||||
: throw new ArgumentException("Moments must carry a time zone: use DateTimeOffset or a UTC DateTime."),
|
||||
Guid g => engine == DbEngine.Sqlite ? g.ToString("D") : g,
|
||||
bool b => engine == DbEngine.Sqlite ? (b ? 1L : 0L) : b,
|
||||
DbJson j => j.Text,
|
||||
int i => (long)i,
|
||||
_ => value,
|
||||
};
|
||||
|
||||
/// <summary>The column type behind each token of the migration files.</summary>
|
||||
public static string ColumnType(string token, DbEngine engine) => (token, engine) switch
|
||||
{
|
||||
("money", DbEngine.Postgres) => "NUMERIC(38,18)",
|
||||
("money", DbEngine.Sqlite) => "TEXT",
|
||||
("uuid", DbEngine.Postgres) => "UUID",
|
||||
("uuid", DbEngine.Sqlite) => "TEXT",
|
||||
("timestamp", DbEngine.Postgres) => "TIMESTAMPTZ",
|
||||
("timestamp", DbEngine.Sqlite) => "TEXT",
|
||||
("json", DbEngine.Postgres) => "JSON",
|
||||
("json", DbEngine.Sqlite) => "TEXT",
|
||||
("bool", DbEngine.Postgres) => "BOOLEAN",
|
||||
("bool", DbEngine.Sqlite) => "INTEGER",
|
||||
("bytes", DbEngine.Postgres) => "BYTEA",
|
||||
("bytes", DbEngine.Sqlite) => "BLOB",
|
||||
("float", DbEngine.Postgres) => "DOUBLE PRECISION",
|
||||
("float", DbEngine.Sqlite) => "REAL",
|
||||
("strict", DbEngine.Postgres) => "",
|
||||
("strict", DbEngine.Sqlite) => " STRICT",
|
||||
_ => throw new InvalidOperationException($"Unknown column type token '{token}'."),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,245 @@
|
||||
-- Phase 1 schema (SPEC 5.4, ADR 0007). The same text runs on PostgreSQL and SQLite: the tokens in braces
|
||||
-- become the engine's column types (DbValues.ColumnType). Every table has a UUIDv7 key and created_at in UTC.
|
||||
-- Tables of later phases (bars, news, models, backtests, signals, decisions) arrive with their own migration.
|
||||
|
||||
-- Identity of the installation and fingerprint of the live database (SPEC 15.1). Exactly one row.
|
||||
CREATE TABLE installation (
|
||||
id {uuid} PRIMARY KEY,
|
||||
fingerprint TEXT NOT NULL,
|
||||
engine TEXT NOT NULL,
|
||||
protected_restart {bool} NOT NULL,
|
||||
created_at {timestamp} NOT NULL,
|
||||
renewed_at {timestamp} NOT NULL
|
||||
){strict};
|
||||
|
||||
CREATE TABLE users (
|
||||
id {uuid} PRIMARY KEY,
|
||||
username TEXT NOT NULL UNIQUE,
|
||||
password_hash TEXT NOT NULL,
|
||||
must_change_password {bool} NOT NULL,
|
||||
totp_secret {bytes},
|
||||
created_at {timestamp} NOT NULL,
|
||||
updated_at {timestamp} NOT NULL
|
||||
){strict};
|
||||
|
||||
CREATE TABLE sessions (
|
||||
id {uuid} PRIMARY KEY,
|
||||
user_id {uuid} NOT NULL REFERENCES users (id),
|
||||
token_hash TEXT NOT NULL UNIQUE,
|
||||
user_agent TEXT,
|
||||
address TEXT,
|
||||
created_at {timestamp} NOT NULL,
|
||||
last_seen_at {timestamp} NOT NULL,
|
||||
expires_at {timestamp} NOT NULL
|
||||
){strict};
|
||||
|
||||
CREATE INDEX sessions_user ON sessions (user_id);
|
||||
|
||||
-- Settings saved from the interface; startup configuration values win over them and are never stored here.
|
||||
CREATE TABLE config (
|
||||
id {uuid} PRIMARY KEY,
|
||||
key TEXT NOT NULL UNIQUE,
|
||||
value {json} NOT NULL,
|
||||
created_at {timestamp} NOT NULL,
|
||||
updated_at {timestamp} NOT NULL
|
||||
){strict};
|
||||
|
||||
-- Change log of settings, activations and the kill switch (SPEC 13.5, 15.4). Append-only.
|
||||
CREATE TABLE config_history (
|
||||
id {uuid} PRIMARY KEY,
|
||||
key TEXT NOT NULL,
|
||||
old_value {json},
|
||||
new_value {json},
|
||||
changed_by TEXT NOT NULL,
|
||||
created_at {timestamp} NOT NULL
|
||||
){strict};
|
||||
|
||||
CREATE INDEX config_history_key ON config_history (key, created_at);
|
||||
|
||||
-- Encrypted secrets (AES-GCM with the installation's secret key). Never returned by the API.
|
||||
CREATE TABLE secrets (
|
||||
id {uuid} PRIMARY KEY,
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
ciphertext {bytes} NOT NULL,
|
||||
created_at {timestamp} NOT NULL,
|
||||
updated_at {timestamp} NOT NULL
|
||||
){strict};
|
||||
|
||||
CREATE TABLE jobs (
|
||||
id {uuid} PRIMARY KEY,
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
description TEXT NOT NULL,
|
||||
last_due_at {timestamp},
|
||||
last_started_at {timestamp},
|
||||
last_finished_at {timestamp},
|
||||
last_outcome TEXT,
|
||||
last_error TEXT,
|
||||
created_at {timestamp} NOT NULL,
|
||||
updated_at {timestamp} NOT NULL
|
||||
){strict};
|
||||
|
||||
CREATE TABLE notifications (
|
||||
id {uuid} PRIMARY KEY,
|
||||
channel TEXT NOT NULL,
|
||||
kind TEXT NOT NULL,
|
||||
message TEXT NOT NULL,
|
||||
status TEXT NOT NULL,
|
||||
sent_at {timestamp},
|
||||
created_at {timestamp} NOT NULL
|
||||
){strict};
|
||||
|
||||
-- The bot's diary (SPEC 13.6): short sentences in Italian with a kind and a reason code.
|
||||
CREATE TABLE journal (
|
||||
id {uuid} PRIMARY KEY,
|
||||
occurred_at {timestamp} NOT NULL,
|
||||
kind TEXT NOT NULL,
|
||||
code TEXT NOT NULL,
|
||||
message TEXT NOT NULL,
|
||||
data {json},
|
||||
created_at {timestamp} NOT NULL
|
||||
){strict};
|
||||
|
||||
CREATE INDEX journal_time ON journal (occurred_at);
|
||||
|
||||
CREATE TABLE instruments (
|
||||
id {uuid} PRIMARY KEY,
|
||||
venue TEXT NOT NULL,
|
||||
symbol TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
asset_class TEXT NOT NULL,
|
||||
quote_currency TEXT NOT NULL,
|
||||
created_at {timestamp} NOT NULL,
|
||||
UNIQUE (venue, symbol)
|
||||
){strict};
|
||||
|
||||
CREATE TABLE accounts (
|
||||
id {uuid} PRIMARY KEY,
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
broker TEXT NOT NULL,
|
||||
environment TEXT NOT NULL,
|
||||
base_currency TEXT NOT NULL,
|
||||
created_at {timestamp} NOT NULL
|
||||
){strict};
|
||||
|
||||
CREATE TABLE strategy_state (
|
||||
id {uuid} PRIMARY KEY,
|
||||
strategy TEXT NOT NULL UNIQUE,
|
||||
account_id {uuid} REFERENCES accounts (id),
|
||||
stage TEXT NOT NULL,
|
||||
capital {money} NOT NULL,
|
||||
state {json} NOT NULL,
|
||||
created_at {timestamp} NOT NULL,
|
||||
updated_at {timestamp} NOT NULL
|
||||
){strict};
|
||||
|
||||
CREATE TABLE strategy_stage_history (
|
||||
id {uuid} PRIMARY KEY,
|
||||
strategy TEXT NOT NULL,
|
||||
from_stage TEXT,
|
||||
to_stage TEXT NOT NULL,
|
||||
reason TEXT NOT NULL,
|
||||
created_at {timestamp} NOT NULL
|
||||
){strict};
|
||||
|
||||
CREATE TABLE orders (
|
||||
id {uuid} PRIMARY KEY,
|
||||
account_id {uuid} NOT NULL REFERENCES accounts (id),
|
||||
instrument_id {uuid} NOT NULL REFERENCES instruments (id),
|
||||
strategy TEXT NOT NULL,
|
||||
client_order_id TEXT NOT NULL UNIQUE,
|
||||
broker_order_id TEXT,
|
||||
side TEXT NOT NULL,
|
||||
order_type TEXT NOT NULL,
|
||||
quantity {money} NOT NULL,
|
||||
limit_price {money},
|
||||
stop_price {money},
|
||||
status TEXT NOT NULL,
|
||||
created_at {timestamp} NOT NULL,
|
||||
updated_at {timestamp} NOT NULL
|
||||
){strict};
|
||||
|
||||
-- Accounting tables (fills, lots, cash_ledger) are append-only: errors are fixed with reversal rows, never UPDATE.
|
||||
-- The same execution, from the stream and from the fallback query, is recorded once (account + execution id).
|
||||
CREATE TABLE fills (
|
||||
id {uuid} PRIMARY KEY,
|
||||
account_id {uuid} NOT NULL REFERENCES accounts (id),
|
||||
order_id {uuid} REFERENCES orders (id),
|
||||
instrument_id {uuid} NOT NULL REFERENCES instruments (id),
|
||||
execution_id TEXT NOT NULL,
|
||||
side TEXT NOT NULL,
|
||||
quantity {money} NOT NULL,
|
||||
price {money} NOT NULL,
|
||||
fee {money} NOT NULL,
|
||||
fee_currency TEXT NOT NULL,
|
||||
reverses_id {uuid} REFERENCES fills (id),
|
||||
executed_at {timestamp} NOT NULL,
|
||||
created_at {timestamp} NOT NULL,
|
||||
UNIQUE (account_id, execution_id)
|
||||
){strict};
|
||||
|
||||
CREATE TABLE lots (
|
||||
id {uuid} PRIMARY KEY,
|
||||
account_id {uuid} NOT NULL REFERENCES accounts (id),
|
||||
instrument_id {uuid} NOT NULL REFERENCES instruments (id),
|
||||
fill_id {uuid} NOT NULL REFERENCES fills (id),
|
||||
strategy TEXT NOT NULL,
|
||||
quantity {money} NOT NULL,
|
||||
price {money} NOT NULL,
|
||||
reverses_id {uuid} REFERENCES lots (id),
|
||||
created_at {timestamp} NOT NULL
|
||||
){strict};
|
||||
|
||||
CREATE TABLE cash_ledger (
|
||||
id {uuid} PRIMARY KEY,
|
||||
account_id {uuid} NOT NULL REFERENCES accounts (id),
|
||||
currency TEXT NOT NULL,
|
||||
amount {money} NOT NULL,
|
||||
kind TEXT NOT NULL,
|
||||
fill_id {uuid} REFERENCES fills (id),
|
||||
reverses_id {uuid} REFERENCES cash_ledger (id),
|
||||
note TEXT,
|
||||
occurred_at {timestamp} NOT NULL,
|
||||
created_at {timestamp} NOT NULL
|
||||
){strict};
|
||||
|
||||
CREATE TABLE positions (
|
||||
id {uuid} PRIMARY KEY,
|
||||
account_id {uuid} NOT NULL REFERENCES accounts (id),
|
||||
instrument_id {uuid} NOT NULL REFERENCES instruments (id),
|
||||
strategy TEXT NOT NULL,
|
||||
quantity {money} NOT NULL,
|
||||
average_price {money} NOT NULL,
|
||||
last_price {money},
|
||||
stop_price {money},
|
||||
opened_at {timestamp} NOT NULL,
|
||||
created_at {timestamp} NOT NULL,
|
||||
updated_at {timestamp} NOT NULL,
|
||||
UNIQUE (account_id, instrument_id, strategy)
|
||||
){strict};
|
||||
|
||||
-- Equity over time: account_id and strategy both empty for the total, one of them set for its own series.
|
||||
CREATE TABLE equity_snapshots (
|
||||
id {uuid} PRIMARY KEY,
|
||||
taken_at {timestamp} NOT NULL,
|
||||
scope TEXT NOT NULL,
|
||||
account_id {uuid} REFERENCES accounts (id),
|
||||
strategy TEXT,
|
||||
equity {money} NOT NULL,
|
||||
cash {money},
|
||||
invested {money},
|
||||
benchmark {money},
|
||||
drawdown_pct {money},
|
||||
created_at {timestamp} NOT NULL
|
||||
){strict};
|
||||
|
||||
CREATE INDEX equity_snapshots_scope ON equity_snapshots (scope, taken_at);
|
||||
|
||||
CREATE TABLE risk_events (
|
||||
id {uuid} PRIMARY KEY,
|
||||
occurred_at {timestamp} NOT NULL,
|
||||
kind TEXT NOT NULL,
|
||||
severity TEXT NOT NULL,
|
||||
message TEXT NOT NULL,
|
||||
data {json},
|
||||
created_at {timestamp} NOT NULL
|
||||
){strict};
|
||||
@@ -0,0 +1,158 @@
|
||||
using System.Globalization;
|
||||
using System.Reflection;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using Anubis.Core;
|
||||
using Anubis.Platform;
|
||||
|
||||
namespace Anubis.Db;
|
||||
|
||||
/// <summary>A numbered SQL migration embedded in the library (Db/Migrations/NNNN_name.sql).</summary>
|
||||
public sealed record Migration(int Version, string Name, string Sql)
|
||||
{
|
||||
/// <summary>Hash of the file as written, so an applied migration changed afterwards is detected.</summary>
|
||||
public string Checksum { get; } = Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(Sql.ReplaceLineEndings("\n"))));
|
||||
|
||||
/// <summary>The SQL with the type tokens replaced for <paramref name="engine"/>.</summary>
|
||||
public string SqlFor(DbEngine engine) => Migrations.Tokens().Replace(Sql, m => DbValues.ColumnType(m.Groups[1].Value, engine));
|
||||
}
|
||||
|
||||
/// <summary>Where the database stands compared to the migrations this version knows.</summary>
|
||||
public sealed record SchemaStatus(int Current, int Latest, IReadOnlyList<Migration> Pending)
|
||||
{
|
||||
public bool IsEmpty => Current == 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The in-house migration runner (ADR 0003): numbered SQL files, a version table, each migration in its own
|
||||
/// transaction. A backup runs before migrating a database that already has data (SPEC 5.4, 15.6).
|
||||
/// </summary>
|
||||
public static partial class Migrations
|
||||
{
|
||||
public const string VersionTable = "schema_versions";
|
||||
|
||||
[GeneratedRegex(@"\{(money|uuid|timestamp|json|bool|bytes|float|strict)\}")]
|
||||
internal static partial Regex Tokens();
|
||||
|
||||
[GeneratedRegex(@"^(\d{4})_([a-z0-9_]+)\.sql$")]
|
||||
private static partial Regex FileName();
|
||||
|
||||
private static readonly Lazy<IReadOnlyList<Migration>> _all = new(Load);
|
||||
|
||||
public static IReadOnlyList<Migration> All => _all.Value;
|
||||
|
||||
public static int Latest => All[^1].Version;
|
||||
|
||||
private static List<Migration> Load()
|
||||
{
|
||||
var assembly = typeof(Migrations).Assembly;
|
||||
const string prefix = "Anubis.Db.Migrations.";
|
||||
var list = new List<Migration>();
|
||||
foreach (var resource in assembly.GetManifestResourceNames().Where(n => n.StartsWith(prefix, StringComparison.Ordinal)))
|
||||
{
|
||||
var match = FileName().Match(resource[prefix.Length..]);
|
||||
if (!match.Success)
|
||||
{
|
||||
throw new InvalidOperationException("Badly named migration: " + resource);
|
||||
}
|
||||
|
||||
using var stream = assembly.GetManifestResourceStream(resource)!;
|
||||
using var reader = new StreamReader(stream, Encoding.UTF8);
|
||||
list.Add(new Migration(int.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture), match.Groups[2].Value, reader.ReadToEnd()));
|
||||
}
|
||||
|
||||
list.Sort((a, b) => a.Version.CompareTo(b.Version));
|
||||
for (var i = 0; i < list.Count; i++)
|
||||
{
|
||||
if (list[i].Version != i + 1)
|
||||
{
|
||||
throw new InvalidOperationException("Migration numbers must be consecutive from 0001.");
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public static async Task<SchemaStatus> StatusAsync(Database db, CancellationToken cancellationToken)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(db);
|
||||
return await db.ReadAsync(async s =>
|
||||
{
|
||||
if (!await TableExistsAsync(s, VersionTable).ConfigureAwait(false))
|
||||
{
|
||||
return new SchemaStatus(0, Latest, All);
|
||||
}
|
||||
|
||||
var applied = await s.QueryAsync($"SELECT version, checksum FROM {VersionTable} ORDER BY version",
|
||||
r => (Version: (int)r.Number("version"), Checksum: r.Text("checksum"))).ConfigureAwait(false);
|
||||
foreach (var (version, checksum) in applied)
|
||||
{
|
||||
if (version > Latest)
|
||||
{
|
||||
throw new SchemaException($"Il database è alla versione {version}, più recente di questa versione di Anubis (che arriva alla {Latest}). Aggiorna Anubis prima di usarlo.");
|
||||
}
|
||||
|
||||
if (All[version - 1].Checksum != checksum)
|
||||
{
|
||||
throw new SchemaException($"La migrazione {version:D4} applicata al database è diversa da quella di questa versione di Anubis.");
|
||||
}
|
||||
}
|
||||
|
||||
var current = applied.Count == 0 ? 0 : applied[^1].Version;
|
||||
return new SchemaStatus(current, Latest, All.Skip(current).ToList());
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Brings the database to the latest version. <paramref name="backupBeforeMigrating"/> runs first when the database
|
||||
/// already has a schema and something is pending; if it fails, nothing is migrated.
|
||||
/// </summary>
|
||||
public static async Task<int> MigrateAsync(Database db, IClock clock, Func<CancellationToken, Task>? backupBeforeMigrating, CancellationToken cancellationToken, int? upTo = null)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(db);
|
||||
ArgumentNullException.ThrowIfNull(clock);
|
||||
var status = await StatusAsync(db, cancellationToken).ConfigureAwait(false);
|
||||
var pending = status.Pending.Where(m => upTo is null || m.Version <= upTo).ToList();
|
||||
if (pending.Count == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!status.IsEmpty && backupBeforeMigrating is not null)
|
||||
{
|
||||
await backupBeforeMigrating(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
foreach (var migration in pending)
|
||||
{
|
||||
await db.WriteAsync(async s =>
|
||||
{
|
||||
if (migration.Version == 1)
|
||||
{
|
||||
await s.ExecuteAsync(
|
||||
$"CREATE TABLE {VersionTable} (version INTEGER PRIMARY KEY, name TEXT NOT NULL, checksum TEXT NOT NULL, applied_at {DbValues.ColumnType("timestamp", db.Engine)} NOT NULL){DbValues.ColumnType("strict", db.Engine)}",
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
await s.ExecuteAsync(migration.SqlFor(db.Engine), cancellationToken).ConfigureAwait(false);
|
||||
await s.ExecuteAsync($"INSERT INTO {VersionTable} (version, name, checksum, applied_at) VALUES (@v, @n, @c, @t)", cancellationToken,
|
||||
("v", migration.Version), ("n", migration.Name), ("c", migration.Checksum), ("t", clock.UtcNow)).ConfigureAwait(false);
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return pending.Count;
|
||||
}
|
||||
|
||||
public static async Task<bool> TableExistsAsync(DbSession session, string table)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(session);
|
||||
var sql = session.Engine == DbEngine.Sqlite
|
||||
? "SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = @t"
|
||||
: "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = current_schema() AND table_name = @t";
|
||||
return await session.CountAsync(sql, ("t", table)).ConfigureAwait(false) > 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The database schema cannot be used by this version: shown on the error page.</summary>
|
||||
public sealed class SchemaException(string message) : Exception(message);
|
||||
@@ -0,0 +1,120 @@
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Anubis.Db;
|
||||
|
||||
public enum ColumnKind
|
||||
{
|
||||
Text,
|
||||
Number,
|
||||
Money,
|
||||
Uuid,
|
||||
Moment,
|
||||
Json,
|
||||
Bool,
|
||||
Bytes,
|
||||
Measure,
|
||||
}
|
||||
|
||||
public sealed record Column(string Name, ColumnKind Kind, bool Nullable);
|
||||
|
||||
public sealed record Table(string Name, IReadOnlyList<Column> Columns);
|
||||
|
||||
/// <summary>
|
||||
/// The tables and typed columns, read from the migration files themselves: the SQL stays the only source of
|
||||
/// truth and export, import and checks know each column's kind on both engines. Tables are listed in creation
|
||||
/// order, which respects foreign keys.
|
||||
/// </summary>
|
||||
public static partial class Schema
|
||||
{
|
||||
/// <summary>Accounting tables: append-only, their money columns are the totals checked after an import (SPEC 5.4).</summary>
|
||||
public static readonly string[] AccountingTables = ["fills", "lots", "cash_ledger"];
|
||||
|
||||
private static readonly Lazy<IReadOnlyList<Table>> _tables = new(() => Parse(Migrations.All));
|
||||
|
||||
public static IReadOnlyList<Table> Tables => _tables.Value;
|
||||
|
||||
public static Table Get(string name) => Tables.First(t => t.Name == name);
|
||||
|
||||
[GeneratedRegex(@"CREATE\s+TABLE\s+(\w+)\s*\((.*?)\)\s*\{strict\}\s*;", RegexOptions.Singleline | RegexOptions.IgnoreCase)]
|
||||
private static partial Regex CreateTable();
|
||||
|
||||
[GeneratedRegex(@"ALTER\s+TABLE\s+(\w+)\s+ADD\s+COLUMN\s+(.+?);", RegexOptions.Singleline | RegexOptions.IgnoreCase)]
|
||||
private static partial Regex AddColumn();
|
||||
|
||||
[GeneratedRegex(@"--[^\n]*")]
|
||||
private static partial Regex Comment();
|
||||
|
||||
internal static IReadOnlyList<Table> Parse(IEnumerable<Migration> migrations)
|
||||
{
|
||||
var tables = new List<(string Name, List<Column> Columns)>();
|
||||
foreach (var migration in migrations)
|
||||
{
|
||||
var sql = Comment().Replace(migration.Sql, "");
|
||||
foreach (Match match in CreateTable().Matches(sql))
|
||||
{
|
||||
var columns = SplitTopLevel(match.Groups[2].Value).Select(ParseColumn).OfType<Column>().ToList();
|
||||
tables.Add((match.Groups[1].Value, columns));
|
||||
}
|
||||
|
||||
foreach (Match match in AddColumn().Matches(sql))
|
||||
{
|
||||
var column = ParseColumn(match.Groups[2].Value) ?? throw new InvalidOperationException("Unreadable column in " + migration.Name);
|
||||
tables.First(t => t.Name == match.Groups[1].Value).Columns.Add(column);
|
||||
}
|
||||
}
|
||||
|
||||
return tables.Select(t => new Table(t.Name, t.Columns)).ToList();
|
||||
}
|
||||
|
||||
private static Column? ParseColumn(string definition)
|
||||
{
|
||||
var parts = definition.Trim().Split((char[])[' ', '\t', '\r', '\n'], StringSplitOptions.RemoveEmptyEntries);
|
||||
if (parts.Length < 2 || parts[0].ToUpperInvariant() is "UNIQUE" or "PRIMARY" or "FOREIGN" or "CHECK" or "CONSTRAINT")
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var kind = parts[1].ToUpperInvariant() switch
|
||||
{
|
||||
"{MONEY}" => ColumnKind.Money,
|
||||
"{UUID}" => ColumnKind.Uuid,
|
||||
"{TIMESTAMP}" => ColumnKind.Moment,
|
||||
"{JSON}" => ColumnKind.Json,
|
||||
"{BOOL}" => ColumnKind.Bool,
|
||||
"{BYTES}" => ColumnKind.Bytes,
|
||||
"{FLOAT}" => ColumnKind.Measure,
|
||||
"TEXT" => ColumnKind.Text,
|
||||
"INTEGER" or "BIGINT" => ColumnKind.Number,
|
||||
var other => throw new InvalidOperationException($"Column type {other} is not portable: use TEXT, INTEGER, BIGINT or a token."),
|
||||
};
|
||||
var upper = definition.ToUpperInvariant();
|
||||
var nullable = !upper.Contains("NOT NULL", StringComparison.Ordinal) && !upper.Contains("PRIMARY KEY", StringComparison.Ordinal);
|
||||
return new Column(parts[0], kind, nullable);
|
||||
}
|
||||
|
||||
private static List<string> SplitTopLevel(string body)
|
||||
{
|
||||
var parts = new List<string>();
|
||||
var depth = 0;
|
||||
var start = 0;
|
||||
for (var i = 0; i < body.Length; i++)
|
||||
{
|
||||
switch (body[i])
|
||||
{
|
||||
case '(':
|
||||
depth++;
|
||||
break;
|
||||
case ')':
|
||||
depth--;
|
||||
break;
|
||||
case ',' when depth == 0:
|
||||
parts.Add(body[start..i]);
|
||||
start = i + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
parts.Add(body[start..]);
|
||||
return parts.Where(p => !string.IsNullOrWhiteSpace(p)).ToList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
namespace Anubis.Platform;
|
||||
|
||||
/// <summary>
|
||||
/// The data folder (SPEC 15.2, 15.6): <c>/data</c> in Docker, the user's application folder on desktop.
|
||||
/// Same layout on both: db/, models/, research/, import/, backups/, logs/, installation.json.
|
||||
/// </summary>
|
||||
public sealed record DataDirectory(string Root)
|
||||
{
|
||||
public static readonly string[] Folders = ["db", "models", "research", "import", "backups", "logs"];
|
||||
|
||||
public string Db => Path.Combine(Root, "db");
|
||||
|
||||
public string Models => Path.Combine(Root, "models");
|
||||
|
||||
public string Research => Path.Combine(Root, "research");
|
||||
|
||||
public string Import => Path.Combine(Root, "import");
|
||||
|
||||
public string Backups => Path.Combine(Root, "backups");
|
||||
|
||||
public string Logs => Path.Combine(Root, "logs");
|
||||
|
||||
public string InstallationFile => Path.Combine(Root, "installation.json");
|
||||
|
||||
public string ConfigFile => Path.Combine(Root, "config.toml");
|
||||
|
||||
public string SqliteFile => Path.Combine(Db, "anubis.sqlite3");
|
||||
|
||||
/// <summary>Creates the folder and its sub-folders when missing. Never deletes anything.</summary>
|
||||
public void Ensure()
|
||||
{
|
||||
Directory.CreateDirectory(Root);
|
||||
foreach (var folder in Folders)
|
||||
{
|
||||
Directory.CreateDirectory(Path.Combine(Root, folder));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The standard per-user folder (SPEC 15.6): %APPDATA%\Anubis, ~/Library/Application Support/Anubis, ~/.local/share/anubis.</summary>
|
||||
public static DataDirectory ForCurrentUser()
|
||||
{
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
return new(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Anubis"));
|
||||
}
|
||||
|
||||
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
|
||||
if (OperatingSystem.IsMacOS())
|
||||
{
|
||||
return new(Path.Combine(home, "Library", "Application Support", "Anubis"));
|
||||
}
|
||||
|
||||
var xdg = Environment.GetEnvironmentVariable("XDG_DATA_HOME");
|
||||
return new(Path.Combine(string.IsNullOrEmpty(xdg) ? Path.Combine(home, ".local", "share") : xdg, "anubis"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
using System.Text.Json;
|
||||
using Anubis.Core;
|
||||
using Anubis.Db;
|
||||
|
||||
namespace Anubis.Platform;
|
||||
|
||||
/// <summary>Content of <c>installation.json</c> in the data folder (SPEC 15.1).</summary>
|
||||
public sealed record InstallationFile(Guid InstallationId, string Fingerprint, string Engine, DateTimeOffset CreatedAt, DateTimeOffset RenewedAt);
|
||||
|
||||
/// <summary>The installation row in the database.</summary>
|
||||
public sealed record InstallationRow(Guid Id, string Fingerprint, string Engine, bool ProtectedRestart, DateTimeOffset CreatedAt, DateTimeOffset RenewedAt);
|
||||
|
||||
public enum IdentityVerdict
|
||||
{
|
||||
/// <summary>Same installation and same fingerprint: the engine may start.</summary>
|
||||
Match,
|
||||
|
||||
/// <summary>Empty database and a data folder that belongs to nobody: a new installation was created.</summary>
|
||||
Created,
|
||||
|
||||
/// <summary>The database knows the installation but the data folder lost its file: it was rewritten from the database.</summary>
|
||||
FileRestored,
|
||||
|
||||
/// <summary>The database is empty while the data folder belongs to an existing installation.</summary>
|
||||
EmptyDatabase,
|
||||
|
||||
/// <summary>Database and data folder belong to two different installations.</summary>
|
||||
DifferentInstallation,
|
||||
|
||||
/// <summary>Same installation, older fingerprint: a stale copy of the database (trap 26).</summary>
|
||||
StaleDatabase,
|
||||
}
|
||||
|
||||
public sealed record IdentityCheck(IdentityVerdict Verdict, InstallationFile? File, InstallationRow? Row)
|
||||
{
|
||||
public bool CanStart => Verdict is IdentityVerdict.Match or IdentityVerdict.Created or IdentityVerdict.FileRestored;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Identity of the installation and fingerprint of the database (SPEC 15.1): one live database per
|
||||
/// installation. Every import, restore or move gets a new fingerprint, written to the target database and to
|
||||
/// installation.json; a database left with an older fingerprint stops at the error page.
|
||||
/// </summary>
|
||||
public static class Installation
|
||||
{
|
||||
private static readonly JsonSerializerOptions _json = new(JsonSerializerDefaults.Web) { WriteIndented = true };
|
||||
|
||||
public static InstallationFile? ReadFile(DataDirectory data)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(data);
|
||||
return File.Exists(data.InstallationFile)
|
||||
? JsonSerializer.Deserialize<InstallationFile>(File.ReadAllText(data.InstallationFile), _json)
|
||||
: null;
|
||||
}
|
||||
|
||||
public static void WriteFile(DataDirectory data, InstallationFile file)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(data);
|
||||
Directory.CreateDirectory(data.Root);
|
||||
var temp = data.InstallationFile + ".tmp";
|
||||
File.WriteAllText(temp, JsonSerializer.Serialize(file, _json));
|
||||
File.Move(temp, data.InstallationFile, overwrite: true);
|
||||
}
|
||||
|
||||
public static async Task<InstallationRow?> ReadRowAsync(Database db, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(db);
|
||||
return await db.ReadAsync(s => s.FirstOrDefaultAsync(
|
||||
"SELECT id, fingerprint, engine, protected_restart, created_at, renewed_at FROM installation",
|
||||
r => new InstallationRow(r.Id("id"), r.Text("fingerprint"), r.Text("engine"), r.Bool("protected_restart"), r.Moment("created_at"), r.Moment("renewed_at"))),
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compares database and data folder at start. Creates the installation only when both are new; never
|
||||
/// touches a database that belongs elsewhere.
|
||||
/// </summary>
|
||||
public static async Task<IdentityCheck> CheckAsync(Database db, DataDirectory data, IClock clock, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(db);
|
||||
ArgumentNullException.ThrowIfNull(clock);
|
||||
var file = ReadFile(data);
|
||||
var row = await ReadRowAsync(db, cancellationToken).ConfigureAwait(false);
|
||||
switch (row, file)
|
||||
{
|
||||
case (null, null):
|
||||
var created = await CreateAsync(db, data, clock, cancellationToken).ConfigureAwait(false);
|
||||
return new IdentityCheck(IdentityVerdict.Created, created.File, created.Row);
|
||||
case (null, not null):
|
||||
return new IdentityCheck(IdentityVerdict.EmptyDatabase, file, null);
|
||||
case (not null, null):
|
||||
var restored = new InstallationFile(row.Id, row.Fingerprint, row.Engine, row.CreatedAt, row.RenewedAt);
|
||||
WriteFile(data, restored);
|
||||
return new IdentityCheck(IdentityVerdict.FileRestored, restored, row);
|
||||
default:
|
||||
if (row.Id != file.InstallationId)
|
||||
{
|
||||
return new IdentityCheck(IdentityVerdict.DifferentInstallation, file, row);
|
||||
}
|
||||
|
||||
return row.Fingerprint == file.Fingerprint
|
||||
? new IdentityCheck(IdentityVerdict.Match, file, row)
|
||||
: new IdentityCheck(IdentityVerdict.StaleDatabase, file, row);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<(InstallationFile File, InstallationRow Row)> CreateAsync(Database db, DataDirectory data, IClock clock, CancellationToken cancellationToken)
|
||||
{
|
||||
var now = clock.UtcNow;
|
||||
var row = new InstallationRow(Ids.NewId(clock), Ids.NewToken(), db.Engine.ToString().ToLowerInvariant(), false, now, now);
|
||||
await db.WriteAsync(s => s.ExecuteAsync(
|
||||
"INSERT INTO installation (id, fingerprint, engine, protected_restart, created_at, renewed_at) VALUES (@id, @f, @e, @p, @c, @r)",
|
||||
cancellationToken, ("id", row.Id), ("f", row.Fingerprint), ("e", row.Engine), ("p", false), ("c", now), ("r", now)), cancellationToken).ConfigureAwait(false);
|
||||
var file = new InstallationFile(row.Id, row.Fingerprint, row.Engine, now, now);
|
||||
WriteFile(data, file);
|
||||
return (file, row);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// After an import, a restore, a move, or when the user adopts this database: a new fingerprint in the database
|
||||
/// and in installation.json, the installation id of the data folder kept (or taken from the database when
|
||||
/// <paramref name="adoptDatabaseIdentity"/>), and the protected restart switched on (SPEC 15.1).
|
||||
/// </summary>
|
||||
public static async Task<InstallationFile> RenewAsync(Database db, DataDirectory data, IClock clock, bool adoptDatabaseIdentity, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(db);
|
||||
ArgumentNullException.ThrowIfNull(clock);
|
||||
var now = clock.UtcNow;
|
||||
var fingerprint = Ids.NewToken();
|
||||
var file = ReadFile(data);
|
||||
var row = await ReadRowAsync(db, cancellationToken).ConfigureAwait(false);
|
||||
var id = adoptDatabaseIdentity || file is null ? row?.Id ?? Ids.NewId(clock) : file.InstallationId;
|
||||
var createdAt = row?.CreatedAt ?? file?.CreatedAt ?? now;
|
||||
var engine = db.Engine.ToString().ToLowerInvariant();
|
||||
await db.WriteAsync(async s =>
|
||||
{
|
||||
await s.ExecuteAsync("DELETE FROM installation", cancellationToken).ConfigureAwait(false);
|
||||
await s.ExecuteAsync(
|
||||
"INSERT INTO installation (id, fingerprint, engine, protected_restart, created_at, renewed_at) VALUES (@id, @f, @e, @p, @c, @r)",
|
||||
cancellationToken, ("id", id), ("f", fingerprint), ("e", engine), ("p", true), ("c", createdAt), ("r", now)).ConfigureAwait(false);
|
||||
|
||||
// Sessions of the old database do not survive: everyone signs in again.
|
||||
await s.ExecuteAsync("DELETE FROM sessions", cancellationToken).ConfigureAwait(false);
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
var renewed = new InstallationFile(id, fingerprint, engine, createdAt, now);
|
||||
WriteFile(data, renewed);
|
||||
return renewed;
|
||||
}
|
||||
|
||||
/// <summary>"Comincia da capo": the data folder forgets its installation so an empty database can start a new one.</summary>
|
||||
public static void StartOver(DataDirectory data, IClock clock)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(data);
|
||||
ArgumentNullException.ThrowIfNull(clock);
|
||||
if (File.Exists(data.InstallationFile))
|
||||
{
|
||||
var stamp = clock.UtcNow.ToString("yyyyMMdd-HHmmss", System.Globalization.CultureInfo.InvariantCulture);
|
||||
File.Move(data.InstallationFile, data.InstallationFile + ".precedente-" + stamp);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The user confirmed the protected restart (phase 9 adds the full procedure of SPEC 15.1).</summary>
|
||||
public static Task ClearProtectedRestartAsync(Database db, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(db);
|
||||
return db.WriteAsync(s => s.ExecuteAsync("UPDATE installation SET protected_restart = @p", cancellationToken, ("p", false)), cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,350 @@
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using Anubis.Core;
|
||||
|
||||
namespace Anubis.Platform;
|
||||
|
||||
/// <summary>
|
||||
/// Docker (SPEC 15.1): everything from environment variables. Missing or wrong values become
|
||||
/// <see cref="ConfigProblem"/>s for the error page; nothing falls back to another database (trap 20).
|
||||
/// </summary>
|
||||
public static class DockerPlatform
|
||||
{
|
||||
public const string DefaultTimeZone = "Europe/Rome";
|
||||
|
||||
/// <summary>Optional keys accepted from the environment (SPEC 15.1), each also with the _DEMO suffix.</summary>
|
||||
public static readonly string[] OptionalKeys =
|
||||
[
|
||||
"ALPACA_API_KEY", "ALPACA_API_SECRET", "OKX_API_KEY", "OKX_API_SECRET", "OKX_API_PASSPHRASE",
|
||||
"TELEGRAM_BOT_TOKEN", "TELEGRAM_CHAT_ID", "LLM_API_KEY", "FINNHUB_API_KEY", "FRED_API_KEY",
|
||||
];
|
||||
|
||||
public static StartupConfig Load(IReadOnlyDictionary<string, string?> env, string dataRoot = "/data")
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(env);
|
||||
string? Get(string name) => env.TryGetValue(name, out var v) && !string.IsNullOrWhiteSpace(v) ? v.Trim() : null;
|
||||
|
||||
var problems = new List<ConfigProblem>();
|
||||
var data = new DataDirectory(dataRoot);
|
||||
|
||||
var zone = ResolveTimeZone(Get("TZ") ?? DefaultTimeZone, problems, "TZ");
|
||||
var db = LoadDb(Get, data, problems);
|
||||
|
||||
var secret = Get("ANUBIS_SECRET_KEY");
|
||||
if (secret is null)
|
||||
{
|
||||
problems.Add(new("ANUBIS_SECRET_KEY", "Manca la chiave che cifra i segreti nel database.",
|
||||
"Imposta ANUBIS_SECRET_KEY con almeno 32 caratteri casuali e riavvia il container. Conservala: se cambia, le chiavi dei conti vanno reinserite."));
|
||||
}
|
||||
else if (secret.Length < EnvironmentSecretKey.MinimumLength)
|
||||
{
|
||||
problems.Add(new("ANUBIS_SECRET_KEY", "La chiave che cifra i segreti è troppo corta.",
|
||||
"Usa almeno 32 caratteri casuali in ANUBIS_SECRET_KEY e riavvia il container."));
|
||||
}
|
||||
|
||||
var admin = Get("ANUBIS_ADMIN_PASSWORD");
|
||||
if (admin is null)
|
||||
{
|
||||
problems.Add(new("ANUBIS_ADMIN_PASSWORD", "Manca la password iniziale.",
|
||||
"Imposta ANUBIS_ADMIN_PASSWORD e riavvia il container. Al primo accesso ne sceglierai una nuova; questa resta la chiave delle scelte di questa pagina."));
|
||||
}
|
||||
|
||||
Redactor.Register(secret);
|
||||
Redactor.Register(admin);
|
||||
Redactor.Register(db?.Password);
|
||||
|
||||
var allowLive = ParseAllowLive(Get("ANUBIS_ALLOW_LIVE"), problems);
|
||||
var port = ParsePort(Get("PORT"), 8787, "PORT", problems);
|
||||
var puid = ParseInt(Get("PUID"), 99, "PUID", problems);
|
||||
var pgid = ParseInt(Get("PGID"), 100, "PGID", problems);
|
||||
|
||||
var keys = new Dictionary<string, string>(StringComparer.Ordinal);
|
||||
foreach (var name in OptionalKeys.SelectMany(k => new[] { k, k + "_DEMO" }))
|
||||
{
|
||||
if (Get(name) is { } value)
|
||||
{
|
||||
keys[name] = value;
|
||||
Redactor.Register(value);
|
||||
}
|
||||
}
|
||||
|
||||
return new StartupConfig
|
||||
{
|
||||
Platform = PlatformKind.Docker,
|
||||
Data = data,
|
||||
Db = db,
|
||||
TimeZone = zone,
|
||||
Port = port,
|
||||
AllowNetwork = true,
|
||||
AllowLive = allowLive,
|
||||
AdminPassword = admin,
|
||||
SecretKey = new EnvironmentSecretKey(secret),
|
||||
Puid = puid,
|
||||
Pgid = pgid,
|
||||
HeartbeatUrl = Get("ANUBIS_HEARTBEAT_URL"),
|
||||
EnvironmentKeys = keys,
|
||||
Problems = problems,
|
||||
};
|
||||
}
|
||||
|
||||
private static DbSettings? LoadDb(Func<string, string?> get, DataDirectory data, List<ConfigProblem> problems)
|
||||
{
|
||||
var engine = get("ANUBIS_DB_ENGINE");
|
||||
if (engine is null)
|
||||
{
|
||||
problems.Add(new("ANUBIS_DB_ENGINE", "Non è indicato quale database usare.",
|
||||
"Imposta ANUBIS_DB_ENGINE a postgres oppure a sqlite e riavvia il container."));
|
||||
return null;
|
||||
}
|
||||
|
||||
switch (engine.ToLowerInvariant())
|
||||
{
|
||||
case "sqlite":
|
||||
return new DbSettings { Engine = DbEngine.Sqlite, SqlitePath = data.SqliteFile };
|
||||
case "postgres":
|
||||
break;
|
||||
default:
|
||||
problems.Add(new("ANUBIS_DB_ENGINE", $"Il valore '{engine}' non è un database conosciuto.",
|
||||
"Usa postgres oppure sqlite e riavvia il container."));
|
||||
return null;
|
||||
}
|
||||
|
||||
var url = get("ANUBIS_DB_URL");
|
||||
if (url is not null)
|
||||
{
|
||||
return new DbSettings { Engine = DbEngine.Postgres, Url = url };
|
||||
}
|
||||
|
||||
var host = get("ANUBIS_DB_HOST");
|
||||
var password = get("ANUBIS_DB_PASSWORD");
|
||||
if (host is null)
|
||||
{
|
||||
problems.Add(new("ANUBIS_DB_HOST", "Manca l'indirizzo del server PostgreSQL.",
|
||||
"Imposta ANUBIS_DB_HOST (per esempio l'indirizzo IP del server o il nome del container) e riavvia."));
|
||||
}
|
||||
|
||||
if (password is null)
|
||||
{
|
||||
problems.Add(new("ANUBIS_DB_PASSWORD", "Manca la password dell'utente di PostgreSQL.",
|
||||
"Imposta ANUBIS_DB_PASSWORD con la password dell'utente dedicato ad Anubis e riavvia."));
|
||||
}
|
||||
|
||||
var port = ParsePort(get("ANUBIS_DB_PORT"), 5432, "ANUBIS_DB_PORT", problems);
|
||||
return host is null || password is null
|
||||
? null
|
||||
: new DbSettings
|
||||
{
|
||||
Engine = DbEngine.Postgres,
|
||||
Host = host,
|
||||
Port = port,
|
||||
Database = get("ANUBIS_DB_NAME") ?? "anubis",
|
||||
User = get("ANUBIS_DB_USER") ?? "anubis",
|
||||
Password = password,
|
||||
};
|
||||
}
|
||||
|
||||
internal static TimeZoneInfo ResolveTimeZone(string id, List<ConfigProblem> problems, string setting)
|
||||
{
|
||||
try
|
||||
{
|
||||
return TimeZoneInfo.FindSystemTimeZoneById(id);
|
||||
}
|
||||
catch (Exception ex) when (ex is TimeZoneNotFoundException or InvalidTimeZoneException)
|
||||
{
|
||||
problems.Add(new(setting, $"Il fuso orario '{id}' non esiste.", "Usa un nome come Europe/Rome e riavvia."));
|
||||
return TimeZoneInfo.Utc;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool ParseAllowLive(string? value, List<ConfigProblem> problems)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
case null or "0":
|
||||
return false;
|
||||
case "1":
|
||||
return true;
|
||||
default:
|
||||
problems.Add(new("ANUBIS_ALLOW_LIVE", $"Il valore '{value}' non è ammesso.", "Usa 0 (predefinito) oppure 1 e riavvia."));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static int ParseInt(string? value, int fallback, string setting, List<ConfigProblem> problems)
|
||||
{
|
||||
if (value is null)
|
||||
{
|
||||
return fallback;
|
||||
}
|
||||
|
||||
if (int.TryParse(value, NumberStyles.None, CultureInfo.InvariantCulture, out var parsed))
|
||||
{
|
||||
return parsed;
|
||||
}
|
||||
|
||||
problems.Add(new(setting, $"Il valore '{value}' non è un numero intero.", $"Correggi {setting} e riavvia."));
|
||||
return fallback;
|
||||
}
|
||||
|
||||
internal static int ParsePort(string? value, int fallback, string setting, List<ConfigProblem> problems)
|
||||
{
|
||||
var port = ParseInt(value, fallback, setting, problems);
|
||||
if (port is < 1 or > 65535)
|
||||
{
|
||||
problems.Add(new(setting, $"La porta {port.ToString(CultureInfo.InvariantCulture)} non è valida.", "Usa un numero tra 1 e 65535."));
|
||||
return fallback;
|
||||
}
|
||||
|
||||
return port;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Desktop (SPEC 15.6): <c>config.toml</c> in the user's data folder, written by the first-run wizard.
|
||||
/// The secret key and the PostgreSQL password never go in the file: they live in the <see cref="IProtectedStore"/>.
|
||||
/// </summary>
|
||||
public static class DesktopPlatform
|
||||
{
|
||||
public const string DbPasswordName = "db-password";
|
||||
|
||||
public static StartupConfig Load(DataDirectory data, IProtectedStore store)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(data);
|
||||
ArgumentNullException.ThrowIfNull(store);
|
||||
var problems = new List<ConfigProblem>();
|
||||
var secretKey = new ProtectedSecretKey(store);
|
||||
if (!File.Exists(data.ConfigFile))
|
||||
{
|
||||
return new StartupConfig
|
||||
{
|
||||
Platform = PlatformKind.Desktop,
|
||||
Data = data,
|
||||
TimeZone = TimeZoneInfo.Local,
|
||||
SecretKey = secretKey,
|
||||
NeedsFirstRun = true,
|
||||
};
|
||||
}
|
||||
|
||||
IReadOnlyDictionary<string, object> toml;
|
||||
try
|
||||
{
|
||||
toml = Toml.Parse(File.ReadAllText(data.ConfigFile));
|
||||
}
|
||||
catch (FormatException ex)
|
||||
{
|
||||
problems.Add(new("config.toml", "Il file di configurazione non si legge: " + ex.Message,
|
||||
$"Correggi il file {data.ConfigFile} oppure rinominalo per ripetere la procedura di primo avvio."));
|
||||
return new StartupConfig { Platform = PlatformKind.Desktop, Data = data, TimeZone = TimeZoneInfo.Local, SecretKey = secretKey, Problems = problems };
|
||||
}
|
||||
|
||||
string? Text(string key) => toml.TryGetValue(key, out var v) ? v as string ?? Fail<string>(key, "testo tra virgolette") : null;
|
||||
long? Integer(string key) => toml.TryGetValue(key, out var v) ? v is long n ? n : Fail<long?>(key, "un numero intero") : null;
|
||||
bool? Flag(string key) => toml.TryGetValue(key, out var v) ? v is bool b ? b : Fail<bool?>(key, "true o false") : null;
|
||||
T? Fail<T>(string key, string expected)
|
||||
{
|
||||
problems.Add(new("config.toml", $"La voce '{key}' deve essere {expected}.", $"Correggi {data.ConfigFile} e riavvia."));
|
||||
return default;
|
||||
}
|
||||
|
||||
var zone = Text("timezone") is { } tz ? DockerPlatform.ResolveTimeZone(tz, problems, "config.toml: timezone") : TimeZoneInfo.Local;
|
||||
DbSettings? db = null;
|
||||
switch (Text("database.engine"))
|
||||
{
|
||||
case "sqlite":
|
||||
db = new DbSettings { Engine = DbEngine.Sqlite, SqlitePath = data.SqliteFile };
|
||||
break;
|
||||
case "postgres":
|
||||
var password = store.Read(DbPasswordName) is { } bytes ? Encoding.UTF8.GetString(bytes) : null;
|
||||
Redactor.Register(password);
|
||||
if (Text("database.host") is not { } host)
|
||||
{
|
||||
problems.Add(new("config.toml", "Manca l'indirizzo del server PostgreSQL (database.host).", $"Correggi {data.ConfigFile} e riavvia."));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (password is null)
|
||||
{
|
||||
problems.Add(new("Password del database", "La password di PostgreSQL non è nel portachiavi.",
|
||||
"Ripeti la procedura di primo avvio rinominando config.toml, oppure torna a SQLite."));
|
||||
}
|
||||
|
||||
db = new DbSettings
|
||||
{
|
||||
Engine = DbEngine.Postgres,
|
||||
Host = host,
|
||||
Port = (int)(Integer("database.port") ?? 5432),
|
||||
Database = Text("database.name") ?? "anubis",
|
||||
User = Text("database.user") ?? "anubis",
|
||||
Password = password,
|
||||
};
|
||||
}
|
||||
|
||||
break;
|
||||
case null:
|
||||
problems.Add(new("config.toml", "Non è indicato quale database usare (database.engine).", $"Scrivi engine = \"sqlite\" oppure \"postgres\" nella sezione [database] di {data.ConfigFile}."));
|
||||
break;
|
||||
case var other:
|
||||
problems.Add(new("config.toml", $"Il database '{other}' non è conosciuto.", "Usa \"sqlite\" oppure \"postgres\"."));
|
||||
break;
|
||||
}
|
||||
|
||||
var port = Integer("port") ?? 8787;
|
||||
if (port is < 1 or > 65535)
|
||||
{
|
||||
problems.Add(new("config.toml", "La porta non è valida.", "Usa un numero tra 1 e 65535."));
|
||||
port = 8787;
|
||||
}
|
||||
|
||||
return new StartupConfig
|
||||
{
|
||||
Platform = PlatformKind.Desktop,
|
||||
Data = data,
|
||||
Db = db,
|
||||
TimeZone = zone,
|
||||
Port = (int)port,
|
||||
AllowNetwork = Flag("allow_network") ?? false,
|
||||
AllowLive = Flag("allow_live") ?? false,
|
||||
SecretKey = secretKey,
|
||||
Problems = problems,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes config.toml at the end of the first-run wizard. <c>allow_live</c> is born false and only a hand edit
|
||||
/// turns it on (SPEC 15.6). The PostgreSQL password goes to the protected store, never into the file.
|
||||
/// </summary>
|
||||
public static void WriteConfig(DataDirectory data, IProtectedStore store, DbSettings db, int port = 8787, bool allowNetwork = false)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(data);
|
||||
ArgumentNullException.ThrowIfNull(store);
|
||||
ArgumentNullException.ThrowIfNull(db);
|
||||
var text = new StringBuilder();
|
||||
text.AppendLine("# Anubis: configurazione d'avvio (SPEC 15.6). Scritta dalla procedura di primo avvio.");
|
||||
text.AppendLine("# La chiave dei segreti e la password del database non stanno qui: sono nel portachiavi.");
|
||||
text.AppendLine();
|
||||
text.AppendLine("# Soldi veri: false. Si cambia solo a mano, mai dall'interfaccia.");
|
||||
text.AppendLine(CultureInfo.InvariantCulture, $"allow_live = false");
|
||||
text.AppendLine(CultureInfo.InvariantCulture, $"port = {port}");
|
||||
text.AppendLine(CultureInfo.InvariantCulture, $"allow_network = {(allowNetwork ? "true" : "false")}");
|
||||
text.AppendLine();
|
||||
text.AppendLine("[database]");
|
||||
if (db.Engine == DbEngine.Sqlite)
|
||||
{
|
||||
text.AppendLine("engine = \"sqlite\"");
|
||||
}
|
||||
else
|
||||
{
|
||||
text.AppendLine("engine = \"postgres\"");
|
||||
text.AppendLine(CultureInfo.InvariantCulture, $"host = {Toml.Quote(db.Host ?? "")}");
|
||||
text.AppendLine(CultureInfo.InvariantCulture, $"port = {db.Port}");
|
||||
text.AppendLine(CultureInfo.InvariantCulture, $"name = {Toml.Quote(db.Database)}");
|
||||
text.AppendLine(CultureInfo.InvariantCulture, $"user = {Toml.Quote(db.User)}");
|
||||
store.Write(DbPasswordName, Encoding.UTF8.GetBytes(db.Password ?? ""));
|
||||
}
|
||||
|
||||
data.Ensure();
|
||||
var temp = data.ConfigFile + ".tmp";
|
||||
File.WriteAllText(temp, text.ToString().ReplaceLineEndings("\n"));
|
||||
File.Move(temp, data.ConfigFile, overwrite: true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
using System.Runtime.Versioning;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace Anubis.Platform;
|
||||
|
||||
/// <summary>
|
||||
/// Custody of the key that encrypts the secrets in the database (SPEC 15.4, 15.6). The interface says
|
||||
/// where the key lives, so Settings can declare it.
|
||||
/// </summary>
|
||||
public interface ISecretKeyStore
|
||||
{
|
||||
/// <summary>Where the key lives, in plain Italian, for Settings → Sistema.</summary>
|
||||
string Description { get; }
|
||||
|
||||
/// <summary>True when the key exists (or, in Docker, the variable is set).</summary>
|
||||
bool HasKey { get; }
|
||||
|
||||
/// <summary>The 32-byte key. Desktop stores create it on first use.</summary>
|
||||
byte[] GetOrCreateKey();
|
||||
}
|
||||
|
||||
/// <summary>Docker: the key derives from <c>ANUBIS_SECRET_KEY</c> (at least 32 characters, SPEC 15.1).</summary>
|
||||
public sealed class EnvironmentSecretKey(string? value) : ISecretKeyStore
|
||||
{
|
||||
public const int MinimumLength = 32;
|
||||
|
||||
public string Description => "Variabile d'ambiente ANUBIS_SECRET_KEY del container";
|
||||
|
||||
public bool HasKey => value is { Length: >= MinimumLength };
|
||||
|
||||
public byte[] GetOrCreateKey()
|
||||
{
|
||||
if (!HasKey)
|
||||
{
|
||||
throw new InvalidOperationException("ANUBIS_SECRET_KEY is missing or shorter than 32 characters.");
|
||||
}
|
||||
|
||||
return HKDF.DeriveKey(HashAlgorithmName.SHA256, Encoding.UTF8.GetBytes(value!), 32, salt: [], info: "anubis-secrets-v1"u8.ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Small values kept outside config.toml: the desktop secret key and the PostgreSQL password (SPEC 15.6).</summary>
|
||||
public interface IProtectedStore
|
||||
{
|
||||
string Description { get; }
|
||||
|
||||
byte[]? Read(string name);
|
||||
|
||||
void Write(string name, byte[] value);
|
||||
}
|
||||
|
||||
/// <summary>Windows: files in the data folder encrypted with DPAPI for the current user (ADR 0003 in place of keyring).</summary>
|
||||
[SupportedOSPlatform("windows")]
|
||||
public sealed class DpapiStore(string folder) : IProtectedStore
|
||||
{
|
||||
private static readonly byte[] _entropy = "Anubis protected store v1"u8.ToArray();
|
||||
|
||||
public string Description => "Protetta da Windows per il tuo utente (DPAPI), nella cartella dei dati";
|
||||
|
||||
public byte[]? Read(string name)
|
||||
{
|
||||
var path = PathOf(name);
|
||||
return File.Exists(path) ? ProtectedData.Unprotect(File.ReadAllBytes(path), _entropy, DataProtectionScope.CurrentUser) : null;
|
||||
}
|
||||
|
||||
public void Write(string name, byte[] value)
|
||||
{
|
||||
Directory.CreateDirectory(folder);
|
||||
File.WriteAllBytes(PathOf(name), ProtectedData.Protect(value, _entropy, DataProtectionScope.CurrentUser));
|
||||
}
|
||||
|
||||
private string PathOf(string name) => Path.Combine(folder, name + ".dpapi");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Where no system keychain exists (Linux without a session, macOS for now, the tests): a file readable
|
||||
/// only by its owner. The interface declares it (SPEC 15.6).
|
||||
/// </summary>
|
||||
public sealed class RestrictedFileStore(string folder) : IProtectedStore
|
||||
{
|
||||
public string Description => "File con permessi ristretti nella cartella dei dati (nessun portachiavi disponibile)";
|
||||
|
||||
public byte[]? Read(string name)
|
||||
{
|
||||
var path = PathOf(name);
|
||||
return File.Exists(path) ? Convert.FromBase64String(File.ReadAllText(path).Trim()) : null;
|
||||
}
|
||||
|
||||
public void Write(string name, byte[] value)
|
||||
{
|
||||
Directory.CreateDirectory(folder);
|
||||
var path = PathOf(name);
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
File.WriteAllText(path, Convert.ToBase64String(value));
|
||||
return;
|
||||
}
|
||||
|
||||
// Create with 0600 from the start, so the value is never readable by others even for an instant.
|
||||
using var stream = new FileStream(path, new FileStreamOptions
|
||||
{
|
||||
Mode = FileMode.Create,
|
||||
Access = FileAccess.Write,
|
||||
UnixCreateMode = UnixFileMode.UserRead | UnixFileMode.UserWrite,
|
||||
});
|
||||
stream.Write(Encoding.ASCII.GetBytes(Convert.ToBase64String(value)));
|
||||
}
|
||||
|
||||
private string PathOf(string name) => Path.Combine(folder, name + ".key");
|
||||
}
|
||||
|
||||
/// <summary>Desktop: a random 32-byte key born on first start and kept in an <see cref="IProtectedStore"/>.</summary>
|
||||
public sealed class ProtectedSecretKey(IProtectedStore store) : ISecretKeyStore
|
||||
{
|
||||
private const string Name = "secret-key";
|
||||
|
||||
public string Description => store.Description;
|
||||
|
||||
public bool HasKey => store.Read(Name) is { Length: 32 };
|
||||
|
||||
public byte[] GetOrCreateKey()
|
||||
{
|
||||
if (store.Read(Name) is { Length: 32 } existing)
|
||||
{
|
||||
return existing;
|
||||
}
|
||||
|
||||
var key = RandomNumberGenerator.GetBytes(32);
|
||||
store.Write(Name, key);
|
||||
return key;
|
||||
}
|
||||
|
||||
/// <summary>The store for this machine: DPAPI on Windows, a restricted file elsewhere.</summary>
|
||||
public static IProtectedStore DefaultStore(DataDirectory data)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(data);
|
||||
var folder = Path.Combine(data.Root, "keys");
|
||||
return OperatingSystem.IsWindows() ? new DpapiStore(folder) : new RestrictedFileStore(folder);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
namespace Anubis.Platform;
|
||||
|
||||
public enum PlatformKind
|
||||
{
|
||||
Docker,
|
||||
Desktop,
|
||||
}
|
||||
|
||||
public enum DbEngine
|
||||
{
|
||||
Sqlite,
|
||||
Postgres,
|
||||
}
|
||||
|
||||
/// <summary>How to reach the database. The password never appears in <see cref="ToString"/>.</summary>
|
||||
public sealed record DbSettings
|
||||
{
|
||||
public required DbEngine Engine { get; init; }
|
||||
|
||||
/// <summary>SQLite file path; null for PostgreSQL.</summary>
|
||||
public string? SqlitePath { get; init; }
|
||||
|
||||
public string? Host { get; init; }
|
||||
|
||||
public int Port { get; init; } = 5432;
|
||||
|
||||
public string Database { get; init; } = "anubis";
|
||||
|
||||
public string User { get; init; } = "anubis";
|
||||
|
||||
public string? Password { get; init; }
|
||||
|
||||
/// <summary>A full PostgreSQL address (<c>ANUBIS_DB_URL</c>) that replaces the five fields.</summary>
|
||||
public string? Url { get; init; }
|
||||
|
||||
/// <summary>What the interface shows: engine and place, never the password.</summary>
|
||||
public string Describe() => Engine switch
|
||||
{
|
||||
DbEngine.Sqlite => "SQLite, " + SqlitePath,
|
||||
_ when Url is not null => "PostgreSQL, indirizzo completo da ANUBIS_DB_URL",
|
||||
_ => $"PostgreSQL, {Host}:{Port}/{Database} come {User}",
|
||||
};
|
||||
|
||||
public override string ToString() => Describe();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The startup configuration (SPEC 15.1, 15.6): everything needed before the database is reachable.
|
||||
/// It comes from environment variables in Docker and from <c>config.toml</c> on desktop. Values here win
|
||||
/// over the ones saved from the interface, which shows them read-only.
|
||||
/// </summary>
|
||||
public sealed record StartupConfig
|
||||
{
|
||||
public required PlatformKind Platform { get; init; }
|
||||
|
||||
public required DataDirectory Data { get; init; }
|
||||
|
||||
/// <summary>Null when the configuration does not say which database to use (the error page explains what is missing).</summary>
|
||||
public DbSettings? Db { get; init; }
|
||||
|
||||
public required TimeZoneInfo TimeZone { get; init; }
|
||||
|
||||
public int Port { get; init; } = 8787;
|
||||
|
||||
/// <summary>Desktop: listen on the home network too (SPEC 15.6). Docker always listens on every interface of the container.</summary>
|
||||
public bool AllowNetwork { get; init; }
|
||||
|
||||
/// <summary><c>ANUBIS_ALLOW_LIVE=1</c> or <c>allow_live = true</c>: only then adapters accept a real account (SPEC 2.2).</summary>
|
||||
public bool AllowLive { get; init; }
|
||||
|
||||
/// <summary>Docker: the initial password (<c>ANUBIS_ADMIN_PASSWORD</c>), also the key of the error page choices.</summary>
|
||||
public string? AdminPassword { get; init; }
|
||||
|
||||
public required ISecretKeyStore SecretKey { get; init; }
|
||||
|
||||
public int Puid { get; init; } = 99;
|
||||
|
||||
public int Pgid { get; init; } = 100;
|
||||
|
||||
public string? HeartbeatUrl { get; init; }
|
||||
|
||||
/// <summary>Optional broker and service keys from the environment (SPEC 15.1), by variable name.</summary>
|
||||
public IReadOnlyDictionary<string, string> EnvironmentKeys { get; init; } = new Dictionary<string, string>();
|
||||
|
||||
/// <summary>Problems that stop the start: the error page lists them with what to do.</summary>
|
||||
public IReadOnlyList<ConfigProblem> Problems { get; init; } = [];
|
||||
|
||||
/// <summary>Desktop only: <c>config.toml</c> does not exist yet, the first-run wizard writes it.</summary>
|
||||
public bool NeedsFirstRun { get; init; }
|
||||
|
||||
public bool IsComplete => Problems.Count == 0 && Db is not null && !NeedsFirstRun;
|
||||
}
|
||||
|
||||
/// <summary>Something missing or wrong in the startup configuration, said in plain Italian with the remedy.</summary>
|
||||
public sealed record ConfigProblem(string Setting, string What, string Remedy);
|
||||
@@ -0,0 +1,165 @@
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
|
||||
namespace Anubis.Platform;
|
||||
|
||||
/// <summary>
|
||||
/// The subset of TOML that <c>config.toml</c> needs (ADR 0003, no external parser): comments, tables
|
||||
/// <c>[name]</c>, and <c>key = value</c> with basic strings, integers and booleans. Anything else is an error
|
||||
/// with its line number, so a hand edit gone wrong shows on the error page instead of being guessed.
|
||||
/// </summary>
|
||||
public static class Toml
|
||||
{
|
||||
/// <summary>Parses into "table.key" → value (string, long or bool). Keys outside a table have no prefix.</summary>
|
||||
public static IReadOnlyDictionary<string, object> Parse(string text)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(text);
|
||||
var result = new Dictionary<string, object>(StringComparer.Ordinal);
|
||||
var table = "";
|
||||
var lineNumber = 0;
|
||||
foreach (var rawLine in text.Split('\n'))
|
||||
{
|
||||
lineNumber++;
|
||||
var line = StripComment(rawLine).Trim();
|
||||
if (line.Length == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line.StartsWith('['))
|
||||
{
|
||||
if (!line.EndsWith(']') || line.StartsWith("[[", StringComparison.Ordinal))
|
||||
{
|
||||
throw new FormatException($"Riga {lineNumber}: intestazione di sezione non valida.");
|
||||
}
|
||||
|
||||
table = line[1..^1].Trim();
|
||||
if (!IsBareKey(table))
|
||||
{
|
||||
throw new FormatException($"Riga {lineNumber}: nome di sezione non valido.");
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
var equals = line.IndexOf('=', StringComparison.Ordinal);
|
||||
if (equals <= 0)
|
||||
{
|
||||
throw new FormatException($"Riga {lineNumber}: manca il segno '='.");
|
||||
}
|
||||
|
||||
var key = line[..equals].Trim();
|
||||
if (!IsBareKey(key))
|
||||
{
|
||||
throw new FormatException($"Riga {lineNumber}: nome non valido '{key}'.");
|
||||
}
|
||||
|
||||
var fullKey = table.Length == 0 ? key : table + "." + key;
|
||||
if (!result.TryAdd(fullKey, ParseValue(line[(equals + 1)..].Trim(), lineNumber)))
|
||||
{
|
||||
throw new FormatException($"Riga {lineNumber}: '{fullKey}' compare due volte.");
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static object ParseValue(string value, int lineNumber)
|
||||
{
|
||||
if (value.StartsWith('"'))
|
||||
{
|
||||
return ParseString(value, lineNumber);
|
||||
}
|
||||
|
||||
if (value is "true")
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (value is "false")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (long.TryParse(value.Replace("_", "", StringComparison.Ordinal), NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var number))
|
||||
{
|
||||
return number;
|
||||
}
|
||||
|
||||
throw new FormatException($"Riga {lineNumber}: valore non riconosciuto. Usa testo tra virgolette, un numero intero, true o false.");
|
||||
}
|
||||
|
||||
private static string ParseString(string value, int lineNumber)
|
||||
{
|
||||
var text = new StringBuilder();
|
||||
for (var i = 1; i < value.Length; i++)
|
||||
{
|
||||
var c = value[i];
|
||||
if (c == '"')
|
||||
{
|
||||
if (i != value.Length - 1)
|
||||
{
|
||||
throw new FormatException($"Riga {lineNumber}: testo dopo la chiusura delle virgolette.");
|
||||
}
|
||||
|
||||
return text.ToString();
|
||||
}
|
||||
|
||||
if (c == '\\')
|
||||
{
|
||||
if (++i >= value.Length)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
text.Append(value[i] switch
|
||||
{
|
||||
'"' => '"',
|
||||
'\\' => '\\',
|
||||
'n' => '\n',
|
||||
't' => '\t',
|
||||
'r' => '\r',
|
||||
_ => throw new FormatException($"Riga {lineNumber}: sequenza '\\{value[i]}' non ammessa."),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
text.Append(c);
|
||||
}
|
||||
|
||||
throw new FormatException($"Riga {lineNumber}: virgolette non chiuse.");
|
||||
}
|
||||
|
||||
// A '#' starts a comment unless it is inside a string.
|
||||
private static string StripComment(string line)
|
||||
{
|
||||
var inString = false;
|
||||
for (var i = 0; i < line.Length; i++)
|
||||
{
|
||||
if (line[i] == '\\' && inString)
|
||||
{
|
||||
i++;
|
||||
}
|
||||
else if (line[i] == '"')
|
||||
{
|
||||
inString = !inString;
|
||||
}
|
||||
else if (line[i] == '#' && !inString)
|
||||
{
|
||||
return line[..i];
|
||||
}
|
||||
}
|
||||
|
||||
return line;
|
||||
}
|
||||
|
||||
private static bool IsBareKey(string key) =>
|
||||
key.Length > 0 && key.All(c => char.IsAsciiLetterOrDigit(c) || c is '_' or '-');
|
||||
|
||||
public static string Quote(string value)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(value);
|
||||
return "\"" + value.Replace("\\", "\\\\", StringComparison.Ordinal).Replace("\"", "\\\"", StringComparison.Ordinal)
|
||||
.Replace("\n", "\\n", StringComparison.Ordinal).Replace("\r", "\\r", StringComparison.Ordinal).Replace("\t", "\\t", StringComparison.Ordinal) + "\"";
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,8 @@ Le cartelle nascono con il primo codice che le riempie:
|
||||
|---|---|---|---|
|
||||
| `Core/` | `Anubis.Core` | orologio, bus degli eventi, pianificatore, impostazioni, segreti, registri, identificativi, guardia, formati | 1 |
|
||||
| `Platform/` | `Anubis.Platform` | configurazione d'avvio Docker e desktop, cartella dei dati, custodia dei segreti | 1 |
|
||||
| `Db/` | `Anubis.Db` | accesso ADO.NET, repository, migrazioni SQL | 1 |
|
||||
| `Db/` | `Anubis.Db` | accesso ADO.NET, tipi portabili, migrazioni SQL, esportazione e importazione, salvataggi | 1 |
|
||||
| `Access/` | `Anubis.Access` | password, secondo fattore, sessioni, limite ai tentativi | 1 |
|
||||
| `Brokers/` | `Anubis.Brokers` | interfaccia, Alpaca, OKX, simulatore, batteria di conformità | 2–3 |
|
||||
| `Data/` | `Anubis.Data` | strumenti, barre, raccolta, qualità, importatori, cambi, calendari | 2 |
|
||||
| `News/` | `Anubis.News` | fonti, pipeline, classificatore, analista, impatto | 7 |
|
||||
|
||||
@@ -1,6 +1,81 @@
|
||||
{
|
||||
"version": 2,
|
||||
"dependencies": {
|
||||
"net10.0": {}
|
||||
"net10.0": {
|
||||
"Microsoft.Data.Sqlite": {
|
||||
"type": "Direct",
|
||||
"requested": "[10.0.14, )",
|
||||
"resolved": "10.0.14",
|
||||
"contentHash": "muyroan6rKrf0omZL8UF7Kw5uOGp/yBg1L/XWvkFN3WhKaNtK2IWqRZw93nIwVqjhTVDO0l4CEGhHIh+mb+sKg==",
|
||||
"dependencies": {
|
||||
"Microsoft.Data.Sqlite.Core": "10.0.14",
|
||||
"SQLitePCLRaw.bundle_e_sqlite3": "2.1.13",
|
||||
"SQLitePCLRaw.core": "2.1.13"
|
||||
}
|
||||
},
|
||||
"Npgsql": {
|
||||
"type": "Direct",
|
||||
"requested": "[10.0.3, )",
|
||||
"resolved": "10.0.3",
|
||||
"contentHash": "7nb5YzXuvWWJxB0J8DiyL3we+X4FOctZrt0fIBnucOIaIevFEEwGQVZKtiu9olXdlNAK1eNgqSral6r/jlhI4w==",
|
||||
"dependencies": {
|
||||
"Microsoft.Extensions.Logging.Abstractions": "10.0.0"
|
||||
}
|
||||
},
|
||||
"System.Security.Cryptography.ProtectedData": {
|
||||
"type": "Direct",
|
||||
"requested": "[10.0.14, )",
|
||||
"resolved": "10.0.14",
|
||||
"contentHash": "UuaBUHgmtc9h6CMxaQqFDDJxkS5JVy7XEB+DbtCvD68GFcVM9/AccuNwFpARx8nwCnvjaxzoOBjG3gPz32P1Eg=="
|
||||
},
|
||||
"Microsoft.Data.Sqlite.Core": {
|
||||
"type": "Transitive",
|
||||
"resolved": "10.0.14",
|
||||
"contentHash": "1p9rO3Q2X/L/9YrYog3vaDzi8u+WZfJCOkDYoHqmofqYP0DLPLPyZcS2Rnh/79JXAodgbs1AgsaNwLpIvC3Xew==",
|
||||
"dependencies": {
|
||||
"SQLitePCLRaw.core": "2.1.13"
|
||||
}
|
||||
},
|
||||
"Microsoft.Extensions.DependencyInjection.Abstractions": {
|
||||
"type": "Transitive",
|
||||
"resolved": "10.0.0",
|
||||
"contentHash": "L3AdmZ1WOK4XXT5YFPEwyt0ep6l8lGIPs7F5OOBZc77Zqeo01Of7XXICy47628sdVl0v/owxYJTe86DTgFwKCA=="
|
||||
},
|
||||
"Microsoft.Extensions.Logging.Abstractions": {
|
||||
"type": "Transitive",
|
||||
"resolved": "10.0.0",
|
||||
"contentHash": "FU/IfjDfwaMuKr414SSQNTIti/69bHEMb+QKrskRb26oVqpx3lNFXMjs/RC9ZUuhBhcwDM2BwOgoMw+PZ+beqQ==",
|
||||
"dependencies": {
|
||||
"Microsoft.Extensions.DependencyInjection.Abstractions": "10.0.0"
|
||||
}
|
||||
},
|
||||
"SQLitePCLRaw.bundle_e_sqlite3": {
|
||||
"type": "Transitive",
|
||||
"resolved": "2.1.13",
|
||||
"contentHash": "NlDixlC7uHmo+ojXRgkNvN0tQtVGpLDZ3x2OpgNj+G07VHh5cXSszFDrAiC4BpUiPwkcaeuQkMV9WQ123xVtmQ==",
|
||||
"dependencies": {
|
||||
"SQLitePCLRaw.lib.e_sqlite3": "2.1.13",
|
||||
"SQLitePCLRaw.provider.e_sqlite3": "2.1.13"
|
||||
}
|
||||
},
|
||||
"SQLitePCLRaw.core": {
|
||||
"type": "Transitive",
|
||||
"resolved": "2.1.13",
|
||||
"contentHash": "2joofoinuHBFs0nMX3nKuXgxpXryRkCOdM2Ua6HBqQLLy6IW+5f28ZI+HAvMyF8vG7XfNz/lwfBfE6V4juru8Q=="
|
||||
},
|
||||
"SQLitePCLRaw.lib.e_sqlite3": {
|
||||
"type": "Transitive",
|
||||
"resolved": "2.1.13",
|
||||
"contentHash": "D2UAuiGxFYBqeRXjjjGgvJ5UQifzn6wefmSqeKU7XcNGKLbJRP6X7kcPZ6opOBJGdCIlgbSxTvDpBbLvsN594A=="
|
||||
},
|
||||
"SQLitePCLRaw.provider.e_sqlite3": {
|
||||
"type": "Transitive",
|
||||
"resolved": "2.1.13",
|
||||
"contentHash": "ZU60dSqoSNRe49KcQDUmOVgdRtjITOssVXFtiISs+zJXDI4D2Sndwpn2NOtzDQNPu0ZAvpoLPQ+GLKv6iEZn8Q==",
|
||||
"dependencies": {
|
||||
"SQLitePCLRaw.core": "2.1.13"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user