refactor: migrate OPC UA driver and browser to SDK 2.0

This commit is contained in:
Joseph Doherty
2026-11-10 10:08:12 -05:00
parent 586ce9422d
commit 52a4476b21
10 changed files with 171 additions and 113 deletions
@@ -196,7 +196,6 @@ public sealed class NamespaceMap
/// <returns>The server-stable reference string for <paramref name="nodeId"/>.</returns>
public string ToStableReference(NodeId nodeId)
{
ArgumentNullException.ThrowIfNull(nodeId);
// Namespace 0 is fixed by the spec — the compact form is already stable.
if (nodeId.NamespaceIndex == 0)
return nodeId.ToString() ?? string.Empty;
@@ -238,7 +237,7 @@ public sealed class NamespaceMap
// reorder since the reference was captured resolves correctly. Plain ns=N forms
// resolve directly.
nodeId = NodeId.Parse(currentSession.MessageContext, reference);
return !NodeId.IsNull(nodeId);
return !nodeId.IsNull;
}
catch
{
@@ -249,10 +248,10 @@ public sealed class NamespaceMap
/// <summary>Render just the identifier portion (<c>s=…</c>, <c>i=…</c>, <c>g=…</c>, <c>b=…</c>) of a NodeId.</summary>
private static string IdentifierPart(NodeId nodeId) => nodeId.IdType switch
{
IdType.Numeric => $"i={nodeId.Identifier}",
IdType.String => $"s={nodeId.Identifier}",
IdType.Guid => $"g={nodeId.Identifier}",
IdType.Opaque => $"b={Convert.ToBase64String((byte[])nodeId.Identifier)}",
_ => $"s={nodeId.Identifier}",
IdType.Numeric => $"i={nodeId.IdentifierAsString}",
IdType.String => $"s={nodeId.IdentifierAsString}",
IdType.Guid => $"g={nodeId.IdentifierAsString}",
IdType.Opaque => $"b={nodeId.IdentifierAsString}",
_ => throw new ArgumentOutOfRangeException(nameof(nodeId)),
};
}
@@ -1,4 +1,5 @@
using Opc.Ua;
using Opc.Ua.Security.Certificates;
using ZB.MOM.WW.Secrets.Abstractions;
namespace ZB.MOM.WW.OtOpcUa.Driver.OpcUaClient;
@@ -37,7 +38,37 @@ public static class OpcUaUserIdentityFactory
/// </summary>
/// <param name="options">Driver options containing certificate configuration.</param>
/// <returns>The certificate-backed <see cref="UserIdentity"/>.</returns>
public static UserIdentity FromCertificate(OpcUaClientDriverOptions options) => new(LoadCertificate(options));
public static UserIdentity FromCertificate(OpcUaClientDriverOptions options)
{
using var certificate = LoadCertificate(options);
var identifier = new CertificateIdentifier { Thumbprint = certificate.Thumbprint };
return new UserIdentity(new X509IdentityTokenHandler(identifier,
new CertificatePasswordProvider(), new FileCertificateProvider(options, certificate.Thumbprint)));
}
// The configured credential is a PFX file, rather than an SDK certificate store. SDK 2 resolves
// signing keys on demand and disposes each returned Certificate. Pin the public identity for this
// lifecycle; a file/secret rotation is picked up only by the next initialise/open.
private sealed class FileCertificateProvider(OpcUaClientDriverOptions options, string thumbprint) : ICertificateProvider
{
public Certificate? TryGetPrivateKeyCertificate(string requestedThumbprint) => null;
public ValueTask<Certificate?> GetPrivateKeyCertificateAsync(CertificateIdentifier identifier,
ICertificatePasswordProvider? passwordProvider = null, string? applicationUri = null,
CancellationToken ct = default)
{
ct.ThrowIfCancellationRequested();
var certificate = LoadCertificate(options);
if (!string.Equals(certificate.Thumbprint, thumbprint, StringComparison.OrdinalIgnoreCase)
|| !string.Equals(identifier.Thumbprint, thumbprint, StringComparison.OrdinalIgnoreCase))
{
certificate.Dispose();
throw new ServiceResultException(StatusCodes.BadIdentityTokenInvalid,
"The user certificate changed; reopen the session to use the rotated credential.");
}
return ValueTask.FromResult<Certificate?>(Certificate.From(certificate));
}
}
/// <summary>
/// Loads (and validates) the user-authentication certificate <see cref="FromCertificate"/> wraps. The caller owns
@@ -0,0 +1,26 @@
using Opc.Ua;
namespace ZB.MOM.WW.OtOpcUa.Driver.OpcUaClient;
/// <summary>Detaches SDK values at the driver boundary while preserving deployed CLR payload types.</summary>
public static class UpstreamValueMapping
{
/// <summary>Materializes scalar and one-dimensional array values without flattening matrices.</summary>
/// <param name="value">The upstream value.</param>
/// <returns>The independent CLR payload.</returns>
public static object? ToClr(Variant value) => Detach(value.AsBoxedObject(Variant.BoxingBehavior.Legacy));
private static object? Detach(object? value) => value switch
{
ByteString bytes => bytes.IsNull ? null : bytes.ToArray(),
DateTimeUtc time => time.ToDateTime(),
Uuid uuid => (Guid)uuid,
ByteString[] bytes => bytes.Select(b => b.IsNull ? null : b.ToArray()).ToArray(),
DateTimeUtc[] times => times.Select(t => t.ToDateTime()).ToArray(),
Uuid[] uuids => uuids.Select(u => (Guid)u).ToArray(),
Variant[] variants => variants.Select(ToClr).ToArray(),
object?[] objects => objects.Select(Detach).ToArray(),
Array array => array.Clone(),
_ => value,
};
}
@@ -34,7 +34,6 @@
ContractsIsDependencyLightTests in Driver.OpcUaClient.Tests.
-->
<ItemGroup>
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.MigrationAnalyzer" PrivateAssets="all" IncludeAssets="analyzers;build;buildtransitive" />
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Client" />
<!-- OpcUaUserIdentityFactory: the secret: resolution of the credential fields feeding the SDK UserIdentity is
shared by the driver and the AdminUI browse (review 2026-10-21 O-5), so the browse presents exactly the