Partial of HostPolicyService that wraps authority-mutating commands in an audit trail. It records a durable audit intent before running the mutation, re-checks that the caller's identity did not change while the intent was persisted, then completes the audit record with the result status and fails closed when storage is unavailable.
using SboxSuperuser.Engine.Audit;
namespace SboxSuperuser.Engine.Authority;
internal sealed partial class HostPolicyService
{
private async Task<CommandResult> AuditedMutationAsync( Func<ActorIdentity> authenticate, CommandOrigin origin,
Guid requestId, string command, IEnumerable<string> targets, Func<Func<ActorIdentity>, Task<CommandResult>> execute )
{
var actor = authenticate();
if ( _audit is null )
{
return await execute( authenticate );
}
if ( actor is null )
{
return new CommandResult( CommandStatus.Denied, "An authenticated caller is required." );
}
var action = new AuditAction( actor, origin, requestId, command, DateTimeOffset.UtcNow, targets: targets );
if ( !await _audit.BeginAsync( action, CancellationToken.None ) )
{
return new CommandResult( CommandStatus.StorageUnavailable, "A durable audit intent is required before changing authority." );
}
ActorIdentity BoundIdentity()
{
var current = authenticate();
return current is not null && actor.Kind == current.Kind && actor.SubjectId == current.SubjectId ? current : null;
}
var result = BoundIdentity() is null
? new CommandResult( CommandStatus.Denied, "Caller identity changed while persisting the audit intent." )
: await execute( BoundIdentity );
return await _audit.CompleteAsync( action, result.Status, CancellationToken.None ) ? result
: new CommandResult( CommandStatus.Unknown, "Authority operation has no confirmed durable audit outcome." );
}
}