From 142815a3abc303bd35f0477a61614326fb880ac4 Mon Sep 17 00:00:00 2001 From: Sluijsens Date: Sun, 8 Nov 2026 20:40:50 +0100 Subject: [PATCH] Opstart zonder mailconfiguratie getest voor alle hosts, Website__LogLevel zonder punt in de naam Oorzaak 502 op ASF test: Email__Smtp__Host stond al in de omgeving maar App__PublicBaseUrl nog niet; de startvalidatie weigert zo'n halve mailconfiguratie. Een host zonder enige mailinstelling start gewoon: nu voor master, slave, SlpSoftware en ASF getest. Website__LogLevel vervangt de Logging__LogLevel__SlpModularCms.Website-variabele, waarvan systemd de naam met punt negeert. Co-Authored-By: Claude Sonnet 5.5 --- README.md | 2 +- .../infrastructure-design.md | 2 +- .../infrastructure-design.md | 6 ++ .../Hosting/DeployedConfigurationTests.cs | 49 +++++++++- .../Hosting/Website/WebsiteLogLevelTests.cs | 89 +++++++++++++++++++ .../SlpModularCms.Core.Tests.csproj | 4 +- .../Website/PrerenderedSiteExtensions.cs | 12 +++ .../Hosting/Website/WebsiteOptions.cs | 11 +++ 8 files changed, 171 insertions(+), 4 deletions(-) create mode 100644 src/SlpModularCms.Core.Tests/Hosting/Website/WebsiteLogLevelTests.cs diff --git a/README.md b/README.md index 502bf68..a7b2831 100644 --- a/README.md +++ b/README.md @@ -345,7 +345,7 @@ In productie moeten gevoelige instellingen worden doorgegeven via Environment Va - `Observability__SentryDsn` — leeg is een normale, ondersteunde staat: Sentry wordt dan overgeslagen en alleen console-logging blijft actief - `SecurityHeaders__AllowedScriptOrigins__0`, `SecurityHeaders__AllowedConnectOrigins__0`, ... — externe origins die de CSP van `/admin` en `/api/v1` mag toestaan (bijv. de Umami-scripthost en Sentry's ingest-endpoint). Zie `appsettings.json → SecurityHeaders` voor de volledige optiesvorm; een onbekende `PathPolicies`-policy-naam laat de applicatie bij **opstarten** falen, niet pas bij de eerste request -- **Voorgerenderde website (`WebsiteMode.PrerenderedPages`)** — een client kiest dit in zijn `Program.cs` via `CmsHostOptions` (standaard `SpaFallback`: alles blijft zoals het was). Instellingen voor deze modus: `Website__NoIndex=true` (testinstantie: `X-Robots-Tag` plus `robots.txt` met `Disallow: /`), `SecurityHeaders__DefaultPolicy=StrictSite` met `SecurityHeaders__AllowedSiteScriptOrigins__0`, `SecurityHeaders__AllowedSiteConnectOrigins__0`, ... (alleen gebruikt door `StrictSite`). Logging van de website (301's en 404's op `Information`) gaat via de logger-categorie `SlpModularCms.Website`: zet per omgeving `Logging:LogLevel:SlpModularCms.Website` (test `Information`, standaard en productie `Warning`). Gedrag per pad staat in [`WEBSITE_WORKSPACE.md`](WEBSITE_WORKSPACE.md). +- **Voorgerenderde website (`WebsiteMode.PrerenderedPages`)** — een client kiest dit in zijn `Program.cs` via `CmsHostOptions` (standaard `SpaFallback`: alles blijft zoals het was). Instellingen voor deze modus: `Website__NoIndex=true` (testinstantie: `X-Robots-Tag` plus `robots.txt` met `Disallow: /`), `SecurityHeaders__DefaultPolicy=StrictSite` met `SecurityHeaders__AllowedSiteScriptOrigins__0`, `SecurityHeaders__AllowedSiteConnectOrigins__0`, ... (alleen gebruikt door `StrictSite`). Logging van de website (301's en 404's op `Information`) gaat via de logger-categorie `SlpModularCms.Website`: zet per omgeving `Website__LogLevel` (test `Information`, standaard en productie `Warning`; leeg = `Logging:LogLevel` beslist). Gebruik niet `Logging__LogLevel__SlpModularCms.Website` in een systemd-`EnvironmentFile`: een variabelenaam met een punt wordt daar genegeerd. Gedrag per pad staat in [`WEBSITE_WORKSPACE.md`](WEBSITE_WORKSPACE.md). ### 2a. Data Protection key ring (Master CMS Module) De API keys van geregistreerde slaves worden versleuteld opgeslagen met ASP.NET Core Data Protection. De key ring wordt **automatisch** persistent opgeslagen in de database (`PersistKeysToDbContext`, met een expliciete, stabiele applicatie-discriminator) — dit hoeft niet meer apart geconfigureerd te worden. Dit is bewust zo gebouwd omdat de atomische release-switch (zie hierboven) bij elke deploy een nieuwe content-root-map gebruikt: zonder een database-backed key ring zou dat elke keer de key ring weggooien en alle opgeslagen slave-API-keys onleesbaar maken. diff --git a/aidlc-docs/features/andreaslop-site-hosting/construction/andreaslop-client/infrastructure-design/infrastructure-design.md b/aidlc-docs/features/andreaslop-site-hosting/construction/andreaslop-client/infrastructure-design/infrastructure-design.md index 224a969..5e6ff9d 100644 --- a/aidlc-docs/features/andreaslop-site-hosting/construction/andreaslop-client/infrastructure-design/infrastructure-design.md +++ b/aidlc-docs/features/andreaslop-site-hosting/construction/andreaslop-client/infrastructure-design/infrastructure-design.md @@ -45,7 +45,7 @@ apps/andreaslopfotografie/test/ | `Observability__SentryDsn` | the website's existing Sentry DSN (backend; the admin reports through the same-origin `/sentry-tunnel`) | | `Observability__Environment` | `test` | | `ForwardedHeaders__KnownProxies__0` | `192.168.1.102` | -| `Logging__LogLevel__SlpModularCms.Website` | `Information` (test only) | +| `Website__LogLevel` | `Information` (test only). Replaces `Logging__LogLevel__SlpModularCms.Website`: a variable name containing a dot is ignored by a systemd `EnvironmentFile`. (The committed default log level is already `Information`, so the website logs 301/404 at that level without it.) | Committed defaults (in the client's `appsettings.json`, no secrets): `SecurityHeaders` with `DefaultPolicy=StrictSite`, the site's Umami and Sentry origins, `Website:NoIndex=true` (secure default; a later production setup must set it to `false` explicitly), JWT issuer/audience, rate limiting values. diff --git a/aidlc-docs/features/identity-email-notifications/construction/mail-foundation/infrastructure-design/infrastructure-design.md b/aidlc-docs/features/identity-email-notifications/construction/mail-foundation/infrastructure-design/infrastructure-design.md index 6afc673..9798c6c 100644 --- a/aidlc-docs/features/identity-email-notifications/construction/mail-foundation/infrastructure-design/infrastructure-design.md +++ b/aidlc-docs/features/identity-email-notifications/construction/mail-foundation/infrastructure-design/infrastructure-design.md @@ -48,3 +48,9 @@ Read-only checks done from the developer PC (by "CMS & API"): `mail.andreaslop.n 2. Port and TLS: not yet confirmed that the server offers STARTTLS on 587 (`Email__Smtp__EnableSsl=true`); if it needs implicit TLS (465) or none, the sender must be adapted (it supports STARTTLS or plain today) and a plain connection would not be acceptable beyond a private network. 3. DKIM: a `dkim._domainkey.andreaslop.nl` record exists; confirm the server signs with that key. DMARC is `p=none` (monitoring only). 4. Credentials for that account go into the ASF test unit's `shared/env` by whoever manages the host (not by this feature's code or by chat). + +## Release notes for the host manager (required environment variables per release) +- **This release adds NO required environment variable.** A host without any mail setting starts exactly as before; this is tested for every deployed host configuration (master, slave, SlpSoftware, ASF). +- Mail is switched on only by setting `Email__Smtp__Host`. **Once a host is set, all of these must be set too, or the host refuses to start** (502 behind the proxy) with a message naming the missing setting: `App__PublicBaseUrl` (https, no path), `Email__FromAddress`/`Email__FromName` (have committed defaults), and `Email__Smtp__Username` together with `Email__Smtp__Password`. Set `App__PublicBaseUrl` BEFORE or together with `Email__Smtp__Host`. +- Optional: `Identity__Invitation__ExpiryHours` (24), `Identity__PasswordReset__TokenLifetimeMinutes` (60), `Identity__PasswordReset__CooldownMinutes` (5), `App__DefaultLanguage` (nl), `Email__AllowedFromAddresses__0..n`, `Website__LogLevel`. +- Incident 2026-11-08: ASF test returned 502 after a restart because `Email__Smtp__Host` was set but `App__PublicBaseUrl` was not (start-up validation, by design: a half-configured mail setup is refused rather than silently ignored). Not a deploy without mail configuration. A softer behaviour (start with mail switched off and an error log) is a possible change, see the open question to Bryan. diff --git a/src/SlpModularCms.Core.Tests/Hosting/DeployedConfigurationTests.cs b/src/SlpModularCms.Core.Tests/Hosting/DeployedConfigurationTests.cs index c7428b5..a796bbe 100644 --- a/src/SlpModularCms.Core.Tests/Hosting/DeployedConfigurationTests.cs +++ b/src/SlpModularCms.Core.Tests/Hosting/DeployedConfigurationTests.cs @@ -28,7 +28,9 @@ public class DeployedConfigurationTests public static TheoryData HostConfigurations => new() { Path.Combine("host-config", "master.appsettings.json"), - Path.Combine("host-config", "slave.appsettings.json") + Path.Combine("host-config", "slave.appsettings.json"), + Path.Combine("host-config", "slpsoftware.appsettings.json"), + Path.Combine("host-config", "andreaslop.appsettings.json") }; private static IConfigurationRoot Load(string relativePath) @@ -130,4 +132,49 @@ public class DeployedConfigurationTests options.PathPolicies .Should().Contain(rule => rule.PathPrefix == "/admin" && rule.Policy == CspPolicyCatalog.Strict); } + + /// + /// A deploy without any mail configuration must never fail to start: the mail validation only runs + /// when an SMTP host is set (and so needs App:PublicBaseUrl only then). + /// + [Theory] + [MemberData(nameof(HostConfigurations))] + public void HostConfiguration_StartsWithoutAnyMailSettings(string relativePath) + { + var configuration = Load(relativePath); + var services = new ServiceCollection(); + services.AddLogging(); + SlpModularCms.Core.Email.EmailServiceCollectionExtensions.AddCmsEmail(services, configuration); + using var sp = services.BuildServiceProvider(); + + var act = () => sp.GetRequiredService().Validate(); + + act.Should().NotThrow(); + configuration.IsEmailEnabledForTest().Should().BeFalse(); + } + + [Theory] + [MemberData(nameof(HostConfigurations))] + public void HostConfiguration_NeedsPublicBaseUrl_OnlyOnceAnSmtpHostIsSet(string relativePath) + { + var configuration = new ConfigurationBuilder() + .AddJsonFile(relativePath, optional: false) + .AddInMemoryCollection(new Dictionary { ["Email:Smtp:Host"] = "mail.example.nl" }) + .Build(); + var services = new ServiceCollection(); + services.AddLogging(); + SlpModularCms.Core.Email.EmailServiceCollectionExtensions.AddCmsEmail(services, configuration); + using var sp = services.BuildServiceProvider(); + + var act = () => sp.GetRequiredService().Validate(); + + // With a host but no App__PublicBaseUrl the host refuses to start, and says which setting is missing. + act.Should().Throw().WithMessage("*App:PublicBaseUrl*"); + } +} + +internal static class ConfigurationTestExtensions +{ + public static bool IsEmailEnabledForTest(this IConfiguration configuration) => + SlpModularCms.Core.Email.EmailServiceCollectionExtensions.IsEmailEnabled(configuration); } diff --git a/src/SlpModularCms.Core.Tests/Hosting/Website/WebsiteLogLevelTests.cs b/src/SlpModularCms.Core.Tests/Hosting/Website/WebsiteLogLevelTests.cs new file mode 100644 index 0000000..729d2a8 --- /dev/null +++ b/src/SlpModularCms.Core.Tests/Hosting/Website/WebsiteLogLevelTests.cs @@ -0,0 +1,89 @@ +using FluentAssertions; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using SlpModularCms.Core.Hosting.Website; + +namespace SlpModularCms.Core.Tests.Hosting.Website; + +/// +/// Website__LogLevel sets the level of the website's logger category. The per-category override +/// Logging__LogLevel__SlpModularCms.Website cannot be used from a systemd EnvironmentFile (a dot in the +/// variable name is ignored there), so this setting must work on its own, with a name without a dot. +/// +public class WebsiteLogLevelTests +{ + private sealed class ProbeProvider : ILoggerProvider + { + public ILogger CreateLogger(string categoryName) => new Probe(); + public void Dispose() { } + + private sealed class Probe : ILogger + { + public IDisposable? BeginScope(TState state) where TState : notnull => null; + public bool IsEnabled(LogLevel logLevel) => true; + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) { } + } + } + + private static ILogger LoggerFor(Dictionary values) + { + var config = new ConfigurationBuilder().AddInMemoryCollection(values).Build(); + var services = new ServiceCollection(); + services.AddLogging(b => b.AddConfiguration(config.GetSection("Logging")).AddProvider(new ProbeProvider())); + services.AddCmsWebsite(config); + var factory = services.BuildServiceProvider().GetRequiredService(); + return factory.CreateLogger(PrerenderedSiteMiddleware.LoggerCategory); + } + + [Fact] + public void WebsiteLogLevel_Warning_SilencesInformationEvenWhenTheDefaultIsInformation() + { + var logger = LoggerFor(new() { ["Logging:LogLevel:Default"] = "Information", ["Website:LogLevel"] = "Warning" }); + + logger.IsEnabled(LogLevel.Information).Should().BeFalse(); + logger.IsEnabled(LogLevel.Warning).Should().BeTrue(); + } + + [Fact] + public void WebsiteLogLevel_Information_ShowsInformationEvenWhenTheDefaultIsWarning() + { + var logger = LoggerFor(new() { ["Logging:LogLevel:Default"] = "Warning", ["Website:LogLevel"] = "information" }); + + logger.IsEnabled(LogLevel.Information).Should().BeTrue(); + logger.IsEnabled(LogLevel.Debug).Should().BeFalse(); + } + + [Fact] + public void WithoutTheSetting_TheNormalLoggingConfigurationDecides() + { + LoggerFor(new() { ["Logging:LogLevel:Default"] = "Warning" }).IsEnabled(LogLevel.Information).Should().BeFalse(); + LoggerFor(new() { ["Logging:LogLevel:Default"] = "Information" }).IsEnabled(LogLevel.Information).Should().BeTrue(); + } + + [Theory] + [InlineData("Information", true)] + [InlineData("warning", true)] + [InlineData("None", true)] + [InlineData("", true)] + [InlineData(null, true)] + [InlineData("verbose", false)] + [InlineData("12x", false)] + public void TheLevelName_IsValidated(string? value, bool valid) => + WebsiteOptions.IsValidLogLevel(value).Should().Be(valid); + + [Fact] + public void ABadLevelName_FailsOptionsValidation_NotSilently() + { + var config = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary { ["Website:LogLevel"] = "loud" }).Build(); + var services = new ServiceCollection(); + services.AddLogging(); + services.AddCmsWebsite(config); + using var sp = services.BuildServiceProvider(); + + var act = () => sp.GetRequiredService>().Value; + + act.Should().Throw().WithMessage("*Website:LogLevel*"); + } +} diff --git a/src/SlpModularCms.Core.Tests/SlpModularCms.Core.Tests.csproj b/src/SlpModularCms.Core.Tests/SlpModularCms.Core.Tests.csproj index 30eca0f..54104f5 100644 --- a/src/SlpModularCms.Core.Tests/SlpModularCms.Core.Tests.csproj +++ b/src/SlpModularCms.Core.Tests/SlpModularCms.Core.Tests.csproj @@ -1,4 +1,4 @@ - + net10.0 @@ -41,6 +41,8 @@ + + \ No newline at end of file diff --git a/src/SlpModularCms.Core/Hosting/Website/PrerenderedSiteExtensions.cs b/src/SlpModularCms.Core/Hosting/Website/PrerenderedSiteExtensions.cs index ea3f566..f52fe64 100644 --- a/src/SlpModularCms.Core/Hosting/Website/PrerenderedSiteExtensions.cs +++ b/src/SlpModularCms.Core/Hosting/Website/PrerenderedSiteExtensions.cs @@ -22,8 +22,20 @@ public static class PrerenderedSiteExtensions { services.AddOptions() .Bind(configuration.GetSection(WebsiteOptions.SectionName)) + .Validate(o => WebsiteOptions.IsValidLogLevel(o.LogLevel), + "Website:LogLevel must be a log level name (Trace, Debug, Information, Warning, Error, Critical, None) or empty.") .ValidateOnStart(); + // Website:LogLevel (env: Website__LogLevel) sets the level of the website's logger category. It exists + // because Logging__LogLevel__SlpModularCms.Website has a dot in its name, which systemd's + // EnvironmentFile does not accept. + var configuredLevel = configuration[$"{WebsiteOptions.SectionName}:LogLevel"]; + if (!string.IsNullOrWhiteSpace(configuredLevel) + && Enum.TryParse(configuredLevel.Trim(), ignoreCase: true, out var level)) + { + services.AddLogging(logging => logging.AddFilter(PrerenderedSiteMiddleware.LoggerCategory, level)); + } + // Presence of the marker switches on the full security-header set for website responses. services.AddSingleton(); diff --git a/src/SlpModularCms.Core/Hosting/Website/WebsiteOptions.cs b/src/SlpModularCms.Core/Hosting/Website/WebsiteOptions.cs index 9864e83..073b533 100644 --- a/src/SlpModularCms.Core/Hosting/Website/WebsiteOptions.cs +++ b/src/SlpModularCms.Core/Hosting/Website/WebsiteOptions.cs @@ -10,4 +10,15 @@ public sealed class WebsiteOptions /// X-Robots-Tag: noindex, nofollow and /robots.txt disallows everything. /// public bool NoIndex { get; set; } + + /// + /// Log level of the website's own logger category (SlpModularCms.Website: 301s and 404s at + /// Information), set as Website__LogLevel. Needed because the usual per-category override + /// Logging__LogLevel__SlpModularCms.Website cannot be set from a systemd EnvironmentFile (a + /// variable name containing a dot is ignored there). Empty = leave it to Logging:LogLevel. + /// + public string? LogLevel { get; set; } + + public static bool IsValidLogLevel(string? value) => + string.IsNullOrWhiteSpace(value) || Enum.TryParse(value.Trim(), ignoreCase: true, out _); }