feat(opcua): OpcUaApplicationHost publishes peer URIs in Server.ServerArray
This commit is contained in:
@@ -63,6 +63,15 @@ public sealed class OpcUaApplicationHostOptions
|
|||||||
/// the Admin UI). Has no effect on <c>None</c> endpoints, which don't exchange certs.
|
/// the Admin UI). Has no effect on <c>None</c> endpoints, which don't exchange certs.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool AutoAcceptUntrustedClientCertificates { get; set; }
|
public bool AutoAcceptUntrustedClientCertificates { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Peer server URIs published in <c>Server.ServerArray</c> after start, in addition to
|
||||||
|
/// the local <see cref="ApplicationUri"/>. Empty by default — set this on warm-redundancy
|
||||||
|
/// deployments so OPC UA clients can discover the partner endpoint via the standard
|
||||||
|
/// Server.ServerArray property (NodeId i=2254). Order does not matter; the local URI
|
||||||
|
/// is always element 0.
|
||||||
|
/// </summary>
|
||||||
|
public IList<string> PeerApplicationUris { get; set; } = new List<string>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -112,6 +121,7 @@ public sealed class OpcUaApplicationHost : IAsyncDisposable
|
|||||||
await _application.Start(server).ConfigureAwait(false);
|
await _application.Start(server).ConfigureAwait(false);
|
||||||
|
|
||||||
AttachUserAuthenticator();
|
AttachUserAuthenticator();
|
||||||
|
PopulateServerArray();
|
||||||
|
|
||||||
_logger.LogInformation("OPC UA server started on opc.tcp://{Host}:{Port}",
|
_logger.LogInformation("OPC UA server started on opc.tcp://{Host}:{Port}",
|
||||||
_options.PublicHostname, _options.OpcUaPort);
|
_options.PublicHostname, _options.OpcUaPort);
|
||||||
@@ -143,6 +153,26 @@ public sealed class OpcUaApplicationHost : IAsyncDisposable
|
|||||||
sessionManager.ImpersonateUser += _impersonateHandler;
|
sessionManager.ImpersonateUser += _impersonateHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Writes the union of <see cref="OpcUaApplicationHostOptions.ApplicationUri"/> and
|
||||||
|
/// <see cref="OpcUaApplicationHostOptions.PeerApplicationUris"/> to the OPC UA standard
|
||||||
|
/// <c>Server.ServerArray</c> property (NodeId i=2254). Clients in a warm-redundancy
|
||||||
|
/// deployment discover the partner endpoint by reading this property.
|
||||||
|
/// </summary>
|
||||||
|
private void PopulateServerArray()
|
||||||
|
{
|
||||||
|
var serverObject = _server?.CurrentInstance?.ServerObject;
|
||||||
|
if (serverObject is null) return;
|
||||||
|
|
||||||
|
var uris = new List<string> { _options.ApplicationUri };
|
||||||
|
foreach (var peer in _options.PeerApplicationUris)
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrWhiteSpace(peer) && !uris.Contains(peer))
|
||||||
|
uris.Add(peer);
|
||||||
|
}
|
||||||
|
serverObject.ServerArray.Value = uris.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
private void OnImpersonateUser(Session session, ImpersonateEventArgs args) =>
|
private void OnImpersonateUser(Session session, ImpersonateEventArgs args) =>
|
||||||
HandleImpersonation(_userAuthenticator, args, _logger);
|
HandleImpersonation(_userAuthenticator, args, _logger);
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
using System.IO;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
using System.Net;
|
||||||
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
|
using Opc.Ua;
|
||||||
|
using Opc.Ua.Server;
|
||||||
|
using Shouldly;
|
||||||
|
using Xunit;
|
||||||
|
using ZB.MOM.WW.OtOpcUa.OpcUaServer;
|
||||||
|
|
||||||
|
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Audit gap closeout — verifies <see cref="OpcUaApplicationHostOptions.PeerApplicationUris"/>
|
||||||
|
/// is reflected in <c>Server.ServerArray</c> after start. Single-server in-process check; the
|
||||||
|
/// cross-server visibility check lives in <c>OtOpcUa.OpcUaServer.IntegrationTests</c>.
|
||||||
|
/// </summary>
|
||||||
|
public sealed class OpcUaApplicationHostServerArrayTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task ServerArray_contains_local_uri_and_configured_peers_after_start()
|
||||||
|
{
|
||||||
|
var pkiRoot = Path.Combine(Path.GetTempPath(), $"otopcua-pki-{Guid.NewGuid():N}");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var options = new OpcUaApplicationHostOptions
|
||||||
|
{
|
||||||
|
ApplicationName = "OtOpcUa.UnitTest",
|
||||||
|
ApplicationUri = "urn:OtOpcUa.UnitTest.NodeA",
|
||||||
|
OpcUaPort = AllocateFreePort(),
|
||||||
|
PublicHostname = "127.0.0.1",
|
||||||
|
PkiStoreRoot = pkiRoot,
|
||||||
|
PeerApplicationUris = new[] { "urn:OtOpcUa.UnitTest.NodeB" },
|
||||||
|
};
|
||||||
|
|
||||||
|
var server = new StandardServer();
|
||||||
|
await using var host = new OpcUaApplicationHost(options, NullLogger<OpcUaApplicationHost>.Instance);
|
||||||
|
await host.StartAsync(server, CancellationToken.None);
|
||||||
|
|
||||||
|
var serverArray = server.CurrentInstance.ServerObject.ServerArray.Value;
|
||||||
|
serverArray.ShouldNotBeNull();
|
||||||
|
serverArray.ShouldContain("urn:OtOpcUa.UnitTest.NodeA");
|
||||||
|
serverArray.ShouldContain("urn:OtOpcUa.UnitTest.NodeB");
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (Directory.Exists(pkiRoot)) Directory.Delete(pkiRoot, recursive: true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int AllocateFreePort()
|
||||||
|
{
|
||||||
|
var listener = new TcpListener(IPAddress.Loopback, 0);
|
||||||
|
listener.Start();
|
||||||
|
var port = ((IPEndPoint)listener.LocalEndpoint).Port;
|
||||||
|
listener.Stop();
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user