fix(management): refuse zero-role principals on the management/CLI surfaces (#145)
ManagementAuthenticator — the only identity source for POST /management, the audit REST endpoints and the debug-stream hub — now enforces "no role, no login" the way #144 does for /auth/login and /auth/token: - HTTP Basic: a bind whose groups map to zero roles, or whose role mapping throws, answers the bad-password 401 AUTH_FAILED body and counts as a LoginThrottle failure. RecordSuccess moved after the mapping, so a refusal no longer clears earlier failures. Request cancellation still propagates. - Bearer: a token with no role claim (minted before #144 shipped) gets the invalid-token 401; no throttle, as for every bearer failure. - Each refusal is logged at Warning (category ManagementAuthenticator) with the request path, the username and the reason. Before this, a zero-role Basic caller reached every command that ManagementActor.GetRequiredRoles opens to any authenticated user. DisableLogin is unchanged. Tests: ManagementZeroRoleRefusalTests — zero roles / mapper throws / one role over Basic and Bearer for /management (ListSites), /api/audit/query and the debug-stream hub; every null-role command is unreachable by a zero-role principal; throttle accounting. 47 of the 58 fail against the old authenticator (the 11 that pass are the one-role/bypass regression guards). Claude-Session: https://claude.ai/code/session_01GT67AFKC45MBz1wPZnVbX8
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.ManagementService.Tests;
|
||||
|
||||
/// <summary>An in-memory logger provider that records every formatted log line with its category and level.</summary>
|
||||
internal sealed class CapturingLoggerProvider : ILoggerProvider
|
||||
{
|
||||
/// <summary>One captured log entry.</summary>
|
||||
/// <param name="Category">The logger category.</param>
|
||||
/// <param name="Level">The log level.</param>
|
||||
/// <param name="Message">The formatted message.</param>
|
||||
/// <param name="Exception">The logged exception, if any.</param>
|
||||
public sealed record Entry(string Category, LogLevel Level, string Message, Exception? Exception);
|
||||
|
||||
/// <summary>Every entry logged through this provider, in order.</summary>
|
||||
public ConcurrentQueue<Entry> Entries { get; } = new();
|
||||
|
||||
/// <inheritdoc />
|
||||
public ILogger CreateLogger(string categoryName) => new Logger(categoryName, Entries);
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
|
||||
private sealed class Logger(string category, ConcurrentQueue<Entry> sink) : ILogger
|
||||
{
|
||||
public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null;
|
||||
|
||||
public bool IsEnabled(LogLevel logLevel) => true;
|
||||
|
||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
|
||||
Func<TState, Exception?, string> formatter) =>
|
||||
sink.Enqueue(new Entry(category, logLevel, formatter(state, exception), exception));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user