test: prove peer provenance and repeated alarm authority with baseline scope oracle
This commit is contained in:
+59
-9
@@ -52,6 +52,44 @@ public sealed class Sdk2TrustedChannelTests
|
||||
rig.Writes.ShouldBe(1, "revocation must be checked before a cached narrow grant or dispatch");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(OpcUaSecurityProfile.Basic256Sha256Sign)]
|
||||
[InlineData(OpcUaSecurityProfile.Basic256Sha256SignAndEncrypt)]
|
||||
public async Task Concurrent_IPv4_and_IPv6_activations_keep_their_own_transport_peer(OpcUaSecurityProfile profile)
|
||||
{
|
||||
var ct = TestContext.Current.CancellationToken;
|
||||
await using var rig = await TrustedRig.StartAsync(profile, ct);
|
||||
rig.HoldPeerActivations = true;
|
||||
// Distinct test attempt markers share one key identity and trusted certificate. Both auth flows await the barrier.
|
||||
var firstConnection = rig.ConnectAsync("contract-key-ipv4", ct, transportHost: "127.0.0.1");
|
||||
var secondConnection = rig.ConnectAsync("contract-key-ipv6", ct, transportHost: "[::1]");
|
||||
ISession[] connections;
|
||||
try { connections = await Task.WhenAll(firstConnection, secondConnection); }
|
||||
catch
|
||||
{
|
||||
if (firstConnection.IsCompletedSuccessfully) (await firstConnection).Dispose();
|
||||
if (secondConnection.IsCompletedSuccessfully) (await secondConnection).Dispose();
|
||||
throw;
|
||||
}
|
||||
using var ipv4 = connections[0];
|
||||
using var ipv6 = connections[1];
|
||||
rig.Peers.Count.ShouldBe(2);
|
||||
var first4 = rig.PeerObservations.Single(x => x.Attempt == "IPv4");
|
||||
var first6 = rig.PeerObservations.Single(x => x.Attempt == "IPv6");
|
||||
first4.Peer.ShouldNotBeNull().Address.ShouldBe(IPAddress.Loopback);
|
||||
first6.Peer.ShouldNotBeNull().Address.ShouldBe(IPAddress.IPv6Loopback);
|
||||
first4.AfterAwaitPeer.ShouldBe(first4.Peer.Address);
|
||||
first6.AfterAwaitPeer.ShouldBe(first6.Peer.Address);
|
||||
foreach (var session in connections)
|
||||
(await session.ReadValueAsync(rig.Node(session), ct)).StatusCode.ShouldBe(StatusCodes.Good);
|
||||
await ipv4.ReconnectAsync(null, null, ct);
|
||||
rig.Peers.Count.ShouldBe(3);
|
||||
rig.Peers.Last().ShouldNotBeNull().Address.ShouldBe(IPAddress.Loopback,
|
||||
"reactivation cannot inherit the concurrent IPv6 activation's peer");
|
||||
await ipv6.CloseAsync(ct);
|
||||
await ipv4.CloseAsync(ct);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(OpcUaSecurityProfile.Basic256Sha256Sign)]
|
||||
[InlineData(OpcUaSecurityProfile.Basic256Sha256SignAndEncrypt)]
|
||||
@@ -85,14 +123,19 @@ public sealed class Sdk2TrustedChannelTests
|
||||
private const string Cluster = "SDK2";
|
||||
private const string PathId = "Plant/Device/Value";
|
||||
private readonly string _root = Path.Combine(Path.GetTempPath(), $"otua-trusted-{Guid.NewGuid():N}");
|
||||
private readonly Dictionary<ApplicationConfiguration, ApplicationInstance> _applications = [];
|
||||
private readonly ConcurrentDictionary<ApplicationConfiguration, ApplicationInstance> _applications = new();
|
||||
private readonly DataPlaneAuthorityState _authority = new();
|
||||
private readonly OpcUaApplicationHostOptions _options;
|
||||
private OpcUaApplicationHost _host = null!;
|
||||
private int _port;
|
||||
private int _writes;
|
||||
private int _authCalls;
|
||||
internal ConcurrentQueue<IPEndPoint?> Peers { get; } = new();
|
||||
internal sealed record PeerObservation(string Attempt, IPEndPoint? Peer, IPAddress? AfterAwaitPeer);
|
||||
internal ConcurrentQueue<PeerObservation> PeerObservations { get; } = new();
|
||||
internal IReadOnlyList<IPEndPoint?> Peers => PeerObservations.Select(x => x.Peer).ToArray();
|
||||
internal bool HoldPeerActivations { get; set; }
|
||||
private readonly TaskCompletionSource _peerBarrier = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
private int _peerBarrierArrivals;
|
||||
internal OtOpcUaSdkServer Server { get; private set; } = null!;
|
||||
internal int Writes => Volatile.Read(ref _writes);
|
||||
internal int AuthCalls => Volatile.Read(ref _authCalls);
|
||||
@@ -173,7 +216,7 @@ public sealed class Sdk2TrustedChannelTests
|
||||
{
|
||||
await config.ValidateAsync(ApplicationType.Client, ct);
|
||||
await app.CheckApplicationInstanceCertificatesAsync(true, lifeTimeInMonths: 12, ct);
|
||||
_applications.Add(config, app);
|
||||
_applications[config] = app;
|
||||
return config;
|
||||
}
|
||||
catch { await app.DisposeAsync(); throw; }
|
||||
@@ -181,10 +224,10 @@ public sealed class Sdk2TrustedChannelTests
|
||||
|
||||
private async Task ReleaseConfigurationAsync(ApplicationConfiguration config)
|
||||
{
|
||||
if (_applications.Remove(config, out var app)) await app.DisposeAsync();
|
||||
if (_applications.TryRemove(config, out var app)) await app.DisposeAsync();
|
||||
}
|
||||
|
||||
internal async Task<ISession> ConnectAsync(string credential, CancellationToken ct, bool untrustedClient = false)
|
||||
internal async Task<ISession> ConnectAsync(string credential, CancellationToken ct, bool untrustedClient = false, string? transportHost = null)
|
||||
{
|
||||
var pki = untrustedClient ? System.IO.Path.Combine(_root, $"untrusted-{Guid.NewGuid():N}") : ClientPki;
|
||||
var config = await ConfigurationAsync(pki, ct);
|
||||
@@ -198,6 +241,7 @@ public sealed class Sdk2TrustedChannelTests
|
||||
var telemetry = DefaultTelemetry.Create(_ => { });
|
||||
var endpoint = await CoreClientUtils.SelectEndpointAsync(config, $"opc.tcp://localhost:{_port}/OtOpcUa", true, telemetry, ct)
|
||||
?? throw new InvalidOperationException("No secure endpoint.");
|
||||
if (transportHost is not null) endpoint.EndpointUrl = $"opc.tcp://{transportHost}:{_port}/OtOpcUa";
|
||||
return await new DefaultSessionFactory(telemetry).CreateAsync(config,
|
||||
new ConfiguredEndpoint(null, endpoint, EndpointConfiguration.Create(config)), false, true,
|
||||
"Sdk2TrustedContract", 60_000, new UserIdentity("apikey", Encoding.UTF8.GetBytes(credential)), default, ct);
|
||||
@@ -217,16 +261,22 @@ public sealed class Sdk2TrustedChannelTests
|
||||
new Dictionary<AclScopeRef, IReadOnlyList<NodeAclPath>>
|
||||
{ [new(NodeAclScopeKind.RawDevice, "device")] = [new([NodeAclRealms.Raw, "Plant", "Device"])] });
|
||||
|
||||
public Task<OpcUaUserAuthResult> AuthenticateUserNameAsync(string username, string password, IPEndPoint? peer, CancellationToken ct)
|
||||
public async Task<OpcUaUserAuthResult> AuthenticateUserNameAsync(string username, string password, IPEndPoint? peer, CancellationToken ct)
|
||||
{
|
||||
Peers.Enqueue(peer is null ? null : new IPEndPoint(peer.Address, peer.Port));
|
||||
return AuthenticateUserNameAsync(username, password, ct);
|
||||
var attempt = password switch { "contract-key-ipv4" => "IPv4", "contract-key-ipv6" => "IPv6", _ => "Other" };
|
||||
if (HoldPeerActivations && attempt != "Other")
|
||||
{
|
||||
if (Interlocked.Increment(ref _peerBarrierArrivals) == 2) _peerBarrier.TrySetResult();
|
||||
await _peerBarrier.Task.WaitAsync(TimeSpan.FromSeconds(15), ct);
|
||||
}
|
||||
PeerObservations.Enqueue(new(attempt, peer is null ? null : new IPEndPoint(peer.Address, peer.Port), Server.ActivationPeerAddress));
|
||||
return await AuthenticateUserNameAsync(username, password, ct);
|
||||
}
|
||||
|
||||
public Task<OpcUaUserAuthResult> AuthenticateUserNameAsync(string username, string password, CancellationToken ct)
|
||||
{
|
||||
Interlocked.Increment(ref _authCalls);
|
||||
return Task.FromResult(username == "apikey" && password == "contract-key"
|
||||
return Task.FromResult(username == "apikey" && (password is "contract-key" or "contract-key-ipv4" or "contract-key-ipv6")
|
||||
? OpcUaUserAuthResult.AllowKey(Principal, [], KeyId) : OpcUaUserAuthResult.Deny("Invalid credential."));
|
||||
}
|
||||
public Task<NodeWriteOutcome> WriteAsync(string nodeId, object? value, AddressSpaceRealm realm, CancellationToken ct)
|
||||
|
||||
Reference in New Issue
Block a user