fix: preserve SDK role identity and continuation authority lifecycle
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Opc.Ua;
|
||||
using Opc.Ua.Server;
|
||||
using Opc.Ua.Identity;
|
||||
using Opc.Ua.Server.Hosting;
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
|
||||
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
||||
using ZB.MOM.WW.OtOpcUa.Core.Authorization;
|
||||
@@ -25,6 +27,21 @@ public sealed class OtOpcUaSdkServer : StandardServer
|
||||
public OtOpcUaSdkServer(ILogger<OtOpcUaNodeManager>? nodeManagerLogger = null)
|
||||
: base(DefaultTelemetry.Create(_ => { })) => _nodeManagerLogger = nodeManagerLogger;
|
||||
|
||||
internal void ConfigureIdentityAuthenticators(IEnumerable<IUserTokenAuthenticator> authenticators) =>
|
||||
AddPreStartupTask(new IdentityStartupTask(authenticators.ToArray()));
|
||||
|
||||
private sealed class IdentityStartupTask(IReadOnlyList<IUserTokenAuthenticator> authenticators) : IServerPreStartupTask
|
||||
{
|
||||
public ValueTask OnServerStartingAsync(IServerContext server, CancellationToken cancellationToken = default)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
if (server is not IServerInternal internalServer)
|
||||
throw new InvalidOperationException("Application identity registry unavailable during server startup.");
|
||||
foreach (var authenticator in authenticators) internalServer.IdentityRegistry.Register(authenticator);
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
private readonly AsyncLocal<System.Net.IPAddress?> _activationPeer = new();
|
||||
|
||||
/// <summary>The transport-observed peer of the current activation request, when available.</summary>
|
||||
@@ -40,16 +57,23 @@ public sealed class OtOpcUaSdkServer : StandardServer
|
||||
ExtensionObject userIdentityToken,
|
||||
SignatureData? userTokenSignature,
|
||||
RequestLifetime requestLifetime)
|
||||
{
|
||||
return await WithActivationPeerAddressAsync(secureChannelContext.PeerAddress,
|
||||
() => base.ActivateSessionAsync(secureChannelContext, requestHeader, clientSignature,
|
||||
clientSoftwareCertificates, localeIds, userIdentityToken, userTokenSignature, requestLifetime))
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
internal async ValueTask<ActivateSessionResponse> WithActivationPeerAddressAsync(
|
||||
System.Net.IPAddress? peer, Func<ValueTask<ActivateSessionResponse>> activation)
|
||||
{
|
||||
// AuthenticationContext does not carry the request peer in 2.0.1. Keep the public
|
||||
// transport evidence in this activation's async flow, including reactivation on a new channel.
|
||||
var previous = _activationPeer.Value;
|
||||
_activationPeer.Value = secureChannelContext.PeerAddress;
|
||||
_activationPeer.Value = peer;
|
||||
try
|
||||
{
|
||||
return await base.ActivateSessionAsync(secureChannelContext, requestHeader, clientSignature,
|
||||
clientSoftwareCertificates, localeIds, userIdentityToken, userTokenSignature, requestLifetime)
|
||||
.ConfigureAwait(false);
|
||||
return await activation().ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -267,6 +291,7 @@ public sealed class OtOpcUaSdkServer : StandardServer
|
||||
defaults.PercentDataGood = ServerAggregateConfiguration.PercentDataGood;
|
||||
defaults.UseSlopedExtrapolation = ServerAggregateConfiguration.UseSlopedExtrapolation;
|
||||
manager.SetDefaultConfiguration(defaults);
|
||||
await Compatibility.Aggregates.CompatibilityAggregateFactory.RegisterAsync(manager, cancellationToken).ConfigureAwait(false);
|
||||
_otOpcUaNodeManager?.SyncAdvertisedAggregates();
|
||||
return manager;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user