// GLAuth integration tests for the 0.5.0 passwordless group lookup — opt-in only. // // Same gate and variables as GLAuthIntegrationTests (ZB_LDAP_IT=1, ZB_LDAP_SERVER/PORT/BASE, // ZB_LDAP_SVC_DN/PW, ZB_LDAP_USER/PW, ZB_LDAP_USERATTR). ZB_LDAP_FALLBACK (optional, "host:port") // names a second live directory for the two-live-endpoint failover case — e.g. the redundant local // pair in scadaproj/infra/glauth-redundant (primary :3893, backup :3894): // // ZB_LDAP_IT=1 ZB_LDAP_SERVER=localhost ZB_LDAP_PORT=3893 ZB_LDAP_FALLBACK=localhost:3894 \ // ZB_LDAP_BASE=dc=zb,dc=local ZB_LDAP_SVC_DN=cn=serviceaccount,dc=zb,dc=local ZB_LDAP_SVC_PW=... \ // ZB_LDAP_USER=multi-role ZB_LDAP_PW=... \ // dotnet test tests/ZB.MOM.WW.Auth.Ldap.Tests --filter "FullyQualifiedName~GLAuthGroupLookupTests" using System.Net.Sockets; using ZB.MOM.WW.Auth.Abstractions.Ldap; namespace ZB.MOM.WW.Auth.Ldap.Tests.Integration; public sealed class GLAuthGroupLookupTests { private sealed record Env(string Server, int Port, string BaseDn, string SvcDn, string SvcPw, string User, string Pw, string UserAttr, string? Fallback); private static Env Require() { Skip.IfNot(Environment.GetEnvironmentVariable("ZB_LDAP_IT") == "1", "Set ZB_LDAP_IT=1 and a reachable GLAuth to run."); var env = new Env( Environment.GetEnvironmentVariable("ZB_LDAP_SERVER") ?? "localhost", int.TryParse(Environment.GetEnvironmentVariable("ZB_LDAP_PORT"), out var p) ? p : 3893, Environment.GetEnvironmentVariable("ZB_LDAP_BASE") ?? "dc=lmxopcua,dc=local", Environment.GetEnvironmentVariable("ZB_LDAP_SVC_DN") ?? "", Environment.GetEnvironmentVariable("ZB_LDAP_SVC_PW") ?? "", Environment.GetEnvironmentVariable("ZB_LDAP_USER") ?? "", Environment.GetEnvironmentVariable("ZB_LDAP_PW") ?? "", Environment.GetEnvironmentVariable("ZB_LDAP_USERATTR") ?? "cn", Environment.GetEnvironmentVariable("ZB_LDAP_FALLBACK")); try { using var tcp = new TcpClient(); if (!tcp.ConnectAsync(env.Server, env.Port).Wait(TimeSpan.FromSeconds(3))) Skip.If(true, $"GLAuth not reachable at {env.Server}:{env.Port} (connect timed out)."); } catch (Exception ex) { Skip.If(true, $"GLAuth not reachable at {env.Server}:{env.Port}: {ex.Message}"); } return env; } private static LdapOptions Options(Env env) => new() { Enabled = true, Server = env.Server, Port = env.Port, Transport = LdapTransport.None, AllowInsecure = true, SearchBase = env.BaseDn, ServiceAccountDn = env.SvcDn, ServiceAccountPassword = env.SvcPw, UserNameAttribute = env.UserAttr, GroupAttribute = "memberOf", ConnectionTimeoutMs = 3000, }; /// The lookup returns exactly the group set, username and display name a real sign-in does. [SkippableFact] public async Task Lookup_ReturnsTheSameGroupsAsSignIn() { var env = Require(); var svc = new LdapAuthService(Options(env)); var login = await svc.AuthenticateAsync(env.User, env.Pw, CancellationToken.None); var lookup = await svc.GetUserGroupsAsync(env.User, CancellationToken.None); Assert.True(login.Succeeded, $"sign-in failed: {login.Failure}"); Assert.Equal(LdapGroupLookupStatus.Found, lookup.Status); Assert.NotEmpty(lookup.Groups); Assert.Equal(login.Groups.OrderBy(g => g, StringComparer.Ordinal), lookup.Groups.OrderBy(g => g, StringComparer.Ordinal)); Assert.Equal(login.Username, lookup.Username); Assert.Equal(login.DisplayName, lookup.DisplayName); } [SkippableFact] public async Task Lookup_OfAnUnknownName_IsUserNotFound() { var env = Require(); var svc = new LdapAuthService(Options(env)); var lookup = await svc.GetUserGroupsAsync("no-such-user-" + Guid.NewGuid().ToString("N")[..8], CancellationToken.None); Assert.Equal(LdapGroupLookupStatus.UserNotFound, lookup.Status); } /// A wrong service-account password is the directory refusing US, not the user: DirectoryUnavailable. [SkippableFact] public async Task Lookup_WithAWrongServiceAccountPassword_IsDirectoryUnavailable() { var env = Require(); var svc = new LdapAuthService(Options(env) with { ServiceAccountPassword = "definitely-wrong" }); var lookup = await svc.GetUserGroupsAsync(env.User, CancellationToken.None); Assert.Equal(LdapGroupLookupStatus.DirectoryUnavailable, lookup.Status); } /// Dead primary (closed local port) fails over to the live directory, twice (sticky). [SkippableFact] public async Task Lookup_FailsOverFromADeadPrimary() { var env = Require(); var svc = new LdapAuthService(Options(env) with { Server = "127.0.0.1", Port = 39999, FallbackServers = new[] { $"{env.Server}:{env.Port}" }, }); var first = await svc.GetUserGroupsAsync(env.User, CancellationToken.None); var second = await svc.GetUserGroupsAsync(env.User, CancellationToken.None); Assert.Equal(LdapGroupLookupStatus.Found, first.Status); Assert.Equal(LdapGroupLookupStatus.Found, second.Status); } /// Every endpoint dead: DirectoryUnavailable, never UserNotFound (which would revoke a session). [SkippableFact] public async Task Lookup_WithEveryEndpointDead_IsDirectoryUnavailable() { var env = Require(); var svc = new LdapAuthService(Options(env) with { Server = "127.0.0.1", Port = 39999, FallbackServers = new[] { "127.0.0.1:39998" }, }); var lookup = await svc.GetUserGroupsAsync(env.User, CancellationToken.None); Assert.Equal(LdapGroupLookupStatus.DirectoryUnavailable, lookup.Status); } /// Two LIVE directories (ZB_LDAP_FALLBACK): primary answers first; a dead primary port walks to the backup. [SkippableFact] public async Task Lookup_AcrossTwoLiveDirectories_AnswersFromEither() { var env = Require(); Skip.If(string.IsNullOrWhiteSpace(env.Fallback), "Set ZB_LDAP_FALLBACK=host:port to a second live directory."); var both = new LdapAuthService(Options(env) with { FallbackServers = new[] { env.Fallback! } }); var backupOnly = new LdapAuthService(Options(env) with { Port = 39999, Server = "127.0.0.1", FallbackServers = new[] { env.Fallback! } }); var viaPrimary = await both.GetUserGroupsAsync(env.User, CancellationToken.None); var viaBackup = await backupOnly.GetUserGroupsAsync(env.User, CancellationToken.None); Assert.Equal(LdapGroupLookupStatus.Found, viaPrimary.Status); Assert.Equal(LdapGroupLookupStatus.Found, viaBackup.Status); Assert.Equal(viaPrimary.Groups.OrderBy(g => g), viaBackup.Groups.OrderBy(g => g)); } }