feat: system and security - start-up, accounts, sign-in, licences, log, jobs, companies (M2)
CI / Build and test (SQLite) (pull_request) Successful in 55s
CI / PostgreSQL 16 integration (pull_request) Skipped
CI / PostgreSQL 17 integration (pull_request) Skipped
CI / YAML check (pull_request) Successful in 5s
CI / Docker image (pull_request) Failing after 2m4s

The instance now starts on its own from KHAEMHAT_* variables: it migrates the system
and company databases, creates the super user and the first company, and only then
reports ready. Users sign in with password and TOTP through revocable database
sessions, and licences decide which modules and how many companies are available.

- Configuration: every wrong variable reported at once (exit code 2), _FILE variants
- Accounts, password policy and lockout, TOTP with recovery codes, session states
  with token rotation, security audit with a hash chain
- Sign-in pages (mockups 001-003), authorization attributes, security headers and a
  CSP without inline scripts, login rate limit
- Signed licences, activation codes, test mode, licence tool
- DataProtection keys in the system database, encrypted with the instance key
- Memory or Redis cache (in-house RESP client) that falls back to memory
- Database log by section with trace id; cron scheduler with locks and system jobs;
  record history (audit_log)
- Company creation, states, company context and X-Company, 503 in maintenance
- In-memory SQLite databases stay alive for the process (tests, demo)
- Tests for all of the above, ADR 0019, wiki, SECURITY, CHANGELOG, STATUS, DATA-MODEL

Refs #15 #16 #17 #18 #19 #20 #21 #22 #23 #24

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-11 15:29:24 +01:00
1 parent 4582837b70
commit 032c602b49
107 files changed
+12202 -116

No files matched your search

+131
View File
@@ -0,0 +1,131 @@
namespace Khaemhat.Core.Logging;
/// <summary>The sections of the application log (SPEC §9.5; mockup 084). Each module writes to its own.</summary>
public enum LogSection
{
/// <summary>Sistema.</summary>
System = 0,
/// <summary>Autenticazione.</summary>
Authentication = 1,
/// <summary>Database.</summary>
Database = 2,
/// <summary>Migrazioni.</summary>
Migrations = 3,
/// <summary>Documenti.</summary>
Documents = 4,
/// <summary>Contabilità.</summary>
Accounting = 5,
/// <summary>Magazzino.</summary>
Inventory = 6,
/// <summary>SDI/e-invoicing.</summary>
EInvoicing = 7,
/// <summary>CRM.</summary>
Crm = 8,
/// <summary>Automazioni.</summary>
Automation = 9,
/// <summary>Attività programmate.</summary>
ScheduledJobs = 10,
/// <summary>Backup.</summary>
Backup = 11,
/// <summary>API/MCP.</summary>
Api = 12,
/// <summary>E-mail/PEC.</summary>
Email = 13,
/// <summary>Integrazioni (VIES, BCE, intermediari).</summary>
Integrations = 14,
/// <summary>UI/circuiti.</summary>
Ui = 15,
/// <summary>Prestazioni.</summary>
Performance = 16,
}
/// <summary>Names, labels and the scope key of <see cref="LogSection"/>.</summary>
public static class LogSections
{
/// <summary>The scope key that carries the section (<c>using (logger.BeginScope(LogSections.Scope(...)))</c>).</summary>
public const string ScopeKey = "Section";
/// <summary>The scope key of the trace identifier.</summary>
public const string TraceIdKey = "TraceId";
/// <summary>The scope key of the user name.</summary>
public const string UserKey = "UserName";
/// <summary>The scope key of the company code.</summary>
public const string CompanyKey = "CompanyCode";
/// <summary>The scope key of the request (method and path).</summary>
public const string RequestKey = "Request";
/// <summary>The scope key of the entity concerned.</summary>
public const string EntityKey = "Entity";
/// <summary>The scope key of the entity identifier.</summary>
public const string EntityIdKey = "EntityId";
/// <summary>The Italian label shown in the log page.</summary>
/// <param name="section">The section.</param>
/// <returns>The label.</returns>
public static string Label(LogSection section) => section switch
{
LogSection.System => "Sistema",
LogSection.Authentication => "Autenticazione",
LogSection.Database => "Database",
LogSection.Migrations => "Migrazioni",
LogSection.Documents => "Documenti",
LogSection.Accounting => "Contabilità",
LogSection.Inventory => "Magazzino",
LogSection.EInvoicing => "SDI/e-invoicing",
LogSection.Crm => "CRM",
LogSection.Automation => "Automazioni",
LogSection.ScheduledJobs => "Attività programmate",
LogSection.Backup => "Backup",
LogSection.Api => "API/MCP",
LogSection.Email => "E-mail/PEC",
LogSection.Integrations => "Integrazioni",
LogSection.Ui => "UI/circuiti",
LogSection.Performance => "Prestazioni",
_ => section.ToString(),
};
/// <summary>A scope state that sets the section of the events logged inside it.</summary>
/// <param name="section">The section.</param>
/// <returns>The scope state.</returns>
public static IReadOnlyList<KeyValuePair<string, object?>> Scope(LogSection section) =>
[new(ScopeKey, section.ToString())];
/// <summary>
/// The section of a logger category when no scope sets one: by namespace, so that the framework's own events land in
/// a sensible place.
/// </summary>
/// <param name="category">The logger category.</param>
/// <returns>The section.</returns>
public static LogSection FromCategory(string? category) => category switch
{
null => LogSection.System,
_ when category.Contains(".Security", StringComparison.Ordinal) || category.Contains("Authentication", StringComparison.Ordinal)
|| category.Contains("Authorization", StringComparison.Ordinal) || category.Contains("Antiforgery", StringComparison.Ordinal) => LogSection.Authentication,
_ when category.Contains("Migration", StringComparison.Ordinal) => LogSection.Migrations,
_ when category.StartsWith("Khaemhat.Data", StringComparison.Ordinal) => LogSection.Database,
_ when category.Contains(".Jobs", StringComparison.Ordinal) => LogSection.ScheduledJobs,
_ when category.StartsWith("Microsoft.AspNetCore.Components", StringComparison.Ordinal) => LogSection.Ui,
_ when category.Contains("SlowQuer", StringComparison.Ordinal) || category.Contains("Performance", StringComparison.Ordinal) => LogSection.Performance,
_ => LogSection.System,
};
}