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,115 @@
|
||||
namespace Khaemhat.Application.Configuration;
|
||||
|
||||
/// <summary>
|
||||
/// Reads the <c>KHAEMHAT_*</c> variables of SPEC §6 into <see cref="KhaemhatOptions"/>, applying the <c>_FILE</c>
|
||||
/// rule of ADR 0014 to the sensitive ones: when <c>X_FILE</c> is set, the value of <c>X</c> is the content of that file
|
||||
/// without the final line break; setting both is a configuration error, and so is a file that cannot be read.
|
||||
/// Problems are collected in <see cref="KhaemhatOptions.ReadErrors"/>; values never appear in messages.
|
||||
/// </summary>
|
||||
public sealed class EnvironmentConfigurationReader
|
||||
{
|
||||
/// <summary>The variables that accept a <c>_FILE</c> variant (ADR 0014).</summary>
|
||||
public static readonly IReadOnlySet<string> FileVariables = new HashSet<string>(StringComparer.Ordinal)
|
||||
{
|
||||
"KHAEMHAT_SYSTEM_DB", "KHAEMHAT_DB_SERVER", "KHAEMHAT_SECRET_KEY", "KHAEMHAT_SECRET_KEY_NEW",
|
||||
"KHAEMHAT_SUPERUSER", "KHAEMHAT_SUPERUSER_PASSWORD", "KHAEMHAT_SUPERUSER_EMAIL", "KHAEMHAT_LICENSE",
|
||||
"KHAEMHAT_REDIS", "KHAEMHAT_SMTP_PASSWORD", "KHAEMHAT_UPDATE_TOKEN",
|
||||
};
|
||||
|
||||
private readonly Func<string, string?> _lookup;
|
||||
private readonly Func<string, string> _readFile;
|
||||
|
||||
/// <summary>Creates a reader.</summary>
|
||||
/// <param name="lookup">Returns a variable, or <see langword="null"/> when unset (the host configuration: environment and command line).</param>
|
||||
/// <param name="readFile">Reads a secret file; the default reads from disk.</param>
|
||||
public EnvironmentConfigurationReader(Func<string, string?> lookup, Func<string, string>? readFile = null)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(lookup);
|
||||
_lookup = lookup;
|
||||
_readFile = readFile ?? File.ReadAllText;
|
||||
}
|
||||
|
||||
/// <summary>Reads every variable.</summary>
|
||||
/// <returns>The options, with the reading problems in <see cref="KhaemhatOptions.ReadErrors"/>.</returns>
|
||||
public KhaemhatOptions Read()
|
||||
{
|
||||
var errors = new List<string>();
|
||||
string? Get(string name) => Value(name, errors);
|
||||
|
||||
return new KhaemhatOptions
|
||||
{
|
||||
SystemDb = Get("KHAEMHAT_SYSTEM_DB"),
|
||||
DbServer = Get("KHAEMHAT_DB_SERVER"),
|
||||
SecretKey = Get("KHAEMHAT_SECRET_KEY"),
|
||||
RotationSecretKey = Get("KHAEMHAT_SECRET_KEY_NEW"),
|
||||
SuperUser = Get("KHAEMHAT_SUPERUSER"),
|
||||
SuperUserPassword = Get("KHAEMHAT_SUPERUSER_PASSWORD"),
|
||||
SuperUserEmail = Get("KHAEMHAT_SUPERUSER_EMAIL"),
|
||||
CompanyCode = Get("KHAEMHAT_COMPANY_CODE"),
|
||||
CompanyName = Get("KHAEMHAT_COMPANY_NAME"),
|
||||
Country = Get("KHAEMHAT_COUNTRY") ?? "IT",
|
||||
LicenseMode = Get("KHAEMHAT_LICENSE_MODE") ?? "production",
|
||||
License = Get("KHAEMHAT_LICENSE"),
|
||||
Redis = Get("KHAEMHAT_REDIS"),
|
||||
DataDir = Get("KHAEMHAT_DATA_DIR") ?? "/data",
|
||||
Urls = Get("KHAEMHAT_URLS"),
|
||||
PublicUrl = Get("KHAEMHAT_PUBLIC_URL"),
|
||||
SmtpHost = Get("KHAEMHAT_SMTP_HOST"),
|
||||
SmtpPort = Get("KHAEMHAT_SMTP_PORT"),
|
||||
SmtpUser = Get("KHAEMHAT_SMTP_USER"),
|
||||
SmtpPassword = Get("KHAEMHAT_SMTP_PASSWORD"),
|
||||
SmtpFrom = Get("KHAEMHAT_SMTP_FROM"),
|
||||
SmtpSecurity = Get("KHAEMHAT_SMTP_SECURITY"),
|
||||
LogLevel = Get("KHAEMHAT_LOG_LEVEL"),
|
||||
SeedBase = Get("KHAEMHAT_SEED_BASE"),
|
||||
SeedDemo = Get("KHAEMHAT_SEED_DEMO"),
|
||||
UpdateFeed = Get("KHAEMHAT_UPDATE_FEED"),
|
||||
UpdateToken = Get("KHAEMHAT_UPDATE_TOKEN"),
|
||||
TimeZone = Get("TZ"),
|
||||
ReadErrors = errors,
|
||||
};
|
||||
}
|
||||
|
||||
private string? Value(string name, List<string> errors)
|
||||
{
|
||||
var direct = Normalize(_lookup(name));
|
||||
if (!FileVariables.Contains(name))
|
||||
{
|
||||
return direct;
|
||||
}
|
||||
|
||||
var path = Normalize(_lookup(name + "_FILE"));
|
||||
if (path is null)
|
||||
{
|
||||
return direct;
|
||||
}
|
||||
|
||||
if (direct is not null)
|
||||
{
|
||||
errors.Add($"{name} and {name}_FILE are both set: keep only one of them.");
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return Normalize(TrimFinalLineBreak(_readFile(path)));
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or ArgumentException or NotSupportedException)
|
||||
{
|
||||
errors.Add($"{name}_FILE points to a file that cannot be read ({ex.GetType().Name}): check the path and the permissions of the secret.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static string? Normalize(string? value) => string.IsNullOrWhiteSpace(value) ? null : value;
|
||||
|
||||
private static string TrimFinalLineBreak(string value)
|
||||
{
|
||||
if (value.EndsWith("\r\n", StringComparison.Ordinal))
|
||||
{
|
||||
return value[..^2];
|
||||
}
|
||||
|
||||
return value.EndsWith('\n') ? value[..^1] : value;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user