104 lines
5.6 KiB
C#
104 lines
5.6 KiB
C#
using Grpc.Core;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Options;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Authorization;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
|
using ZB.MOM.WW.OtOpcUa.Host.Configuration;
|
|
using ZB.MOM.WW.OtOpcUa.Host.Grpc;
|
|
using ZB.MOM.WW.OtOpcUa.Host.OpcUa;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.Redundancy;
|
|
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Redundancy;
|
|
using W = ZB.MOM.WW.OtOpcUa.Commons.Protos.DataConnection.V1;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Host.Tests.DataConnections;
|
|
|
|
[Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "P07")]
|
|
public sealed class GrpcDataServiceDispatchTests
|
|
{
|
|
[Fact]
|
|
public void Serving_provenance_uses_the_observed_role_instead_of_inventing_a_secondary_from_a_false_verdict()
|
|
{
|
|
var role = new RedundancyRoleView();
|
|
role.DataServingRole.ShouldBe("Unknown");
|
|
role.PublishPrimaryRole(false);
|
|
role.DataServingRole.ShouldBe("Unknown");
|
|
role.PublishDataServingRole(RedundancyRole.Secondary, false);
|
|
role.DataServingRole.ShouldBe("Secondary");
|
|
role.ShouldActAsPrimary.ShouldBeFalse();
|
|
var generation = role.PrimaryGeneration;
|
|
role.PublishDataServingRole(null, false);
|
|
role.DataServingRole.ShouldBe("Unknown");
|
|
role.PrimaryGeneration.ShouldBeGreaterThan(generation);
|
|
role.PublishDataServingRole(RedundancyRole.Primary, true);
|
|
role.DataServingRole.ShouldBe("Primary");
|
|
role.ShouldActAsPrimary.ShouldBeTrue();
|
|
}
|
|
[Fact]
|
|
public async Task Ingress_binds_the_trusted_caller_and_explicit_target_to_the_local_facade()
|
|
{
|
|
var fixture = Create();
|
|
var failure = await Should.ThrowAsync<RpcException>(() => fixture.Service.GetCapabilities(new() { ProtocolMajor = 1 }, fixture.Context));
|
|
failure.StatusCode.ShouldBe(StatusCode.Unavailable);
|
|
fixture.Context.WriteOptions!.Flags.ShouldBe(WriteFlags.NoCompress);
|
|
fixture.Factory.Caller!.PrincipalId.ShouldBe("peer");
|
|
fixture.Factory.Target.ShouldBe(new(DataTargetMode.Node, "cluster", "node"));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Revocation_after_preparse_authentication_prevents_facade_creation()
|
|
{
|
|
var fixture = Create();
|
|
fixture.Authority.Apply("cluster", "revoked", [], [], new Dictionary<AclScopeRef, IReadOnlyList<NodeAclPath>>());
|
|
var failure = await Should.ThrowAsync<RpcException>(() => fixture.Service.GetCapabilities(new() { ProtocolMajor = 1 }, fixture.Context));
|
|
failure.StatusCode.ShouldBe(StatusCode.Unauthenticated);
|
|
fixture.Factory.Caller.ShouldBeNull();
|
|
}
|
|
|
|
private static (DataConnectionGrpcService Service, CallContext Context, RejectingFactory Factory, DataPlaneAuthorityState Authority) Create()
|
|
{
|
|
var (bearer, id, hash) = ApiKeySecret.Generate("pepper");
|
|
var authority = new DataPlaneAuthorityState();
|
|
authority.Apply("cluster", "v1", [new ApiKey { ApiKeyId = id, Name = "test", Principal = "peer", SecretHash = hash, Enabled = true }],
|
|
[], new Dictionary<AclScopeRef, IReadOnlyList<NodeAclPath>>());
|
|
var authenticator = new DataConnectionAuthenticator(authority, Options.Create(new ApiKeyOptions { Pepper = "pepper" }), TimeProvider.System);
|
|
var options = new DataConnectionOptions { Enabled = true, ListenPort = 9443, AllowedHosts = ["source.local"], CertificatePath = "server.pem" };
|
|
var context = new CallContext();
|
|
var http = new DefaultHttpContext();
|
|
http.Connection.LocalPort = 9443;
|
|
http.Request.Protocol = "HTTP/2"; http.Request.Scheme = "https"; http.Request.Method = "POST";
|
|
http.Request.Host = new HostString("source.local", 9443); http.Request.Path = context.Method;
|
|
http.Items[typeof(DataAuthenticatedContext)] = authenticator.Authenticate(bearer);
|
|
http.Items[typeof(DataTarget)] = new DataTarget(DataTargetMode.Node, "cluster", "node");
|
|
context.UserState["__HttpContext"] = http;
|
|
var factory = new RejectingFactory();
|
|
return (new(factory, Options.Create(options), authenticator), context, factory, authority);
|
|
}
|
|
|
|
private sealed class RejectingFactory : IDataConnectionFactory
|
|
{
|
|
public DataAuthenticatedContext? Caller;
|
|
public DataTarget? Target;
|
|
public IDataConnection Create(DataAuthenticatedContext context, DataTarget target)
|
|
{ Caller = context; Target = target; throw new InvalidOperationException("Not ready."); }
|
|
}
|
|
|
|
private sealed class CallContext : ServerCallContext
|
|
{
|
|
protected override string MethodCore => DataConnectionEndpoints.ServicePrefix + "GetCapabilities";
|
|
protected override string HostCore => "source.local";
|
|
protected override string PeerCore => "ipv4:127.0.0.1:0";
|
|
protected override DateTime DeadlineCore => DateTime.MaxValue;
|
|
protected override Metadata RequestHeadersCore => [];
|
|
protected override CancellationToken CancellationTokenCore => TestContext.Current.CancellationToken;
|
|
protected override Metadata ResponseTrailersCore { get; } = [];
|
|
protected override Status StatusCore { get; set; }
|
|
protected override WriteOptions? WriteOptionsCore { get; set; }
|
|
protected override AuthContext AuthContextCore => new(null, new Dictionary<string, List<AuthProperty>>());
|
|
protected override ContextPropagationToken CreatePropagationTokenCore(ContextPropagationOptions? options) => throw new NotSupportedException();
|
|
protected override Task WriteResponseHeadersAsyncCore(Metadata responseHeaders) => Task.CompletedTask;
|
|
}
|
|
}
|