Files
lmxopcua/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Tests/Runtime/GalaxyDriverSecuredWriteIdentityTests.cs
T
Joseph Doherty 29ad36c028 feat(galaxy): #1074 secured writes over gRPC data connections are a separate opt-in, default off
- WriteCaller gains an additive Origin (WriteCallerOrigin: Unspecified (default) | OpcUaSession | DataConnection),
  stamped by the runtime at the receiving seam: OtOpcUaNodeManager -> OpcUaSession, the rich (gRPC data-connection)
  write path -> DataConnection. Never parsed from the principal/key.
- New driver-config option MxAccess.SecuredWritesOverDataConnections (default false; factory key
  mxAccess.securedWritesOverDataConnections; AdminUI "Also for gRPC data-connection writes", owned path). The
  Galaxy driver gives a caller an identity source only in ApiKeyIdentity mode AND for an allowed origin:
  OpcUaSession always, DataConnection only with the option, Unspecified never (fail closed). Off => a secured write
  over a data connection is refused exactly as before #1074 (local BadUserAccessDenied, no identity lookup; the gRPC
  caller sees the same OutcomeUnknown as before).
- Tests: option off refuses a gRPC write end to end into the real GalaxyDriver (with an OPC UA positive control on
  the same driver); option-on end-to-end kept; driver-level origin matrix incl. Unspecified; factory binding; AdminUI
  round-trip; node-manager callers now carry OpcUaSession.
- Docs: Galaxy.md (origin table, opt-in, forwarded-write delegation caveat, live-gate leg), security.md,
  DataConnections.md, Configuration.md, rules/drivers.md.
2026-12-03 16:51:26 -05:00

