1d2170a152
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
38 lines
1.5 KiB
C#
38 lines
1.5 KiB
C#
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));
|
|
}
|
|
}
|