fix(secrets-ui): Reveal fails closed when its audit cannot be written (scadaproj#45)
Secrets-015: RevealButton now writes the secret.reveal Success audit before showing the value and
shows nothing ("audit record could not be written") if that write throws; the failure is logged as
a warning with actor and name, never the value. The cancellation token is captured up front, so a
component disposed mid-reveal can no longer turn the audit into a swallowed ObjectDisposedException.
Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
using Bunit;
|
||||
using Bunit.TestDoubles;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using ZB.MOM.WW.Audit;
|
||||
using ZB.MOM.WW.Secrets.Abstractions;
|
||||
using ZB.MOM.WW.Secrets.Ui.Components;
|
||||
using ZB.MOM.WW.Secrets.Ui.Tests.Fakes;
|
||||
|
||||
namespace ZB.MOM.WW.Secrets.Ui.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Secrets-015: a reveal whose audit write fails must not show the plaintext (fail closed), and the
|
||||
/// failure must be logged — actor and name, never the value.
|
||||
/// </summary>
|
||||
public class RevealAuditFailureTests : TestContext
|
||||
{
|
||||
private const string PlainValue = "the-plaintext-value-42";
|
||||
|
||||
private sealed class CapturingLogger<T> : ILogger<T>
|
||||
{
|
||||
public List<(LogLevel Level, string Message)> Entries { get; } = new();
|
||||
|
||||
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) => Entries.Add((logLevel, formatter(state, exception)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_reveal_whose_audit_fails_shows_nothing_and_logs_it()
|
||||
{
|
||||
var store = new RecordingSecretStore();
|
||||
var audit = new CapturingAuditWriter { SuccessFault = new InvalidOperationException("audit sink down") };
|
||||
var logger = new CapturingLogger<RevealButton>();
|
||||
Services.AddSingleton<ISecretStore>(store);
|
||||
Services.AddSingleton<IAuditWriter>(audit);
|
||||
Services.AddSingleton<ISecretCipher>(new FakeCipher());
|
||||
Services.AddSingleton<ILogger<RevealButton>>(logger);
|
||||
var auth = this.AddTestAuthorization();
|
||||
auth.SetAuthorized("mallory");
|
||||
auth.SetPolicies(SecretsAuthorization.RevealPolicy);
|
||||
|
||||
var name = new SecretName("prod/db");
|
||||
store.Seed(new FakeCipher().Encrypt(name, PlainValue, SecretContentType.Text) with
|
||||
{
|
||||
Revision = 1,
|
||||
CreatedUtc = DateTimeOffset.UnixEpoch,
|
||||
UpdatedUtc = DateTimeOffset.UnixEpoch,
|
||||
});
|
||||
|
||||
var cut = RenderComponent<RevealButton>(p => p.Add(c => c.Name, name));
|
||||
cut.Find("button.reveal-secret").Click();
|
||||
|
||||
Assert.DoesNotContain(PlainValue, cut.Markup);
|
||||
Assert.Empty(cut.FindAll("[data-testid=revealed-value]"));
|
||||
Assert.Contains("audit", cut.Find("[data-testid=reveal-error]").TextContent);
|
||||
|
||||
(LogLevel level, string message) = Assert.Single(logger.Entries);
|
||||
Assert.Equal(LogLevel.Warning, level);
|
||||
Assert.Contains("prod/db", message);
|
||||
Assert.Contains("mallory", message);
|
||||
Assert.DoesNotContain(PlainValue, message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user