185 lines
8.8 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Config;
using ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Runtime;
namespace ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Tests.Runtime;
/// <summary>
/// #1017 — the driver hands its writer the CALLER's Galaxy identity source only when
/// <c>MxAccess.SecuredWrites = ApiKeyIdentity</c> AND the write carries a caller; the source resolves the key's
/// own identity (<see cref="IGalaxyAckCredentialResolver.ResolveKeyIdentityAsync"/> — never the ack fallback).
/// </summary>
public sealed class GalaxyDriverSecuredWriteIdentityTests
{
private static GalaxyDriverOptions Opts(GalaxySecuredWriteMode mode, bool overDataConnections = false) => new(
new GalaxyGatewayOptions("https://mxgw.test:5001", "key"),
new GalaxyMxAccessOptions("OtOpcUa-A") { SecuredWrites = mode, SecuredWritesOverDataConnections = overDataConnections },
new GalaxyRepositoryOptions(WatchDeployEvents: false),
new GalaxyReconnectOptions())
{
RawTags = [new RawTagEntry("Plant/Tank1/SP", """{"attributeRef":"Tank1.SP"}""", false)],
};
private sealed class IdentityRecordingWriter : IGalaxyDataWriter
{
public List<Func<CancellationToken, Task<GalaxyAckCredentialResolution>>?> Sources { get; } = [];
public Task<IReadOnlyList<WriteResult>> WriteAsync(
IReadOnlyList<WriteRequest> writes, Func<string, SecurityClassification> securityResolver,
CancellationToken cancellationToken)
{
Sources.Add(null);
return Task.FromResult<IReadOnlyList<WriteResult>>([.. writes.Select(_ => new WriteResult(StatusCodeMap.Good))]);
}
Task<IReadOnlyList<WriteResult>> IGalaxyDataWriter.WriteAsync(
IReadOnlyList<WriteRequest> writes, Func<string, SecurityClassification> securityResolver,
Func<CancellationToken, Task<GalaxyAckCredentialResolution>>? securedCredential, CancellationToken cancellationToken)
{
Sources.Add(securedCredential);
return Task.FromResult<IReadOnlyList<WriteResult>>([.. writes.Select(_ => new WriteResult(StatusCodeMap.Good))]);
}
public void InvalidateHandleCaches() { }
}
private static (GalaxyDriver Driver, IdentityRecordingWriter Writer, FixedAckCredentialResolver Resolver) Build(
GalaxySecuredWriteMode mode, bool overDataConnections = false)
{
var writer = new IdentityRecordingWriter();
var resolver = FixedAckCredentialResolver.Fallback();
var driver = new GalaxyDriver("g", Opts(mode, overDataConnections), hierarchySource: null, dataWriter: writer,
ackCredentialResolver: resolver);
return (driver, writer, resolver);
}
[Fact]
public async Task ApiKeyIdentity_mode_with_a_caller_passes_a_key_only_identity_source()
{
var (driver, writer, resolver) = Build(GalaxySecuredWriteMode.ApiKeyIdentity);
await ((ICallerAwareWritable)driver).WriteAsync(
[new WriteRequest("Plant/Tank1/SP", 1.0)], new WriteCaller("svc-mes", "0a1b2c3d4e5f6071", WriteCallerOrigin.OpcUaSession),
TestContext.Current.CancellationToken);
var source = writer.Sources.ShouldHaveSingleItem().ShouldNotBeNull();
var resolution = await source(TestContext.Current.CancellationToken);
resolution.Credential.ShouldNotBeNull();
resolver.SecuredWriteKeyIds.ShouldBe(["0a1b2c3d4e5f6071"]);
resolver.RequestedKeyIds.ShouldBeEmpty("a secured write never uses the ack resolution (with its fallback)");
}
[Fact]
public async Task ApiKeyIdentity_mode_for_an_anonymous_caller_resolves_a_refusal()
{
var (driver, writer, resolver) = Build(GalaxySecuredWriteMode.ApiKeyIdentity);
await driver.WriteAsync([new WriteRequest("Plant/Tank1/SP", 1.0)], new WriteCaller("anonymous", null, WriteCallerOrigin.OpcUaSession),
TestContext.Current.CancellationToken);
var resolution = await writer.Sources.ShouldHaveSingleItem().ShouldNotBeNull()(TestContext.Current.CancellationToken);
resolution.Credential.ShouldBeNull();
resolver.SecuredWriteKeyIds.ShouldBe([null]);
}
/// <summary>
/// #1074: a data-connection caller gets NO identity source unless
/// <c>MxAccess.SecuredWritesOverDataConnections</c> is on — the writer then refuses the SecuredWrite locally,
/// exactly as before #1074 — while an OPC UA session with the same key on the same driver still does.
/// </summary>
[Fact]
public async Task ApiKeyIdentity_mode_refuses_a_data_connection_caller_unless_opted_in()
{
var (driver, writer, resolver) = Build(GalaxySecuredWriteMode.ApiKeyIdentity);
await driver.WriteAsync([new WriteRequest("Plant/Tank1/SP", 1.0)],
new WriteCaller("svc-mes", "0a1b2c3d4e5f6071", WriteCallerOrigin.DataConnection),
TestContext.Current.CancellationToken);
await driver.WriteAsync([new WriteRequest("Plant/Tank1/SP", 1.0)],
new WriteCaller("svc-mes", "0a1b2c3d4e5f6071", WriteCallerOrigin.OpcUaSession),
TestContext.Current.CancellationToken);
writer.Sources.Count.ShouldBe(2);
writer.Sources[0].ShouldBeNull("a data-connection write is refused while the opt-in is off");
writer.Sources[1].ShouldNotBeNull("positive control: the OPC UA plane is unaffected by the data-connection opt-in");
resolver.SecuredWriteKeyIds.ShouldBeEmpty();
}
[Fact]
public async Task ApiKeyIdentity_mode_with_the_data_connection_opt_in_resolves_the_connection_keys_identity()
{
var (driver, writer, resolver) = Build(GalaxySecuredWriteMode.ApiKeyIdentity, overDataConnections: true);
await driver.WriteAsync([new WriteRequest("Plant/Tank1/SP", 1.0)],
new WriteCaller("svc-mes", "0a1b2c3d4e5f6071", WriteCallerOrigin.DataConnection),
TestContext.Current.CancellationToken);
var resolution = await writer.Sources.ShouldHaveSingleItem().ShouldNotBeNull()(TestContext.Current.CancellationToken);
resolution.Credential.ShouldNotBeNull();
resolver.SecuredWriteKeyIds.ShouldBe(["0a1b2c3d4e5f6071"]);
}
[Fact]
public async Task The_data_connection_opt_in_does_nothing_in_Refuse_mode()
{
var (driver, writer, _) = Build(GalaxySecuredWriteMode.Refuse, overDataConnections: true);
await driver.WriteAsync([new WriteRequest("Plant/Tank1/SP", 1.0)],
new WriteCaller("svc", "0a1b2c3d4e5f6071", WriteCallerOrigin.DataConnection), TestContext.Current.CancellationToken);
writer.Sources.ShouldHaveSingleItem().ShouldBeNull();
}
/// <summary>#1074 fail-closed default: a caller whose origin was never stated gets no identity, opt-ins or not.</summary>
[Fact]
public async Task A_caller_with_an_unspecified_origin_passes_no_identity_source()
{
var (driver, writer, _) = Build(GalaxySecuredWriteMode.ApiKeyIdentity, overDataConnections: true);
await driver.WriteAsync([new WriteRequest("Plant/Tank1/SP", 1.0)], new WriteCaller("svc", "0a1b2c3d4e5f6071"),
TestContext.Current.CancellationToken);
writer.Sources.ShouldHaveSingleItem().ShouldBeNull();
}
[Fact]
public async Task Refuse_mode_passes_no_identity_source_even_with_a_caller()
{
var (driver, writer, _) = Build(GalaxySecuredWriteMode.Refuse);
await driver.WriteAsync([new WriteRequest("Plant/Tank1/SP", 1.0)], new WriteCaller("svc", "0a1b2c3d4e5f6071", WriteCallerOrigin.OpcUaSession),
TestContext.Current.CancellationToken);
writer.Sources.ShouldHaveSingleItem().ShouldBeNull();
}
[Fact]
public async Task A_write_without_a_caller_passes_no_identity_source()
{
var (driver, writer, _) = Build(GalaxySecuredWriteMode.ApiKeyIdentity);
await driver.WriteAsync([new WriteRequest("Plant/Tank1/SP", 1.0)], TestContext.Current.CancellationToken);
writer.Sources.ShouldHaveSingleItem().ShouldBeNull();
}
[Theory]
[InlineData(null, GalaxySecuredWriteMode.Refuse)]
[InlineData("", GalaxySecuredWriteMode.Refuse)]
[InlineData("refuse", GalaxySecuredWriteMode.Refuse)]
[InlineData("ApiKeyIdentity", GalaxySecuredWriteMode.ApiKeyIdentity)]
[InlineData("apikeyidentity", GalaxySecuredWriteMode.ApiKeyIdentity)]
public void The_factory_parses_SecuredWrites(string? value, GalaxySecuredWriteMode expected)
=> GalaxyDriverFactoryExtensions.ParseSecuredWrites(value, "g").ShouldBe(expected);
[Theory]
[InlineData("ApiKey")]
[InlineData("1")]
[InlineData("Fallback")]
public void The_factory_refuses_an_unknown_SecuredWrites_value(string value)
=> Should.Throw<InvalidOperationException>(() => GalaxyDriverFactoryExtensions.ParseSecuredWrites(value, "g"))
.Message.ShouldContain("SecuredWrites");
}