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,210 @@
|
||||
using Khaemhat.Core.Instance;
|
||||
using Khaemhat.Core.Security;
|
||||
using Khaemhat.Data.Mapping;
|
||||
|
||||
namespace Khaemhat.Data.SystemDatabase;
|
||||
|
||||
/// <summary>The account columns shared by <c>sys_users</c> and <c>users</c>.</summary>
|
||||
internal static class AccountColumns
|
||||
{
|
||||
public static void Configure<T>(EntityMap<T> map)
|
||||
where T : class, IAccount, new()
|
||||
{
|
||||
map.Map(x => x.UserName).MaxLength(100);
|
||||
map.Map(x => x.DisplayName).MaxLength(200);
|
||||
map.Map(x => x.Email).MaxLength(254);
|
||||
map.Map(x => x.PasswordHash).MaxLength(200);
|
||||
map.Map(x => x.PasswordHistory).Json();
|
||||
map.Map(x => x.TotpRecoveryCodes).Json();
|
||||
map.Map(x => x.RecoveryTokenHash).MaxLength(64);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="SystemUser"/> to <c>sys_users</c>.</summary>
|
||||
public sealed class SystemUserMap : EntityMap<SystemUser>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public SystemUserMap()
|
||||
{
|
||||
ToTable("sys_users");
|
||||
AccountColumns.Configure(this);
|
||||
Map(x => x.LinkedCompanyCode).MaxLength(20);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="SystemSession"/> to <c>sys_sessions</c>.</summary>
|
||||
public sealed class SystemSessionMap : EntityMap<SystemSession>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public SystemSessionMap()
|
||||
{
|
||||
ToTable("sys_sessions");
|
||||
SessionColumns.Configure(this);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The session columns shared by <c>sys_sessions</c> and <c>user_sessions</c>.</summary>
|
||||
internal static class SessionColumns
|
||||
{
|
||||
public static void Configure<T>(EntityMap<T> map)
|
||||
where T : SessionRecord, new()
|
||||
{
|
||||
map.Map(x => x.TokenHash).MaxLength(64);
|
||||
map.Map(x => x.CompanyCode).MaxLength(20);
|
||||
map.Map(x => x.IpAddress).MaxLength(64);
|
||||
map.Map(x => x.UserAgent).MaxLength(400);
|
||||
map.Map(x => x.RevokedReason).MaxLength(100);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="RoleTemplate"/> to <c>sys_roles_templates</c>.</summary>
|
||||
public sealed class RoleTemplateMap : EntityMap<RoleTemplate>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public RoleTemplateMap()
|
||||
{
|
||||
ToTable("sys_roles_templates");
|
||||
Map(x => x.Code).MaxLength(50);
|
||||
Map(x => x.Name).MaxLength(100);
|
||||
Map(x => x.Permissions).Json();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="LicenseRecord"/> to <c>sys_licenses</c>.</summary>
|
||||
public sealed class LicenseRecordMap : EntityMap<LicenseRecord>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public LicenseRecordMap()
|
||||
{
|
||||
ToTable("sys_licenses");
|
||||
Map(x => x.Licensee).MaxLength(200);
|
||||
Map(x => x.Modules).MaxLength(400);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="LicenseActivation"/> to <c>sys_license_activations</c>.</summary>
|
||||
public sealed class LicenseActivationMap : EntityMap<LicenseActivation>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public LicenseActivationMap()
|
||||
{
|
||||
ToTable("sys_license_activations");
|
||||
Map(x => x.Action).MaxLength(40);
|
||||
Map(x => x.Modules).MaxLength(400);
|
||||
Map(x => x.Outcome).MaxLength(60);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="DataProtectionKey"/> to <c>sys_data_protection_keys</c>.</summary>
|
||||
public sealed class DataProtectionKeyMap : EntityMap<DataProtectionKey>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public DataProtectionKeyMap()
|
||||
{
|
||||
ToTable("sys_data_protection_keys");
|
||||
Map(x => x.FriendlyName).MaxLength(200);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="LogEntry"/> to <c>sys_log</c>.</summary>
|
||||
public sealed class LogEntryMap : EntityMap<LogEntry>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public LogEntryMap()
|
||||
{
|
||||
ToTable("sys_log");
|
||||
Map(x => x.Level).MaxLength(20);
|
||||
Map(x => x.Section).MaxLength(40);
|
||||
Map(x => x.Source).MaxLength(200);
|
||||
Map(x => x.UserName).MaxLength(100);
|
||||
Map(x => x.CompanyCode).MaxLength(20);
|
||||
Map(x => x.TraceId).MaxLength(64);
|
||||
Map(x => x.Request).MaxLength(400);
|
||||
Map(x => x.Entity).MaxLength(100);
|
||||
Map(x => x.EntityId).MaxLength(100);
|
||||
Map(x => x.Data).Json();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="SecurityAuditEntry"/> to <c>sys_security_audit</c>.</summary>
|
||||
public sealed class SecurityAuditEntryMap : EntityMap<SecurityAuditEntry>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public SecurityAuditEntryMap()
|
||||
{
|
||||
ToTable("sys_security_audit");
|
||||
Map(x => x.Action).MaxLength(100);
|
||||
Map(x => x.UserName).MaxLength(100);
|
||||
Map(x => x.CompanyCode).MaxLength(20);
|
||||
Map(x => x.IpAddress).MaxLength(64);
|
||||
Map(x => x.Target).MaxLength(200);
|
||||
Map(x => x.Details).Json();
|
||||
Map(x => x.PreviousHash).MaxLength(64);
|
||||
Map(x => x.Hash).MaxLength(64);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="ScheduledTask"/> to <c>sys_scheduled_tasks</c>.</summary>
|
||||
public sealed class ScheduledTaskMap : EntityMap<ScheduledTask>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public ScheduledTaskMap()
|
||||
{
|
||||
ToTable("sys_scheduled_tasks");
|
||||
Map(x => x.Code).MaxLength(100);
|
||||
Map(x => x.Name).MaxLength(200);
|
||||
Map(x => x.JobType).MaxLength(100);
|
||||
Map(x => x.CompanyCode).MaxLength(20);
|
||||
Map(x => x.Cron).MaxLength(100);
|
||||
Map(x => x.TimeZone).MaxLength(64);
|
||||
Map(x => x.Parameters).Json();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="JobRun"/> to <c>sys_job_runs</c>.</summary>
|
||||
public sealed class JobRunMap : EntityMap<JobRun>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public JobRunMap()
|
||||
{
|
||||
ToTable("sys_job_runs");
|
||||
Map(x => x.TaskCode).MaxLength(100);
|
||||
Map(x => x.TraceId).MaxLength(64);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="JobLock"/> to <c>sys_job_locks</c>.</summary>
|
||||
public sealed class JobLockMap : EntityMap<JobLock>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public JobLockMap()
|
||||
{
|
||||
ToTable("sys_job_locks");
|
||||
HasKey(x => x.Id, identity: false);
|
||||
Map(x => x.Id).MaxLength(100);
|
||||
Map(x => x.Owner).MaxLength(100);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Map of <see cref="SystemNotification"/> to <c>sys_notifications</c>.</summary>
|
||||
public sealed class SystemNotificationMap : EntityMap<SystemNotification>
|
||||
{
|
||||
/// <summary>Creates the map.</summary>
|
||||
public SystemNotificationMap()
|
||||
{
|
||||
ToTable("sys_notifications");
|
||||
NotificationColumns.Configure(this);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The notification columns shared by <c>sys_notifications</c> and <c>notifications</c>.</summary>
|
||||
internal static class NotificationColumns
|
||||
{
|
||||
public static void Configure<T>(EntityMap<T> map)
|
||||
where T : NotificationRecord, new()
|
||||
{
|
||||
map.Map(x => x.Category).MaxLength(40);
|
||||
map.Map(x => x.Title).MaxLength(200);
|
||||
map.Map(x => x.Link).MaxLength(400);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user