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 <noreply@anthropic.com>
This commit is contained in:
2026-11-08 20:40:50 +01:00
co-authored by Claude Sonnet 5.5
parent 57e77edcf9
commit 142815a3ab
8 changed files with 171 additions and 4 deletions
+1 -1
View File
@@ -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.
@@ -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.
@@ -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.
@@ -28,7 +28,9 @@ public class DeployedConfigurationTests
public static TheoryData<string> 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);
}
/// <summary>
/// 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 <c>App:PublicBaseUrl</c> only then).
/// </summary>
[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<IStartupValidator>().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<string, string?> { ["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<IStartupValidator>().Validate();
// With a host but no App__PublicBaseUrl the host refuses to start, and says which setting is missing.
act.Should().Throw<OptionsValidationException>().WithMessage("*App:PublicBaseUrl*");
}
}
internal static class ConfigurationTestExtensions
{
public static bool IsEmailEnabledForTest(this IConfiguration configuration) =>
SlpModularCms.Core.Email.EmailServiceCollectionExtensions.IsEmailEnabled(configuration);
}
@@ -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;
/// <summary>
/// <c>Website__LogLevel</c> sets the level of the website's logger category. The per-category override
/// <c>Logging__LogLevel__SlpModularCms.Website</c> 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.
/// </summary>
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>(TState state) where TState : notnull => null;
public bool IsEnabled(LogLevel logLevel) => true;
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) { }
}
}
private static ILogger LoggerFor(Dictionary<string, string?> 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<ILoggerFactory>();
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<string, string?> { ["Website:LogLevel"] = "loud" }).Build();
var services = new ServiceCollection();
services.AddLogging();
services.AddCmsWebsite(config);
using var sp = services.BuildServiceProvider();
var act = () => sp.GetRequiredService<IOptions<WebsiteOptions>>().Value;
act.Should().Throw<OptionsValidationException>().WithMessage("*Website:LogLevel*");
}
}
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
@@ -41,6 +41,8 @@
<ItemGroup>
<Content Include="..\SlpModularCms.Api\appsettings.json" Link="host-config\master.appsettings.json" CopyToOutputDirectory="PreserveNewest" />
<Content Include="..\SlpModularCms.Api.Slave\appsettings.json" Link="host-config\slave.appsettings.json" CopyToOutputDirectory="PreserveNewest" />
<Content Include="..\SlpModularCms.Api.SlpSoftware\appsettings.json" Link="host-config\slpsoftware.appsettings.json" CopyToOutputDirectory="PreserveNewest" />
<Content Include="..\SlpModularCms.Api.AndreaSlopFotografie\appsettings.json" Link="host-config\andreaslop.appsettings.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
</Project>
@@ -22,8 +22,20 @@ public static class PrerenderedSiteExtensions
{
services.AddOptions<WebsiteOptions>()
.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<LogLevel>(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<WebsiteHostingMarker>();
@@ -10,4 +10,15 @@ public sealed class WebsiteOptions
/// <c>X-Robots-Tag: noindex, nofollow</c> and <c>/robots.txt</c> disallows everything.
/// </summary>
public bool NoIndex { get; set; }
/// <summary>
/// Log level of the website's own logger category (<c>SlpModularCms.Website</c>: 301s and 404s at
/// Information), set as <c>Website__LogLevel</c>. Needed because the usual per-category override
/// <c>Logging__LogLevel__SlpModularCms.Website</c> cannot be set from a systemd EnvironmentFile (a
/// variable name containing a dot is ignored there). Empty = leave it to <c>Logging:LogLevel</c>.
/// </summary>
public string? LogLevel { get; set; }
public static bool IsValidLogLevel(string? value) =>
string.IsNullOrWhiteSpace(value) || Enum.TryParse<Microsoft.Extensions.Logging.LogLevel>(value.Trim(), ignoreCase: true, out _);
}