refactor: migrate OPC UA server SDK 2.0 compilation and boundaries

This commit is contained in:
Joseph Doherty
2026-11-10 10:04:14 -05:00
parent d2076e75c6
commit 9af1a824b3
14 changed files with 416 additions and 451 deletions
@@ -297,7 +297,7 @@ public sealed class DataPlaneAuthorizer
// The s= identifier, read once: it is both the cache key and the raw material the scope is split
// from, and pulling it up here absorbs what used to be TryBuildScope's own guard. A NodeId that
// carries no string addresses no ACL scope at all, so it can never be answered from the cache.
if (nodeId?.Identifier is not string identifier || identifier.Length == 0)
if (!nodeId.TryGetValue(out string identifier) || identifier.Length == 0)
{
return Deny(operation, nodeId, identity.Principal,
"the node id carries no string identifier, so it addresses no ACL scope", cached);
@@ -1,106 +0,0 @@
using System.Collections;
using System.Net;
using System.Reflection;
using Opc.Ua;
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.Security;
/// <summary>
/// Resolves the client's remote IP endpoint for a secure channel, so session activation can account
/// pre-authentication API-key misses per client (lmxopcua#751).
/// </summary>
/// <remarks>
/// <para>
/// ⚠️ <b>The OPC UA SDK (1.5.378) does not expose the client address on any public surface</b> — not
/// on <c>ISession</c>, <c>SecureChannelContext</c>, <c>ImpersonateEventArgs</c> nor the transport
/// listener. The TCP listener holds it privately: <c>TcpTransportListener.m_channels</c> (channel id
/// → channel) → the channel's non-public <c>Socket</c> → the public
/// <c>IMessageSocket.RemoteEndpoint</c>. This class walks that path by reflection.
/// </para>
/// <para>
/// It is strictly <b>best effort and fail-soft</b>: any missing member (an SDK upgrade renamed it), an
/// unknown channel or a closed socket yields <see langword="null"/>, and a caller treats null as
/// "address unknown" — the per-endpoint budget is skipped and the process-wide one still applies. It
/// never throws. An over-the-wire test (<c>RemoteEndpointResolutionTests</c>) pins that it resolves on
/// the SDK version in use, so an upgrade that breaks the path fails a test instead of silently
/// degrading to process-wide accounting.
/// </para>
/// </remarks>
public static class SecureChannelRemoteEndpoint
{
private const BindingFlags Instance = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
/// <summary>
/// Finds the remote endpoint of the secure channel <paramref name="secureChannelId"/> among
/// <paramref name="listeners"/>.
/// </summary>
/// <param name="listeners">The server's transport listeners.</param>
/// <param name="secureChannelId">The global secure channel id (<c>{ListenerId}-{ChannelId}</c>).</param>
/// <returns>The client's IP endpoint (IPv4-mapped addresses normalised to IPv4), or null when unknown.</returns>
public static IPEndPoint? Resolve(IEnumerable<ITransportListener>? listeners, string? secureChannelId)
{
if (listeners is null || string.IsNullOrEmpty(secureChannelId)) return null;
try
{
foreach (var listener in listeners)
{
if (listener is null) continue;
var listenerId = listener.ListenerId;
if (string.IsNullOrEmpty(listenerId)
|| !secureChannelId.StartsWith(listenerId + "-", StringComparison.Ordinal)
|| !uint.TryParse(secureChannelId.AsSpan(listenerId.Length + 1), out var channelId))
{
continue;
}
if (FindChannel(listener, channelId) is { } channel && RemoteEndpointOf(channel) is { } endpoint)
return endpoint;
}
}
catch
{
// Fail-soft by contract: the caller falls back to process-wide accounting.
}
return null;
}
/// <summary>The listener's channel with id <paramref name="channelId"/>, read from its private channel map.</summary>
private static object? FindChannel(ITransportListener listener, uint channelId)
{
var field = FindField(listener.GetType(), "m_channels");
if (field?.GetValue(listener) is not IDictionary channels) return null;
return channels.Contains(channelId) ? channels[channelId] : null;
}
/// <summary>The channel's socket's remote endpoint, as an <see cref="IPEndPoint"/>.</summary>
private static IPEndPoint? RemoteEndpointOf(object channel)
{
var socketProperty = FindProperty(channel.GetType(), "Socket");
var socket = socketProperty?.GetValue(channel);
if (socket is null) return null;
var remote = FindProperty(socket.GetType(), "RemoteEndpoint")?.GetValue(socket) as IPEndPoint;
if (remote is null) return null;
return remote.Address.IsIPv4MappedToIPv6 ? new IPEndPoint(remote.Address.MapToIPv4(), remote.Port) : remote;
}
private static FieldInfo? FindField(Type? type, string name)
{
for (; type is not null; type = type.BaseType)
{
if (type.GetField(name, Instance | BindingFlags.DeclaredOnly) is { } field) return field;
}
return null;
}
private static PropertyInfo? FindProperty(Type? type, string name)
{
for (; type is not null; type = type.BaseType)
{
if (type.GetProperty(name, Instance | BindingFlags.DeclaredOnly) is { } property) return property;
}
return null;
}
}