feat(secrets-ui): add/rotate/delete modal + audited gated reveal

This commit is contained in:
Joseph Doherty
2026-07-15 17:20:01 -04:00
parent d7f8ca455b
commit 21556cc1a7
15 changed files with 950 additions and 14 deletions
@@ -0,0 +1,95 @@
@namespace ZB.MOM.WW.Secrets.Ui.Components
@implements IDisposable
@inject ISecretStore Store
@inject IAuditWriter Audit
@inject IServiceProvider Services
@* Components/ConfirmDeleteModal.razor — an in-page Blazor overlay (NOT a JS confirm/alert dialog).
Shown while Visible is true; Confirm tombstones + audits the delete, Cancel dismisses. *@
@if (Visible)
{
<div class="modal-backdrop-inpage" data-testid="delete-modal-backdrop"></div>
<div class="modal" role="dialog" aria-modal="true" data-testid="delete-modal">
<div class="modal-card">
<h3 class="modal-title">Delete secret '@Name.Value'?</h3>
<p>This tombstones the secret so the removal can propagate. The action is audited.</p>
@if (_error is not null)
{
<p class="field-error s-bad" data-testid="delete-error">@_error</p>
}
<div class="btn-row">
<TechButton Variant="ButtonVariant.Danger" Busy="_busy"
data-testid="confirm-delete" @onclick="ConfirmAsync">
Delete
</TechButton>
<TechButton Variant="ButtonVariant.Secondary"
data-testid="cancel-delete" @onclick="CancelAsync">
Cancel
</TechButton>
</div>
</div>
</div>
}
@code {
private readonly CancellationTokenSource _cts = new();
private string? _error;
private bool _busy;
/// <summary>Whether the modal overlay is rendered.</summary>
[Parameter] public bool Visible { get; set; }
/// <summary>The secret the modal offers to delete.</summary>
[Parameter] public SecretName Name { get; set; }
/// <summary>Raised after the secret has been tombstoned and audited.</summary>
[Parameter] public EventCallback OnDeleted { get; set; }
/// <summary>Raised when the user cancels the deletion.</summary>
[Parameter] public EventCallback OnCancelled { get; set; }
/// <summary>The cascading authentication state, used as the fallback source of the audit actor.</summary>
[CascadingParameter] private Task<AuthenticationState>? AuthState { get; set; }
private async Task ConfirmAsync()
{
_error = null;
_busy = true;
try
{
string actor = await SecretActorResolver.ResolveAsync(Services, AuthState);
await Store.DeleteAsync(Name, actor, _cts.Token);
await Audit.WriteAsync(
new AuditEvent
{
EventId = Guid.NewGuid(),
OccurredAtUtc = DateTimeOffset.UtcNow,
Actor = actor,
Action = "secret.delete",
Outcome = AuditOutcome.Success,
Category = "Secrets",
Target = Name.Value,
},
_cts.Token);
await OnDeleted.InvokeAsync();
}
finally
{
_busy = false;
}
}
private Task CancelAsync() => OnCancelled.InvokeAsync();
/// <inheritdoc />
public void Dispose()
{
_cts.Cancel();
_cts.Dispose();
}
}
@@ -0,0 +1,110 @@
@namespace ZB.MOM.WW.Secrets.Ui.Components
@implements IDisposable
@inject ISecretStore Store
@inject ISecretCipher Cipher
@inject IAuditWriter Audit
@inject IServiceProvider Services
@* Components/RevealButton.razor — a policy-gated, audited reveal. The plaintext is loaded and
shown only AFTER the click, straight from the store+cipher (a human reveal shows the CURRENT
value, distinct from any cached machine resolve). The value never enters an audit field. *@
@if (!_revealed)
{
<button type="button" class="btn btn-outline-secondary reveal-secret"
disabled="@_busy" data-secret-name="@Name.Value"
data-testid="reveal-button" @onclick="RevealAsync">
Reveal
</button>
}
else
{
<div class="reveal-panel" data-testid="reveal-panel">
@if (_value is not null)
{
<code class="revealed-value" data-testid="revealed-value">@_value</code>
}
else
{
<span class="field-error s-bad" data-testid="reveal-error">@(_error ?? "Unavailable.")</span>
}
<button type="button" class="btn btn-link hide-secret"
data-testid="hide-button" @onclick="Hide">
Hide
</button>
</div>
}
@code {
private readonly CancellationTokenSource _cts = new();
private bool _revealed;
private string? _value;
private string? _error;
private bool _busy;
/// <summary>The secret whose plaintext this button reveals.</summary>
[Parameter] public SecretName Name { get; set; }
/// <summary>The cascading authentication state, used as the fallback source of the audit actor.</summary>
[CascadingParameter] private Task<AuthenticationState>? AuthState { get; set; }
private async Task RevealAsync()
{
_busy = true;
AuditOutcome outcome;
try
{
StoredSecret? row = await Store.GetAsync(Name, _cts.Token);
if (row is null)
{
_value = null;
_error = "Secret not found.";
outcome = AuditOutcome.Failure;
}
else
{
_value = Cipher.Decrypt(row);
_error = null;
outcome = AuditOutcome.Success;
}
}
catch (SecretDecryptionException)
{
_value = null;
_error = "Unable to decrypt secret.";
outcome = AuditOutcome.Failure;
}
finally
{
_busy = false;
_revealed = true;
}
string actor = await SecretActorResolver.ResolveAsync(Services, AuthState);
await Audit.WriteAsync(
new AuditEvent
{
EventId = Guid.NewGuid(),
OccurredAtUtc = DateTimeOffset.UtcNow,
Actor = actor,
Action = "secret.reveal",
Outcome = outcome,
Category = "Secrets",
Target = Name.Value,
},
_cts.Token);
}
private void Hide()
{
_revealed = false;
_value = null;
_error = null;
}
/// <inheritdoc />
public void Dispose()
{
_cts.Cancel();
_cts.Dispose();
}
}
@@ -0,0 +1,153 @@
@namespace ZB.MOM.WW.Secrets.Ui.Components
@implements IDisposable
@inject ISecretCipher Cipher
@inject ISecretStore Store
@inject IAuditWriter Audit
@inject IServiceProvider Services
@* Components/SecretEditor.razor — add (ExistingName == null) or rotate (ExistingName set) a secret.
The value is entered through a masked input and never rendered back or placed in any audit field. *@
<TechCard Title="@(IsRotate ? "Rotate secret" : "Add secret")">
<TechField Label="Name">
@if (IsRotate)
{
<input class="form-control mono" type="text" readonly
value="@_name" data-testid="secret-name" />
}
else
{
<input class="form-control mono" type="text"
@bind="_name" data-testid="secret-name" />
}
</TechField>
<TechField Label="Description">
<input class="form-control" type="text" @bind="_description" data-testid="secret-description" />
</TechField>
<TechField Label="Content type">
<select class="form-select" @bind="_contentType" data-testid="secret-content-type">
@foreach (SecretContentType ct in Enum.GetValues<SecretContentType>())
{
<option value="@ct">@ct</option>
}
</select>
</TechField>
<TechField Label="Value">
<input class="form-control mono" type="password" autocomplete="new-password"
@bind="_value" data-testid="secret-value" />
</TechField>
@if (_error is not null)
{
<p class="field-error s-bad" data-testid="editor-error">@_error</p>
}
<div class="btn-row">
<TechButton Variant="ButtonVariant.Primary" Busy="_busy"
data-testid="editor-submit" @onclick="SubmitAsync">
@(IsRotate ? "Rotate" : "Add")
</TechButton>
<TechButton Variant="ButtonVariant.Secondary"
data-testid="editor-cancel" @onclick="CancelAsync">
Cancel
</TechButton>
</div>
</TechCard>
@code {
private readonly CancellationTokenSource _cts = new();
private string _name = string.Empty;
private string _description = string.Empty;
private SecretContentType _contentType = SecretContentType.Text;
private string _value = string.Empty;
private string? _error;
private bool _busy;
/// <summary>
/// The secret being rotated. When <c>null</c> the editor is in <em>add</em> mode (name is
/// editable); when set, the editor is in <em>rotate</em> mode (name is read-only and fixed).
/// </summary>
[Parameter] public SecretName? ExistingName { get; set; }
/// <summary>Raised after a successful add or rotate, so a parent can refresh its listing.</summary>
[Parameter] public EventCallback OnSaved { get; set; }
/// <summary>Raised when the user cancels the editor without saving.</summary>
[Parameter] public EventCallback OnCancelled { get; set; }
/// <summary>The cascading authentication state, used as the fallback source of the audit actor.</summary>
[CascadingParameter] private Task<AuthenticationState>? AuthState { get; set; }
private bool IsRotate => ExistingName is not null;
/// <inheritdoc />
protected override void OnParametersSet()
{
if (IsRotate)
{
_name = ExistingName!.Value.Value;
}
}
private async Task SubmitAsync()
{
_error = null;
SecretName name;
try
{
name = ExistingName ?? new SecretName(_name);
}
catch (ArgumentException ex)
{
_error = ex.Message;
return;
}
_busy = true;
try
{
string actor = await SecretActorResolver.ResolveAsync(Services, AuthState);
StoredSecret row = Cipher.Encrypt(name, _value, _contentType);
row = row with
{
Description = string.IsNullOrWhiteSpace(_description) ? null : _description,
CreatedBy = actor,
UpdatedBy = actor,
};
await Store.UpsertAsync(row, _cts.Token);
await Audit.WriteAsync(
new AuditEvent
{
EventId = Guid.NewGuid(),
OccurredAtUtc = DateTimeOffset.UtcNow,
Actor = actor,
Action = IsRotate ? "secret.rotate" : "secret.add",
Outcome = AuditOutcome.Success,
Category = "Secrets",
Target = name.Value,
},
_cts.Token);
await OnSaved.InvokeAsync();
}
finally
{
_busy = false;
}
}
private Task CancelAsync() => OnCancelled.InvokeAsync();
/// <inheritdoc />
public void Dispose()
{
_cts.Cancel();
_cts.Dispose();
}
}
@@ -1,7 +1,24 @@
@namespace ZB.MOM.WW.Secrets.Ui
@implements IDisposable
@inject ISecretStore Store
@* Components/SecretsList.razor — metadata-only listing of secrets. Never renders a value or ciphertext. *@
@* Components/SecretsList.razor — metadata-only listing of secrets. Never renders a value or
ciphertext. Add/rotate use the in-page SecretEditor; delete uses the in-page ConfirmDeleteModal;
reveal is the policy-gated, audited RevealButton. The list reloads after any mutation. *@
<AuthorizeView Policy="@SecretsAuthorization.ManagePolicy">
<div class="toolbar btn-row">
<TechButton Variant="ButtonVariant.Primary" data-testid="add-secret" @onclick="ShowAdd">
Add secret
</TechButton>
</div>
</AuthorizeView>
@if (_editorMode != EditorMode.None)
{
<SecretEditor ExistingName="_rotateName"
OnSaved="OnMutatedAsync"
OnCancelled="CloseEditor" />
}
<TechCard Title="Secrets">
@if (_secrets is null)
@@ -43,16 +60,20 @@
<td class="num">@secret.Revision</td>
<td class="mono">@secret.UpdatedUtc.UtcDateTime.ToString("u")</td>
<td>@secret.UpdatedBy</td>
<td>
<td class="row-actions">
<AuthorizeView Policy="@SecretsAuthorization.RevealPolicy">
@* Placeholder — actual reveal wiring is a later task. Gated so it
renders only for principals holding the reveal policy. *@
<button type="button"
class="btn btn-outline-secondary reveal-secret"
disabled
title="Reveal (coming soon)"
data-secret-name="@secret.Name.Value">
Reveal
<RevealButton Name="secret.Name" />
</AuthorizeView>
<AuthorizeView Policy="@SecretsAuthorization.ManagePolicy">
<button type="button" class="btn btn-outline-secondary rotate-secret"
data-secret-name="@secret.Name.Value"
@onclick="() => ShowRotate(secret.Name)">
Rotate
</button>
<button type="button" class="btn btn-danger delete-secret"
data-secret-name="@secret.Name.Value"
@onclick="() => ShowDelete(secret.Name)">
Delete
</button>
</AuthorizeView>
</td>
@@ -65,14 +86,59 @@
}
</TechCard>
<ConfirmDeleteModal Visible="_deleteTarget.HasValue"
Name="_deleteTarget.GetValueOrDefault()"
OnDeleted="OnMutatedAsync"
OnCancelled="CloseDelete" />
@code {
private readonly CancellationTokenSource _cts = new();
private IReadOnlyList<SecretMetadata>? _secrets;
/// <inheritdoc />
protected override async Task OnInitializedAsync()
private EditorMode _editorMode = EditorMode.None;
private SecretName? _rotateName;
private SecretName? _deleteTarget;
private enum EditorMode
{
_secrets = await Store.ListAsync(includeDeleted: false, _cts.Token);
None,
Add,
Rotate,
}
/// <inheritdoc />
protected override async Task OnInitializedAsync() => await ReloadAsync();
private async Task ReloadAsync()
=> _secrets = await Store.ListAsync(includeDeleted: false, _cts.Token);
private void ShowAdd()
{
_rotateName = null;
_editorMode = EditorMode.Add;
}
private void ShowRotate(SecretName name)
{
_rotateName = name;
_editorMode = EditorMode.Rotate;
}
private void CloseEditor()
{
_editorMode = EditorMode.None;
_rotateName = null;
}
private void ShowDelete(SecretName name) => _deleteTarget = name;
private void CloseDelete() => _deleteTarget = null;
private async Task OnMutatedAsync()
{
CloseEditor();
CloseDelete();
await ReloadAsync();
}
/// <inheritdoc />
@@ -0,0 +1,45 @@
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.DependencyInjection;
using ZB.MOM.WW.Secrets.Abstractions;
namespace ZB.MOM.WW.Secrets.Ui;
/// <summary>
/// Resolves the principal to record as the audit <c>Actor</c> for a UI-driven secret mutation or
/// reveal. The resolution order is: an explicitly wired <see cref="ISecretActorAccessor"/> (if the
/// host registered one and it reports a non-blank actor); otherwise the authenticated user's
/// <see cref="System.Security.Principal.IIdentity.Name"/> from the cascading authentication state;
/// otherwise the literal <c>"unknown"</c> so an action is never attributed to a blank actor.
/// </summary>
internal static class SecretActorResolver
{
/// <summary>The fallback actor used when no accessor and no authenticated identity name is available.</summary>
public const string Unknown = "unknown";
/// <summary>
/// Resolves the audit actor. Never returns <c>null</c> or blank.
/// </summary>
/// <param name="services">The component's service provider, used to optionally resolve an <see cref="ISecretActorAccessor"/>.</param>
/// <param name="authState">The cascading authentication-state task, or <c>null</c> if unavailable.</param>
/// <returns>The resolved actor identity string.</returns>
public static async Task<string> ResolveAsync(IServiceProvider services, Task<AuthenticationState>? authState)
{
string? fromAccessor = services.GetService<ISecretActorAccessor>()?.CurrentActor;
if (!string.IsNullOrWhiteSpace(fromAccessor))
{
return fromAccessor;
}
if (authState is not null)
{
AuthenticationState state = await authState.ConfigureAwait(false);
string? name = state.User.Identity?.Name;
if (!string.IsNullOrWhiteSpace(name))
{
return name;
}
}
return Unknown;
}
}
@@ -29,6 +29,7 @@
<ItemGroup>
<PackageReference Include="ZB.MOM.WW.Theme" />
<PackageReference Include="ZB.MOM.WW.Auth.AspNetCore" />
<PackageReference Include="ZB.MOM.WW.Audit" />
</ItemGroup>
<ItemGroup>
@@ -2,6 +2,9 @@
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Authorization
@using Microsoft.Extensions.DependencyInjection
@using ZB.MOM.WW.Theme
@using ZB.MOM.WW.Audit
@using ZB.MOM.WW.Secrets.Abstractions
@using ZB.MOM.WW.Secrets.Ui
@using ZB.MOM.WW.Secrets.Ui.Components