fix(auth): ApiKeyVerifier reports store faults as StoreUnavailable (Auth-010)

A key-store lookup that threw (shared SQL Server unreachable, SQLite I/O
fault) escaped VerifyAsync despite the "only exception path is cancellation"
contract, surfacing as an unaudited 500 / gRPC Unknown. ApiKeyFailure gains
StoreUnavailable (appended; numeric values unchanged) and the lookup maps any
non-cancellation exception to it. Unit theory and an offline refused-port
SqlServerApiKeyStore test fail pre-fix.

(scadaproj#46)

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-09 15:21:00 -05:00
parent b8d3fa2ba1
commit c4366539ca
6 changed files with 153 additions and 6 deletions
@@ -356,6 +356,45 @@ public class ApiKeyVerifierTests
// --- Cancellation ---
// --- StoreUnavailable (Auth-010, scadaproj#46) ---
public static TheoryData<Exception> StoreFaults() => new()
{
// Stand-ins for what real stores throw: a database fault (SqlClient's SqlException on a
// closed port or failover), an InvalidOperationException (e.g. a pooled connection in a bad
// state), and an I/O fault from a local SQLite file.
new System.Data.DataException("store unreachable"),
new InvalidOperationException("The connection is in a broken state."),
new IOException("disk I/O error"),
};
[Theory]
[MemberData(nameof(StoreFaults))]
public async Task VerifyAsync_StoreLookupThrows_ReturnsStoreUnavailable_InsteadOfThrowing(Exception fault)
{
var store = new FakeApiKeyStore { FindException = fault };
var verifier = BuildVerifier(store, new FakePepperProvider(Pepper));
ApiKeyVerification result = await verifier.VerifyAsync(Header(KeyId, Secret), CancellationToken.None);
Assert.False(result.Succeeded);
Assert.Equal(ApiKeyFailure.StoreUnavailable, result.Failure);
Assert.Null(result.Identity);
Assert.True(store.FindByKeyIdCalled);
Assert.False(store.MarkUsedCalled);
}
[Fact]
public async Task VerifyAsync_StoreLookupCancelled_Propagates()
{
// Cancellation stays the one exception path.
var store = new FakeApiKeyStore { FindException = new OperationCanceledException() };
var verifier = BuildVerifier(store, new FakePepperProvider(Pepper));
await Assert.ThrowsAnyAsync<OperationCanceledException>(
() => verifier.VerifyAsync(Header(KeyId, Secret), CancellationToken.None));
}
[Fact]
public async Task VerifyAsync_AlreadyCancelled_Throws()
{
@@ -398,10 +437,13 @@ public class ApiKeyVerifierTests
/// <summary>When set, <see cref="MarkUsedAsync"/> throws this exception (after recording the call).</summary>
public Exception? MarkUsedException { get; set; }
/// <summary>When set, <see cref="FindByKeyIdAsync"/> throws this exception (after recording the call).</summary>
public Exception? FindException { get; set; }
public Task<ApiKeyRecord?> FindByKeyIdAsync(string keyId, CancellationToken ct)
{
FindByKeyIdCalled = true;
return Task.FromResult(Record);
return FindException is not null ? Task.FromException<ApiKeyRecord?>(FindException) : Task.FromResult(Record);
}
public Task<ApiKeyRecord?> FindActiveByKeyIdAsync(string keyId, CancellationToken ct) =>