using System.Reflection;
using Grpc.Core;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using ZB.MOM.WW.Secrets.Replicator.Grpc.V1;
namespace ZB.MOM.WW.Secrets.Replicator.Grpc.Tests;
///
/// Covers the hub's authentication boundary. Every case here is a security invariant, not a
/// behaviour preference: fail-closed on an unset key, uniform denial that never says why,
/// and no token material in any log line.
///
public sealed class SecretsHubAuthInterceptorTests
{
private const string ConfiguredToken = "s3cr3t-hub-token-value";
[Fact]
public async Task Valid_bearer_token_reaches_the_continuation()
{
SecretsHubAuthInterceptor interceptor = Build(ConfiguredToken, out _);
var context = HubServerCallContextStub.WithAuthorization($"Bearer {ConfiguredToken}");
bool invoked = false;
GetManifestResponse response = await interceptor.UnaryServerHandler(
new GetManifestRequest(),
context,
(_, _) =>
{
invoked = true;
return Task.FromResult(new GetManifestResponse());
});
Assert.True(invoked);
Assert.NotNull(response);
}
[Theory]
[InlineData("Bearer wrong-token-value")]
[InlineData("Bearer s3cr3t-hub-token-valu")] // one char short — length mismatch
[InlineData("Bearer s3cr3t-hub-token-values")] // one char long
[InlineData("Bearer S3CR3T-HUB-TOKEN-VALUE")] // case must matter
public async Task Wrong_token_is_denied(string headerValue)
{
await AssertDeniedAsync(Build(ConfiguredToken, out _), headerValue);
}
[Fact]
public async Task Missing_authorization_header_is_denied()
{
await AssertDeniedAsync(Build(ConfiguredToken, out _), header: null);
}
[Theory]
[InlineData("")]
[InlineData(" ")]
[InlineData(ConfiguredToken)] // bare token, no scheme
[InlineData("Basic " + ConfiguredToken)] // wrong scheme
[InlineData("Bearer")] // scheme only
[InlineData("Bearer ")] // scheme, empty token
[InlineData("bearer " + ConfiguredToken)] // scheme case
[InlineData("Bearer " + ConfiguredToken)] // extra space is part of the token
public async Task Malformed_authorization_header_is_denied(string headerValue)
{
await AssertDeniedAsync(Build(ConfiguredToken, out _), headerValue);
}
[Fact]
public async Task Unset_configured_token_denies_a_valid_looking_request()
{
// Fail closed: with no key configured there is no credential that can be correct, so a
// well-formed bearer request is refused exactly like a malformed one.
await AssertDeniedAsync(Build(string.Empty, out _), $"Bearer {ConfiguredToken}");
}
[Fact]
public async Task Unset_configured_token_warns_once_naming_the_exact_config_key()
{
SecretsHubAuthInterceptor interceptor = Build(string.Empty, out HubCapturingLogger logger);
for (int i = 0; i < 3; i++)
{
await AssertDeniedAsync(interceptor, $"Bearer {ConfiguredToken}");
}
List warnings = logger.Entries
.Where(e => e.Level == LogLevel.Warning)
.Select(e => e.Message)
.ToList();
string warning = Assert.Single(warnings);
Assert.Contains("Secrets:GrpcHub:BearerToken", warning, StringComparison.Ordinal);
}
[Fact]
public async Task Unset_token_warning_names_the_section_path_it_was_registered_under()
{
var options = new SecretsGrpcHubServerOptions { BearerToken = string.Empty };
options.SectionPath = "Central:Secrets:Hub";
var logger = new HubCapturingLogger();
var interceptor = new SecretsHubAuthInterceptor(Options.Create(options), logger);
await AssertDeniedAsync(interceptor, "Bearer anything");
Assert.Contains(
logger.Entries,
e => e.Level == LogLevel.Warning
&& e.Message.Contains("Central:Secrets:Hub:BearerToken", StringComparison.Ordinal));
}
[Fact]
public async Task Every_failure_mode_produces_the_identical_denial()
{
SecretsHubAuthInterceptor configured = Build(ConfiguredToken, out _);
SecretsHubAuthInterceptor unset = Build(string.Empty, out _);
var details = new List
{
(await AssertDeniedAsync(configured, header: null)).Status.Detail,
(await AssertDeniedAsync(configured, "")).Status.Detail,
(await AssertDeniedAsync(configured, "Basic abc")).Status.Detail,
(await AssertDeniedAsync(configured, "Bearer ")).Status.Detail,
(await AssertDeniedAsync(configured, "Bearer wrong-token-value")).Status.Detail,
(await AssertDeniedAsync(unset, $"Bearer {ConfiguredToken}")).Status.Detail,
};
Assert.Single(details.Distinct(StringComparer.Ordinal));
// And it must not narrate the reason to the caller.
foreach (string detail in details)
{
foreach (string leak in new[] { "expired", "missing", "malformed", "unset", "no token", "wrong" })
{
Assert.DoesNotContain(leak, detail, StringComparison.OrdinalIgnoreCase);
}
}
}
[Fact]
public async Task Denied_call_never_reaches_the_continuation()
{
SecretsHubAuthInterceptor interceptor = Build(ConfiguredToken, out _);
bool invoked = false;
await Assert.ThrowsAsync(() => interceptor.UnaryServerHandler(
new GetManifestRequest(),
HubServerCallContextStub.WithAuthorization("Bearer nope"),
(_, _) =>
{
invoked = true;
return Task.FromResult(new GetManifestResponse());
}));
Assert.False(invoked);
}
[Fact]
public async Task Server_streaming_calls_are_gated_too()
{
SecretsHubAuthInterceptor interceptor = Build(ConfiguredToken, out _);
bool invoked = false;
RpcException error = await Assert.ThrowsAsync(
() => interceptor.ServerStreamingServerHandler(
new GetSecretsRequest(),
new HubServerStreamWriterStub(),
HubServerCallContextStub.WithAuthorization(header: null),
(_, _, _) =>
{
invoked = true;
return Task.CompletedTask;
}));
Assert.Equal(StatusCode.Unauthenticated, error.StatusCode);
Assert.False(invoked);
}
[Fact]
public async Task Client_streaming_calls_are_gated_too()
{
// The contract is unary-only today, so all four handler shapes are covered deliberately:
// an overload left ungated would be discovered by the first RPC that used it, in production.
SecretsHubAuthInterceptor interceptor = Build(ConfiguredToken, out _);
bool invoked = false;
RpcException error = await Assert.ThrowsAsync(
() => interceptor.ClientStreamingServerHandler(
new HubAsyncStreamReaderStub(),
HubServerCallContextStub.WithAuthorization(header: null),
(_, _) =>
{
invoked = true;
return Task.FromResult(new GetSecretsResponse());
}));
Assert.Equal(StatusCode.Unauthenticated, error.StatusCode);
Assert.False(invoked);
}
[Fact]
public async Task Duplex_streaming_calls_are_gated_too()
{
SecretsHubAuthInterceptor interceptor = Build(ConfiguredToken, out _);
bool invoked = false;
RpcException error = await Assert.ThrowsAsync(
() => interceptor.DuplexStreamingServerHandler(
new HubAsyncStreamReaderStub(),
new HubServerStreamWriterStub(),
HubServerCallContextStub.WithAuthorization(header: null),
(_, _, _) =>
{
invoked = true;
return Task.CompletedTask;
}));
Assert.Equal(StatusCode.Unauthenticated, error.StatusCode);
Assert.False(invoked);
}
[Fact]
public async Task A_valid_token_reaches_every_streaming_continuation()
{
SecretsHubAuthInterceptor interceptor = Build(ConfiguredToken, out _);
string header = $"Bearer {ConfiguredToken}";
var reached = new List();
await interceptor.ServerStreamingServerHandler(
new GetSecretsRequest(),
new HubServerStreamWriterStub(),
HubServerCallContextStub.WithAuthorization(header),
(_, _, _) =>
{
reached.Add("server");
return Task.CompletedTask;
});
await interceptor.ClientStreamingServerHandler(
new HubAsyncStreamReaderStub(),
HubServerCallContextStub.WithAuthorization(header),
(_, _) =>
{
reached.Add("client");
return Task.FromResult(new GetSecretsResponse());
});
await interceptor.DuplexStreamingServerHandler(
new HubAsyncStreamReaderStub(),
new HubServerStreamWriterStub(),
HubServerCallContextStub.WithAuthorization(header),
(_, _, _) =>
{
reached.Add("duplex");
return Task.CompletedTask;
});
Assert.Equal(["server", "client", "duplex"], reached);
}
[Fact]
public async Task No_log_line_ever_contains_token_material()
{
const string presented = "presented-token-that-must-not-be-logged";
SecretsHubAuthInterceptor configured = Build(ConfiguredToken, out HubCapturingLogger configuredLog);
SecretsHubAuthInterceptor unset = Build(string.Empty, out HubCapturingLogger unsetLog);
await AssertDeniedAsync(configured, $"Bearer {presented}");
await AssertDeniedAsync(configured, header: null);
await configured.UnaryServerHandler(
new GetManifestRequest(),
HubServerCallContextStub.WithAuthorization($"Bearer {ConfiguredToken}"),
(_, _) => Task.FromResult(new GetManifestResponse()));
await AssertDeniedAsync(unset, $"Bearer {presented}");
foreach (string message in configuredLog.Entries.Concat(unsetLog.Entries).Select(e => e.Message))
{
Assert.DoesNotContain(ConfiguredToken, message, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain(presented, message, StringComparison.OrdinalIgnoreCase);
}
}
[Fact]
public void Interceptor_has_exactly_one_public_constructor()
{
// Grpc.AspNetCore's interceptor activator silently stops invoking an interceptor whose
// constructor is ambiguous — the failure mode is an UNAUTHENTICATED gate that quietly
// stops running, so this is pinned rather than left to review.
ConstructorInfo[] ctors = typeof(SecretsHubAuthInterceptor)
.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
Assert.Single(ctors);
}
private static SecretsHubAuthInterceptor Build(string token, out HubCapturingLogger logger)
{
logger = new HubCapturingLogger();
var options = new SecretsGrpcHubServerOptions { BearerToken = token };
return new SecretsHubAuthInterceptor(Options.Create(options), logger);
}
private static async Task AssertDeniedAsync(
SecretsHubAuthInterceptor interceptor, string? header)
{
RpcException error = await Assert.ThrowsAsync(
() => interceptor.UnaryServerHandler(
new GetManifestRequest(),
HubServerCallContextStub.WithAuthorization(header),
(_, _) => Task.FromResult(new GetManifestResponse())));
Assert.Equal(StatusCode.Unauthenticated, error.StatusCode);
return error;
}
}
///
/// A minimal for exercising interceptors and service methods
/// directly. Hand-rolled because Grpc.Core.Testing.TestServerCallContext ships in a
/// package this project deliberately does not reference.
///
internal sealed class HubServerCallContextStub : ServerCallContext
{
private readonly Metadata _requestHeaders;
private readonly CancellationToken _cancellationToken;
private readonly Metadata _responseTrailers = new();
private readonly AuthContext _authContext = new(null, new Dictionary>());
private readonly Dictionary