fix(server): detect a missing API-key pepper by type, not message text (mxaccessgw#195)
Auth.ApiKeys 0.3.0 throws a plain InvalidOperationException("pepper unavailable")
with no typed exception to catch, so take the issue's second option: check
IApiKeyPepperProvider before Create/Rotate and drop the message-matching catch.
Adds create/rotate missing-pepper tests.
This commit is contained in:
+39
-2
@@ -280,6 +280,7 @@ public sealed class DashboardApiKeyManagementServiceTests : IDisposable
|
||||
new DashboardApiKeyAuthorization(),
|
||||
services.GetRequiredService<ApiKeyAdminCommands>(),
|
||||
services.GetRequiredService<IApiKeyAdminStore>(),
|
||||
services.GetRequiredService<ZB.MOM.WW.Auth.ApiKeys.IApiKeyPepperProvider>(),
|
||||
recordingWriter,
|
||||
new HttpContextAccessor { HttpContext = httpContext });
|
||||
|
||||
@@ -299,6 +300,41 @@ public sealed class DashboardApiKeyManagementServiceTests : IDisposable
|
||||
Assert.Equal(ZB.MOM.WW.Audit.AuditOutcome.Success, dashboardEvent.Outcome);
|
||||
}
|
||||
|
||||
/// <summary>Verifies that create reports a missing pepper as a failure result without persisting a key (mxaccessgw#195).</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task CreateAsync_PepperNotConfigured_FailsWithoutCreating()
|
||||
{
|
||||
await using ServiceProvider services = BuildServices(pepper: null);
|
||||
DashboardApiKeyManagementService service = CreateService(services);
|
||||
|
||||
DashboardApiKeyManagementResult result = await service.CreateAsync(
|
||||
CreateAuthorizedUser(),
|
||||
CreateRequest(),
|
||||
CancellationToken.None);
|
||||
|
||||
Assert.False(result.Succeeded);
|
||||
Assert.Equal("API key pepper is not configured.", result.Message);
|
||||
Assert.Empty(await ListAsync(services));
|
||||
}
|
||||
|
||||
/// <summary>Verifies that rotate reports a missing pepper as a failure result (mxaccessgw#195).</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task RotateAsync_PepperNotConfigured_Fails()
|
||||
{
|
||||
await using ServiceProvider services = BuildServices(pepper: null);
|
||||
DashboardApiKeyManagementService service = CreateService(services);
|
||||
|
||||
DashboardApiKeyManagementResult result = await service.RotateAsync(
|
||||
CreateAuthorizedUser(),
|
||||
"operator01",
|
||||
CancellationToken.None);
|
||||
|
||||
Assert.False(result.Succeeded);
|
||||
Assert.Equal("API key pepper is not configured.", result.Message);
|
||||
}
|
||||
|
||||
private DashboardApiKeyManagementService CreateService(ServiceProvider services)
|
||||
{
|
||||
DefaultHttpContext httpContext = new();
|
||||
@@ -308,11 +344,12 @@ public sealed class DashboardApiKeyManagementServiceTests : IDisposable
|
||||
new DashboardApiKeyAuthorization(),
|
||||
services.GetRequiredService<ApiKeyAdminCommands>(),
|
||||
services.GetRequiredService<IApiKeyAdminStore>(),
|
||||
services.GetRequiredService<ZB.MOM.WW.Auth.ApiKeys.IApiKeyPepperProvider>(),
|
||||
services.GetRequiredService<ZB.MOM.WW.Audit.IAuditWriter>(),
|
||||
new HttpContextAccessor { HttpContext = httpContext });
|
||||
}
|
||||
|
||||
private ServiceProvider BuildServices()
|
||||
private ServiceProvider BuildServices(string? pepper = "test-pepper")
|
||||
{
|
||||
TempDatabaseDirectory directory = TempDatabaseDirectory.Create("mxgateway-dashboard-apikey-tests");
|
||||
_tempDirectories.Add(directory);
|
||||
@@ -322,7 +359,7 @@ public sealed class DashboardApiKeyManagementServiceTests : IDisposable
|
||||
new Dictionary<string, string?>
|
||||
{
|
||||
["MxGateway:Authentication:SqlitePath"] = directory.DatabasePath(),
|
||||
["MxGateway:ApiKeyPepper"] = "test-pepper"
|
||||
["MxGateway:ApiKeyPepper"] = pepper,
|
||||
})
|
||||
.Build();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user