feat(security): re-query LDAP group membership for active sessions (#46)
Auth 0.5.0 (all five packages) adds the passwordless ILdapGroupLookup. A new singleton DirectoryGroupRefresher asks it for a session's current groups once the new zb:lastgrouprefresh anchor (stamped at login) is older than ScadaBridge:Security:LdapGroupRefresh:IntervalMinutes (default 15, Enabled default true, CacheSeconds 60 per-node answer reuse). CookieSessionValidator replaces the stored groups and re-maps roles; this reaches HTTP and live circuits (once per interval, independent of the 10 s circuit refresh). Failure policy: directory unavailable keeps the stored groups (warn, all paths incl. fail-closed mutation checks); user not found / disabled / ambiguous / no groups ends the session. Inert under BindMode=User (NotSupported), LDAP disabled, or DisableLogin. Auth 0.4.4 ApiKeyFailure.StoreUnavailable now answers 503 SERVICE_UNAVAILABLE on the inbound API instead of a misleading 401. Docs: Component-Security.md, components/Security.md, Component-Host.md, register row 23 closed. Claude-Session: https://claude.ai/code/session_01GT67ACmpMR7b9LGRPLijMi
This commit is contained in:
@@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NSubstitute;
|
||||
using ZB.MOM.WW.Auth.Abstractions.Ldap;
|
||||
using ZB.MOM.WW.Auth.Abstractions.Roles;
|
||||
using ZB.MOM.WW.ScadaBridge.CentralUI.Auth;
|
||||
using ZB.MOM.WW.ScadaBridge.Security;
|
||||
@@ -78,4 +79,103 @@ public class CircuitRevocationRegressionTests
|
||||
await Assert.ThrowsAsync<UnauthorizedAccessException>(() => commands.ExecuteAsync(new CreateRoleMappingCommand("blocked", Roles.Viewer)));
|
||||
await transaction.DidNotReceive().ExecuteAsync(Arg.Any<Func<IServiceProvider, Task<object?>>>());
|
||||
}
|
||||
|
||||
// ---- ScadaBridge#46: directory group re-query reaches a live circuit ----
|
||||
|
||||
private static (AuthenticationStateProvider Auth, ILdapGroupLookup Lookup, ServiceProvider Provider) DirectoryCircuit(
|
||||
IReadOnlyList<string> loginGroups, bool disableLogin = false)
|
||||
{
|
||||
// Session issued 16 minutes ago, so the 15-minute group re-query is due on the first read.
|
||||
var issued = DateTimeOffset.UtcNow.AddMinutes(-16);
|
||||
var initial = SessionClaimBuilder.Build("alice", "Alice", loginGroups,
|
||||
new RoleMappingResult([Roles.Administrator], [], true), issued);
|
||||
var mapper = Substitute.For<IGroupRoleMapper<string>>();
|
||||
mapper.MapAsync(Arg.Any<IReadOnlyList<string>>(), Arg.Any<CancellationToken>())
|
||||
.Returns(call =>
|
||||
{
|
||||
var groups = call.Arg<IReadOnlyList<string>>();
|
||||
var roles = groups.Contains("SCADA-Admins") ? new[] { Roles.Administrator } : new[] { Roles.Viewer };
|
||||
return new GroupRoleMapping<string>(roles, Scope: new RoleMappingResult(roles, [], true));
|
||||
});
|
||||
var lookup = Substitute.For<ILdapGroupLookup>();
|
||||
var services = new ServiceCollection().AddLogging();
|
||||
services.AddSingleton(mapper);
|
||||
services.AddSingleton(lookup);
|
||||
services.AddSingleton<TimeProvider>(TimeProvider.System);
|
||||
// Cache off, so "no second query" below is proven by the session's advanced anchor, not the cache.
|
||||
services.AddSingleton(Options.Create(new SecurityOptions { LdapGroupRefresh = { CacheSeconds = 0 } }));
|
||||
services.AddSingleton(Options.Create(new ZB.MOM.WW.ScadaBridge.Security.Auth.AuthDisableLoginOptions { DisableLogin = disableLogin }));
|
||||
services.AddSingleton<DirectoryGroupRefresher>();
|
||||
services.AddScoped<CookieSessionValidator>();
|
||||
services.AddSingleton<IHttpContextAccessor>(new HttpContextAccessor { HttpContext = new DefaultHttpContext { User = initial } });
|
||||
services.AddScoped<AuthenticationStateProvider, CookieAuthenticationStateProvider>();
|
||||
var provider = services.BuildServiceProvider();
|
||||
return (provider.CreateScope().ServiceProvider.GetRequiredService<AuthenticationStateProvider>(), lookup, provider);
|
||||
}
|
||||
|
||||
/// <summary>A directory group removal lands in an open circuit without re-login (ScadaBridge#46).</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task SameCircuit_PicksUpDirectoryGroupRemoval_WithoutReLogin()
|
||||
{
|
||||
var (auth, lookup, provider) = DirectoryCircuit(["SCADA-Admins"]);
|
||||
using var _ = provider;
|
||||
lookup.GetUserGroupsAsync("alice", Arg.Any<CancellationToken>())
|
||||
.Returns(LdapGroupLookupResult.Found("alice", "Alice", ["SCADA-Viewers"]));
|
||||
|
||||
var state = await auth.GetAuthoritativeStateAsync();
|
||||
|
||||
var roles = state.User.FindAll(JwtTokenService.RoleClaimType).Select(c => c.Value).ToList();
|
||||
Assert.Equal([Roles.Viewer], roles);
|
||||
Assert.True(state.User.Identity!.IsAuthenticated);
|
||||
await lookup.Received(1).GetUserGroupsAsync("alice", Arg.Any<CancellationToken>());
|
||||
|
||||
// The circuit's rebuilt principal carries the advanced anchor: the next read does not re-query.
|
||||
await auth.GetAuthoritativeStateAsync();
|
||||
await lookup.Received(1).GetUserGroupsAsync("alice", Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
/// <summary>A user disabled in the directory loses the circuit's authentication; a gate then denies.</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task SameCircuit_DirectoryDisabledUser_BecomesAnonymous()
|
||||
{
|
||||
var (auth, lookup, provider) = DirectoryCircuit(["SCADA-Admins"]);
|
||||
using var _ = provider;
|
||||
lookup.GetUserGroupsAsync("alice", Arg.Any<CancellationToken>())
|
||||
.Returns(LdapGroupLookupResult.Of(LdapGroupLookupStatus.UserDisabled));
|
||||
|
||||
var state = await auth.GetAuthoritativeStateAsync();
|
||||
|
||||
Assert.False(state.User.Identity?.IsAuthenticated ?? false);
|
||||
Assert.Empty(state.User.FindAll(JwtTokenService.RoleClaimType));
|
||||
}
|
||||
|
||||
/// <summary>An LDAP outage keeps the circuit's roles, even on the fail-closed authoritative path.</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task SameCircuit_DirectoryUnavailable_KeepsRoles()
|
||||
{
|
||||
var (auth, lookup, provider) = DirectoryCircuit(["SCADA-Admins"]);
|
||||
using var _ = provider;
|
||||
lookup.GetUserGroupsAsync("alice", Arg.Any<CancellationToken>())
|
||||
.Returns(LdapGroupLookupResult.Of(LdapGroupLookupStatus.DirectoryUnavailable));
|
||||
|
||||
var state = await auth.GetAuthoritativeStateAsync();
|
||||
|
||||
Assert.Contains(state.User.FindAll(JwtTokenService.RoleClaimType), c => c.Value == Roles.Administrator);
|
||||
}
|
||||
|
||||
/// <summary>DisableLogin (auto-login) never queries the directory.</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task DisableLogin_NeverQueriesTheDirectory()
|
||||
{
|
||||
var (auth, lookup, provider) = DirectoryCircuit([], disableLogin: true);
|
||||
using var _ = provider;
|
||||
|
||||
await auth.GetAuthoritativeStateAsync();
|
||||
|
||||
await lookup.DidNotReceive().GetUserGroupsAsync(Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user