feat(secrets-ui): add/rotate/delete modal + audited gated reveal
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
using Bunit;
|
||||
using Bunit.TestDoubles;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
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;
|
||||
|
||||
public class ConfirmDeleteModalTests : TestContext
|
||||
{
|
||||
[Fact]
|
||||
public void ConfirmDeleteModal_IsInPageModal_NoJsDialog()
|
||||
{
|
||||
// Strict JS interop mode: any IJSRuntime call (a JS confirm/alert dialog) would throw,
|
||||
// proving the confirmation is a rendered in-page modal, not a browser dialog.
|
||||
JSInterop.Mode = JSRuntimeMode.Strict;
|
||||
|
||||
var store = new RecordingSecretStore();
|
||||
var audit = new CapturingAuditWriter();
|
||||
Services.AddSingleton<ISecretStore>(store);
|
||||
Services.AddSingleton<IAuditWriter>(audit);
|
||||
|
||||
var name = new SecretName("db/primary");
|
||||
var auth = this.AddTestAuthorization();
|
||||
auth.SetAuthorized("carol");
|
||||
auth.SetPolicies(SecretsAuthorization.ManagePolicy);
|
||||
|
||||
bool deleted = false;
|
||||
var cut = RenderComponent<ConfirmDeleteModal>(p => p
|
||||
.Add(c => c.Visible, true)
|
||||
.Add(c => c.Name, name)
|
||||
.Add(c => c.OnDeleted, () => deleted = true));
|
||||
|
||||
// The confirmation is a rendered in-page modal element.
|
||||
Assert.NotEmpty(cut.FindAll(".modal"));
|
||||
Assert.Contains("db/primary", cut.Markup);
|
||||
|
||||
cut.Find("[data-testid=confirm-delete]").Click();
|
||||
|
||||
// Confirm tombstoned the row (with the resolved actor) and wrote a secret.delete audit.
|
||||
Assert.Single(store.Deletes);
|
||||
Assert.Equal("db/primary", store.Deletes[0].Name.Value);
|
||||
Assert.Equal("carol", store.Deletes[0].Actor);
|
||||
Assert.True(deleted);
|
||||
|
||||
Assert.Single(audit.Events);
|
||||
AuditEvent evt = audit.Events[0];
|
||||
Assert.Equal("secret.delete", evt.Action);
|
||||
Assert.Equal(AuditOutcome.Success, evt.Outcome);
|
||||
Assert.Equal("Secrets", evt.Category);
|
||||
Assert.Equal("db/primary", evt.Target);
|
||||
Assert.Equal("carol", evt.Actor);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConfirmDeleteModal_Cancel_DoesNotDelete()
|
||||
{
|
||||
var store = new RecordingSecretStore();
|
||||
var audit = new CapturingAuditWriter();
|
||||
Services.AddSingleton<ISecretStore>(store);
|
||||
Services.AddSingleton<IAuditWriter>(audit);
|
||||
|
||||
var name = new SecretName("db/primary");
|
||||
var auth = this.AddTestAuthorization();
|
||||
auth.SetAuthorized("carol");
|
||||
auth.SetPolicies(SecretsAuthorization.ManagePolicy);
|
||||
|
||||
bool cancelled = false;
|
||||
var cut = RenderComponent<ConfirmDeleteModal>(p => p
|
||||
.Add(c => c.Visible, true)
|
||||
.Add(c => c.Name, name)
|
||||
.Add(c => c.OnCancelled, () => cancelled = true));
|
||||
|
||||
cut.Find("[data-testid=cancel-delete]").Click();
|
||||
|
||||
Assert.True(cancelled);
|
||||
Assert.Empty(store.Deletes);
|
||||
Assert.Empty(audit.Events);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConfirmDeleteModal_NotVisible_RendersNothing()
|
||||
{
|
||||
var store = new RecordingSecretStore();
|
||||
var audit = new CapturingAuditWriter();
|
||||
Services.AddSingleton<ISecretStore>(store);
|
||||
Services.AddSingleton<IAuditWriter>(audit);
|
||||
|
||||
var name = new SecretName("db/primary");
|
||||
this.AddTestAuthorization().SetAuthorized("carol");
|
||||
|
||||
var cut = RenderComponent<ConfirmDeleteModal>(p => p
|
||||
.Add(c => c.Visible, false)
|
||||
.Add(c => c.Name, name));
|
||||
|
||||
Assert.Empty(cut.FindAll(".modal"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using System.Collections.Concurrent;
|
||||
using ZB.MOM.WW.Audit;
|
||||
|
||||
namespace ZB.MOM.WW.Secrets.Ui.Tests.Fakes;
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="IAuditWriter"/> test double that collects every <see cref="AuditEvent"/> written,
|
||||
/// so a test can assert on the audit trail (action, outcome, target, actor) and — critically — that
|
||||
/// no plaintext ever appears in any audit field.
|
||||
/// </summary>
|
||||
public sealed class CapturingAuditWriter : IAuditWriter
|
||||
{
|
||||
private readonly ConcurrentQueue<AuditEvent> _events = new();
|
||||
|
||||
/// <summary>The events captured so far, in write order.</summary>
|
||||
public IReadOnlyList<AuditEvent> Events => _events.ToArray();
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task WriteAsync(AuditEvent evt, CancellationToken ct = default)
|
||||
{
|
||||
_events.Enqueue(evt);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using System.Text;
|
||||
using ZB.MOM.WW.Secrets.Abstractions;
|
||||
|
||||
namespace ZB.MOM.WW.Secrets.Ui.Tests.Fakes;
|
||||
|
||||
/// <summary>
|
||||
/// A trivial, reversible <see cref="ISecretCipher"/> stand-in for UI tests. It does not perform real
|
||||
/// cryptography — it simply carries the UTF-8 plaintext in <see cref="StoredSecret.Ciphertext"/> so a
|
||||
/// test can prove the editor's encrypt→upsert round-trips back to the entered value. Never used in
|
||||
/// production. A <see cref="FailDecrypt"/> flag lets a test drive the decrypt-failure reveal path.
|
||||
/// </summary>
|
||||
public sealed class FakeCipher : ISecretCipher
|
||||
{
|
||||
/// <summary>When <c>true</c>, <see cref="Decrypt"/> throws to exercise the failure path.</summary>
|
||||
public bool FailDecrypt { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public StoredSecret Encrypt(SecretName name, string plaintext, SecretContentType contentType)
|
||||
=> new()
|
||||
{
|
||||
Name = name,
|
||||
ContentType = contentType,
|
||||
Ciphertext = Encoding.UTF8.GetBytes(plaintext),
|
||||
Nonce = [1, 2, 3],
|
||||
Tag = [4, 5, 6],
|
||||
WrappedDek = [7, 8, 9],
|
||||
WrapNonce = [10, 11, 12],
|
||||
WrapTag = [13, 14, 15],
|
||||
KekId = "fake-kek",
|
||||
Revision = 0,
|
||||
CreatedUtc = default,
|
||||
UpdatedUtc = default,
|
||||
};
|
||||
|
||||
/// <inheritdoc />
|
||||
public string Decrypt(StoredSecret secret)
|
||||
{
|
||||
if (FailDecrypt)
|
||||
{
|
||||
throw new SecretDecryptionException("Fake decrypt failure.");
|
||||
}
|
||||
|
||||
return Encoding.UTF8.GetString(secret.Ciphertext);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
using System.Collections.Concurrent;
|
||||
using ZB.MOM.WW.Secrets.Abstractions;
|
||||
|
||||
namespace ZB.MOM.WW.Secrets.Ui.Tests.Fakes;
|
||||
|
||||
/// <summary>
|
||||
/// A hand-written <see cref="ISecretStore"/> test double (the family does not use Moq) that keeps
|
||||
/// an in-memory table of rows and records every <see cref="UpsertAsync"/> and
|
||||
/// <see cref="DeleteAsync"/> call so a test can assert on the mutation trail.
|
||||
/// </summary>
|
||||
public sealed class RecordingSecretStore : ISecretStore
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, StoredSecret> _rows = new();
|
||||
private readonly ConcurrentQueue<StoredSecret> _upserts = new();
|
||||
private readonly ConcurrentQueue<(SecretName Name, string? Actor)> _deletes = new();
|
||||
|
||||
/// <summary>Rows upserted, in call order.</summary>
|
||||
public IReadOnlyList<StoredSecret> Upserts => _upserts.ToArray();
|
||||
|
||||
/// <summary>Delete calls (name + recorded actor), in call order.</summary>
|
||||
public IReadOnlyList<(SecretName Name, string? Actor)> Deletes => _deletes.ToArray();
|
||||
|
||||
/// <summary>Seeds a row so <see cref="GetAsync"/> and <see cref="ListAsync"/> can return it.</summary>
|
||||
/// <param name="row">The row to seed.</param>
|
||||
public void Seed(StoredSecret row) => _rows[row.Name.Value] = row;
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<StoredSecret?> GetAsync(SecretName name, CancellationToken ct)
|
||||
=> Task.FromResult(_rows.TryGetValue(name.Value, out StoredSecret? row) ? row : null);
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task UpsertAsync(StoredSecret row, CancellationToken ct)
|
||||
{
|
||||
_rows[row.Name.Value] = row;
|
||||
_upserts.Enqueue(row);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<bool> DeleteAsync(SecretName name, string? actor, CancellationToken ct)
|
||||
{
|
||||
_deletes.Enqueue((name, actor));
|
||||
return Task.FromResult(_rows.TryRemove(name.Value, out _));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<IReadOnlyList<SecretMetadata>> ListAsync(bool includeDeleted, CancellationToken ct)
|
||||
{
|
||||
IReadOnlyList<SecretMetadata> projection = _rows.Values
|
||||
.Where(r => includeDeleted || !r.IsDeleted)
|
||||
.Select(r => new SecretMetadata
|
||||
{
|
||||
Name = r.Name,
|
||||
Description = r.Description,
|
||||
ContentType = r.ContentType,
|
||||
KekId = r.KekId,
|
||||
Revision = r.Revision,
|
||||
IsDeleted = r.IsDeleted,
|
||||
CreatedUtc = r.CreatedUtc,
|
||||
UpdatedUtc = r.UpdatedUtc,
|
||||
CreatedBy = r.CreatedBy,
|
||||
UpdatedBy = r.UpdatedBy,
|
||||
})
|
||||
.ToArray();
|
||||
return Task.FromResult(projection);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<IReadOnlyList<SecretManifestEntry>> GetManifestAsync(CancellationToken ct)
|
||||
=> throw new NotSupportedException();
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task ApplyReplicatedAsync(StoredSecret row, CancellationToken ct)
|
||||
=> throw new NotSupportedException();
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
using Bunit;
|
||||
using Bunit.TestDoubles;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
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;
|
||||
|
||||
public class RevealButtonTests : TestContext
|
||||
{
|
||||
private const string PlainValue = "the-plaintext-value-42";
|
||||
|
||||
private (RecordingSecretStore Store, CapturingAuditWriter Audit, FakeCipher Cipher) RegisterServices()
|
||||
{
|
||||
var store = new RecordingSecretStore();
|
||||
var audit = new CapturingAuditWriter();
|
||||
var cipher = new FakeCipher();
|
||||
Services.AddSingleton<ISecretStore>(store);
|
||||
Services.AddSingleton<IAuditWriter>(audit);
|
||||
Services.AddSingleton<ISecretCipher>(cipher);
|
||||
return (store, audit, cipher);
|
||||
}
|
||||
|
||||
private static StoredSecret Row(SecretName name, string plaintext)
|
||||
=> new FakeCipher().Encrypt(name, plaintext, SecretContentType.Text) with
|
||||
{
|
||||
Revision = 1,
|
||||
CreatedUtc = DateTimeOffset.UnixEpoch,
|
||||
UpdatedUtc = DateTimeOffset.UnixEpoch,
|
||||
};
|
||||
|
||||
[Fact]
|
||||
public void RevealButton_ShowsValueOnClick_AndAudits()
|
||||
{
|
||||
var (store, audit, _) = RegisterServices();
|
||||
var name = new SecretName("api/token");
|
||||
store.Seed(Row(name, PlainValue));
|
||||
|
||||
var auth = this.AddTestAuthorization();
|
||||
auth.SetAuthorized("carol");
|
||||
auth.SetPolicies(SecretsAuthorization.RevealPolicy);
|
||||
|
||||
var cut = RenderComponent<RevealButton>(p => p.Add(c => c.Name, name));
|
||||
|
||||
// The value is NOT present before the reveal click.
|
||||
Assert.DoesNotContain(PlainValue, cut.Markup);
|
||||
Assert.Empty(audit.Events);
|
||||
|
||||
cut.Find("button.reveal-secret").Click();
|
||||
|
||||
// After the click the plaintext is shown and a Success reveal audit is recorded.
|
||||
Assert.Contains(PlainValue, cut.Markup);
|
||||
Assert.Single(audit.Events);
|
||||
AuditEvent evt = audit.Events[0];
|
||||
Assert.Equal("secret.reveal", evt.Action);
|
||||
Assert.Equal(AuditOutcome.Success, evt.Outcome);
|
||||
Assert.Equal("Secrets", evt.Category);
|
||||
Assert.Equal("api/token", evt.Target);
|
||||
Assert.Equal("carol", evt.Actor);
|
||||
AssertNoValueLeak(evt);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RevealButton_MissingSecret_AuditsFailure()
|
||||
{
|
||||
var (_, audit, _) = RegisterServices();
|
||||
var name = new SecretName("api/missing");
|
||||
|
||||
var auth = this.AddTestAuthorization();
|
||||
auth.SetAuthorized("carol");
|
||||
auth.SetPolicies(SecretsAuthorization.RevealPolicy);
|
||||
|
||||
var cut = RenderComponent<RevealButton>(p => p.Add(c => c.Name, name));
|
||||
|
||||
cut.Find("button.reveal-secret").Click();
|
||||
|
||||
// No value shown; a Failure reveal audit recorded.
|
||||
Assert.DoesNotContain(PlainValue, cut.Markup);
|
||||
Assert.Single(audit.Events);
|
||||
AuditEvent evt = audit.Events[0];
|
||||
Assert.Equal("secret.reveal", evt.Action);
|
||||
Assert.Equal(AuditOutcome.Failure, evt.Outcome);
|
||||
Assert.Equal("api/missing", evt.Target);
|
||||
}
|
||||
|
||||
private static void AssertNoValueLeak(AuditEvent evt)
|
||||
{
|
||||
foreach (string? field in new[] { evt.Action, evt.Actor, evt.Category, evt.Target, evt.DetailsJson, evt.ToString() })
|
||||
{
|
||||
if (field is not null)
|
||||
{
|
||||
Assert.DoesNotContain(PlainValue, field, StringComparison.Ordinal);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
using Bunit;
|
||||
using Bunit.TestDoubles;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
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;
|
||||
|
||||
public class SecretEditorTests : TestContext
|
||||
{
|
||||
private const string EnteredValue = "sup3r-s3cret-value!";
|
||||
|
||||
private (RecordingSecretStore Store, CapturingAuditWriter Audit, FakeCipher Cipher) RegisterServices()
|
||||
{
|
||||
var store = new RecordingSecretStore();
|
||||
var audit = new CapturingAuditWriter();
|
||||
var cipher = new FakeCipher();
|
||||
Services.AddSingleton<ISecretStore>(store);
|
||||
Services.AddSingleton<IAuditWriter>(audit);
|
||||
Services.AddSingleton<ISecretCipher>(cipher);
|
||||
return (store, audit, cipher);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SecretEditor_Add_EncryptsAndUpserts()
|
||||
{
|
||||
var (store, audit, cipher) = RegisterServices();
|
||||
var auth = this.AddTestAuthorization();
|
||||
auth.SetAuthorized("carol");
|
||||
auth.SetPolicies(SecretsAuthorization.ManagePolicy);
|
||||
|
||||
var cut = RenderComponent<SecretEditor>(p => p
|
||||
.Add(c => c.OnSaved, () => { }));
|
||||
|
||||
// The value control is a masked password input.
|
||||
var valueInput = cut.Find("[data-testid=secret-value]");
|
||||
Assert.Equal("password", valueInput.GetAttribute("type"));
|
||||
|
||||
cut.Find("[data-testid=secret-name]").Change("app/thing");
|
||||
cut.Find("[data-testid=secret-description]").Change("A thing");
|
||||
cut.Find("[data-testid=secret-content-type]").Change(nameof(SecretContentType.Json));
|
||||
valueInput.Change(EnteredValue);
|
||||
|
||||
cut.Find("[data-testid=editor-submit]").Click();
|
||||
|
||||
// Exactly one upsert, round-tripping to the entered value under the entered name.
|
||||
Assert.Single(store.Upserts);
|
||||
StoredSecret row = store.Upserts[0];
|
||||
Assert.Equal("app/thing", row.Name.Value);
|
||||
Assert.Equal(SecretContentType.Json, row.ContentType);
|
||||
Assert.Equal("A thing", row.Description);
|
||||
Assert.Equal("carol", row.UpdatedBy);
|
||||
Assert.Equal("carol", row.CreatedBy);
|
||||
Assert.Equal(EnteredValue, cipher.Decrypt(row));
|
||||
|
||||
// A single secret.add audit was written, Success, correct target/actor, and NO value leak.
|
||||
Assert.Single(audit.Events);
|
||||
AuditEvent evt = audit.Events[0];
|
||||
Assert.Equal("secret.add", evt.Action);
|
||||
Assert.Equal(AuditOutcome.Success, evt.Outcome);
|
||||
Assert.Equal("Secrets", evt.Category);
|
||||
Assert.Equal("app/thing", evt.Target);
|
||||
Assert.Equal("carol", evt.Actor);
|
||||
AssertNoValueLeak(evt);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SecretEditor_Rotate_PreservesName_Overwrites()
|
||||
{
|
||||
var (store, audit, cipher) = RegisterServices();
|
||||
var name = new SecretName("db/primary");
|
||||
var auth = this.AddTestAuthorization();
|
||||
auth.SetAuthorized("dave");
|
||||
auth.SetPolicies(SecretsAuthorization.ManagePolicy);
|
||||
|
||||
var cut = RenderComponent<SecretEditor>(p => p
|
||||
.Add(c => c.ExistingName, name)
|
||||
.Add(c => c.OnSaved, () => { }));
|
||||
|
||||
// In rotate mode the name field is read-only and shows the existing name.
|
||||
var nameInput = cut.Find("[data-testid=secret-name]");
|
||||
Assert.True(nameInput.HasAttribute("readonly"));
|
||||
Assert.Equal("db/primary", nameInput.GetAttribute("value"));
|
||||
|
||||
cut.Find("[data-testid=secret-value]").Change("rotated-value");
|
||||
cut.Find("[data-testid=editor-submit]").Click();
|
||||
|
||||
Assert.Single(store.Upserts);
|
||||
StoredSecret row = store.Upserts[0];
|
||||
Assert.Equal("db/primary", row.Name.Value);
|
||||
Assert.Equal("rotated-value", cipher.Decrypt(row));
|
||||
|
||||
Assert.Single(audit.Events);
|
||||
Assert.Equal("secret.rotate", audit.Events[0].Action);
|
||||
Assert.Equal("db/primary", audit.Events[0].Target);
|
||||
AssertNoValueLeak(audit.Events[0], "rotated-value");
|
||||
}
|
||||
|
||||
private static void AssertNoValueLeak(AuditEvent evt, string value = EnteredValue)
|
||||
{
|
||||
foreach (string? field in new[] { evt.Action, evt.Actor, evt.Category, evt.Target, evt.DetailsJson, evt.ToString() })
|
||||
{
|
||||
if (field is not null)
|
||||
{
|
||||
Assert.DoesNotContain(value, field, StringComparison.Ordinal);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
using Bunit;
|
||||
using Bunit.TestDoubles;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ZB.MOM.WW.Audit;
|
||||
using ZB.MOM.WW.Secrets.Abstractions;
|
||||
using ZB.MOM.WW.Secrets.Ui;
|
||||
using ZB.MOM.WW.Secrets.Ui.Tests.Fakes;
|
||||
|
||||
namespace ZB.MOM.WW.Secrets.Ui.Tests;
|
||||
|
||||
@@ -33,7 +35,13 @@ public class SecretsListTests : TestContext
|
||||
};
|
||||
|
||||
private void RegisterStore(params SecretMetadata[] rows)
|
||||
=> Services.AddSingleton<ISecretStore>(new FakeSecretStore(rows));
|
||||
{
|
||||
// SecretsList now embeds RevealButton / ConfirmDeleteModal / SecretEditor, which inject the
|
||||
// cipher and audit writer, so those seams must be registered even for list-only assertions.
|
||||
Services.AddSingleton<ISecretStore>(new FakeSecretStore(rows));
|
||||
Services.AddSingleton<ISecretCipher>(new FakeCipher());
|
||||
Services.AddSingleton<IAuditWriter>(new CapturingAuditWriter());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Renders_Metadata_ForEachSecret()
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
<PackageReference Include="xunit" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" />
|
||||
<PackageReference Include="coverlet.collector" />
|
||||
<PackageReference Include="ZB.MOM.WW.Audit" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user