diff --git a/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/DataConnections/ICallerAwareWriteOutcomeCapability.cs b/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/DataConnections/ICallerAwareWriteOutcomeCapability.cs new file mode 100644 index 000000000..fc640ecb5 --- /dev/null +++ b/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/DataConnections/ICallerAwareWriteOutcomeCapability.cs @@ -0,0 +1,21 @@ +namespace ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections; + +/// +/// #1091 — on behalf of a caller: the gRPC data-connection write path's +/// explicit-outcome overload for a driver that also acts on the caller's identity (; +/// the Galaxy driver's SecuredWrite attributes). The runtime calls it when the admitted write carries a +/// (the connection's key, and only when an export covering the node opts in to secured writes, +/// #1084); a write without one goes to . Either way the +/// driver reports each write's outcome definitively where it can (e.g. a refused secured write is +/// + BadUserAccessDenied, not OutcomeUnknown). +/// +public interface ICallerAwareWriteOutcomeCapability : IWriteOutcomeCapability +{ + /// Write a batch on behalf of ; one outcome per request, in order. + /// Pairs of full reference + value to write. + /// Who issued the write. + /// Cancellation token; the driver should abort the batch if cancelled. + /// One per requested write, in the same order. + Task> WriteWithOutcomesAsync( + IReadOnlyList writes, WriteCaller caller, CancellationToken cancellationToken); +} diff --git a/src/Tooling/ZB.MOM.WW.OtOpcUa.Analyzers/UnwrappedCapabilityCallAnalyzer.cs b/src/Tooling/ZB.MOM.WW.OtOpcUa.Analyzers/UnwrappedCapabilityCallAnalyzer.cs index 92a71f056..0cf1cc24c 100644 --- a/src/Tooling/ZB.MOM.WW.OtOpcUa.Analyzers/UnwrappedCapabilityCallAnalyzer.cs +++ b/src/Tooling/ZB.MOM.WW.OtOpcUa.Analyzers/UnwrappedCapabilityCallAnalyzer.cs @@ -79,6 +79,11 @@ public sealed class UnwrappedCapabilityCallAnalyzer : DiagnosticAnalyzer // work and so must run under the invoker's timeout/breaker/telemetry. "ZB.MOM.WW.OtOpcUa.Core.Abstractions.IAcknowledgedAcquisitionSource", "ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections.IWriteOutcomeCapability", + // #1091: the caller-aware overloads. Each DERIVES from a guarded interface but declares its own member, and a + // derived interface's new member is not an implementation of the base's — so `callerAware.WriteAsync(w, caller, ct)` + // on an ICallerAwareWritable receiver used to pass unflagged. They must be listed themselves. + "ZB.MOM.WW.OtOpcUa.Core.Abstractions.ICallerAwareWritable", + "ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections.ICallerAwareWriteOutcomeCapability", ]; /// @@ -109,7 +114,7 @@ public sealed class UnwrappedCapabilityCallAnalyzer : DiagnosticAnalyzer category: "OtOpcUa.Resilience", defaultSeverity: DiagnosticSeverity.Warning, isEnabledByDefault: true, - description: "Phase 6.1 Stream A requires every IReadable/IWritable/ITagDiscovery/ISubscribable/IHostConnectivityProbe/IAlarmSource/IAcknowledgedAcquisitionSource/IWriteOutcomeCapability call to route through the shared Polly pipeline. Direct calls skip the pipeline + lose per-host isolation, retry semantics, and telemetry. If the caller is Core/Server/Driver dispatch code, wrap the call in CapabilityInvoker.ExecuteAsync. If the caller is a unit test invoking the driver directly to test its wire-level behavior, either suppress with a pragma or move the suppression into a NoWarn for the test project."); + description: "Phase 6.1 Stream A requires every IReadable/IWritable/ITagDiscovery/ISubscribable/IHostConnectivityProbe/IAlarmSource/IAcknowledgedAcquisitionSource/IWriteOutcomeCapability/ICallerAwareWritable/ICallerAwareWriteOutcomeCapability call to route through the shared Polly pipeline. Direct calls skip the pipeline + lose per-host isolation, retry semantics, and telemetry. If the caller is Core/Server/Driver dispatch code, wrap the call in CapabilityInvoker.ExecuteAsync. If the caller is a unit test invoking the driver directly to test its wire-level behavior, either suppress with a pragma or move the suppression into a NoWarn for the test project."); /// public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(Rule); diff --git a/tests/Tooling/ZB.MOM.WW.OtOpcUa.Analyzers.Tests/UnwrappedCapabilityCallAnalyzerTests.cs b/tests/Tooling/ZB.MOM.WW.OtOpcUa.Analyzers.Tests/UnwrappedCapabilityCallAnalyzerTests.cs index afa46d2f5..f00512e9f 100644 --- a/tests/Tooling/ZB.MOM.WW.OtOpcUa.Analyzers.Tests/UnwrappedCapabilityCallAnalyzerTests.cs +++ b/tests/Tooling/ZB.MOM.WW.OtOpcUa.Analyzers.Tests/UnwrappedCapabilityCallAnalyzerTests.cs @@ -32,6 +32,10 @@ namespace ZB.MOM.WW.OtOpcUa.Core.Abstractions { public interface IWritable { ValueTask WriteAsync(IReadOnlyList ops, CancellationToken ct); } + public sealed class WriteCaller { } + public interface ICallerAwareWritable : IWritable { + ValueTask WriteAsync(IReadOnlyList ops, WriteCaller caller, CancellationToken ct); + } public interface ITagDiscovery { Task DiscoverAsync(CancellationToken ct); } @@ -72,6 +76,9 @@ namespace ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections { public interface IWriteOutcomeCapability { Task> WriteWithOutcomesAsync(IReadOnlyList writes, CancellationToken ct); } + public interface ICallerAwareWriteOutcomeCapability : IWriteOutcomeCapability { + Task> WriteWithOutcomesAsync(IReadOnlyList writes, ZB.MOM.WW.OtOpcUa.Core.Abstractions.WriteCaller caller, CancellationToken ct); + } } namespace ZB.MOM.WW.OtOpcUa.Core.Resilience { using System; @@ -801,6 +808,69 @@ namespace ZB.MOM.WW.OtOpcUa.Server { diags.ShouldBeEmpty(); } + /// #1091: the caller-aware write overload (a NEW member on an interface deriving from IWritable, so not an + /// implementation of IWritable.WriteAsync) trips on an interface receiver and on a concrete driver. Failed before + /// ICallerAwareWritable joined the guarded list (0 diagnostics). + [Fact] + public async Task Direct_CallerAware_WriteAsync_Call_TripsDiagnostic() + { + const string userSrc = """ +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using ZB.MOM.WW.OtOpcUa.Core.Abstractions; + +namespace ZB.MOM.WW.OtOpcUa.Server { + public sealed class CallerDriver : ICallerAwareWritable { + public ValueTask WriteAsync(IReadOnlyList ops, CancellationToken ct) => default; + public ValueTask WriteAsync(IReadOnlyList ops, WriteCaller caller, CancellationToken ct) => default; + } + public sealed class BadCallerWrite { + public async Task ViaInterface(ICallerAwareWritable driver) { + await driver.WriteAsync(new List(), new WriteCaller(), CancellationToken.None); + } + public async Task ViaConcrete(CallerDriver driver) { + await driver.WriteAsync(new List(), new WriteCaller(), CancellationToken.None); + } + } +} +"""; + var diags = await Compile(userSrc); + diags.Length.ShouldBe(2); + diags.ShouldAllBe(d => d.GetMessage().Contains("WriteAsync")); + } + + /// #1091: the caller-aware outcome overload trips too; the same calls wrapped pass (positive control). + [Fact] + public async Task CallerAware_outcome_and_write_overloads_trip_unwrapped_and_pass_wrapped() + { + const string userSrc = """ +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using ZB.MOM.WW.OtOpcUa.Core.Abstractions; +using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections; +using ZB.MOM.WW.OtOpcUa.Core.Resilience; + +namespace ZB.MOM.WW.OtOpcUa.Server { + public sealed class Mixed { + public async Task Bad(ICallerAwareWriteOutcomeCapability driver) { + var _ = await driver.WriteWithOutcomesAsync(new List(), new WriteCaller(), CancellationToken.None); + } + public async Task Good(ICallerAwareWriteOutcomeCapability rich, ICallerAwareWritable legacy, CapabilityInvoker invoker) { + var _ = await invoker.ExecuteWriteAsync("h1", false, + async ct => await rich.WriteWithOutcomesAsync(new List(), new WriteCaller(), ct), CancellationToken.None); + await invoker.ExecuteWriteAsync("h1", false, + async ct => await legacy.WriteAsync(new List(), new WriteCaller(), ct), CancellationToken.None); + } + } +} +"""; + var diags = await Compile(userSrc); + diags.Length.ShouldBe(1); + diags[0].GetMessage().ShouldContain("WriteWithOutcomesAsync"); + } + private static async Task> CompileWithoutStubs(string userSource) { var syntaxTrees = new[] { CSharpSyntaxTree.ParseText(userSource) };