using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Shouldly; using Xunit; using ZB.MOM.WW.Configuration; using ZB.MOM.WW.OtOpcUa.Host.Configuration; using ZB.MOM.WW.OtOpcUa.Security.Ldap; using LdapBindMode = ZB.MOM.WW.Auth.Abstractions.Ldap.LdapBindMode; using LdapTransport = ZB.MOM.WW.Auth.Abstractions.Ldap.LdapTransport; namespace ZB.MOM.WW.OtOpcUa.Host.IntegrationTests; /// /// lmxopcua#913 — OtOpcUa's own ports ONLY two rules from the /// shared library's validator (which OtOpcUa does not run): every FallbackServers entry /// parses as host[:port], and BindMode=User needs a UserBindTemplate with /// {0} exactly once. The library's ServiceAccountDn-required rule is deliberately NOT /// ported: DEV boots on the code defaults with an empty DN (NAM cutover plan, review B2). /// public sealed class LdapOptionsValidatorNamCutoverTests { private static readonly LdapOptionsValidator Sut = new(); private static LdapOptions Valid() => new() { Enabled = true, Server = "dc1.example.test", Port = 636, SearchBase = "dc=example,dc=test", Transport = LdapTransport.Ldaps, }; /// /// Review B2: today's DEV shape — Enabled=true, Server=localhost, /// ServiceAccountDn="", Transport=Ldaps — must keep passing. /// [Fact] public void Dev_shaped_config_with_empty_ServiceAccountDn_passes() { var options = new LdapOptions { Enabled = true, Server = "localhost", ServiceAccountDn = "", Transport = LdapTransport.Ldaps, }; var result = Sut.Validate(null, options); result.Succeeded.ShouldBeTrue(result.FailureMessage); } /// The same DEV shape boots through the host's real registration path /// (AddValidatedOptions + ValidateOnStart) when bound from configuration. [Fact] public void Dev_shaped_config_survives_AddValidatedOptions() { var configuration = new ConfigurationBuilder() .AddInMemoryCollection(new Dictionary { ["Security:Ldap:Enabled"] = "true", ["Security:Ldap:Server"] = "localhost", ["Security:Ldap:ServiceAccountDn"] = "", ["Security:Ldap:Transport"] = "Ldaps", }) .Build(); var services = new ServiceCollection(); services.AddValidatedOptions(configuration, LdapOptions.SectionName); using var sp = services.BuildServiceProvider(); Should.NotThrow(() => sp.GetRequiredService>().Value); } /// Two well-formed fallback DCs (bare host and host:port) pass. [Fact] public void Wellformed_fallback_servers_pass() { var options = Valid(); options.FallbackServers = ["dc2.example.test", " dc3.example.test:3269 "]; Sut.Validate(null, options).Succeeded.ShouldBeTrue(); } /// Each malformed fallback entry fails with a field-naming message. [Theory] [InlineData("")] [InlineData(" ")] [InlineData(":636")] [InlineData("dc2:0")] [InlineData("dc2:65536")] [InlineData("dc2:ldaps")] [InlineData("fe80::1")] public void Malformed_fallback_server_fails(string entry) { var options = Valid(); options.FallbackServers = ["dc2.example.test", entry]; var result = Sut.Validate(null, options); result.Failed.ShouldBeTrue(); result.Failures.ShouldContain(f => f.StartsWith("Ldap:FallbackServers contains an invalid entry")); } /// User mode with exactly one placeholder passes — and needs no ServiceAccountDn. [Theory] [InlineData("NAM\\{0}")] [InlineData("{0}@example.test")] public void User_mode_with_one_placeholder_passes(string template) { var options = Valid(); options.BindMode = LdapBindMode.User; options.UserBindTemplate = template; Sut.Validate(null, options).Succeeded.ShouldBeTrue(); } /// User mode with zero or two placeholders fails. [Theory] [InlineData("")] [InlineData("NAM\\svc")] [InlineData("{0}\\{0}")] public void User_mode_without_exactly_one_placeholder_fails(string template) { var options = Valid(); options.BindMode = LdapBindMode.User; options.UserBindTemplate = template; var result = Sut.Validate(null, options); result.Failed.ShouldBeTrue(); result.Failures.ShouldContain(f => f.StartsWith("Ldap:UserBindTemplate must contain {0} exactly once")); } /// ServiceAccount mode ignores the template rule (the template is User-only). [Fact] public void ServiceAccount_mode_ignores_an_empty_template() { var options = Valid(); options.BindMode = LdapBindMode.ServiceAccount; options.UserBindTemplate = ""; Sut.Validate(null, options).Succeeded.ShouldBeTrue(); } /// The ported rules follow the existing gate: disabled or DevStub configs skip them. [Theory] [InlineData(false, false)] [InlineData(true, true)] public void Disabled_or_devstub_skips_the_ported_rules(bool enabled, bool devStub) { var options = Valid(); options.Enabled = enabled; options.DevStubMode = devStub; options.FallbackServers = ["dc2:notaport"]; options.BindMode = LdapBindMode.User; options.UserBindTemplate = ""; Sut.Validate(null, options).Succeeded.ShouldBeTrue(); } /// /// Binding test: Security:Ldap:FallbackServers:0 and the other three lmxopcua#913 keys /// reach the LIBRARY options, from JSON and from Security__Ldap__… environment variables. /// [Theory] [InlineData("json")] [InlineData("env")] public void New_keys_bind_and_reach_the_library_options(string source) { var builder = new ConfigurationBuilder(); if (source == "json") { var json = """ {"Security":{"Ldap":{ "FallbackServers":["dc2.example.test","dc3.example.test:3269"], "BindMode":"User", "UserBindTemplate":"NAM\\{0}", "AcceptDomainQualifiedNames":false }}} """; builder.AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json))); } else { var prefix = $"OTOPCUA913_{Guid.NewGuid():N}_"; var vars = new Dictionary { ["Security__Ldap__FallbackServers__0"] = "dc2.example.test", ["Security__Ldap__FallbackServers__1"] = "dc3.example.test:3269", ["Security__Ldap__BindMode"] = "User", ["Security__Ldap__UserBindTemplate"] = "NAM\\{0}", ["Security__Ldap__AcceptDomainQualifiedNames"] = "false", }; foreach (var (k, v) in vars) Environment.SetEnvironmentVariable(prefix + k, v); try { builder.AddEnvironmentVariables(prefix); AssertBound(builder.Build()); return; } finally { foreach (var k in vars.Keys) Environment.SetEnvironmentVariable(prefix + k, null); } } AssertBound(builder.Build()); static void AssertBound(IConfiguration configuration) { var app = configuration.GetSection(LdapOptions.SectionName).Get(); app.ShouldNotBeNull(); var lib = app.ToLibraryOptions(); lib.FallbackServers.ShouldBe(["dc2.example.test", "dc3.example.test:3269"]); lib.BindMode.ShouldBe(LdapBindMode.User); lib.UserBindTemplate.ShouldBe("NAM\\{0}"); lib.AcceptDomainQualifiedNames.ShouldBeFalse(); } } }