feat: validate OpcUa host options at startup (route through IOptions + ValidateOnStart)

This commit is contained in:
Joseph Doherty
2026-06-01 18:45:55 -04:00
parent f35ebd7aaf
commit 88e773af36
4 changed files with 104 additions and 7 deletions
@@ -0,0 +1,32 @@
using ZB.MOM.WW.Configuration;
using ZB.MOM.WW.OtOpcUa.OpcUaServer;
namespace ZB.MOM.WW.OtOpcUa.Host.Configuration;
/// <summary>
/// Fail-fast startup validator for <see cref="OpcUaApplicationHostOptions"/>, built on the
/// shared <c>ZB.MOM.WW.Configuration</c> <see cref="OptionsValidatorBase{TOptions}"/>. The C#
/// defaults are all valid, so a host with no explicit <c>"OpcUa"</c> section passes untouched;
/// the validator exists to reject explicit prod/env overrides before the OPC UA SDK boots.
/// Identity/transport essentials (<c>ApplicationName</c>, <c>ApplicationUri</c>,
/// <c>PublicHostname</c>, <c>PkiStoreRoot</c>, <c>OpcUaPort</c>) must be present/valid and at
/// least one security profile must be enabled. Optional fields — <c>ApplicationConfigPath</c>,
/// <c>PeerApplicationUris</c>, <c>AutoAcceptUntrustedClientCertificates</c>, and
/// <c>ProductUri</c> — are intentionally not validated. Failure messages carry the real
/// <c>"OpcUa:"</c> section prefix matching the bound configuration section.
/// </summary>
public sealed class OpcUaApplicationHostOptionsValidator : OptionsValidatorBase<OpcUaApplicationHostOptions>
{
/// <inheritdoc />
protected override void Validate(ValidationBuilder builder, OpcUaApplicationHostOptions o)
{
builder.Required(o.ApplicationName, "OpcUa:ApplicationName");
builder.Required(o.ApplicationUri, "OpcUa:ApplicationUri");
builder.Required(o.PublicHostname, "OpcUa:PublicHostname");
builder.Required(o.PkiStoreRoot, "OpcUa:PkiStoreRoot");
builder.Port(o.OpcUaPort, "OpcUa:OpcUaPort");
// EnabledSecurityProfiles is typed IList<T>, which does not implement IReadOnlyCollection<T>;
// ToList() bridges to the shared MinCount primitive while preserving the count (and message).
builder.MinCount(o.EnabledSecurityProfiles?.ToList(), 1, "OpcUa:EnabledSecurityProfiles");
}
}
@@ -1,6 +1,6 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
using ZB.MOM.WW.OtOpcUa.OpcUaServer;
using ZB.MOM.WW.OtOpcUa.OpcUaServer.Security;
@@ -20,7 +20,7 @@ namespace ZB.MOM.WW.OtOpcUa.Host.OpcUa;
/// </summary>
public sealed class OtOpcUaServerHostedService : IHostedService, IAsyncDisposable
{
private readonly IConfiguration _configuration;
private readonly OpcUaApplicationHostOptions _options;
private readonly DeferredAddressSpaceSink _deferredSink;
private readonly DeferredServiceLevelPublisher _deferredServiceLevel;
private readonly IOpcUaUserAuthenticator _userAuthenticator;
@@ -33,19 +33,19 @@ public sealed class OtOpcUaServerHostedService : IHostedService, IAsyncDisposabl
/// <summary>
/// Initializes a new instance of the OtOpcUaServerHostedService class.
/// </summary>
/// <param name="configuration">The application configuration.</param>
/// <param name="options">The validated OPC UA host options (bound from the <c>OpcUa</c> section and validated at startup via <c>ValidateOnStart</c>).</param>
/// <param name="deferredSink">The deferred address space sink that receives the real sink once the server is ready.</param>
/// <param name="deferredServiceLevel">The deferred service level publisher that receives the real publisher once the server is ready.</param>
/// <param name="userAuthenticator">The OPC UA user authenticator.</param>
/// <param name="loggerFactory">The logger factory for creating loggers.</param>
public OtOpcUaServerHostedService(
IConfiguration configuration,
IOptions<OpcUaApplicationHostOptions> options,
DeferredAddressSpaceSink deferredSink,
DeferredServiceLevelPublisher deferredServiceLevel,
IOpcUaUserAuthenticator userAuthenticator,
ILoggerFactory loggerFactory)
{
_configuration = configuration;
_options = options.Value;
_deferredSink = deferredSink;
_deferredServiceLevel = deferredServiceLevel;
_userAuthenticator = userAuthenticator;
@@ -59,8 +59,7 @@ public sealed class OtOpcUaServerHostedService : IHostedService, IAsyncDisposabl
/// <param name="cancellationToken">Cancellation token.</param>
public async Task StartAsync(CancellationToken cancellationToken)
{
var options = new OpcUaApplicationHostOptions();
_configuration.GetSection("OpcUa").Bind(options);
var options = _options;
_server = new OtOpcUaSdkServer();
_appHost = new OpcUaApplicationHost(
@@ -16,6 +16,7 @@ using ZB.MOM.WW.OtOpcUa.Host.Engines;
using ZB.MOM.WW.OtOpcUa.Host.Health;
using ZB.MOM.WW.OtOpcUa.Host.Observability;
using ZB.MOM.WW.OtOpcUa.Host.OpcUa;
using ZB.MOM.WW.OtOpcUa.OpcUaServer;
using ZB.MOM.WW.OtOpcUa.OpcUaServer.Security;
using ZB.MOM.WW.OtOpcUa.Runtime;
using ZB.MOM.WW.OtOpcUa.Security;
@@ -102,6 +103,12 @@ if (hasDriver)
builder.Services.AddSingleton<ILdapAuthService, LdapAuthService>();
builder.Services.AddSingleton<IOpcUaUserAuthenticator, LdapOpcUaUserAuthenticator>();
// Bind + validate the OPC UA host options the same way (fail-fast at start via ValidateOnStart)
// and let OtOpcUaServerHostedService consume the validated IOptions instance rather than
// re-binding the section imperatively. Defaults pass; this guards explicit prod/env overrides.
builder.Services.AddValidatedOptions<OpcUaApplicationHostOptions, OpcUaApplicationHostOptionsValidator>(
builder.Configuration, "OpcUa");
builder.Services.AddHostedService<OtOpcUaServerHostedService>();
}
@@ -0,0 +1,59 @@
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Host.Configuration;
using ZB.MOM.WW.OtOpcUa.OpcUaServer;
namespace ZB.MOM.WW.OtOpcUa.Host.IntegrationTests;
/// <summary>
/// Task 4 — verifies the net-new <see cref="OpcUaApplicationHostOptionsValidator"/> (built on the
/// shared <c>ZB.MOM.WW.Configuration</c> <c>OptionsValidatorBase</c>/<c>ValidationBuilder</c>) that
/// gates the OPC UA host options at startup. The C# defaults are all valid so a host with no
/// explicit <c>"OpcUa"</c> section still passes; the validator exists to reject explicit
/// prod/env overrides. Failure messages carry the real <c>"OpcUa:"</c> section prefix and the
/// exact shared-primitive wording so they read correctly when surfaced via <c>ValidateOnStart</c>.
/// </summary>
public sealed class OpcUaApplicationHostOptionsValidatorTests
{
private static readonly OpcUaApplicationHostOptionsValidator Sut = new();
/// <summary>The C# defaults (the as-bound shape when the section is absent) pass validation.</summary>
[Fact]
public void Default_options_succeed()
{
Sut.Validate(null, new OpcUaApplicationHostOptions()).Succeeded.ShouldBeTrue();
}
/// <summary>A port of 0 reports the shared port-range failure with the OpcUa prefix.</summary>
[Fact]
public void Zero_port_fails()
{
var result = Sut.Validate(null, new OpcUaApplicationHostOptions { OpcUaPort = 0 });
result.Failed.ShouldBeTrue();
result.Failures.ShouldContain("OpcUa:OpcUaPort must be between 1 and 65535 (was 0)");
}
/// <summary>A blank public hostname reports the shared required failure with the OpcUa prefix.</summary>
[Fact]
public void Blank_public_hostname_fails()
{
var result = Sut.Validate(null, new OpcUaApplicationHostOptions { PublicHostname = "" });
result.Failed.ShouldBeTrue();
result.Failures.ShouldContain("OpcUa:PublicHostname is required");
}
/// <summary>An empty security-profile list reports the shared min-count failure with the OpcUa prefix.</summary>
[Fact]
public void Empty_security_profiles_fails()
{
var result = Sut.Validate(null, new OpcUaApplicationHostOptions
{
EnabledSecurityProfiles = new List<OpcUaSecurityProfile>(),
});
result.Failed.ShouldBeTrue();
result.Failures.ShouldContain("OpcUa:EnabledSecurityProfiles must contain at least 1 item(s) (had 0)");
}
}