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
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:
1 parent
4582837b70
commit
032c602b49
107 files changed
+12202
-116
No files matched your search
@@ -0,0 +1,349 @@
|
||||
using Khaemhat.Core.Entities;
|
||||
|
||||
namespace Khaemhat.Core.Instance;
|
||||
|
||||
/// <summary>Where an installed licence comes from.</summary>
|
||||
public enum LicenseSource
|
||||
{
|
||||
/// <summary>A signed <c>.khl</c> instance licence.</summary>
|
||||
File = 0,
|
||||
|
||||
/// <summary>A signed activation code for one module.</summary>
|
||||
ActivationCode = 1,
|
||||
|
||||
/// <summary>A fictitious code of SPEC §8.3, accepted only in test mode.</summary>
|
||||
TestCode = 2,
|
||||
}
|
||||
|
||||
/// <summary>An installed licence or activation code (<c>sys_licenses</c>).</summary>
|
||||
public sealed class LicenseRecord : Entity<long>
|
||||
{
|
||||
/// <summary>Where the licence comes from.</summary>
|
||||
public LicenseSource Source { get; set; }
|
||||
|
||||
/// <summary>The licence text (<c>.khl</c> content or code), as installed.</summary>
|
||||
public string Payload { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>The licence holder.</summary>
|
||||
public string? Licensee { get; set; }
|
||||
|
||||
/// <summary>The module codes, comma separated.</summary>
|
||||
public string Modules { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>Concurrent users granted (0 = not stated by this licence).</summary>
|
||||
public int MaxConcurrentUsers { get; set; }
|
||||
|
||||
/// <summary>Companies granted (0 = not stated by this licence).</summary>
|
||||
public int MaxCompanies { get; set; }
|
||||
|
||||
/// <summary>When the licence was issued.</summary>
|
||||
public DateTime? IssuedAt { get; set; }
|
||||
|
||||
/// <summary>When the licence expires; <see langword="null"/> for no expiry.</summary>
|
||||
public DateTime? ExpiresAt { get; set; }
|
||||
|
||||
/// <summary>Days of full use after the expiry.</summary>
|
||||
public int GracePeriodDays { get; set; }
|
||||
|
||||
/// <summary>Whether the licence counts (a removed licence stays for the history).</summary>
|
||||
public bool IsActive { get; set; } = true;
|
||||
}
|
||||
|
||||
/// <summary>One step in the history of licences (<c>sys_license_activations</c>).</summary>
|
||||
public sealed class LicenseActivation : Entity<long>
|
||||
{
|
||||
/// <summary>What happened: <c>activated</c>, <c>deactivated</c>, <c>rejected</c>, <c>verified</c>, <c>expired</c>.</summary>
|
||||
public string Action { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>The licence source.</summary>
|
||||
public LicenseSource Source { get; set; }
|
||||
|
||||
/// <summary>The modules concerned, comma separated.</summary>
|
||||
public string? Modules { get; set; }
|
||||
|
||||
/// <summary>The outcome code, for example <c>ok</c>, <c>license_test_only</c>, <c>signature_invalid</c>.</summary>
|
||||
public string Outcome { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>A short explanation.</summary>
|
||||
public string? Message { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>An ASP.NET Core DataProtection key kept in the system database (<c>sys_data_protection_keys</c>).</summary>
|
||||
public sealed class DataProtectionKey : Entity<long>
|
||||
{
|
||||
/// <summary>The key name given by DataProtection.</summary>
|
||||
public string FriendlyName { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>The key XML. The key material inside is encrypted with the instance key.</summary>
|
||||
public string Xml { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>One event of the application log (<c>sys_log</c>, SPEC §9.5).</summary>
|
||||
public sealed class LogEntry : IEntity<long>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public long Id { get; set; }
|
||||
|
||||
/// <summary>When the event happened (UTC).</summary>
|
||||
public DateTime At { get; set; }
|
||||
|
||||
/// <summary>The level: Trace, Debug, Information, Warning, Error, Critical.</summary>
|
||||
public string Level { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>The section (SPEC §9.5), for example <c>Authentication</c>.</summary>
|
||||
public string Section { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>The logger category.</summary>
|
||||
public string Source { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>The message.</summary>
|
||||
public string Message { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>The exception with its stack, if any.</summary>
|
||||
public string? Exception { get; set; }
|
||||
|
||||
/// <summary>The user name.</summary>
|
||||
public string? UserName { get; set; }
|
||||
|
||||
/// <summary>The company code.</summary>
|
||||
public string? CompanyCode { get; set; }
|
||||
|
||||
/// <summary>The trace identifier shown to the user as the error code.</summary>
|
||||
public string? TraceId { get; set; }
|
||||
|
||||
/// <summary>The request (method and path).</summary>
|
||||
public string? Request { get; set; }
|
||||
|
||||
/// <summary>The entity concerned.</summary>
|
||||
public string? Entity { get; set; }
|
||||
|
||||
/// <summary>The identifier of the entity concerned.</summary>
|
||||
public string? EntityId { get; set; }
|
||||
|
||||
/// <summary>Further structured data as JSON.</summary>
|
||||
public string? Data { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A security or instance action (<c>sys_security_audit</c>): sign-ins, permissions, licences, databases, maintenance.
|
||||
/// Rows are never updated; each one carries the hash of the previous row, so a removed or changed row breaks the chain.
|
||||
/// </summary>
|
||||
public sealed class SecurityAuditEntry : IEntity<long>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public long Id { get; set; }
|
||||
|
||||
/// <summary>When it happened (UTC).</summary>
|
||||
public DateTime At { get; set; }
|
||||
|
||||
/// <summary>The action code, for example <c>login.succeeded</c>.</summary>
|
||||
public string Action { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>Who acted.</summary>
|
||||
public string? UserName { get; set; }
|
||||
|
||||
/// <summary>The company concerned.</summary>
|
||||
public string? CompanyCode { get; set; }
|
||||
|
||||
/// <summary>The client address.</summary>
|
||||
public string? IpAddress { get; set; }
|
||||
|
||||
/// <summary>What the action was about (a user, a module, a database...).</summary>
|
||||
public string? Target { get; set; }
|
||||
|
||||
/// <summary>Details as JSON (never secrets).</summary>
|
||||
public string? Details { get; set; }
|
||||
|
||||
/// <summary>The hash of the previous row (empty for the first).</summary>
|
||||
public string PreviousHash { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>SHA-256 of this row's content and <see cref="PreviousHash"/>.</summary>
|
||||
public string Hash { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>A change to company data (<c>audit_log</c>, SPEC §7.3), shown in the "Storico" of a record.</summary>
|
||||
public sealed class AuditEntry : IEntity<long>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public long Id { get; set; }
|
||||
|
||||
/// <summary>When it happened (UTC).</summary>
|
||||
public DateTime At { get; set; }
|
||||
|
||||
/// <summary>The entity (table) changed.</summary>
|
||||
public string Entity { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>The identifier of the record.</summary>
|
||||
public string EntityId { get; set; } = string.Empty;
|
||||
|
||||
/// <summary><c>insert</c>, <c>update</c> or <c>delete</c>.</summary>
|
||||
public string Action { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>Who changed it.</summary>
|
||||
public string? UserName { get; set; }
|
||||
|
||||
/// <summary>The changed fields as JSON: <c>{"field": {"old": ..., "new": ...}}</c>.</summary>
|
||||
public string? Diff { get; set; }
|
||||
|
||||
/// <summary>The trace of the request that made the change.</summary>
|
||||
public string? TraceId { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>The outcome of a job run.</summary>
|
||||
public enum JobOutcome
|
||||
{
|
||||
/// <summary>Still running.</summary>
|
||||
Running = 0,
|
||||
|
||||
/// <summary>Completed.</summary>
|
||||
Succeeded = 1,
|
||||
|
||||
/// <summary>Failed after the retries.</summary>
|
||||
Failed = 2,
|
||||
|
||||
/// <summary>Stopped by the timeout.</summary>
|
||||
TimedOut = 3,
|
||||
|
||||
/// <summary>Not started because another run held the lock.</summary>
|
||||
Skipped = 4,
|
||||
}
|
||||
|
||||
/// <summary>A scheduled task (<c>sys_scheduled_tasks</c>): a system job or an activity set up by administrators.</summary>
|
||||
public sealed class ScheduledTask : Entity<long>
|
||||
{
|
||||
/// <summary>The task code, unique, for example <c>system.log-retention</c>.</summary>
|
||||
public string Code { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>The name shown in the interface.</summary>
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>The job type that runs it.</summary>
|
||||
public string JobType { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>The company it works on; <see langword="null"/> for the instance.</summary>
|
||||
public string? CompanyCode { get; set; }
|
||||
|
||||
/// <summary>The cron expression (five fields).</summary>
|
||||
public string Cron { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>The time zone of the cron expression (IANA name).</summary>
|
||||
public string TimeZone { get; set; } = "Europe/Rome";
|
||||
|
||||
/// <summary>The longest a run may last, in seconds.</summary>
|
||||
public int TimeoutSeconds { get; set; } = 600;
|
||||
|
||||
/// <summary>How many times a failed run is retried.</summary>
|
||||
public int MaxRetries { get; set; }
|
||||
|
||||
/// <summary>Whether the task runs.</summary>
|
||||
public bool IsEnabled { get; set; } = true;
|
||||
|
||||
/// <summary>A system job, which administrators can suspend but not delete.</summary>
|
||||
public bool IsSystem { get; set; }
|
||||
|
||||
/// <summary>The parameters as JSON.</summary>
|
||||
public string? Parameters { get; set; }
|
||||
|
||||
/// <summary>The last start.</summary>
|
||||
public DateTime? LastRunAt { get; set; }
|
||||
|
||||
/// <summary>The next planned start.</summary>
|
||||
public DateTime? NextRunAt { get; set; }
|
||||
|
||||
/// <summary>The outcome of the last run.</summary>
|
||||
public JobOutcome? LastOutcome { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>One run of a scheduled task (<c>sys_job_runs</c>).</summary>
|
||||
public sealed class JobRun : IEntity<long>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public long Id { get; set; }
|
||||
|
||||
/// <summary>The task code.</summary>
|
||||
public string TaskCode { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>When the run started (UTC).</summary>
|
||||
public DateTime StartedAt { get; set; }
|
||||
|
||||
/// <summary>When the run ended.</summary>
|
||||
public DateTime? FinishedAt { get; set; }
|
||||
|
||||
/// <summary>The outcome.</summary>
|
||||
public JobOutcome Outcome { get; set; }
|
||||
|
||||
/// <summary>The attempt number, from 1.</summary>
|
||||
public int Attempt { get; set; } = 1;
|
||||
|
||||
/// <summary>A short result or error message.</summary>
|
||||
public string? Message { get; set; }
|
||||
|
||||
/// <summary>The trace identifier of the run, to find its log.</summary>
|
||||
public string? TraceId { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A lock held by a job run (<c>sys_job_locks</c>): a row per running task, with an expiry so that a process that died
|
||||
/// does not block the task for ever.
|
||||
/// </summary>
|
||||
public sealed class JobLock : IEntity<string>
|
||||
{
|
||||
/// <summary>The lock key (the task code).</summary>
|
||||
public string Id { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>The process that holds the lock.</summary>
|
||||
public string Owner { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>When the lock was taken.</summary>
|
||||
public DateTime AcquiredAt { get; set; }
|
||||
|
||||
/// <summary>When the lock lapses.</summary>
|
||||
public DateTime ExpiresAt { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>The weight of a notification.</summary>
|
||||
public enum NotificationSeverity
|
||||
{
|
||||
/// <summary>Information.</summary>
|
||||
Info = 0,
|
||||
|
||||
/// <summary>Something completed.</summary>
|
||||
Success = 1,
|
||||
|
||||
/// <summary>Needs attention.</summary>
|
||||
Warning = 2,
|
||||
|
||||
/// <summary>Something failed.</summary>
|
||||
Error = 3,
|
||||
}
|
||||
|
||||
/// <summary>An in-app notification (SPEC §9; mockup 010).</summary>
|
||||
public abstract class NotificationRecord : Entity<long>
|
||||
{
|
||||
/// <summary>The recipient; <see langword="null"/> for every user of the scope (all administrators, all users).</summary>
|
||||
public long? RecipientUserId { get; set; }
|
||||
|
||||
/// <summary>The area: <c>security</c>, <c>jobs</c>, <c>system</c>, <c>licenses</c>...</summary>
|
||||
public string Category { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>The weight.</summary>
|
||||
public NotificationSeverity Severity { get; set; }
|
||||
|
||||
/// <summary>The title.</summary>
|
||||
public string Title { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>The text.</summary>
|
||||
public string? Body { get; set; }
|
||||
|
||||
/// <summary>A link inside the application.</summary>
|
||||
public string? Link { get; set; }
|
||||
|
||||
/// <summary>When the recipient read it.</summary>
|
||||
public DateTime? ReadAt { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>A notification for instance accounts (<c>sys_notifications</c>).</summary>
|
||||
public sealed class SystemNotification : NotificationRecord;
|
||||
|
||||
/// <summary>A notification for company users (<c>notifications</c>).</summary>
|
||||
public sealed class Notification : NotificationRecord;
|
||||
Reference in new issue
Block a user