From 53ca01b7c32e0669e7799b4e8ebebd0f800b6821 Mon Sep 17 00:00:00 2001 From: Alberto Balbo Date: Tue, 10 Nov 2026 11:09:42 +0100 Subject: [PATCH] feat(core): platform layer, portable database, archives and access (refs #7 #8 #9 #10 #11) Phase 1 foundations in the Anubis library: - Platform: startup configuration from environment variables (Docker) or config.toml (desktop, in-house TOML subset), data folder layout, secret key custody (ANUBIS_SECRET_KEY, DPAPI on Windows, restricted file elsewhere), installation identity and database fingerprint. - Db: ADO.NET with Npgsql and Microsoft.Data.Sqlite, money as NUMERIC(38,18) or canonical text rounded to 18 decimals, UTC moments, UUIDv7 ids; numbered SQL migrations with type tokens and a version table; engine-independent export/import with count and accounting-total checks; backups keeping 14. - Core: injectable clock, channel event bus, scheduler with fixed, interval and market-relative times, settings with change log, AES-GCM secrets, log redaction, Italian formats. - Access: PBKDF2-SHA512 hashes, RFC 6238 TOTP, hashed session tokens, sign-in attempt limit. SQLitePCLRaw (Apache-2.0) arrives with Microsoft.Data.Sqlite and is declared in DependencyPolicyTests and API-NOTES. Co-Authored-By: Claude Opus 5.5 --- .editorconfig | 4 + .gitea/workflows/ci.yml | 1 + Directory.Packages.props | 7 + docs/API-NOTES.md | 4 +- src/Anubis.Desktop/packages.lock.json | 81 ++++- src/Anubis.Guard/packages.lock.json | 81 ++++- src/Anubis.Server/packages.lock.json | 65 +++- src/Anubis.Setup/packages.lock.json | 81 ++++- src/Anubis/Access/AccessStore.cs | 268 +++++++++++++++ src/Anubis/Access/Passwords.cs | 186 +++++++++++ src/Anubis/Anubis.csproj | 8 + src/Anubis/Core/Clock.cs | 110 ++++++ src/Anubis/Core/EventBus.cs | 97 ++++++ src/Anubis/Core/Format.cs | 200 +++++++++++ src/Anubis/Core/Ids.cs | 21 ++ src/Anubis/Core/Redactor.cs | 72 ++++ src/Anubis/Core/Scheduling.cs | 258 +++++++++++++++ src/Anubis/Core/SecretStore.cs | 136 ++++++++ src/Anubis/Core/Settings.cs | 112 +++++++ src/Anubis/Db/Archive.cs | 301 +++++++++++++++++ src/Anubis/Db/Backups.cs | 90 +++++ src/Anubis/Db/Database.cs | 329 ++++++++++++++++++ src/Anubis/Db/DbValues.cs | 100 ++++++ src/Anubis/Db/Migrations/0001_initial.sql | 245 ++++++++++++++ src/Anubis/Db/Migrator.cs | 158 +++++++++ src/Anubis/Db/Schema.cs | 120 +++++++ src/Anubis/Platform/DataDirectory.cs | 56 ++++ src/Anubis/Platform/Installation.cs | 168 ++++++++++ src/Anubis/Platform/PlatformLoader.cs | 350 ++++++++++++++++++++ src/Anubis/Platform/SecretKeyStore.cs | 141 ++++++++ src/Anubis/Platform/StartupConfig.cs | 95 ++++++ src/Anubis/Platform/Toml.cs | 165 +++++++++ src/Anubis/README.md | 3 +- src/Anubis/packages.lock.json | 77 ++++- tests/Anubis.Tests/ArchiveTests.cs | 186 +++++++++++ tests/Anubis.Tests/CoreTests.cs | 246 ++++++++++++++ tests/Anubis.Tests/DatabaseTests.cs | 249 ++++++++++++++ tests/Anubis.Tests/DependencyPolicyTests.cs | 9 +- tests/Anubis.Tests/FormatTests.cs | 108 ++++++ tests/Anubis.Tests/PlatformTests.cs | 199 +++++++++++ tests/Anubis.Tests/SecretScanTests.cs | 7 +- tests/Anubis.Tests/StoresTests.cs | 264 +++++++++++++++ tests/Anubis.Tests/TestSupport.cs | 117 ++++++- tests/Anubis.Tests/packages.lock.json | 81 ++++- 44 files changed, 5644 insertions(+), 12 deletions(-) create mode 100644 src/Anubis/Access/AccessStore.cs create mode 100644 src/Anubis/Access/Passwords.cs create mode 100644 src/Anubis/Core/Clock.cs create mode 100644 src/Anubis/Core/EventBus.cs create mode 100644 src/Anubis/Core/Format.cs create mode 100644 src/Anubis/Core/Ids.cs create mode 100644 src/Anubis/Core/Redactor.cs create mode 100644 src/Anubis/Core/Scheduling.cs create mode 100644 src/Anubis/Core/SecretStore.cs create mode 100644 src/Anubis/Core/Settings.cs create mode 100644 src/Anubis/Db/Archive.cs create mode 100644 src/Anubis/Db/Backups.cs create mode 100644 src/Anubis/Db/Database.cs create mode 100644 src/Anubis/Db/DbValues.cs create mode 100644 src/Anubis/Db/Migrations/0001_initial.sql create mode 100644 src/Anubis/Db/Migrator.cs create mode 100644 src/Anubis/Db/Schema.cs create mode 100644 src/Anubis/Platform/DataDirectory.cs create mode 100644 src/Anubis/Platform/Installation.cs create mode 100644 src/Anubis/Platform/PlatformLoader.cs create mode 100644 src/Anubis/Platform/SecretKeyStore.cs create mode 100644 src/Anubis/Platform/StartupConfig.cs create mode 100644 src/Anubis/Platform/Toml.cs create mode 100644 tests/Anubis.Tests/ArchiveTests.cs create mode 100644 tests/Anubis.Tests/CoreTests.cs create mode 100644 tests/Anubis.Tests/DatabaseTests.cs create mode 100644 tests/Anubis.Tests/FormatTests.cs create mode 100644 tests/Anubis.Tests/PlatformTests.cs create mode 100644 tests/Anubis.Tests/StoresTests.cs diff --git a/.editorconfig b/.editorconfig index cb644d7..09e32b4 100644 --- a/.editorconfig +++ b/.editorconfig @@ -24,3 +24,7 @@ csharp_using_directive_placement = outside_namespace:warning dotnet_style_qualification_for_field = false:warning dotnet_style_qualification_for_property = false:warning dotnet_style_qualification_for_method = false:warning + +# Tests: literal arrays in assertions read better than static fields. +[tests/**.cs] +dotnet_diagnostic.CA1861.severity = none diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index fa38f3d..96bc28f 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -32,6 +32,7 @@ jobs: ANUBIS_TEST_DB: ${{ matrix.db }} ANUBIS_TEST_PG_HOST: postgres ANUBIS_TEST_PG_PORT: "5432" + ANUBIS_TEST_PG_PASSWORD: anubis-ci DOTNET_CLI_TELEMETRY_OPTOUT: "1" DOTNET_NOLOGO: "1" TESTINGPLATFORM_TELEMETRY_OPTOUT: "1" diff --git a/Directory.Packages.props b/Directory.Packages.props index 9bd7a72..4318d09 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -9,4 +9,11 @@ + + + + + + + diff --git a/docs/API-NOTES.md b/docs/API-NOTES.md index f170a47..7a5a863 100644 --- a/docs/API-NOTES.md +++ b/docs/API-NOTES.md @@ -170,7 +170,7 @@ Ultime versioni stabili lette dall'API di NuGet il 10 novembre 2026. Quelle già | `Microsoft.NET.Test.Sdk` | 18.11.0 | MIT | Fase 0 | https://www.nuget.org/packages/Microsoft.NET.Test.Sdk | | `Microsoft.Data.Sqlite` | 10.0.14 | MIT | Fase 1 | https://www.nuget.org/packages/Microsoft.Data.Sqlite/10.0.14 | | `Npgsql` | 10.0.3 | PostgreSQL License | Fase 1 | https://www.nuget.org/packages/Npgsql/10.0.3 | -| `System.Security.Cryptography.ProtectedData` | 10.0.14 | MIT | Fase 10 | https://www.nuget.org/packages/System.Security.Cryptography.ProtectedData/10.0.14 | +| `System.Security.Cryptography.ProtectedData` | 10.0.14 | MIT | Fase 1 (la custodia della chiave dei segreti su desktop nasce con lo strato di piattaforma) | https://www.nuget.org/packages/System.Security.Cryptography.ProtectedData/10.0.14 | | `Microsoft.Web.WebView2` | 1.0.4314.35 | Licenza Microsoft di tipo BSD | Fase 10 | https://www.nuget.org/packages/Microsoft.Web.WebView2/1.0.4314.35 | | `Microsoft.ML`, `Microsoft.ML.LightGbm`, `Microsoft.ML.FastTree` | 5.0.1 | MIT | Fase 8 | https://www.nuget.org/packages/Microsoft.ML/5.0.1 | | `Microsoft.ML.OnnxRuntime` | 1.31.1 (librerie native per linux-x64 e win-x64) | MIT | Fase 7 | https://www.nuget.org/packages/Microsoft.ML.OnnxRuntime/1.31.1 | @@ -202,7 +202,7 @@ Quando la fase indicata aggiunge il pacchetto, il nome entra nell'elenco del tes | Dipendenza | Di chi, licenza | Arriva con | Fase | Nota | |---|---|---|---|---| -| `SQLitePCLRaw.bundle_e_sqlite3`, `SQLitePCLRaw.core` 2.1.13 | Eric Sink, Apache-2.0 | `Microsoft.Data.Sqlite` | 1 | Contiene la libreria nativa di SQLite. | +| `SQLitePCLRaw.bundle_e_sqlite3`, `SQLitePCLRaw.core`, `SQLitePCLRaw.lib.e_sqlite3`, `SQLitePCLRaw.provider.e_sqlite3` 2.1.13 | Eric Sink, Apache-2.0 | `Microsoft.Data.Sqlite` | 1 (in uso) | Contengono la libreria nativa di SQLite e il suo caricatore. Sono nell'elenco del test dalla Fase 1. | | `Newtonsoft.Json` 13.0.3 | James Newton-King, MIT | `Microsoft.ML`, LightGbm, FastTree | 8 | — | | `LightGBM` 4.6.0 | progetto microsoft/LightGBM, pubblicato a nome di Guolin Ke, MIT | `Microsoft.ML.LightGbm` | 8 | Libreria nativa; in Docker serve `libgomp1`. | | `Google.Protobuf` 3.30.2 | Google, BSD-3-Clause | `Microsoft.ML.Tokenizers` | 7 | — | diff --git a/src/Anubis.Desktop/packages.lock.json b/src/Anubis.Desktop/packages.lock.json index 8f93eae..e5f82ab 100644 --- a/src/Anubis.Desktop/packages.lock.json +++ b/src/Anubis.Desktop/packages.lock.json @@ -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==" } } } diff --git a/src/Anubis.Guard/packages.lock.json b/src/Anubis.Guard/packages.lock.json index c644c09..e42c9af 100644 --- a/src/Anubis.Guard/packages.lock.json +++ b/src/Anubis.Guard/packages.lock.json @@ -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==" } } } diff --git a/src/Anubis.Server/packages.lock.json b/src/Anubis.Server/packages.lock.json index c644c09..7d7d6af 100644 --- a/src/Anubis.Server/packages.lock.json +++ b/src/Anubis.Server/packages.lock.json @@ -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==" } } } diff --git a/src/Anubis.Setup/packages.lock.json b/src/Anubis.Setup/packages.lock.json index 8f93eae..e5f82ab 100644 --- a/src/Anubis.Setup/packages.lock.json +++ b/src/Anubis.Setup/packages.lock.json @@ -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==" } } } diff --git a/src/Anubis/Access/AccessStore.cs b/src/Anubis/Access/AccessStore.cs new file mode 100644 index 0000000..2c5dba5 --- /dev/null +++ b/src/Anubis/Access/AccessStore.cs @@ -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, +} + +/// +/// 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. +/// +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> _failures = new(StringComparer.Ordinal); + private readonly List _all = []; + + /// How long must still wait, or zero. + 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); + } + } + } +} + +/// +/// 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. +/// +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 HasUserAsync(CancellationToken cancellationToken = default) => + db.ReadAsync(async s => await s.CountAsync("SELECT COUNT(*) FROM users").ConfigureAwait(false) > 0, cancellationToken); + + /// Creates the user when there is none. In Docker the initial password must be changed at first sign-in. + public async Task 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); + } + + /// The session behind a cookie token, or null when unknown, idle too long or expired. + public async Task 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); + + /// Changes the password and ends every other session. + public async Task 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; + } + + /// Turns on the second factor after the user proved the app produces valid codes. + public async Task 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 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 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]; +} diff --git a/src/Anubis/Access/Passwords.cs b/src/Anubis/Access/Passwords.cs new file mode 100644 index 0000000..91c9c4d --- /dev/null +++ b/src/Anubis/Access/Passwords.cs @@ -0,0 +1,186 @@ +using System.Globalization; +using System.Security.Cryptography; +using System.Text; + +namespace Anubis.Access; + +/// +/// Password hashes with PBKDF2-SHA512 (), +/// in place of Argon2id (ADR 0003). Stored as pbkdf2-sha512$iterations$salt$hash so the cost can grow later. +/// +public static class Passwords +{ + /// OWASP's figure for PBKDF2-HMAC-SHA512. + 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)); + } + + /// Checks a password in constant time. A malformed stored hash never verifies. + 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); + } + + /// What is wrong with a new password, in plain Italian, or null when it is acceptable. + 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; + } +} + +/// Base32 without padding (RFC 4648), the format authenticator apps expect for TOTP secrets. +public static class Base32 +{ + private const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; + + public static string Encode(ReadOnlySpan 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(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]; + } +} + +/// +/// 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. +/// +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 secret, DateTimeOffset at, int digits = 6, int stepSeconds = 30, string algorithm = "SHA1") + { + var counter = at.ToUnixTimeSeconds() / stepSeconds; + Span 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'); + } + + /// Accepts the code of the current step and of one step before or after, for clocks a little apart. + public static bool Verify(ReadOnlySpan 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; + } + + /// The address authenticator apps read (otpauth://), for manual entry or a QR code. + public static string Uri(string issuer, string account, ReadOnlySpan 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"; +} diff --git a/src/Anubis/Anubis.csproj b/src/Anubis/Anubis.csproj index a65610b..c75e33c 100644 --- a/src/Anubis/Anubis.csproj +++ b/src/Anubis/Anubis.csproj @@ -2,6 +2,14 @@ Anubis + + + + + + + + diff --git a/src/Anubis/Core/Clock.cs b/src/Anubis/Core/Clock.cs new file mode 100644 index 0000000..1c90931 --- /dev/null +++ b/src/Anubis/Core/Clock.cs @@ -0,0 +1,110 @@ +namespace Anubis.Core; + +/// +/// 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. +/// +public interface IClock +{ + DateTimeOffset UtcNow { get; } + + /// Waits until the clock reaches . A manual clock returns when it is moved past it. + Task WaitUntilAsync(DateTimeOffset dueUtc, CancellationToken cancellationToken); +} + +/// The real clock of the machine. +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); + } + } +} + +/// A clock that moves only when told to: tests and the frozen demo clock (SPEC 13.1). +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 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); + + /// Number of callers waiting for a future moment; lets tests know a loop has gone to sleep. + 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; + } +} diff --git a/src/Anubis/Core/EventBus.cs b/src/Anubis/Core/EventBus.cs new file mode 100644 index 0000000..09774ea --- /dev/null +++ b/src/Anubis/Core/EventBus.cs @@ -0,0 +1,97 @@ +using System.Threading.Channels; + +namespace Anubis.Core; + +/// +/// In-process event bus (ADR 0003) on . 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). +/// +public sealed class EventBus +{ + private readonly Lock _gate = new(); + private readonly List _subscriptions = []; + + /// Delivers to every current subscriber of (or a base type). + public void Publish(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); + } + } + } + + /// Starts receiving messages of type . Dispose the result to stop. + public EventSubscription Subscribe(int capacity = 256) + { + var channel = Channel.CreateBounded(new BoundedChannelOptions(capacity) + { + FullMode = BoundedChannelFullMode.DropOldest, + SingleReader = true, + }); + var subscription = new Subscription(typeof(T), channel.Writer); + lock (_gate) + { + _subscriptions.Add(subscription); + } + + return new EventSubscription(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 Writer); +} + +/// A live subscription to the . +public sealed class EventSubscription(ChannelReader reader, Action unsubscribe) : IDisposable +{ + public async IAsyncEnumerable 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(); +} diff --git a/src/Anubis/Core/Format.cs b/src/Anubis/Core/Format.cs new file mode 100644 index 0000000..66380c7 --- /dev/null +++ b/src/Anubis/Core/Format.cs @@ -0,0 +1,200 @@ +using System.Globalization; +using System.Text; + +namespace Anubis.Core; + +/// +/// 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. +/// +public static class Format +{ + /// U+2212, the typographic minus. + public const char Minus = '−'; + + /// U+00A0, the space between a number and its currency symbol. + 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"]; + + /// A number with Italian separators, rounded half away from zero: 10.412, −2,57. + 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; + } + + /// An amount with its currency after a non-breaking space: 1.299,84 €, +18,40 €, −26,00 €. + 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(), + }; + + /// A percentage given in percent units: Percent(4.12m, 2, true) is +4,12%. + public static string Percent(decimal percent, int decimals = 1, bool withSign = false) => + Number(percent, decimals, withSign) + "%"; + + /// + /// 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. + /// + public static IReadOnlyList Shares(IReadOnlyList 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; + } + + /// Short spoken date in the user's time zone: Oggi 06:10, Ieri 21:58, mar 6 ott. + 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; + } + + /// Full date for the top bar: Venerdì 9 ottobre 2026, ore 08:21. + 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}"); + } + + /// A date without time: 12 agosto 2026. + public static string LongDate(DateOnly date) => + string.Create(CultureInfo.InvariantCulture, $"{date.Day} {_monthsLong[date.Month - 1]} {date.Year}"); + + /// How long ago, in words: 4 secondi fa, 3 minuti fa. + 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"); + } + + /// + /// 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. + /// + public static byte[] Csv(IReadOnlyList headers, IEnumerable> 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; +} diff --git a/src/Anubis/Core/Ids.cs b/src/Anubis/Core/Ids.cs new file mode 100644 index 0000000..0b2b908 --- /dev/null +++ b/src/Anubis/Core/Ids.cs @@ -0,0 +1,21 @@ +using System.Security.Cryptography; + +namespace Anubis.Core; + +/// Surrogate keys (SPEC 5.4): UUID version 7, ordered by creation time. +public static class Ids +{ + /// A new UUIDv7 whose time part comes from the injected clock, so ids follow simulated time too. + public static Guid NewId(IClock clock) + { + ArgumentNullException.ThrowIfNull(clock); + return Guid.CreateVersion7(clock.UtcNow); + } + + /// A random token for sessions and fingerprints, URL-safe base64 of 32 bytes. + public static string NewToken(int bytes = 32) + { + var buffer = RandomNumberGenerator.GetBytes(bytes); + return Convert.ToBase64String(buffer).TrimEnd('=').Replace('+', '-').Replace('/', '_'); + } +} diff --git a/src/Anubis/Core/Redactor.cs b/src/Anubis/Core/Redactor.cs new file mode 100644 index 0000000..2b81c5f --- /dev/null +++ b/src/Anubis/Core/Redactor.cs @@ -0,0 +1,72 @@ +using System.Text.RegularExpressions; + +namespace Anubis.Core; + +/// +/// 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. +/// +public static partial class Redactor +{ + public const string Mask = "***"; + + private static readonly Lock _gate = new(); + private static string[] _known = []; + + /// Registers a secret value (a key read from the environment, a password) so it is masked wherever it appears. + 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(@"(?\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)(?\s*[:=]\s*)[^\r\n]+", RegexOptions.IgnoreCase)] + private static partial Regex SensitiveHeader(); + + [GeneratedRegex(@"(?\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(@"(?[""']?[A-Za-z0-9_\-]*(?:password|passwd|secret|token|api[_-]?key|apikey|passphrase|signature|sign|private[_-]?key)[""']?)(?\s*[:=]\s*[""']?)[^\s""'&,;}]+", RegexOptions.IgnoreCase)] + private static partial Regex SensitivePair(); + + // Credentials inside connection URLs: postgres://user:password@host. + [GeneratedRegex(@"(?\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(); +} diff --git a/src/Anubis/Core/Scheduling.cs b/src/Anubis/Core/Scheduling.cs new file mode 100644 index 0000000..6ca717e --- /dev/null +++ b/src/Anubis/Core/Scheduling.cs @@ -0,0 +1,258 @@ +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; + +namespace Anubis.Core; + +/// A trading session as the broker's calendar gives it (SPEC 5.5): early closes included. +public sealed record MarketSession(DateOnly Date, DateTimeOffset OpenUtc, DateTimeOffset CloseUtc); + +/// +/// 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. +/// +public interface IMarketCalendar +{ + /// The session of the exchange's local , or null when the market is closed. + MarketSession? SessionOn(DateOnly day); +} + +public enum MarketAnchor +{ + Open, + Close, +} + +/// When a job runs. is pure: it depends only on its argument. +public abstract record Schedule +{ + /// The first run strictly after , or null if there is none in sight. + public abstract DateTimeOffset? NextAfter(DateTimeOffset afterUtc); + + /// Every day at a wall-clock time in a time zone, e.g. 03:30 Europe/Rome or 00:05 UTC. + public static Schedule Daily(TimeOnly time, TimeZoneInfo zone) => new DailySchedule(time, zone); + + /// At fixed intervals from midnight UTC, e.g. every 5 minutes. + public static Schedule Every(TimeSpan interval) => new IntervalSchedule(interval); + + /// Relative to the open or close of each trading session, e.g. 5 minutes after the open. + 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; + } +} + +/// A job registered with the . +public sealed record ScheduledJob(string Name, string Description, Schedule Schedule, Func Run) +{ + /// + /// 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). + /// + public TimeSpan MaxLateness { get; init; } = TimeSpan.FromMinutes(15); +} + +public enum JobOutcome +{ + Completed, + Failed, + Skipped, +} + +/// Published on the bus after every run, skip or failure. +public sealed record JobRun(string Name, DateTimeOffset DueUtc, DateTimeOffset StartedUtc, DateTimeOffset FinishedUtc, JobOutcome Outcome, string? Error); + +/// A run coming up, for "Prossime mosse" and the jobs list. +public sealed record UpcomingRun(string Name, string Description, DateTimeOffset DueUtc); + +/// +/// Runs jobs at fixed times, at intervals, or relative to market open and close (SPEC 5.5). +/// All waiting goes through , so tests drive it with a manual clock. +/// +public sealed partial class Scheduler(IClock clock, EventBus bus, ILogger? logger = null) +{ + private readonly ILogger _logger = logger ?? NullLogger.Instance; + private readonly Lock _gate = new(); + private readonly Dictionary _jobs = new(StringComparer.Ordinal); + private TaskCompletionSource _changed = new(TaskCreationOptions.RunContinuationsAsynchronously); + + /// Moment of the last loop iteration; the health check uses it to see the loop is alive. + 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 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); + } + } + + /// Runs every job whose time has come, once, and schedules its next run. + 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); +} diff --git a/src/Anubis/Core/SecretStore.cs b/src/Anubis/Core/SecretStore.cs new file mode 100644 index 0000000..2d11b21 --- /dev/null +++ b/src/Anubis/Core/SecretStore.cs @@ -0,0 +1,136 @@ +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; +using Anubis.Db; + +namespace Anubis.Core; + +/// Whether a secret is set and where it comes from. Never the value (SPEC 13.5, 15.4). +public sealed record SecretStatus(string Name, bool IsSet, bool FromEnvironment, DateTimeOffset? UpdatedAt); + +/// +/// 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 as soon as they are read. +/// A key from the environment wins over the saved one (SPEC 15.1). +/// +public sealed class SecretStore(Database db, IClock clock, byte[] key, IReadOnlyDictionary? environmentKeys = null) +{ + private const int NonceSize = 12; + private const int TagSize = 16; + + private readonly IReadOnlyDictionary _environment = environmentKeys ?? new Dictionary(); + + 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 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); + } + + /// The plain value, for the code that needs it (adapters). Never pass it to the interface. + public async Task 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 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 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)); +} diff --git a/src/Anubis/Core/Settings.cs b/src/Anubis/Core/Settings.cs new file mode 100644 index 0000000..fe9eaf8 --- /dev/null +++ b/src/Anubis/Core/Settings.cs @@ -0,0 +1,112 @@ +using System.Text.Json; +using Anubis.Db; + +namespace Anubis.Core; + +/// A setting the interface may change: key, default and allowed values. +public sealed record SettingDefinition(string Key, string Label, string Default, IReadOnlyList? Allowed = null); + +/// A value fixed by the startup configuration: it wins and is shown read-only with where to change it (SPEC 13.5). +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); + +/// Published on the bus after a change, so open pages update. +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; +} + +/// +/// 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. +/// +public sealed class SettingsStore(Database db, IClock clock, EventBus bus, IReadOnlyDictionary? 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"]); + + /// Bot state behind the top bar buttons: active, paused (no new entries) or stopped (kill switch, SPEC 9.3). + public static readonly SettingDefinition BotState = new("bot.state", "Stato del bot", "active", ["active", "paused", "stopped"]); + + public static readonly IReadOnlyList All = [Theme, Currency, BotState]; + + private readonly IReadOnlyDictionary _overrides = overrides ?? new Dictionary(); + + public static SettingDefinition Definition(string key) => + All.FirstOrDefault(d => d.Key == key) ?? throw new ArgumentException("Impostazione sconosciuta: " + key, nameof(key)); + + public async Task 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(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)); + } + } + + /// The change log, newest first. Secret values never appear: their entries say only "impostato" or "tolto". + public Task> 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(json); +} diff --git a/src/Anubis/Db/Archive.cs b/src/Anubis/Db/Archive.cs new file mode 100644 index 0000000..8798c2b --- /dev/null +++ b/src/Anubis/Db/Archive.cs @@ -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; + +/// What an archive contains: written as index.json next to one JSONL file per table. +public sealed record ArchiveIndex( + string Format, + int FormatVersion, + int SchemaVersion, + DateTimeOffset CreatedAt, + string SourceEngine, + IReadOnlyList Tables, + IReadOnlyDictionary AccountingTotals); + +public sealed record ArchiveTable(string Name, string File, long Count); + +/// +/// Engine-independent export and import (SPEC 5.4): anubis db export and anubis db import. +/// Used by nightly backups, restores and moves between PostgreSQL and SQLite. +/// +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 }; + + /// + /// 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. + /// + public static async Task 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(); + 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 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(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; + } + + /// + /// 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. + /// + public static async Task 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; + } + + /// Row counts of every table, for comparisons in tests and on the System page. + public static async Task> CountsAsync(Database db, CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(db); + return await db.ReadAsync(async s => + { + var counts = new Dictionary(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); + } + + /// Accounting totals computed in C#, never in SQL, so both engines give the same result (SPEC 5.4). + public static async Task> 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)totals.ToDictionary(); + }, cancellationToken).ConfigureAwait(false); + } + + private static async Task IsEmptyAsync(Database db, CancellationToken cancellationToken) + { + var counts = await CountsAsync(db, cancellationToken).ConfigureAwait(false); + return counts.Values.All(c => c == 0); + } + + private static async Task 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()), + ColumnKind.Uuid => Guid.Parse(node.GetValue()), + ColumnKind.Moment => DbValues.ParseMoment(node.GetValue()), + ColumnKind.Json => new DbJson(node.GetValue()), + ColumnKind.Bool => node.GetValue(), + ColumnKind.Bytes => Convert.FromBase64String(node.GetValue()), + ColumnKind.Measure => node.GetValue(), + ColumnKind.Number => node.GetValue(), + _ => node.GetValue(), + }; + } + + /// Sum of every money column of the accounting tables, as exact decimal text keyed "table.column". + private sealed class Totals + { + private readonly SortedDictionary _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(), NumberStyles.Number, CultureInfo.InvariantCulture) : 0m; + _sums[key] = (_sums.TryGetValue(key, out var sum) ? sum : 0m) + value; + } + } + + public Dictionary ToDictionary() => + _sums.ToDictionary(kv => kv.Key, kv => DbValues.Normalize(kv.Value).ToString(CultureInfo.InvariantCulture), StringComparer.Ordinal); + } +} diff --git a/src/Anubis/Db/Backups.cs b/src/Anubis/Db/Backups.cs new file mode 100644 index 0000000..fecd744 --- /dev/null +++ b/src/Anubis/Db/Backups.cs @@ -0,0 +1,90 @@ +using System.Globalization; +using Anubis.Core; +using Anubis.Platform; + +namespace Anubis.Db; + +/// A backup folder in /data/backups. +public sealed record BackupInfo(string Folder, string Name, DateTimeOffset TakenAt, string Reason, bool HasSqliteCopy); + +/// +/// 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. +/// +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 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)!; + } + + /// Backups from the newest, only complete ones. + public IReadOnlyList List() => + !Directory.Exists(data.Backups) + ? [] + : Directory.EnumerateDirectories(data.Backups) + .Where(d => !d.EndsWith(".partial", StringComparison.Ordinal)) + .Select(Describe) + .OfType() + .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))); + } +} diff --git a/src/Anubis/Db/Database.cs b/src/Anubis/Db/Database.cs new file mode 100644 index 0000000..f6d358b --- /dev/null +++ b/src/Anubis/Db/Database.cs @@ -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; + +/// +/// Access to the configured database with plain ADO.NET (ADR 0003). The same SQL runs on PostgreSQL and +/// SQLite; values pass through . On SQLite all writes go through one writer at a time +/// (SPEC 5.4) while reads run in parallel thanks to WAL. +/// +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; } + + /// Engine and place, without the password. + public string Description { get; } + + /// + /// Opens the configured database. With SQLite, 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). + /// + 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 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; + } + } + + /// Checks the database answers. Throws when it does not. + 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); + } + + /// Runs in a read-only transaction that sees a single instant of the database. + public async Task ReadAsync(Func> 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; + } + + /// Runs in a write transaction, committed only if it completes. + public async Task WriteAsync(Func> 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 work, CancellationToken cancellationToken = default) => + WriteAsync(async s => + { + await work(s).ConfigureAwait(false); + return true; + }, cancellationToken); + + /// A consistent copy of the SQLite file through the online backup API (SPEC 5.4). Not available on PostgreSQL. + 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) + ")"; +} + +/// One open transaction. Parameters are named @name on both engines. +public sealed class DbSession(DbEngine engine, DbConnection connection, DbTransaction transaction) +{ + public DbEngine Engine => engine; + + public DbConnection Connection => connection; + + public async Task 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 ExecuteAsync(string sql, params (string Name, object? Value)[] args) => ExecuteAsync(sql, CancellationToken.None, args); + + public async Task> QueryAsync(string sql, Func 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(); + var row = new DbRow(reader, engine); + while (await reader.ReadAsync().ConfigureAwait(false)) + { + result.Add(map(row)); + } + + return result; + } + + public async Task FirstOrDefaultAsync(string sql, Func map, params (string Name, object? Value)[] args) => + (await QueryAsync(sql, map, args).ConfigureAwait(false)).FirstOrDefault(); + + public async Task 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); + } + + /// Streams rows without loading them all, for exports. + public async Task ForEachAsync(string sql, Func 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; + } +} + +/// Typed reading of the current row, the mirror of . +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); +} diff --git a/src/Anubis/Db/DbValues.cs b/src/Anubis/Db/DbValues.cs new file mode 100644 index 0000000..03c1eaa --- /dev/null +++ b/src/Anubis/Db/DbValues.cs @@ -0,0 +1,100 @@ +using System.Globalization; +using Anubis.Platform; + +namespace Anubis.Db; + +/// A JSON document column (SPEC 5.4: generic JSON, no engine-specific operators). Stored as written. +public readonly record struct DbJson(string Text); + +/// +/// The in-house column types of SPEC 5.4, the same on both engines (ADR 0007): +/// +/// money, quantities and prices: , NUMERIC(38,18) on PostgreSQL and canonical text on SQLite, rounded to 18 decimals before writing (trap 19); +/// moments: in UTC, microsecond precision, ISO text on SQLite; a without time zone is refused; +/// identifiers: (UUIDv7), UUID on PostgreSQL and lowercase text on SQLite. +/// +/// +public static class DbValues +{ + /// NUMERIC(38,18) keeps 20 digits before the decimal point. + 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'"; + + /// Rounds to 18 decimals (half away from zero, as PostgreSQL does) and refuses values the column cannot hold. + 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)); + } + + /// Removes trailing zeros so the same number always has the same text: 1.500 becomes 1.5. + 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)); + + /// UTC, truncated to microseconds (the precision of PostgreSQL TIMESTAMPTZ). + 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); + } + + /// Converts a .NET value to what the engine's driver should receive. + 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, + }; + + /// The column type behind each token of the migration files. + 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}'."), + }; +} diff --git a/src/Anubis/Db/Migrations/0001_initial.sql b/src/Anubis/Db/Migrations/0001_initial.sql new file mode 100644 index 0000000..5418e2d --- /dev/null +++ b/src/Anubis/Db/Migrations/0001_initial.sql @@ -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}; diff --git a/src/Anubis/Db/Migrator.cs b/src/Anubis/Db/Migrator.cs new file mode 100644 index 0000000..7a2f39b --- /dev/null +++ b/src/Anubis/Db/Migrator.cs @@ -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; + +/// A numbered SQL migration embedded in the library (Db/Migrations/NNNN_name.sql). +public sealed record Migration(int Version, string Name, string Sql) +{ + /// Hash of the file as written, so an applied migration changed afterwards is detected. + public string Checksum { get; } = Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(Sql.ReplaceLineEndings("\n")))); + + /// The SQL with the type tokens replaced for . + public string SqlFor(DbEngine engine) => Migrations.Tokens().Replace(Sql, m => DbValues.ColumnType(m.Groups[1].Value, engine)); +} + +/// Where the database stands compared to the migrations this version knows. +public sealed record SchemaStatus(int Current, int Latest, IReadOnlyList Pending) +{ + public bool IsEmpty => Current == 0; +} + +/// +/// 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). +/// +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> _all = new(Load); + + public static IReadOnlyList All => _all.Value; + + public static int Latest => All[^1].Version; + + private static List Load() + { + var assembly = typeof(Migrations).Assembly; + const string prefix = "Anubis.Db.Migrations."; + var list = new List(); + 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 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); + } + + /// + /// Brings the database to the latest version. runs first when the database + /// already has a schema and something is pending; if it fails, nothing is migrated. + /// + public static async Task MigrateAsync(Database db, IClock clock, Func? 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 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; + } +} + +/// The database schema cannot be used by this version: shown on the error page. +public sealed class SchemaException(string message) : Exception(message); diff --git a/src/Anubis/Db/Schema.cs b/src/Anubis/Db/Schema.cs new file mode 100644 index 0000000..3eeb38a --- /dev/null +++ b/src/Anubis/Db/Schema.cs @@ -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 Columns); + +/// +/// 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. +/// +public static partial class Schema +{ + /// Accounting tables: append-only, their money columns are the totals checked after an import (SPEC 5.4). + public static readonly string[] AccountingTables = ["fills", "lots", "cash_ledger"]; + + private static readonly Lazy> _tables = new(() => Parse(Migrations.All)); + + public static IReadOnlyList 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
Parse(IEnumerable migrations) + { + var tables = new List<(string Name, List 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().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 SplitTopLevel(string body) + { + var parts = new List(); + 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(); + } +} diff --git a/src/Anubis/Platform/DataDirectory.cs b/src/Anubis/Platform/DataDirectory.cs new file mode 100644 index 0000000..1c90675 --- /dev/null +++ b/src/Anubis/Platform/DataDirectory.cs @@ -0,0 +1,56 @@ +namespace Anubis.Platform; + +/// +/// The data folder (SPEC 15.2, 15.6): /data in Docker, the user's application folder on desktop. +/// Same layout on both: db/, models/, research/, import/, backups/, logs/, installation.json. +/// +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"); + + /// Creates the folder and its sub-folders when missing. Never deletes anything. + public void Ensure() + { + Directory.CreateDirectory(Root); + foreach (var folder in Folders) + { + Directory.CreateDirectory(Path.Combine(Root, folder)); + } + } + + /// The standard per-user folder (SPEC 15.6): %APPDATA%\Anubis, ~/Library/Application Support/Anubis, ~/.local/share/anubis. + 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")); + } +} diff --git a/src/Anubis/Platform/Installation.cs b/src/Anubis/Platform/Installation.cs new file mode 100644 index 0000000..7b23b0b --- /dev/null +++ b/src/Anubis/Platform/Installation.cs @@ -0,0 +1,168 @@ +using System.Text.Json; +using Anubis.Core; +using Anubis.Db; + +namespace Anubis.Platform; + +/// Content of installation.json in the data folder (SPEC 15.1). +public sealed record InstallationFile(Guid InstallationId, string Fingerprint, string Engine, DateTimeOffset CreatedAt, DateTimeOffset RenewedAt); + +/// The installation row in the database. +public sealed record InstallationRow(Guid Id, string Fingerprint, string Engine, bool ProtectedRestart, DateTimeOffset CreatedAt, DateTimeOffset RenewedAt); + +public enum IdentityVerdict +{ + /// Same installation and same fingerprint: the engine may start. + Match, + + /// Empty database and a data folder that belongs to nobody: a new installation was created. + Created, + + /// The database knows the installation but the data folder lost its file: it was rewritten from the database. + FileRestored, + + /// The database is empty while the data folder belongs to an existing installation. + EmptyDatabase, + + /// Database and data folder belong to two different installations. + DifferentInstallation, + + /// Same installation, older fingerprint: a stale copy of the database (trap 26). + StaleDatabase, +} + +public sealed record IdentityCheck(IdentityVerdict Verdict, InstallationFile? File, InstallationRow? Row) +{ + public bool CanStart => Verdict is IdentityVerdict.Match or IdentityVerdict.Created or IdentityVerdict.FileRestored; +} + +/// +/// 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. +/// +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(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 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); + } + + /// + /// Compares database and data folder at start. Creates the installation only when both are new; never + /// touches a database that belongs elsewhere. + /// + public static async Task 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); + } + + /// + /// 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 + /// ), and the protected restart switched on (SPEC 15.1). + /// + public static async Task 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; + } + + /// "Comincia da capo": the data folder forgets its installation so an empty database can start a new one. + 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); + } + } + + /// The user confirmed the protected restart (phase 9 adds the full procedure of SPEC 15.1). + 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); + } +} diff --git a/src/Anubis/Platform/PlatformLoader.cs b/src/Anubis/Platform/PlatformLoader.cs new file mode 100644 index 0000000..83eb38a --- /dev/null +++ b/src/Anubis/Platform/PlatformLoader.cs @@ -0,0 +1,350 @@ +using System.Globalization; +using System.Text; +using Anubis.Core; + +namespace Anubis.Platform; + +/// +/// Docker (SPEC 15.1): everything from environment variables. Missing or wrong values become +/// s for the error page; nothing falls back to another database (trap 20). +/// +public static class DockerPlatform +{ + public const string DefaultTimeZone = "Europe/Rome"; + + /// Optional keys accepted from the environment (SPEC 15.1), each also with the _DEMO suffix. + 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 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(); + 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(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 get, DataDirectory data, List 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 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 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 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 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; + } +} + +/// +/// Desktop (SPEC 15.6): config.toml 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 . +/// +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(); + 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 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(key, "testo tra virgolette") : null; + long? Integer(string key) => toml.TryGetValue(key, out var v) ? v is long n ? n : Fail(key, "un numero intero") : null; + bool? Flag(string key) => toml.TryGetValue(key, out var v) ? v is bool b ? b : Fail(key, "true o false") : null; + T? Fail(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, + }; + } + + /// + /// Writes config.toml at the end of the first-run wizard. allow_live 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. + /// + 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); + } +} diff --git a/src/Anubis/Platform/SecretKeyStore.cs b/src/Anubis/Platform/SecretKeyStore.cs new file mode 100644 index 0000000..54b8424 --- /dev/null +++ b/src/Anubis/Platform/SecretKeyStore.cs @@ -0,0 +1,141 @@ +using System.Runtime.Versioning; +using System.Security.Cryptography; +using System.Text; + +namespace Anubis.Platform; + +/// +/// 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. +/// +public interface ISecretKeyStore +{ + /// Where the key lives, in plain Italian, for Settings → Sistema. + string Description { get; } + + /// True when the key exists (or, in Docker, the variable is set). + bool HasKey { get; } + + /// The 32-byte key. Desktop stores create it on first use. + byte[] GetOrCreateKey(); +} + +/// Docker: the key derives from ANUBIS_SECRET_KEY (at least 32 characters, SPEC 15.1). +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()); + } +} + +/// Small values kept outside config.toml: the desktop secret key and the PostgreSQL password (SPEC 15.6). +public interface IProtectedStore +{ + string Description { get; } + + byte[]? Read(string name); + + void Write(string name, byte[] value); +} + +/// Windows: files in the data folder encrypted with DPAPI for the current user (ADR 0003 in place of keyring). +[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"); +} + +/// +/// 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). +/// +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"); +} + +/// Desktop: a random 32-byte key born on first start and kept in an . +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; + } + + /// The store for this machine: DPAPI on Windows, a restricted file elsewhere. + 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); + } +} diff --git a/src/Anubis/Platform/StartupConfig.cs b/src/Anubis/Platform/StartupConfig.cs new file mode 100644 index 0000000..7d596ef --- /dev/null +++ b/src/Anubis/Platform/StartupConfig.cs @@ -0,0 +1,95 @@ +namespace Anubis.Platform; + +public enum PlatformKind +{ + Docker, + Desktop, +} + +public enum DbEngine +{ + Sqlite, + Postgres, +} + +/// How to reach the database. The password never appears in . +public sealed record DbSettings +{ + public required DbEngine Engine { get; init; } + + /// SQLite file path; null for PostgreSQL. + 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; } + + /// A full PostgreSQL address (ANUBIS_DB_URL) that replaces the five fields. + public string? Url { get; init; } + + /// What the interface shows: engine and place, never the password. + 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(); +} + +/// +/// The startup configuration (SPEC 15.1, 15.6): everything needed before the database is reachable. +/// It comes from environment variables in Docker and from config.toml on desktop. Values here win +/// over the ones saved from the interface, which shows them read-only. +/// +public sealed record StartupConfig +{ + public required PlatformKind Platform { get; init; } + + public required DataDirectory Data { get; init; } + + /// Null when the configuration does not say which database to use (the error page explains what is missing). + public DbSettings? Db { get; init; } + + public required TimeZoneInfo TimeZone { get; init; } + + public int Port { get; init; } = 8787; + + /// Desktop: listen on the home network too (SPEC 15.6). Docker always listens on every interface of the container. + public bool AllowNetwork { get; init; } + + /// ANUBIS_ALLOW_LIVE=1 or allow_live = true: only then adapters accept a real account (SPEC 2.2). + public bool AllowLive { get; init; } + + /// Docker: the initial password (ANUBIS_ADMIN_PASSWORD), also the key of the error page choices. + 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; } + + /// Optional broker and service keys from the environment (SPEC 15.1), by variable name. + public IReadOnlyDictionary EnvironmentKeys { get; init; } = new Dictionary(); + + /// Problems that stop the start: the error page lists them with what to do. + public IReadOnlyList Problems { get; init; } = []; + + /// Desktop only: config.toml does not exist yet, the first-run wizard writes it. + public bool NeedsFirstRun { get; init; } + + public bool IsComplete => Problems.Count == 0 && Db is not null && !NeedsFirstRun; +} + +/// Something missing or wrong in the startup configuration, said in plain Italian with the remedy. +public sealed record ConfigProblem(string Setting, string What, string Remedy); diff --git a/src/Anubis/Platform/Toml.cs b/src/Anubis/Platform/Toml.cs new file mode 100644 index 0000000..70b6118 --- /dev/null +++ b/src/Anubis/Platform/Toml.cs @@ -0,0 +1,165 @@ +using System.Globalization; +using System.Text; + +namespace Anubis.Platform; + +/// +/// The subset of TOML that config.toml needs (ADR 0003, no external parser): comments, tables +/// [name], and key = value 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. +/// +public static class Toml +{ + /// Parses into "table.key" → value (string, long or bool). Keys outside a table have no prefix. + public static IReadOnlyDictionary Parse(string text) + { + ArgumentNullException.ThrowIfNull(text); + var result = new Dictionary(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) + "\""; + } +} diff --git a/src/Anubis/README.md b/src/Anubis/README.md index 6b16a2a..1237f4e 100644 --- a/src/Anubis/README.md +++ b/src/Anubis/README.md @@ -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 | diff --git a/src/Anubis/packages.lock.json b/src/Anubis/packages.lock.json index 6afd678..03cfac9 100644 --- a/src/Anubis/packages.lock.json +++ b/src/Anubis/packages.lock.json @@ -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" + } + } + } } } \ No newline at end of file diff --git a/tests/Anubis.Tests/ArchiveTests.cs b/tests/Anubis.Tests/ArchiveTests.cs new file mode 100644 index 0000000..01454e8 --- /dev/null +++ b/tests/Anubis.Tests/ArchiveTests.cs @@ -0,0 +1,186 @@ +using Anubis.Core; +using Anubis.Db; +using Anubis.Platform; + +namespace Anubis.Tests; + +/// Export, import and backups (issue #9, SPEC 5.4). +[TestClass] +public sealed class ArchiveTests +{ + private static readonly ManualClock _clock = new(new DateTimeOffset(2026, 10, 9, 6, 21, 0, TimeSpan.Zero)); + + /// Fills a database with rows of every column kind, including accounting rows and a reversal. + internal static async Task SeedAsync(Database db) + { + var (account, instrument) = await DatabaseTests.SeedAccountAsync(db).ConfigureAwait(false); + await db.WriteAsync(async s => + { + var fill = Guid.CreateVersion7(_clock.UtcNow.AddSeconds(1)); + var reversal = Guid.CreateVersion7(_clock.UtcNow.AddSeconds(2)); + await s.ExecuteAsync( + "INSERT INTO fills (id, account_id, instrument_id, execution_id, side, quantity, price, fee, fee_currency, executed_at, created_at) VALUES (@id, @a, @i, 'EX-1', 'buy', @q, @p, @f, 'EUR', @t, @t)", + ("id", fill), ("a", account), ("i", instrument), ("q", 0.0182m), ("p", 68950m), ("f", 1.254890000000000001m), ("t", _clock.UtcNow)).ConfigureAwait(false); + await s.ExecuteAsync( + "INSERT INTO fills (id, account_id, instrument_id, execution_id, side, quantity, price, fee, fee_currency, reverses_id, executed_at, created_at) VALUES (@id, @a, @i, 'EX-1-R', 'buy', @q, @p, @f, 'EUR', @r, @t, @t)", + ("id", reversal), ("a", account), ("i", instrument), ("q", -0.0182m), ("p", 68950m), ("f", -1.254890000000000001m), ("r", fill), ("t", _clock.UtcNow)).ConfigureAwait(false); + for (var i = 0; i < 25; i++) + { + await s.ExecuteAsync("INSERT INTO cash_ledger (id, account_id, currency, amount, kind, note, occurred_at, created_at) VALUES (@id, @a, 'EUR', @v, 'deposit', @n, @t, @t)", + ("id", Guid.CreateVersion7(_clock.UtcNow.AddSeconds(10 + i))), ("a", account), ("v", 100.01m * i - 333.333333333333333333m), ("n", i % 3 == 0 ? null : "nota \"" + i + "\" ;\n"), + ("t", _clock.UtcNow.AddDays(-i))).ConfigureAwait(false); + } + + await s.ExecuteAsync("INSERT INTO journal (id, occurred_at, kind, code, message, data, created_at) VALUES (@id, @t, 'Ordini', 'stop_raised', @m, @d, @t)", + ("id", Guid.CreateVersion7(_clock.UtcNow)), ("t", _clock.UtcNow), ("m", "Alzato lo stop di BTC/EUR da 65.400 € a 66.180 €."), ("d", new DbJson("{\"from\": \"65400\", \"to\": \"66180\"}"))).ConfigureAwait(false); + await s.ExecuteAsync("INSERT INTO users (id, username, password_hash, must_change_password, totp_secret, created_at, updated_at) VALUES (@id, 'admin', 'h', @m, @s, @t, @t)", + ("id", Guid.CreateVersion7(_clock.UtcNow)), ("m", true), ("s", new byte[] { 0, 1, 2, 255 }), ("t", _clock.UtcNow)).ConfigureAwait(false); + }).ConfigureAwait(false); + } + + [TestMethod] + [TestCategory(Category.Integration)] + public async Task ExportThenImportOnTheSameEngineKeepsEverything() + { + await using var source = await DatabaseTests.MigratedAsync().ConfigureAwait(false); + await using var target = await TestDb.CreateAsync().ConfigureAwait(false); + await RoundTripAsync(source, target).ConfigureAwait(false); + } + + [TestMethod] + [TestCategory(Category.Integration)] + public async Task ExportFromOneEngineImportsIntoTheOtherWithSameCountsAndTotals() + { + if (!await TestDb.PostgresAvailableAsync().ConfigureAwait(false)) + { + Assert.Inconclusive("PostgreSQL per i test non raggiungibile: la prova tra motori gira in CI e con `tasks ci`."); + } + + foreach (var (from, to) in new[] { (DbEngine.Sqlite, DbEngine.Postgres), (DbEngine.Postgres, DbEngine.Sqlite) }) + { + await using var source = await TestDb.CreateAsync(from).ConfigureAwait(false); + await Migrations.MigrateAsync(source.Db, _clock, null, CancellationToken.None).ConfigureAwait(false); + await using var target = await TestDb.CreateAsync(to).ConfigureAwait(false); + await RoundTripAsync(source, target).ConfigureAwait(false); + } + } + + private static async Task RoundTripAsync(TestDb source, TestDb target) + { + await SeedAsync(source.Db).ConfigureAwait(false); + var folder = TempFolder.Create("archive"); + try + { + var index = await Archive.ExportAsync(source.Db, Path.Combine(folder, "a"), _clock).ConfigureAwait(false); + Assert.AreEqual(Migrations.Latest, index.SchemaVersion); + Assert.AreEqual(2L, index.Tables.Single(t => t.Name == "fills").Count); + Assert.AreEqual("0", index.AccountingTotals["fills.quantity"], "il fill e il suo storno si annullano"); + + await Archive.ImportAsync(target.Db, Path.Combine(folder, "a"), _clock).ConfigureAwait(false); + + CollectionAssert.AreEquivalent( + (await Archive.CountsAsync(source.Db).ConfigureAwait(false)).ToList(), + (await Archive.CountsAsync(target.Db).ConfigureAwait(false)).ToList(), $"{source.Db.Engine} → {target.Db.Engine}"); + CollectionAssert.AreEquivalent( + (await Archive.AccountingTotalsAsync(source.Db).ConfigureAwait(false)).ToList(), + (await Archive.AccountingTotalsAsync(target.Db).ConfigureAwait(false)).ToList()); + + // A second export of the target is identical row by row. + await Archive.ExportAsync(target.Db, Path.Combine(folder, "b"), _clock).ConfigureAwait(false); + foreach (var table in Schema.Tables) + { + CollectionAssert.AreEqual( + File.ReadAllLines(Path.Combine(folder, "a", table.Name + ".jsonl")), + File.ReadAllLines(Path.Combine(folder, "b", table.Name + ".jsonl")), table.Name); + } + } + finally + { + TempFolder.Delete(folder); + } + } + + [TestMethod] + [TestCategory(Category.Integration)] + public async Task ImportRefusesANonEmptyDatabaseAndATamperedArchive() + { + await using var source = await DatabaseTests.MigratedAsync().ConfigureAwait(false); + await SeedAsync(source.Db).ConfigureAwait(false); + var folder = TempFolder.Create("archive-bad"); + try + { + await Archive.ExportAsync(source.Db, Path.Combine(folder, "a"), _clock).ConfigureAwait(false); + await Assert.ThrowsAsync(() => Archive.ImportAsync(source.Db, Path.Combine(folder, "a"), _clock)).ConfigureAwait(false); + + var cash = Path.Combine(folder, "a", "cash_ledger.jsonl"); + var lines = File.ReadAllLines(cash); + File.WriteAllLines(cash, lines.Skip(1)); + await using var target = await TestDb.CreateAsync().ConfigureAwait(false); + await Assert.ThrowsAsync(() => Archive.ImportAsync(target.Db, Path.Combine(folder, "a"), _clock)).ConfigureAwait(false); + Assert.AreEqual(0L, (await Archive.CountsAsync(target.Db).ConfigureAwait(false))["accounts"], "un'importazione fallita non lascia righe"); + + await Assert.ThrowsAsync(() => Archive.ExportAsync(source.Db, Path.Combine(folder, "a"), _clock)).ConfigureAwait(false); + } + finally + { + TempFolder.Delete(folder); + } + } + + [TestMethod] + [TestCategory(Category.Integration)] + public async Task BackupsKeepTheLastFourteenAndCopyTheSqliteFile() + { + await using var test = await DatabaseTests.MigratedAsync().ConfigureAwait(false); + await SeedAsync(test.Db).ConfigureAwait(false); + var folder = TempFolder.Create("backups"); + try + { + var clock = new ManualClock(_clock.UtcNow); + var backups = new Backups(new DataDirectory(folder), clock); + for (var i = 0; i < 16; i++) + { + await backups.TakeAsync(test.Db, i == 0 ? "prima-della-migrazione" : "notturno").ConfigureAwait(false); + clock.Advance(TimeSpan.FromDays(1)); + } + + var list = backups.List(); + Assert.HasCount(Backups.Keep, list); + Assert.AreEqual(clock.UtcNow.AddDays(-1).Date, list[0].TakenAt.UtcDateTime.Date, "la più recente è la prima"); + Assert.AreEqual(test.Db.Engine == DbEngine.Sqlite, list[0].HasSqliteCopy); + Assert.IsFalse(Directory.EnumerateDirectories(folder, "*", SearchOption.AllDirectories).Any(d => d.EndsWith(".partial", StringComparison.Ordinal))); + + if (test.Db.Engine == DbEngine.Sqlite) + { + // The copy made with the backup API opens and has the same rows. + await using var copy = Database.Open(new DbSettings { Engine = DbEngine.Sqlite, SqlitePath = Path.Combine(list[0].Folder, Backups.SqliteCopy) }, allowCreate: false); + CollectionAssert.AreEquivalent((await Archive.CountsAsync(test.Db).ConfigureAwait(false)).ToList(), (await Archive.CountsAsync(copy).ConfigureAwait(false)).ToList()); + } + } + finally + { + TempFolder.Delete(folder); + } + } + + [TestMethod] + [TestCategory(Category.Integration)] + public async Task MigratingADatabaseWithDataTakesABackupFirst() + { + await using var test = await TestDb.CreateAsync().ConfigureAwait(false); + var calls = 0; + await Migrations.MigrateAsync(test.Db, _clock, _ => + { + calls++; + return Task.CompletedTask; + }, CancellationToken.None).ConfigureAwait(false); + Assert.AreEqual(0, calls, "un database vuoto non ha nulla da salvare"); + + // A failing backup stops the migration (simulated on an older schema by removing the version row). + await test.Db.WriteAsync(s => s.ExecuteAsync($"DELETE FROM {Migrations.VersionTable} WHERE version = @v", ("v", Migrations.Latest))).ConfigureAwait(false); + if (Migrations.Latest > 1) + { + await Assert.ThrowsAsync(() => Migrations.MigrateAsync(test.Db, _clock, _ => throw new IOException("disco pieno"), CancellationToken.None)).ConfigureAwait(false); + } + } +} diff --git a/tests/Anubis.Tests/CoreTests.cs b/tests/Anubis.Tests/CoreTests.cs new file mode 100644 index 0000000..a538139 --- /dev/null +++ b/tests/Anubis.Tests/CoreTests.cs @@ -0,0 +1,246 @@ +using Anubis.Core; + +namespace Anubis.Tests; + +[TestClass] +public sealed class CoreTests +{ + [TestMethod] + [TestCategory(Category.Unit)] + public void IdsAreVersion7AndFollowTheInjectedClock() + { + var clock = new ManualClock(new DateTimeOffset(2026, 10, 9, 6, 21, 0, TimeSpan.Zero)); + var first = Ids.NewId(clock); + clock.Advance(TimeSpan.FromMilliseconds(5)); + var second = Ids.NewId(clock); + Assert.AreEqual(7, first.Version); + Assert.IsLessThan(0, string.CompareOrdinal(first.ToString(), second.ToString()), "gli identificativi seguono il tempo"); + Assert.AreNotEqual(Ids.NewToken(), Ids.NewToken()); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public async Task ManualClockReleasesWaitersOnlyWhenMoved() + { + var clock = new ManualClock(DateTimeOffset.UnixEpoch); + var waiting = clock.WaitUntilAsync(DateTimeOffset.UnixEpoch.AddMinutes(5), CancellationToken.None); + clock.Advance(TimeSpan.FromMinutes(4)); + Assert.IsFalse(waiting.IsCompleted); + clock.Advance(TimeSpan.FromMinutes(1)); + await waiting.WaitAsync(TimeSpan.FromSeconds(5)).ConfigureAwait(false); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public async Task BusDeliversToEachSubscriberAndDropsOldestWhenFull() + { + var bus = new EventBus(); + using var all = bus.Subscribe(); + using var small = bus.Subscribe(capacity: 2); + bus.Publish("a"); + bus.Publish("b"); + bus.Publish("c"); + bus.Publish(42); + + var received = new List(); + while (small.TryRead(out var message)) + { + received.Add(message!); + } + + CollectionAssert.AreEqual(new[] { "b", "c" }, received, "un lettore lento perde i più vecchi, non blocca chi pubblica"); + var count = 0; + while (all.TryRead(out _)) + { + count++; + } + + Assert.AreEqual(4, count); + all.Dispose(); + Assert.AreEqual(1, bus.SubscriberCount); + await Task.CompletedTask.ConfigureAwait(false); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void RedactorRemovesKeysSignaturesAndHeaders() + { + Redactor.Register("registered-secret-value-123"); + var samples = new Dictionary + { + ["OK-ACCESS-SIGN: " + "abc123signature=="] = "abc123signature", + ["APCA-API-SECRET-KEY: " + "Sk1234567890abcdef"] = "Sk1234567890abcdef", + ["Authorization: Bearer " + "eyJhbGciOiJIUzI1NiJ9.payload"] = "eyJhbGciOiJIUzI1NiJ9", + ["{\"api_key\": \"" + "AK12345678\"}"] = "AK12345678", + ["password=" + "hunter2hunter2&x=1"] = "hunter2hunter2", + ["postgres://anubis:" + "S3cretPw@db:5432/anubis"] = "S3cretPw", + ["bot" + "123456789:" + "AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsawq"] = "AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsawq", + ["value registered-secret-value-123 in text"] = "registered-secret-value-123", + }; + foreach (var (input, secret) in samples) + { + var output = Redactor.Redact(input); + Assert.IsFalse(output.Contains(secret, StringComparison.Ordinal), $"Segreto rimasto in: {output}"); + Assert.Contains(Redactor.Mask, output); + } + + Assert.AreEqual("Ordine eseguito: 2 AAPL a 259,80 $", Redactor.Redact("Ordine eseguito: 2 AAPL a 259,80 $")); + } +} + +/// The scheduler with a fake calendar (issue #10): fixed times, intervals, open and close, sleeps. +[TestClass] +public sealed class SchedulerTests +{ + private static readonly TimeZoneInfo _rome = TimeZoneInfo.FindSystemTimeZoneById("Europe/Rome"); + private static readonly TimeZoneInfo _newYork = TimeZoneInfo.FindSystemTimeZoneById("America/New_York"); + + /// New York, 09:30–16:00 on weekdays, 13:00 close on the day after Thanksgiving 2026, closed on 3 July. + private sealed class FakeCalendar : IMarketCalendar + { + public MarketSession? SessionOn(DateOnly day) + { + if (day.DayOfWeek is DayOfWeek.Saturday or DayOfWeek.Sunday || day == new DateOnly(2026, 7, 3)) + { + return null; + } + + var close = day == new DateOnly(2026, 11, 27) ? new TimeOnly(13, 0) : new TimeOnly(16, 0); + return new MarketSession(day, At(day, new TimeOnly(9, 30)), At(day, close)); + } + + private static DateTimeOffset At(DateOnly day, TimeOnly time) + { + var local = day.ToDateTime(time); + return new DateTimeOffset(local, _newYork.GetUtcOffset(local)).ToUniversalTime(); + } + } + + private static DateTimeOffset Rome(int y, int mo, int d, int h, int mi) + { + var local = new DateTime(y, mo, d, h, mi, 0); + return new DateTimeOffset(local, _rome.GetUtcOffset(local)); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void MarketTimesFollowTheExchangeNotItalianTime() + { + var calendar = new FakeCalendar(); + var afterOpen = Schedule.Market(MarketAnchor.Open, TimeSpan.FromMinutes(5), calendar); + + // Normal week: 09:35 in New York is 15:35 in Rome. + Assert.AreEqual(Rome(2026, 10, 9, 15, 35), afterOpen.NextAfter(Rome(2026, 10, 9, 8, 21))); + + // 16 March 2026: the USA already moved to summer time, Europe not yet: 09:35 NY is 14:35 in Rome. + Assert.AreEqual(Rome(2026, 3, 16, 14, 35), afterOpen.NextAfter(Rome(2026, 3, 16, 8, 0))); + + // Friday after the open: the next run is Monday. + Assert.AreEqual(Rome(2026, 10, 12, 15, 35), afterOpen.NextAfter(Rome(2026, 10, 9, 16, 0))); + + // Holiday skipped (Friday 3 July): Monday 6 July. + Assert.AreEqual(Rome(2026, 7, 6, 15, 35), afterOpen.NextAfter(Rome(2026, 7, 2, 16, 0))); + + // Early close: 5 minutes before a 13:00 close is 12:55 in New York, 18:55 in Rome. + var beforeClose = Schedule.Market(MarketAnchor.Close, TimeSpan.FromMinutes(-5), calendar); + Assert.AreEqual(Rome(2026, 11, 27, 18, 55), beforeClose.NextAfter(Rome(2026, 11, 27, 9, 0))); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void DailyAndIntervalSchedules() + { + var backup = Schedule.Daily(new TimeOnly(3, 30), _rome); + Assert.AreEqual(Rome(2026, 10, 10, 3, 30), backup.NextAfter(Rome(2026, 10, 9, 8, 21))); + + // The night summer time ends (25 October 2026) 03:30 exists once, in winter time. + Assert.AreEqual(Rome(2026, 10, 25, 3, 30), backup.NextAfter(Rome(2026, 10, 24, 12, 0))); + + // 02:30 does not exist on 29 March 2026 in Rome: it moves to 03:00. + var skipped = Schedule.Daily(new TimeOnly(2, 30), _rome); + Assert.AreEqual(new DateTimeOffset(2026, 3, 29, 1, 0, 0, TimeSpan.Zero), skipped.NextAfter(Rome(2026, 3, 28, 12, 0))); + + var crypto = Schedule.Daily(new TimeOnly(0, 5), TimeZoneInfo.Utc); + Assert.AreEqual(new DateTimeOffset(2026, 10, 10, 0, 5, 0, TimeSpan.Zero), crypto.NextAfter(Rome(2026, 10, 9, 8, 21))); + + var every5 = Schedule.Every(TimeSpan.FromMinutes(5)); + Assert.AreEqual(new DateTimeOffset(2026, 10, 9, 6, 25, 0, TimeSpan.Zero), every5.NextAfter(new DateTimeOffset(2026, 10, 9, 6, 21, 0, TimeSpan.Zero))); + Assert.AreEqual(new DateTimeOffset(2026, 10, 9, 6, 30, 0, TimeSpan.Zero), every5.NextAfter(new DateTimeOffset(2026, 10, 9, 6, 25, 0, TimeSpan.Zero))); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public async Task RunsDueJobsOnceAndSkipsRunsMissedDuringASleep() + { + var clock = new ManualClock(Rome(2026, 10, 9, 8, 21)); + var bus = new EventBus(); + using var runs = bus.Subscribe(); + var scheduler = new Scheduler(clock, bus); + var count = 0; + scheduler.Add(new ScheduledJob("backup", "Salvataggio", Schedule.Daily(new TimeOnly(3, 30), _rome), _ => + { + count++; + return Task.CompletedTask; + })); + scheduler.Add(new ScheduledJob("broken", "Rotto", Schedule.Daily(new TimeOnly(3, 30), _rome), _ => throw new InvalidOperationException("guasto"))); + + Assert.HasCount(2, scheduler.Upcoming()); + + clock.Set(Rome(2026, 10, 10, 3, 30)); + await scheduler.RunDueAsync(CancellationToken.None).ConfigureAwait(false); + Assert.AreEqual(1, count); + await scheduler.RunDueAsync(CancellationToken.None).ConfigureAwait(false); + Assert.AreEqual(1, count, "un lavoro già eseguito non riparte"); + + // The computer slept two days: the 03:30 run is found late by hours and is skipped, not run blindly. + clock.Set(Rome(2026, 10, 11, 9, 0)); + await scheduler.RunDueAsync(CancellationToken.None).ConfigureAwait(false); + Assert.AreEqual(1, count); + + var outcomes = new List(); + while (runs.TryRead(out var run)) + { + outcomes.Add(run!); + } + + Assert.IsTrue(outcomes.Any(r => r.Name == "backup" && r.Outcome == JobOutcome.Completed)); + Assert.IsTrue(outcomes.Any(r => r.Name == "backup" && r.Outcome == JobOutcome.Skipped)); + Assert.IsTrue(outcomes.Any(r => r.Name == "broken" && r.Outcome == JobOutcome.Failed && r.Error == "guasto")); + Assert.AreEqual(Rome(2026, 10, 12, 3, 30), scheduler.Upcoming().Single(u => u.Name == "backup").DueUtc); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public async Task LoopWaitsOnTheInjectedClock() + { + var clock = new ManualClock(new DateTimeOffset(2026, 10, 9, 6, 21, 0, TimeSpan.Zero)); + var scheduler = new Scheduler(clock, new EventBus()); + var ran = new TaskCompletionSource(); + scheduler.Add(new ScheduledJob("tick", "Prova", Schedule.Every(TimeSpan.FromMinutes(5)), _ => + { + ran.TrySetResult(); + return Task.CompletedTask; + })); + using var cts = new CancellationTokenSource(); + var loop = scheduler.RunAsync(cts.Token); + await WaitUntil(() => clock.Waiting > 0).ConfigureAwait(false); + Assert.IsFalse(ran.Task.IsCompleted, "nessun lavoro prima dell'orario"); + + clock.Set(new DateTimeOffset(2026, 10, 9, 6, 25, 0, TimeSpan.Zero)); + await ran.Task.WaitAsync(TimeSpan.FromSeconds(10)).ConfigureAwait(false); + Assert.IsNotNull(scheduler.LastBeatUtc); + + await cts.CancelAsync().ConfigureAwait(false); + await Assert.ThrowsAsync(() => loop).ConfigureAwait(false); + } + + internal static async Task WaitUntil(Func condition) + { + for (var i = 0; i < 500 && !condition(); i++) + { + await Task.Delay(10).ConfigureAwait(false); + } + + Assert.IsTrue(condition(), "condizione non raggiunta in tempo"); + } +} diff --git a/tests/Anubis.Tests/DatabaseTests.cs b/tests/Anubis.Tests/DatabaseTests.cs new file mode 100644 index 0000000..239b547 --- /dev/null +++ b/tests/Anubis.Tests/DatabaseTests.cs @@ -0,0 +1,249 @@ +using Anubis.Core; +using Anubis.Db; +using Anubis.Platform; + +namespace Anubis.Tests; + +/// +/// The portable database (issue #8): every test runs on the engine of this run (ANUBIS_TEST_DB), so the same +/// battery passes on SQLite and on PostgreSQL. +/// +[TestClass] +public sealed class DatabaseTests +{ + private static readonly ManualClock _clock = new(new DateTimeOffset(2026, 10, 9, 6, 21, 0, TimeSpan.Zero)); + + internal static async Task MigratedAsync() + { + var test = await TestDb.CreateAsync().ConfigureAwait(false); + await Migrations.MigrateAsync(test.Db, _clock, null, CancellationToken.None).ConfigureAwait(false); + return test; + } + + [TestMethod] + [TestCategory(Category.Integration)] + public async Task MigrationsCreateTheSchemaAndRunOnce() + { + await using var test = await TestDb.CreateAsync().ConfigureAwait(false); + Assert.IsTrue((await Migrations.StatusAsync(test.Db, CancellationToken.None).ConfigureAwait(false)).IsEmpty); + Assert.AreEqual(Migrations.Latest, await Migrations.MigrateAsync(test.Db, _clock, null, CancellationToken.None).ConfigureAwait(false)); + Assert.AreEqual(0, await Migrations.MigrateAsync(test.Db, _clock, null, CancellationToken.None).ConfigureAwait(false)); + var status = await Migrations.StatusAsync(test.Db, CancellationToken.None).ConfigureAwait(false); + Assert.AreEqual(Migrations.Latest, status.Current); + } + + [TestMethod] + [TestCategory(Category.Integration)] + public async Task ColumnsInTheDatabaseMatchTheSchemaReadFromMigrations() + { + await using var test = await MigratedAsync().ConfigureAwait(false); + foreach (var table in Schema.Tables) + { + var actual = await test.Db.ReadAsync(s => s.QueryAsync( + s.Engine == DbEngine.Sqlite + ? $"SELECT name, type, \"notnull\" AS required FROM pragma_table_info('{table.Name}')" + : $"SELECT column_name AS name, data_type AS type, CASE WHEN is_nullable = 'NO' THEN 1 ELSE 0 END AS required FROM information_schema.columns WHERE table_schema = current_schema() AND table_name = '{table.Name}'", + r => (Name: r.Text("name"), Type: r.Text("type").ToUpperInvariant(), Required: r.Number("required") != 0))).ConfigureAwait(false); + CollectionAssert.AreEquivalent(table.Columns.Select(c => c.Name).ToList(), actual.Select(a => a.Name).ToList(), table.Name); + foreach (var column in table.Columns) + { + var found = actual.Single(a => a.Name == column.Name); + if (column.Kind == ColumnKind.Money) + { + Assert.IsTrue(found.Type is "TEXT" or "NUMERIC", $"{table.Name}.{column.Name} è {found.Type}: il denaro non è mai in virgola mobile"); + } + + if (!column.Nullable) + { + Assert.IsTrue(found.Required || column.Name == "id", $"{table.Name}.{column.Name} dovrebbe essere obbligatoria"); + } + } + } + } + + [TestMethod] + [TestCategory(Category.Integration)] + public async Task DecimalsDatesAndIdsRoundTripExactly() + { + await using var test = await MigratedAsync().ConfigureAwait(false); + await test.Db.WriteAsync(s => s.ExecuteAsync("INSERT INTO accounts (id, name, broker, environment, base_currency, created_at) VALUES (@id, 'Prova', 'paper', 'simulation', 'EUR', @t)", + ("id", Guid.CreateVersion7(_clock.UtcNow)), ("t", _clock.UtcNow))).ConfigureAwait(false); + var account = await test.Db.ReadAsync(s => s.FirstOrDefaultAsync("SELECT id FROM accounts", r => r.Id("id"))).ConfigureAwait(false); + + decimal[] values = + [ + 0m, 1m, -1m, 0.000000000000000001m, -0.000000000000000001m, 1299.84m, 0.1m + 0.2m, + 99_999_999_999_999_999_999m, -99_999_999_999_999_999_999m, 1234567890.123456789012345678m, + 12345678901.12345678901234567m, 7.000000000000000000m, 79228162514.264337593543950335m, + ]; + var moments = new[] + { + new DateTimeOffset(2026, 10, 9, 8, 21, 0, TimeSpan.FromHours(2)), + new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero), + new DateTimeOffset(2099, 12, 31, 23, 59, 59, TimeSpan.Zero).AddTicks(1234567), + }; + + var rows = new List<(Guid Id, decimal Value, DateTimeOffset At)>(); + for (var i = 0; i < values.Length; i++) + { + rows.Add((Guid.CreateVersion7(_clock.UtcNow.AddMilliseconds(i)), values[i], moments[i % moments.Length])); + } + + await test.Db.WriteAsync(async s => + { + foreach (var (id, value, at) in rows) + { + await s.ExecuteAsync("INSERT INTO cash_ledger (id, account_id, currency, amount, kind, occurred_at, created_at) VALUES (@id, @a, 'EUR', @v, 'test', @at, @t)", + ("id", id), ("a", account), ("v", value), ("at", at), ("t", _clock.UtcNow)).ConfigureAwait(false); + } + }).ConfigureAwait(false); + + var read = await test.Db.ReadAsync(s => s.QueryAsync("SELECT id, amount, occurred_at FROM cash_ledger ORDER BY id", + r => (Id: r.Id("id"), Value: r.Money("amount"), At: r.Moment("occurred_at")))).ConfigureAwait(false); + Assert.HasCount(rows.Count, read); + for (var i = 0; i < rows.Count; i++) + { + Assert.AreEqual(rows[i].Id, read[i].Id); + Assert.AreEqual(DbValues.Money(rows[i].Value), read[i].Value, $"valore {rows[i].Value}"); + Assert.AreEqual(DbValues.Moment(rows[i].At), read[i].At); + Assert.AreEqual(TimeSpan.Zero, read[i].At.Offset, "si rilegge sempre in UTC"); + } + + // More than 18 decimals: rounded half away from zero, the same on both engines. + Assert.AreEqual(0.000000000000000002m, DbValues.Money(0.0000000000000000015m)); + Assert.AreEqual(1.123456789012345679m, DbValues.Money(1.1234567890123456789m)); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void ValuesOutsideNumeric38And18AndMomentsWithoutZoneAreRefused() + { + Assert.Throws(() => DbValues.Money(100_000_000_000_000_000_000m)); + Assert.Throws(() => DbValues.Money(decimal.MaxValue)); + Assert.Throws(() => DbValues.ToProvider(new DateTime(2026, 10, 9), DbEngine.Sqlite)); + Assert.Throws(() => DbValues.ParseMoment("2026-10-09T08:21:00")); + Assert.AreEqual("1.5", DbValues.MoneyText(1.500m)); + Assert.AreEqual("0", DbValues.MoneyText(0.000m)); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void MoneyRoundTripsForAnyValue() => + Property.ForAll(g => g.Decimal(-1_000_000m, 1_000_000m, 12), value => + { + var text = DbValues.MoneyText(value); + Assert.AreEqual(DbValues.Money(value), DbValues.ParseMoney(text)); + }); + + [TestMethod] + [TestCategory(Category.Integration)] + public async Task SameExecutionIsRecordedOnce() + { + await using var test = await MigratedAsync().ConfigureAwait(false); + var (account, instrument) = await SeedAccountAsync(test.Db).ConfigureAwait(false); + async Task InsertFill() => await test.Db.WriteAsync(s => s.ExecuteAsync( + "INSERT INTO fills (id, account_id, instrument_id, execution_id, side, quantity, price, fee, fee_currency, executed_at, created_at) VALUES (@id, @a, @i, 'EX-1', 'buy', 1, 100, 0, 'EUR', @t, @t)", + ("id", Guid.NewGuid()), ("a", account), ("i", instrument), ("t", _clock.UtcNow))).ConfigureAwait(false); + await InsertFill().ConfigureAwait(false); + await Assert.ThrowsAsync(InsertFill).ConfigureAwait(false); + } + + [TestMethod] + [TestCategory(Category.Integration)] + public async Task FailedWritesLeaveNothingBehind() + { + await using var test = await MigratedAsync().ConfigureAwait(false); + await Assert.ThrowsAsync(() => test.Db.WriteAsync(async s => + { + await s.ExecuteAsync("INSERT INTO accounts (id, name, broker, environment, base_currency, created_at) VALUES (@id, 'X', 'paper', 'simulation', 'EUR', @t)", + ("id", Guid.NewGuid()), ("t", _clock.UtcNow)).ConfigureAwait(false); + throw new InvalidOperationException("stop"); + })).ConfigureAwait(false); + Assert.AreEqual(0L, await test.Db.ReadAsync(s => s.CountAsync("SELECT COUNT(*) FROM accounts")).ConfigureAwait(false)); + } + + [TestMethod] + [TestCategory(Category.Integration)] + public async Task SqliteUsesWalFullSyncAndForeignKeys() + { + if (TestDatabase.Engine != "sqlite") + { + Assert.Inconclusive("Solo su SQLite."); + } + + await using var test = await MigratedAsync().ConfigureAwait(false); + var pragmas = await test.Db.ReadAsync(async s => ( + Journal: await s.FirstOrDefaultAsync("PRAGMA journal_mode", r => r.Text("journal_mode")).ConfigureAwait(false), + Sync: await s.CountAsync("PRAGMA synchronous").ConfigureAwait(false), + Keys: await s.CountAsync("PRAGMA foreign_keys").ConfigureAwait(false), + Busy: await s.CountAsync("PRAGMA busy_timeout").ConfigureAwait(false))).ConfigureAwait(false); + Assert.AreEqual("wal", pragmas.Journal); + Assert.AreEqual(2L, pragmas.Sync, "synchronous=FULL"); + Assert.AreEqual(1L, pragmas.Keys); + Assert.AreEqual(5000L, pragmas.Busy); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void SqliteDoesNotCreateAMissingFileWhenAskedNotTo() + { + var folder = TempFolder.Create("nocreate"); + try + { + var path = Path.Combine(folder, "db", "anubis.sqlite3"); + var db = Database.Open(new DbSettings { Engine = DbEngine.Sqlite, SqlitePath = path }, allowCreate: false); + Assert.ThrowsAsync(() => db.PingAsync(CancellationToken.None)).GetAwaiter().GetResult(); + Assert.IsFalse(File.Exists(path)); + } + finally + { + TempFolder.Delete(folder); + } + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void MigrationsUseOnlyPortableSql() + { + string[] forbidden = ["SERIAL", "AUTOINCREMENT", "JSONB", "MATERIALIZED", "PARTITION", "LISTEN", "NOTIFY", "REAL", "FLOAT", "DOUBLE", "pg_advisory", "RETURNING"]; + foreach (var migration in Migrations.All) + { + var sql = System.Text.RegularExpressions.Regex.Replace(migration.Sql, @"--[^\n]*|\{[a-z]+\}", ""); + foreach (var word in forbidden) + { + Assert.IsFalse(System.Text.RegularExpressions.Regex.IsMatch(sql, $@"\b{word}\b", System.Text.RegularExpressions.RegexOptions.IgnoreCase), + $"{migration.Version:D4}: '{word}' non è portabile"); + } + } + + Assert.IsTrue(Schema.Tables.All(t => t.Columns.Any(c => c.Name == "id" && c.Kind == ColumnKind.Uuid)), "ogni tabella ha una chiave UUID"); + Assert.IsTrue(Schema.Tables.All(t => t.Columns.Any(c => c.Name == "created_at" && c.Kind == ColumnKind.Moment)), "ogni tabella ha created_at"); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void AccountingTablesAreNeverUpdatedOrDeletedInCode() + { + var pattern = new System.Text.RegularExpressions.Regex(@"\b(UPDATE\s+(fills|lots|cash_ledger)\b|DELETE\s+FROM\s+(fills|lots|cash_ledger)\b)", System.Text.RegularExpressions.RegexOptions.IgnoreCase); + var offenders = Repo.Files() + .Where(f => f.Extension is ".cs" or ".sql" && Repo.Relative(f).StartsWith("src/", StringComparison.Ordinal)) + .Where(f => pattern.IsMatch(File.ReadAllText(f.FullName))) + .Select(Repo.Relative) + .ToList(); + Assert.IsEmpty(offenders, "Le tabelle contabili si correggono con righe di storno: " + string.Join(", ", offenders)); + } + + internal static async Task<(Guid Account, Guid Instrument)> SeedAccountAsync(Database db) + { + var account = Guid.CreateVersion7(_clock.UtcNow); + var instrument = Guid.CreateVersion7(_clock.UtcNow.AddMilliseconds(1)); + await db.WriteAsync(async s => + { + await s.ExecuteAsync("INSERT INTO accounts (id, name, broker, environment, base_currency, created_at) VALUES (@id, 'OKX Europe', 'okx', 'simulation', 'EUR', @t)", + ("id", account), ("t", _clock.UtcNow)).ConfigureAwait(false); + await s.ExecuteAsync("INSERT INTO instruments (id, venue, symbol, name, asset_class, quote_currency, created_at) VALUES (@id, 'okx', 'BTC/EUR', 'Bitcoin', 'crypto', 'EUR', @t)", + ("id", instrument), ("t", _clock.UtcNow)).ConfigureAwait(false); + }).ConfigureAwait(false); + return (account, instrument); + } +} diff --git a/tests/Anubis.Tests/DependencyPolicyTests.cs b/tests/Anubis.Tests/DependencyPolicyTests.cs index bdffcaf..ed0a72e 100644 --- a/tests/Anubis.Tests/DependencyPolicyTests.cs +++ b/tests/Anubis.Tests/DependencyPolicyTests.cs @@ -28,7 +28,14 @@ public sealed class DependencyPolicyTests /// Third-party packages pulled in by an authorised one. Each must also be listed in docs/API-NOTES.md /// and reported to the user (checked below). Microsoft.* and System.* ids are reserved to Microsoft on nuget.org. /// - private static readonly HashSet _knownThirdPartyTransitive = new(StringComparer.OrdinalIgnoreCase); + private static readonly HashSet _knownThirdPartyTransitive = new(StringComparer.OrdinalIgnoreCase) + { + // Native SQLite library and its loader, brought by Microsoft.Data.Sqlite (phase 1). + "SQLitePCLRaw.bundle_e_sqlite3", + "SQLitePCLRaw.core", + "SQLitePCLRaw.lib.e_sqlite3", + "SQLitePCLRaw.provider.e_sqlite3", + }; private static readonly string[] _packageElements = ["PackageReference", "GlobalPackageReference", "PackageDownload"]; diff --git a/tests/Anubis.Tests/FormatTests.cs b/tests/Anubis.Tests/FormatTests.cs new file mode 100644 index 0000000..2ae347c --- /dev/null +++ b/tests/Anubis.Tests/FormatTests.cs @@ -0,0 +1,108 @@ +using System.Text; +using Anubis.Core; + +namespace Anubis.Tests; + +/// Italian formats of SPEC 13.3, with the values of the mockups (appendix F). +[TestClass] +public sealed class FormatTests +{ + private const string Nbsp = " "; + private static readonly TimeZoneInfo _rome = TimeZoneInfo.FindSystemTimeZoneById("Europe/Rome"); + + [TestMethod] + [TestCategory(Category.Unit)] + [DataRow(1299.84, "1.299,84" + Nbsp + "€")] + [DataRow(10412.46, "10.412,46" + Nbsp + "€")] + [DataRow(0.0, "0,00" + Nbsp + "€")] + [DataRow(-26.0, "−26,00" + Nbsp + "€")] + [DataRow(1234567.891, "1.234.567,89" + Nbsp + "€")] + public void MoneyUsesCommaDotsAndSymbolAfterNonBreakingSpace(double value, string expected) => + Assert.AreEqual(expected, Format.Money((decimal)value)); + + [TestMethod] + [TestCategory(Category.Unit)] + public void ResultsCarryPlusAndTypographicMinus() + { + Assert.AreEqual("+412,46" + Nbsp + "€", Format.Money(412.46m, withSign: true)); + Assert.AreEqual("−9,53" + Nbsp + "€", Format.Money(-9.53m, withSign: true)); + Assert.AreEqual("0,00" + Nbsp + "€", Format.Money(0m, withSign: true)); + Assert.AreEqual("259,80" + Nbsp + "$", Format.Money(259.8m, "USD")); + Assert.IsFalse(Format.Money(-1m).Contains('-', StringComparison.Ordinal), "il meno deve essere tipografico"); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void RoundingIsHalfAwayFromZero() + { + Assert.AreEqual("0,13" + Nbsp + "€", Format.Money(0.125m)); + Assert.AreEqual("−0,13" + Nbsp + "€", Format.Money(-0.125m)); + Assert.AreEqual("0,00" + Nbsp + "€", Format.Money(-0.004m), "un valore che arrotondato è zero non ha segno"); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void PercentagesAndNumbers() + { + Assert.AreEqual("+4,12%", Format.Percent(4.12m, 2, withSign: true)); + Assert.AreEqual("−0,26%", Format.Percent(-0.26m, 2, withSign: true)); + Assert.AreEqual("64,1%", Format.Percent(64.1m)); + Assert.AreEqual("10.412", Format.Number(10412.46m)); + Assert.AreEqual("0,0182", Format.Number(0.0182m, 4)); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void SharesAlwaysAddUpToOneHundred() + { + // Appendix F.1: open positions by strategy (F.4) and cash, out of 10.412,46 €. + var shares = Format.Shares([2326.50m, 3899.31m, 445.32m, 3741.33m]); + Assert.AreEqual(100m, shares.Sum()); + CollectionAssert.AreEqual(new[] { 22.3m, 37.5m, 4.3m, 35.9m }, shares.ToArray()); + + Assert.AreEqual(100m, Format.Shares([1m, 1m, 1m]).Sum(), "tre terzi fanno 100"); + CollectionAssert.AreEqual(new[] { 33.4m, 33.3m, 33.3m }, Format.Shares([1m, 1m, 1m]).ToArray()); + CollectionAssert.AreEqual(new[] { 0m, 0m }, Format.Shares([0m, -5m]).ToArray()); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void SharesHoldForAnyWeights() => + Property.ForAll( + g => Enumerable.Range(0, g.Int(1, 12)).Select(_ => g.Decimal(0m, 10_000m, 2)).ToArray(), + weights => + { + var shares = Format.Shares(weights, 1); + if (weights.Any(w => w > 0)) + { + Assert.AreEqual(100m, shares.Sum()); + } + + Assert.IsTrue(shares.All(s => s >= 0)); + }); + + [TestMethod] + [TestCategory(Category.Unit)] + public void SpokenDatesInItalianTime() + { + var now = new DateTimeOffset(2026, 10, 9, 6, 21, 0, TimeSpan.Zero); // 08:21 in Rome + Assert.AreEqual("Oggi 06:10", Format.ShortDate(new DateTimeOffset(2026, 10, 9, 4, 10, 0, TimeSpan.Zero), now, _rome)); + Assert.AreEqual("Ieri 21:58", Format.ShortDate(new DateTimeOffset(2026, 10, 8, 19, 58, 0, TimeSpan.Zero), now, _rome)); + Assert.AreEqual("mar 6 ott", Format.ShortDate(new DateTimeOffset(2026, 10, 6, 13, 41, 0, TimeSpan.Zero), now, _rome)); + Assert.AreEqual("mer 31 dic 2025", Format.ShortDate(new DateTimeOffset(2025, 12, 31, 10, 0, 0, TimeSpan.Zero), now, _rome)); + Assert.AreEqual("Venerdì 9 ottobre 2026, ore 08:21", Format.LongDateTime(now, _rome)); + Assert.AreEqual("12 agosto 2026", Format.LongDate(new DateOnly(2026, 8, 12))); + Assert.AreEqual("4 secondi fa", Format.Ago(TimeSpan.FromSeconds(4))); + Assert.AreEqual("1 minuto fa", Format.Ago(TimeSpan.FromSeconds(65))); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void CsvHasBomSemicolonsAndDecimalComma() + { + var bytes = Format.Csv(["Strumento", "Valore €", "Nota"], [["BTC/EUR", 1299.84m, "stop; mobile"], ["AAPL", -2.57m, null]]); + CollectionAssert.AreEqual(Encoding.UTF8.GetPreamble(), bytes.Take(3).ToArray()); + var text = Encoding.UTF8.GetString(bytes, 3, bytes.Length - 3); + Assert.AreEqual("Strumento;Valore €;Nota\r\nBTC/EUR;1299,84;\"stop; mobile\"\r\nAAPL;-2,57;\r\n", text); + } +} diff --git a/tests/Anubis.Tests/PlatformTests.cs b/tests/Anubis.Tests/PlatformTests.cs new file mode 100644 index 0000000..b01df63 --- /dev/null +++ b/tests/Anubis.Tests/PlatformTests.cs @@ -0,0 +1,199 @@ +using Anubis.Platform; + +namespace Anubis.Tests; + +/// Startup configuration in Docker and on desktop (issue #7, SPEC 15.1, 15.6). +[TestClass] +public sealed class PlatformTests +{ + private static readonly string _key = new('k', 40); + + private static Dictionary Env(params (string Key, string? Value)[] values) + { + var env = new Dictionary + { + ["ANUBIS_DB_ENGINE"] = "sqlite", + ["ANUBIS_SECRET_KEY"] = _key, + ["ANUBIS_ADMIN_PASSWORD"] = "initial-pass-1", + }; + foreach (var (key, value) in values) + { + env[key] = value; + } + + return env; + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void DockerWithSqliteNeedsNothingElse() + { + var config = DockerPlatform.Load(Env(), "/data"); + Assert.IsTrue(config.IsComplete, string.Join("; ", config.Problems.Select(p => p.What))); + Assert.AreEqual(DbEngine.Sqlite, config.Db!.Engine); + Assert.AreEqual(Path.Combine("/data", "db", "anubis.sqlite3"), config.Db.SqlitePath); + Assert.AreEqual(8787, config.Port); + Assert.AreEqual(99, config.Puid); + Assert.AreEqual(100, config.Pgid); + Assert.IsFalse(config.AllowLive); + Assert.AreEqual("Europe/Rome", config.TimeZone.Id); + Assert.IsTrue(config.SecretKey.HasKey); + Assert.HasCount(32, config.SecretKey.GetOrCreateKey()); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void DockerPostgresUsesDefaultsAndNeverShowsThePassword() + { + var config = DockerPlatform.Load(Env(("ANUBIS_DB_ENGINE", "postgres"), ("ANUBIS_DB_HOST", "192.168.30.23"), ("ANUBIS_DB_PASSWORD", "pg-password-xyz"))); + Assert.IsTrue(config.IsComplete); + Assert.AreEqual(5432, config.Db!.Port); + Assert.AreEqual("anubis", config.Db.Database); + Assert.AreEqual("anubis", config.Db.User); + Assert.IsFalse(config.Db.Describe().Contains("pg-password-xyz", StringComparison.Ordinal)); + Assert.IsFalse(config.Db.ToString().Contains("pg-password-xyz", StringComparison.Ordinal)); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void MissingOrWrongValuesBecomeProblemsAndNoDatabaseIsChosen() + { + var empty = DockerPlatform.Load(new Dictionary()); + Assert.IsNull(empty.Db, "senza ANUBIS_DB_ENGINE nessun database viene scelto al posto dell'utente"); + CollectionAssert.IsSubsetOf(new[] { "ANUBIS_DB_ENGINE", "ANUBIS_SECRET_KEY", "ANUBIS_ADMIN_PASSWORD" }, empty.Problems.Select(p => p.Setting).ToList()); + Assert.IsTrue(empty.Problems.All(p => p.Remedy.Length > 0)); + + var wrong = DockerPlatform.Load(Env(("ANUBIS_DB_ENGINE", "mysql"))); + Assert.IsNull(wrong.Db); + Assert.IsFalse(wrong.IsComplete); + + var postgresWithoutHost = DockerPlatform.Load(Env(("ANUBIS_DB_ENGINE", "postgres"))); + Assert.IsNull(postgresWithoutHost.Db, "PostgreSQL incompleto non ripiega su SQLite"); + CollectionAssert.IsSubsetOf(new[] { "ANUBIS_DB_HOST", "ANUBIS_DB_PASSWORD" }, postgresWithoutHost.Problems.Select(p => p.Setting).ToList()); + + var shortKey = DockerPlatform.Load(Env(("ANUBIS_SECRET_KEY", "short"))); + Assert.IsTrue(shortKey.Problems.Any(p => p.Setting == "ANUBIS_SECRET_KEY")); + + var badLive = DockerPlatform.Load(Env(("ANUBIS_ALLOW_LIVE", "yes"))); + Assert.IsFalse(badLive.AllowLive); + Assert.IsTrue(badLive.Problems.Any(p => p.Setting == "ANUBIS_ALLOW_LIVE")); + + var badZone = DockerPlatform.Load(Env(("TZ", "Mars/Olympus"))); + Assert.IsTrue(badZone.Problems.Any(p => p.Setting == "TZ")); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void OptionalKeysAreReadWithTheDemoSuffix() + { + var config = DockerPlatform.Load(Env(("OKX_API_KEY_DEMO", "demo-key-123456"), ("TELEGRAM_CHAT_ID", "12345"))); + Assert.AreEqual("demo-key-123456", config.EnvironmentKeys["OKX_API_KEY_DEMO"]); + Assert.AreEqual("12345", config.EnvironmentKeys["TELEGRAM_CHAT_ID"]); + Assert.IsFalse(config.EnvironmentKeys.ContainsKey("OKX_API_KEY")); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void TomlSubsetParsesAndRejectsWithLineNumbers() + { + var values = Toml.Parse(""" + # comment + allow_live = false + port = 8_787 # trailing comment + name = "a \"quoted\" # not a comment" + + [database] + engine = "postgres" + """); + Assert.IsFalse((bool)values["allow_live"]); + Assert.AreEqual(8787L, values["port"]); + Assert.AreEqual("a \"quoted\" # not a comment", values["name"]); + Assert.AreEqual("postgres", values["database.engine"]); + + var error = Assert.Throws(() => Toml.Parse("a = 1\nb = [1, 2]\n")); + Assert.Contains("Riga 2", error.Message); + Assert.Throws(() => Toml.Parse("a = 1\na = 2")); + Assert.Throws(() => Toml.Parse("a = \"open")); + } + + [TestMethod] + [TestCategory(Category.Integration)] + public void DesktopStartsFromAnEmptyFolderAndKeepsSecretsOutOfConfigToml() + { + var folder = TempFolder.Create("desktop"); + try + { + var data = new DataDirectory(folder); + var store = ProtectedSecretKey.DefaultStore(data); + var first = DesktopPlatform.Load(data, store); + Assert.IsTrue(first.NeedsFirstRun); + Assert.IsFalse(first.IsComplete); + + var key = first.SecretKey.GetOrCreateKey(); + DesktopPlatform.WriteConfig(data, store, new DbSettings { Engine = DbEngine.Postgres, Host = "localhost", Password = "fake-pg-pass-1" }); + + var text = File.ReadAllText(data.ConfigFile); + Assert.Contains("allow_live = false", text); + Assert.IsFalse(text.Contains("fake-pg-pass-1", StringComparison.Ordinal), "la password del database non va in config.toml"); + Assert.IsFalse(text.Contains(Convert.ToBase64String(key), StringComparison.Ordinal), "la chiave dei segreti non va in config.toml"); + Assert.IsFalse(text.Contains("secret", StringComparison.OrdinalIgnoreCase) && text.Contains("key =", StringComparison.Ordinal)); + + var second = DesktopPlatform.Load(data, store); + Assert.IsTrue(second.IsComplete, string.Join("; ", second.Problems.Select(p => p.What))); + Assert.AreEqual("fake-pg-pass-1", second.Db!.Password); + CollectionAssert.AreEqual(key, second.SecretKey.GetOrCreateKey(), "la chiave resta la stessa tra un avvio e l'altro"); + Assert.IsFalse(second.AllowLive); + Assert.IsFalse(second.AllowNetwork); + + DesktopPlatform.WriteConfig(data, store, new DbSettings { Engine = DbEngine.Sqlite }); + var sqlite = DesktopPlatform.Load(data, store); + Assert.AreEqual(data.SqliteFile, sqlite.Db!.SqlitePath); + } + finally + { + TempFolder.Delete(folder); + } + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void BrokenConfigTomlIsAProblemNotAGuess() + { + var folder = TempFolder.Create("desktop-bad"); + try + { + var data = new DataDirectory(folder); + File.WriteAllText(data.ConfigFile, "[database]\nengine = sqlite\n"); + var config = DesktopPlatform.Load(data, ProtectedSecretKey.DefaultStore(data)); + Assert.IsNull(config.Db); + Assert.IsFalse(config.IsComplete); + Assert.Contains("Riga 2", config.Problems[0].What); + } + finally + { + TempFolder.Delete(folder); + } + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void DataFolderHasTheLayoutOfSpec152() + { + var folder = TempFolder.Create("layout"); + try + { + var data = new DataDirectory(Path.Combine(folder, "data")); + data.Ensure(); + foreach (var name in new[] { "db", "models", "research", "import", "backups", "logs" }) + { + Assert.IsTrue(Directory.Exists(Path.Combine(data.Root, name)), name); + } + + Assert.AreEqual(Path.Combine(data.Root, "installation.json"), data.InstallationFile); + } + finally + { + TempFolder.Delete(folder); + } + } +} diff --git a/tests/Anubis.Tests/SecretScanTests.cs b/tests/Anubis.Tests/SecretScanTests.cs index 0750267..f0dd29a 100644 --- a/tests/Anubis.Tests/SecretScanTests.cs +++ b/tests/Anubis.Tests/SecretScanTests.cs @@ -53,6 +53,11 @@ public sealed partial class SecretScanTests Assert.IsFalse(LooksLikeSecret("REGISTRY_TOKEN: ${{ secrets.REGISTRY_TOKEN }}")); Assert.IsFalse(LooksLikeSecret("private const string SecretKeyVariable = \"ANUBIS_SECRET_KEY\";")); Assert.IsFalse(LooksLikeSecret("OKX_API_PASSPHRASE=")); + + // Code that assigns a computed value is not a secret. + Assert.IsFalse(LooksLikeSecret("var key = RandomNumberGenerator.GetBytes(32);")); + Assert.IsFalse(LooksLikeSecret("builder.Password = Uri.UnescapeDataString(userInfo[1]);")); + Assert.IsFalse(LooksLikeSecret("?secret={Base32.Encode(secret)}&issuer=x")); } private static bool LooksLikeSecret(string line) => @@ -80,6 +85,6 @@ public sealed partial class SecretScanTests // Variables named like the project's secrets (…KEY, …SECRET, …TOKEN, …PASSPHRASE, …PASSWORD) with a value // of at least 8 characters that mixes letters and digits, as real keys and passwords do. - [GeneratedRegex("""(?i)[A-Za-z0-9_\-]*(key|secret|token|passphrase|password)["']?\s*[:=]\s*["']?(?(?=[^\s"',;#]*\d)(?=[^\s"',;#]*[A-Za-z])[^\s"',;#]{8,})""")] + [GeneratedRegex("""(?i)[A-Za-z0-9_\-]*(key|secret|token|passphrase|password)["']?\s*[:=]\s*["']?(?(?=[^\s"',;#]*\d)(?=[^\s"',;#]*[A-Za-z])[^\s"',;#(){}\[\]<>]{8,})(?=$|[\s"',;#])""")] private static partial Regex NamedSecret(); } diff --git a/tests/Anubis.Tests/StoresTests.cs b/tests/Anubis.Tests/StoresTests.cs new file mode 100644 index 0000000..bcf5b5a --- /dev/null +++ b/tests/Anubis.Tests/StoresTests.cs @@ -0,0 +1,264 @@ +using System.Text; +using Anubis.Access; +using Anubis.Core; +using Anubis.Db; +using Anubis.Platform; + +namespace Anubis.Tests; + +/// Installation identity, settings, secrets (issues #7, #10). +[TestClass] +public sealed class StoresTests +{ + private static readonly byte[] _key = Enumerable.Range(1, 32).Select(i => (byte)i).ToArray(); + + [TestMethod] + [TestCategory(Category.Integration)] + public async Task InstallationIdentityAndFingerprint() + { + var clock = new ManualClock(new DateTimeOffset(2026, 10, 9, 6, 21, 0, TimeSpan.Zero)); + await using var test = await DatabaseTests.MigratedAsync().ConfigureAwait(false); + var folder = TempFolder.Create("installation"); + try + { + var data = new DataDirectory(folder); + var first = await Installation.CheckAsync(test.Db, data, clock).ConfigureAwait(false); + Assert.AreEqual(IdentityVerdict.Created, first.Verdict); + Assert.IsTrue(File.Exists(data.InstallationFile)); + Assert.AreEqual(IdentityVerdict.Match, (await Installation.CheckAsync(test.Db, data, clock).ConfigureAwait(false)).Verdict); + + // Data folder lost: rewritten from the database. + File.Delete(data.InstallationFile); + Assert.AreEqual(IdentityVerdict.FileRestored, (await Installation.CheckAsync(test.Db, data, clock).ConfigureAwait(false)).Verdict); + + // A stale copy of the database: same installation, older fingerprint. + var before = Installation.ReadFile(data)!; + Installation.WriteFile(data, before with { Fingerprint = "newer" }); + var stale = await Installation.CheckAsync(test.Db, data, clock).ConfigureAwait(false); + Assert.AreEqual(IdentityVerdict.StaleDatabase, stale.Verdict); + Assert.IsFalse(stale.CanStart); + + // Another installation's folder. + Installation.WriteFile(data, before with { InstallationId = Guid.NewGuid() }); + Assert.AreEqual(IdentityVerdict.DifferentInstallation, (await Installation.CheckAsync(test.Db, data, clock).ConfigureAwait(false)).Verdict); + + // The user adopts this database: new fingerprint on both sides, protected restart on. + var renewed = await Installation.RenewAsync(test.Db, data, clock, adoptDatabaseIdentity: true).ConfigureAwait(false); + Assert.AreEqual(before.InstallationId, renewed.InstallationId); + Assert.AreNotEqual(before.Fingerprint, renewed.Fingerprint); + var row = await Installation.ReadRowAsync(test.Db).ConfigureAwait(false); + Assert.IsTrue(row!.ProtectedRestart); + Assert.AreEqual(IdentityVerdict.Match, (await Installation.CheckAsync(test.Db, data, clock).ConfigureAwait(false)).Verdict); + + // An empty database while the folder belongs to an installation: nothing is created. + await using var empty = await DatabaseTests.MigratedAsync().ConfigureAwait(false); + var check = await Installation.CheckAsync(empty.Db, data, clock).ConfigureAwait(false); + Assert.AreEqual(IdentityVerdict.EmptyDatabase, check.Verdict); + Assert.IsNull(await Installation.ReadRowAsync(empty.Db).ConfigureAwait(false)); + + Installation.StartOver(data, clock); + Assert.AreEqual(IdentityVerdict.Created, (await Installation.CheckAsync(empty.Db, data, clock).ConfigureAwait(false)).Verdict); + } + finally + { + TempFolder.Delete(folder); + } + } + + [TestMethod] + [TestCategory(Category.Integration)] + public async Task SettingsKeepAChangeLogAndStartupValuesAreReadOnly() + { + var clock = new ManualClock(new DateTimeOffset(2026, 10, 9, 6, 21, 0, TimeSpan.Zero)); + await using var test = await DatabaseTests.MigratedAsync().ConfigureAwait(false); + var bus = new EventBus(); + using var changes = bus.Subscribe(); + var settings = new SettingsStore(test.Db, clock, bus, new Dictionary + { + ["appearance.currency"] = new("USD", "variabile d'ambiente di prova"), + }); + + Assert.AreEqual("auto", (await settings.GetAsync("appearance.theme").ConfigureAwait(false)).Value); + await settings.SetAsync("appearance.theme", "dark", "admin").ConfigureAwait(false); + clock.Advance(TimeSpan.FromMinutes(1)); + await settings.SetAsync("appearance.theme", "light", "admin").ConfigureAwait(false); + await settings.SetAsync("appearance.theme", "light", "admin").ConfigureAwait(false); + Assert.AreEqual("light", (await settings.GetAsync("appearance.theme").ConfigureAwait(false)).Value); + + var history = await settings.HistoryAsync("appearance.theme").ConfigureAwait(false); + Assert.HasCount(2, history, "un valore uguale non crea una voce"); + Assert.AreEqual(("dark", "light"), (history[0].OldValue, history[0].NewValue)); + Assert.AreEqual(("auto", "dark"), (history[1].OldValue, history[1].NewValue)); + + var currency = await settings.GetAsync("appearance.currency").ConfigureAwait(false); + Assert.IsTrue(currency.ReadOnly); + Assert.AreEqual("USD", currency.Value); + await Assert.ThrowsAsync(() => settings.SetAsync("appearance.currency", "EUR", "admin")).ConfigureAwait(false); + await Assert.ThrowsAsync(() => settings.SetAsync("appearance.theme", "pink", "admin")).ConfigureAwait(false); + Assert.IsTrue(changes.TryRead(out var changed)); + Assert.AreEqual("dark", changed!.Value); + } + + [TestMethod] + [TestCategory(Category.Integration)] + public async Task SecretsAreEncryptedNeverLoggedAndEnvironmentWins() + { + var clock = new ManualClock(new DateTimeOffset(2026, 10, 9, 6, 21, 0, TimeSpan.Zero)); + await using var test = await DatabaseTests.MigratedAsync().ConfigureAwait(false); + var secrets = new SecretStore(test.Db, clock, _key, new Dictionary { ["OKX_API_KEY_DEMO"] = "from-environment-1" }); + const string value = "alpaca-paper-secret-0123456789"; + + await secrets.SetAsync("ALPACA_API_SECRET_DEMO", value, "admin").ConfigureAwait(false); + var stored = await test.Db.ReadAsync(s => s.FirstOrDefaultAsync("SELECT ciphertext FROM secrets", r => r.Bytes("ciphertext"))).ConfigureAwait(false); + Assert.IsFalse(Encoding.UTF8.GetString(stored!).Contains(value, StringComparison.Ordinal), "nel database il segreto è cifrato"); + Assert.AreEqual(value, await secrets.RevealAsync("ALPACA_API_SECRET_DEMO").ConfigureAwait(false)); + Assert.IsTrue((await secrets.StatusAsync("ALPACA_API_SECRET_DEMO").ConfigureAwait(false)).IsSet); + Assert.IsFalse(Redactor.Redact("got " + value).Contains(value, StringComparison.Ordinal), "un segreto letto viene filtrato dai registri"); + + var history = await new SettingsStore(test.Db, clock, new EventBus()).HistoryAsync("secret:ALPACA_API_SECRET_DEMO").ConfigureAwait(false); + Assert.AreEqual("impostato", history.Single().NewValue); + Assert.IsFalse(history.Any(h => (h.NewValue ?? "").Contains(value, StringComparison.Ordinal) || (h.OldValue ?? "").Contains(value, StringComparison.Ordinal))); + + // Environment keys win and cannot be overwritten from the interface. + Assert.AreEqual("from-environment-1", await secrets.RevealAsync("OKX_API_KEY_DEMO").ConfigureAwait(false)); + Assert.IsTrue((await secrets.StatusAsync("OKX_API_KEY_DEMO").ConfigureAwait(false)).FromEnvironment); + await Assert.ThrowsAsync(() => secrets.SetAsync("OKX_API_KEY_DEMO", "x-123456789", "admin")).ConfigureAwait(false); + + // With another key the value cannot be read: the error says to enter it again. + var otherKey = new SecretStore(test.Db, clock, Enumerable.Repeat((byte)7, 32).ToArray()); + var error = await Assert.ThrowsAsync(() => otherKey.RevealAsync("ALPACA_API_SECRET_DEMO")).ConfigureAwait(false); + Assert.Contains("Reinserisci", error.Message); + + // A value sealed under one name does not open under another. + var sealedValue = secrets.Seal("A", "x"); + Assert.Throws(() => secrets.Open("B", sealedValue)); + + Assert.IsTrue(await secrets.RemoveAsync("ALPACA_API_SECRET_DEMO", "admin").ConfigureAwait(false)); + Assert.IsNull(await secrets.RevealAsync("ALPACA_API_SECRET_DEMO").ConfigureAwait(false)); + } +} + +/// Passwords, TOTP, sessions and the attempt limit (issue #11). +[TestClass] +public sealed class AccessTests +{ + [TestMethod] + [TestCategory(Category.Unit)] + public void PasswordHashesVerifyAndCarryTheirCost() + { + var hash = Passwords.Hash("correct horse battery", 1000); + Assert.StartsWith("pbkdf2-sha512$1000$", hash); + Assert.IsTrue(Passwords.Verify("correct horse battery", hash)); + Assert.IsFalse(Passwords.Verify("correct horse batterY", hash)); + Assert.IsFalse(Passwords.Verify("x", "garbage")); + Assert.IsFalse(Passwords.Verify("x", "pbkdf2-sha512$abc$$")); + Assert.AreNotEqual(Passwords.Hash("same", 1000), Passwords.Hash("same", 1000), "sale diverso a ogni hash"); + Assert.IsNotNull(Passwords.Problem("short")); + Assert.IsNotNull(Passwords.Problem("initial-pass-1", "initial-pass-1")); + Assert.IsNull(Passwords.Problem("una password lunga")); + } + + // RFC 6238, appendix B: the official test vectors. + [TestMethod] + [TestCategory(Category.Unit)] + [DataRow(59L, "94287082", "46119246", "90693936")] + [DataRow(1111111109L, "07081804", "68084774", "25091201")] + [DataRow(1111111111L, "14050471", "67062674", "99943326")] + [DataRow(1234567890L, "89005924", "91819424", "93441116")] + [DataRow(2000000000L, "69279037", "90698825", "38618901")] + [DataRow(20000000000L, "65353130", "77737706", "47863826")] + public void TotpMatchesTheRfcVectors(long unixSeconds, string sha1, string sha256, string sha512) + { + var at = DateTimeOffset.FromUnixTimeSeconds(unixSeconds); + Assert.AreEqual(sha1, Totp.Compute("12345678901234567890"u8, at, 8, 30, "SHA1")); + Assert.AreEqual(sha256, Totp.Compute("12345678901234567890123456789012"u8, at, 8, 30, "SHA256")); + Assert.AreEqual(sha512, Totp.Compute("1234567890123456789012345678901234567890123456789012345678901234"u8, at, 8, 30, "SHA512")); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void TotpAcceptsOneStepOfDriftAndBase32RoundTrips() + { + var secret = Totp.NewSecret(); + var now = new DateTimeOffset(2026, 10, 9, 6, 21, 0, TimeSpan.Zero); + Assert.IsTrue(Totp.Verify(secret, Totp.Compute(secret, now), now)); + Assert.IsTrue(Totp.Verify(secret, Totp.Compute(secret, now.AddSeconds(-30)), now)); + Assert.IsFalse(Totp.Verify(secret, Totp.Compute(secret, now.AddSeconds(-90)), now)); + Assert.IsFalse(Totp.Verify(secret, "12a456", now)); + CollectionAssert.AreEqual(secret, Base32.Decode(Base32.Encode(secret))); + Assert.AreEqual("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ", Base32.Encode("12345678901234567890"u8)); + Assert.StartsWith("otpauth://totp/Anubis:admin?secret=", Totp.Uri("Anubis", "admin", secret)); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void LoginLimiterBlocksAnAddressAfterFiveFailures() + { + var clock = new ManualClock(DateTimeOffset.UnixEpoch); + var limiter = new LoginLimiter(clock); + for (var i = 0; i < LoginLimiter.PerAddress; i++) + { + Assert.AreEqual(TimeSpan.Zero, limiter.Wait("10.0.0.2")); + limiter.Failed("10.0.0.2"); + } + + Assert.AreEqual(LoginLimiter.Window, limiter.Wait("10.0.0.2")); + Assert.AreEqual(TimeSpan.Zero, limiter.Wait("10.0.0.3"), "un altro indirizzo non è bloccato"); + clock.Advance(LoginLimiter.Window); + Assert.AreEqual(TimeSpan.Zero, limiter.Wait("10.0.0.2")); + + for (var i = 0; i < LoginLimiter.Overall; i++) + { + limiter.Failed("host-" + i); + } + + Assert.IsGreaterThan(TimeSpan.Zero, limiter.Wait("10.0.0.9"), "troppi errori in tutto: aspettano tutti"); + } + + [TestMethod] + [TestCategory(Category.Integration)] + public async Task SignInSessionsSecondFactorAndPasswordChange() + { + var clock = new ManualClock(new DateTimeOffset(2026, 10, 9, 6, 21, 0, TimeSpan.Zero)); + await using var test = await DatabaseTests.MigratedAsync().ConfigureAwait(false); + var secrets = new SecretStore(test.Db, clock, new byte[32]); + var access = new AccessStore(test.Db, clock, secrets, iterations: 1000); + + Assert.IsTrue(await access.EnsureUserAsync("initial-pass-1", mustChange: true).ConfigureAwait(false)); + Assert.IsFalse(await access.EnsureUserAsync("other-password", mustChange: false).ConfigureAwait(false), "un solo utente"); + + Assert.AreEqual(LoginResult.WrongPassword, (await access.SignInAsync("wrong", null, null, null).ConfigureAwait(false)).Result); + var (result, token) = await access.SignInAsync("initial-pass-1", null, "test", "127.0.0.1").ConfigureAwait(false); + Assert.AreEqual(LoginResult.Ok, result); + var session = await access.ValidateAsync(token).ConfigureAwait(false); + Assert.IsNotNull(session); + Assert.IsTrue(session.User.MustChangePassword); + + var stored = await test.Db.ReadAsync(s => s.FirstOrDefaultAsync("SELECT token_hash FROM sessions", r => r.Text("token_hash"))).ConfigureAwait(false); + Assert.AreNotEqual(token, stored, "nel database c'è solo l'impronta del token"); + + // Second session, then a password change ends every other session. + var (_, other) = await access.SignInAsync("initial-pass-1", null, null, null).ConfigureAwait(false); + Assert.IsTrue(await access.ChangePasswordAsync(session.User.Id, "initial-pass-1", "una password nuova", session.Id).ConfigureAwait(false)); + Assert.IsNotNull(await access.ValidateAsync(token).ConfigureAwait(false)); + Assert.IsNull(await access.ValidateAsync(other).ConfigureAwait(false)); + Assert.IsFalse((await access.ValidateAsync(token).ConfigureAwait(false))!.User.MustChangePassword); + + // Second factor. + var secret = Totp.NewSecret(); + Assert.IsFalse(await access.EnableSecondFactorAsync(session.User.Id, secret, Totp.Compute(secret, clock.UtcNow.AddMinutes(10))).ConfigureAwait(false)); + Assert.IsTrue(await access.EnableSecondFactorAsync(session.User.Id, secret, Totp.Compute(secret, clock.UtcNow)).ConfigureAwait(false)); + Assert.AreEqual(LoginResult.SecondFactorNeeded, (await access.SignInAsync("una password nuova", null, null, null).ConfigureAwait(false)).Result); + var badCode = Totp.Compute(secret, clock.UtcNow.AddMinutes(10)); + Assert.AreEqual(LoginResult.WrongSecondFactor, (await access.SignInAsync("una password nuova", badCode, null, null).ConfigureAwait(false)).Result); + Assert.AreEqual(LoginResult.Ok, (await access.SignInAsync("una password nuova", Totp.Compute(secret, clock.UtcNow), null, null).ConfigureAwait(false)).Result); + Assert.IsTrue(await access.DisableSecondFactorAsync(session.User.Id, "una password nuova").ConfigureAwait(false)); + + // Idle and absolute timeouts. + clock.Advance(AccessStore.IdleTimeout); + Assert.IsNull(await access.ValidateAsync(token).ConfigureAwait(false)); + + var history = await new SettingsStore(test.Db, clock, new EventBus()).HistoryAsync().ConfigureAwait(false); + CollectionAssert.IsSubsetOf(new[] { "security.password", "security.second_factor" }, history.Select(h => h.Key).Distinct().ToList()); + } +} diff --git a/tests/Anubis.Tests/TestSupport.cs b/tests/Anubis.Tests/TestSupport.cs index db5c9a6..478f59b 100644 --- a/tests/Anubis.Tests/TestSupport.cs +++ b/tests/Anubis.Tests/TestSupport.cs @@ -82,7 +82,7 @@ internal static class TestDatabase /// Host and port of the PostgreSQL used by the tests, from ANUBIS_TEST_PG_HOST and ANUBIS_TEST_PG_PORT. public static (string Host, int Port) Postgres { get; } = ( - Environment.GetEnvironmentVariable("ANUBIS_TEST_PG_HOST") ?? "localhost", + Environment.GetEnvironmentVariable("ANUBIS_TEST_PG_HOST") ?? "127.0.0.1", int.TryParse(Environment.GetEnvironmentVariable("ANUBIS_TEST_PG_PORT"), out var port) ? port : 55432); public static async Task PostgresReachableAsync(TimeSpan timeout) @@ -100,3 +100,118 @@ internal static class TestDatabase } } } + +/// +/// A fresh, empty database for one test: a SQLite file in a temporary folder, or a new PostgreSQL database +/// created on the test server and dropped afterwards. Never the developer's or the user's database. +/// +internal sealed class TestDb : IAsyncDisposable +{ + private readonly string? _folder; + private readonly string? _postgresName; + + private TestDb(Anubis.Platform.DbSettings settings, string? folder, string? postgresName) + { + Settings = settings; + _folder = folder; + _postgresName = postgresName; + Db = Anubis.Db.Database.Open(settings); + } + + public Anubis.Platform.DbSettings Settings { get; } + + public Anubis.Db.Database Db { get; } + + public static string PostgresPassword { get; } = Environment.GetEnvironmentVariable("ANUBIS_TEST_PG_PASSWORD") ?? "anubis-dev"; + + /// A database on the engine of this run (ANUBIS_TEST_DB). + public static Task CreateAsync() => + CreateAsync(TestDatabase.Engine == "postgres" ? Anubis.Platform.DbEngine.Postgres : Anubis.Platform.DbEngine.Sqlite); + + public static async Task CreateAsync(Anubis.Platform.DbEngine engine) + { + if (engine == Anubis.Platform.DbEngine.Sqlite) + { + var folder = TempFolder.Create("db"); + return new TestDb(new() { Engine = engine, SqlitePath = Path.Combine(folder, "test.sqlite3") }, folder, null); + } + + var name = "anubis_t_" + Guid.NewGuid().ToString("N"); + await using (var admin = Anubis.Db.Database.Open(AdminSettings())) + { + await using var connection = await admin.OpenConnectionAsync(CancellationToken.None).ConfigureAwait(false); + await using var command = connection.CreateCommand(); + command.CommandText = $"CREATE DATABASE {name}"; + await command.ExecuteNonQueryAsync().ConfigureAwait(false); + } + + return new TestDb(AdminSettings() with { Database = name }, null, name); + } + + /// True when a PostgreSQL server for tests answers: cross-engine tests run only then. + public static Task PostgresAvailableAsync() => TestDatabase.PostgresReachableAsync(TimeSpan.FromSeconds(5)); + + private static Anubis.Platform.DbSettings AdminSettings() => new() + { + Engine = Anubis.Platform.DbEngine.Postgres, + Host = TestDatabase.Postgres.Host, + Port = TestDatabase.Postgres.Port, + Database = "anubis", + User = "anubis", + Password = PostgresPassword, + }; + + public async ValueTask DisposeAsync() + { + await Db.DisposeAsync().ConfigureAwait(false); + if (_postgresName is not null) + { + await using var admin = Anubis.Db.Database.Open(AdminSettings()); + await using var connection = await admin.OpenConnectionAsync(CancellationToken.None).ConfigureAwait(false); + await using var command = connection.CreateCommand(); + command.CommandText = $"DROP DATABASE IF EXISTS {_postgresName} WITH (FORCE)"; + await command.ExecuteNonQueryAsync().ConfigureAwait(false); + } + + if (_folder is not null) + { + TempFolder.Delete(_folder); + } + } +} + +/// Temporary folders for tests, under the system temp directory. +internal static class TempFolder +{ + public static string Create(string purpose) + { + var path = Path.Combine(Path.GetTempPath(), "anubis-tests", purpose + "-" + Guid.NewGuid().ToString("N")[..12]); + Directory.CreateDirectory(path); + return path; + } + + public static void Delete(string path) + { + Microsoft.Data.Sqlite.SqliteConnection.ClearAllPools(); + for (var attempt = 0; attempt < 5; attempt++) + { + try + { + if (Directory.Exists(path)) + { + Directory.Delete(path, recursive: true); + } + + return; + } + catch (IOException) when (attempt < 4) + { + Thread.Sleep(100); + } + catch (UnauthorizedAccessException) when (attempt < 4) + { + Thread.Sleep(100); + } + } + } +} diff --git a/tests/Anubis.Tests/packages.lock.json b/tests/Anubis.Tests/packages.lock.json index cba2da5..c711790 100644 --- a/tests/Anubis.Tests/packages.lock.json +++ b/tests/Anubis.Tests/packages.lock.json @@ -44,6 +44,27 @@ "resolved": "18.11.0", "contentHash": "MhN9wpvL1Es4hfIYslrsVgIsUdDFxuOBTbAl9mGIMvFo8PP5CLf4DrGdNrfwWSyn0lqi6sssm+I/qkkLL3MU+g==" }, + "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" + } + }, "Microsoft.Testing.Extensions.Telemetry": { "type": "Transitive", "resolved": "2.5.2", @@ -92,14 +113,72 @@ "resolved": "4.5.2", "contentHash": "t6Dj9AmWO9IfkMBi3yYTf7bfgSTAetsi4LFA3xGdKbJ+gBbSDqbo41U5vYiW9puD8UGl2elsizNeJVYtwNim0Q==" }, + "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, )" + } }, "anubis.server": { "type": "Project", "dependencies": { "Anubis": "[0.0.1, )" } + }, + "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==" } } }