Files
mxaccessgw/src/ZB.MOM.WW.MxGateway.Tests/Security/Authorization/RevocableServerCallContextTests.cs
T
Joseph Doherty 835ed0bd04 fix(auth): revoking, rotating or deleting a key ends its open gRPC streams (SEC-004)
A streaming call was authenticated only at open, and revoke/rotate/delete only
evicted the verification cache, so a leaked key's open StreamEvents/StreamAlarms
kept delivering after the revoke. The interceptor now registers every streaming
call by key id in ApiKeyStreamRegistry and gives the handler a call context
whose token also fires on revoke; the dashboard key actions call Revoke, and the
call ends with Unauthenticated.
2026-12-03 09:55:43 -05:00

41 lines
1.7 KiB
C#

using Grpc.Core;
using Microsoft.AspNetCore.Http;
using ZB.MOM.WW.MxGateway.Server.Security.Authorization;
using ZB.MOM.WW.MxGateway.Tests.TestSupport;
namespace ZB.MOM.WW.MxGateway.Tests.Security.Authorization;
/// <summary>
/// The context handed to streaming handlers (SEC-004) must behave as the real call context in
/// every respect except its cancellation token.
/// </summary>
public sealed class RevocableServerCallContextTests
{
/// <summary>Metadata, status, trailers, user state and the HttpContext all pass through.</summary>
[Fact]
public void Wrapper_ForwardsEverythingButCancellation()
{
DefaultHttpContext httpContext = new();
TestServerCallContext inner = new(
[new Metadata.Entry("x-test", "1")],
peer: "ipv4:192.0.2.1:5000");
inner.UserState["__HttpContext"] = httpContext;
using CancellationTokenSource revocation = new();
RevocableServerCallContext wrapper = new(inner, revocation.Token);
wrapper.Status = new Status(StatusCode.Unauthenticated, "revoked");
wrapper.ResponseTrailers.Add("x-trailer", "2");
Assert.Equal("ipv4:192.0.2.1:5000", wrapper.Peer);
Assert.Equal("1", wrapper.RequestHeaders.GetValue("x-test"));
Assert.Equal(StatusCode.Unauthenticated, inner.Status.StatusCode);
Assert.Equal("2", inner.ResponseTrailers.GetValue("x-trailer"));
Assert.Same(httpContext, wrapper.GetHttpContext());
Assert.False(wrapper.CancellationToken.IsCancellationRequested);
revocation.Cancel();
Assert.True(wrapper.CancellationToken.IsCancellationRequested);
Assert.False(inner.CancellationToken.IsCancellationRequested);
}
}