fix(secrets-ui): preserve metadata on rotate + audit unexpected mutation/reveal failures

This commit is contained in:
Joseph Doherty
2026-07-15 17:31:34 -04:00
parent 0ab276dac0
commit 294f53b49a
8 changed files with 306 additions and 37 deletions
@@ -82,13 +82,26 @@
[CascadingParameter] private Task<AuthenticationState>? AuthState { get; set; }
private bool IsRotate => ExistingName is not null;
private bool _metadataLoaded;
/// <inheritdoc />
protected override void OnParametersSet()
protected override async Task OnParametersSetAsync()
{
if (IsRotate)
{
_name = ExistingName!.Value.Value;
// Rotate must preserve the existing row's metadata: the store's upsert overwrites the
// description and content type from the incoming row, so pre-fill both from the current
// row (metadata only — no plaintext is read or decrypted). Load once so a re-render does
// not clobber operator edits. The operator still supplies the NEW value.
if (!_metadataLoaded)
{
_metadataLoaded = true;
StoredSecret? row = await Store.GetAsync(ExistingName.Value, _cts.Token);
_description = row?.Description ?? string.Empty;
_contentType = row?.ContentType ?? SecretContentType.Text;
}
}
}
@@ -108,37 +121,79 @@
}
_busy = true;
bool saved = false;
try
{
string actor = await SecretActorResolver.ResolveAsync(Services, AuthState);
StoredSecret row = Cipher.Encrypt(name, _value, _contentType);
row = row with
try
{
Description = string.IsNullOrWhiteSpace(_description) ? null : _description,
CreatedBy = actor,
UpdatedBy = actor,
};
await Store.UpsertAsync(row, _cts.Token);
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);
saved = true;
}
catch (Exception)
{
// An unexpected store/cipher fault (e.g. master key unavailable, timeout) must still
// be audited as a Failure and surfaced to the operator — never the value — instead of
// tearing down the circuit. The detail carries no plaintext.
_error = IsRotate ? "Unable to rotate the secret." : "Unable to add the secret.";
await WriteFailureAuditAsync(
actor, IsRotate ? "secret.rotate" : "secret.add", name.Value);
}
}
finally
{
_busy = false;
}
if (saved)
{
await OnSaved.InvokeAsync();
}
}
private async Task WriteFailureAuditAsync(string actor, string action, string target)
{
try
{
await Audit.WriteAsync(
new AuditEvent
{
EventId = Guid.NewGuid(),
OccurredAtUtc = DateTimeOffset.UtcNow,
Actor = actor,
Action = IsRotate ? "secret.rotate" : "secret.add",
Outcome = AuditOutcome.Success,
Action = action,
Outcome = AuditOutcome.Failure,
Category = "Secrets",
Target = name.Value,
Target = target,
DetailsJson = "{\"reason\":\"error\"}",
},
_cts.Token);
await OnSaved.InvokeAsync();
}
finally
catch
{
_busy = false;
// Best-effort audit of a failure; never let audit itself surface into the event pipeline.
}
}