feat(auth): 0.5.0 passwordless group lookup ILdapGroupLookup (ScadaBridge#46)

ILdapGroupLookup.GetUserGroupsAsync(username, ct) re-reads a known user's
groups with the service account: same extraction, canonicalisation, endpoint
walk (FallbackServers), timeouts and TLS policy as AuthenticateAsync, via one
shared WalkEndpoints<T>. Explicit statuses: Found, UserNotFound, UserDisabled
(AD userAccountControl), AmbiguousUser, DirectoryUnavailable, NotSupported
(BindMode=User / no service account / foreign ILdapAuthService),
ProviderDisabled. AddZbLdapAuth registers it as the same singleton.

0.5.0 is the first published version carrying 0.4.2-0.4.4; scadaproj#51 was
live-verified against the NAM AD (24/24, identity check mutation-proven).

Claude-Session: https://claude.ai/code/session_01GT67ACmpMR7b9LGRPLijMi
This commit is contained in:
Joseph Doherty
2026-11-12 20:10:40 -05:00
parent 3ca7e1936f
commit f5ef0efedd
10 changed files with 828 additions and 41 deletions
@@ -0,0 +1,159 @@
// 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,
};
/// <summary>The lookup returns exactly the group set, username and display name a real sign-in does.</summary>
[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);
}
/// <summary>A wrong service-account password is the directory refusing US, not the user: DirectoryUnavailable.</summary>
[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);
}
/// <summary>Dead primary (closed local port) fails over to the live directory, twice (sticky).</summary>
[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);
}
/// <summary>Every endpoint dead: DirectoryUnavailable, never UserNotFound (which would revoke a session).</summary>
[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);
}
/// <summary>Two LIVE directories (ZB_LDAP_FALLBACK): primary answers first; a dead primary port walks to the backup.</summary>
[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));
}
}