ae91e6b7f7
#816 finding: the SDK checks that the server certificate names the host only when a session is created with checkDomain:true. The driver, the AdminUI browser and Client.Shared all called the 8-argument DefaultSessionFactory.CreateAsync, which forwards checkDomain:false — so no host-name check ever ran (why a trusted NAT cert for *.invalid browsed fine over an IP in #813). When enabled the SDK (Session.OpenAsync -> ValidateDomains -> FindDomain) compares ConfiguredEndpoint.EndpointUrl.IdnHost — the CONFIGURED host, because OpcUaEndpointSelection/EndpointSelector rewrote the advertised one — against the cert's DNS/IP SANs; secured sessions only, and the auto-accept handler accepts a mismatch. Policy: certificateValidation.validateServerHostName, tri-state. Unset = OpcUaCertificateValidationOptions.DefaultValidateServerHostName = false, the pre-#816 behaviour, so nothing changes on upgrade; true = validate the configured host; false = explicit NAT/port-publish opt-out. Threaded into the driver, the AdminUI browser (same JSON) and the AdminUI form (a select that keeps "unset" unset), Client.Shared ConnectionSettings.ValidateServerHostName and the CLI --validate-host-name. A driver refusal names the opt-out. #818: the NAT fixture's /app/pki is on the opc-plc-nat-pki volume (verified: same thumbprint across --force-recreate), its PUBLIC cert is committed as Docker/nat/opc-plc-nat-server.der, and NatPublishedEndpointBrowseTests run every secure browse on a throwaway PKI (internal browser seam): refusal with nothing trusted, positive with the cert trusted (unset/false host check), and a BadCertificateHostNameInvalid refusal with the check on. The trusted cert is OPCUA_NAT_SERVER_CERT or the committed one (skip on thumbprint mismatch); OPCUA_NAT_SERVER_CERT_TRUSTED is retired. CI exports its throwaway NAT container's public cert. ServerHostNameValidationTests pin the driver against the plain opc-plc. MIN_EXECUTED not raised (needs a hosted run): +6 in v2-integration (440 -> 446), documented in scripts/ci/README.md.
87 lines
3.9 KiB
C#
87 lines
3.9 KiB
C#
using Opc.Ua;
|
|
using ZB.MOM.WW.OtOpcUa.Client.Shared.Adapters;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Client.Shared.Tests.Fakes;
|
|
|
|
internal sealed class FakeSessionFactory : ISessionFactory
|
|
{
|
|
private readonly List<FakeSessionAdapter> _createdSessions = [];
|
|
private readonly Queue<FakeSessionAdapter> _sessions = new();
|
|
|
|
/// <summary>Gets the number of times CreateSessionAsync has been called.</summary>
|
|
public int CreateCallCount { get; private set; }
|
|
/// <summary>Gets or sets a value indicating whether CreateSessionAsync should throw.</summary>
|
|
public bool ThrowOnCreate { get; set; }
|
|
/// <summary>Gets the endpoint URL from the last CreateSessionAsync call.</summary>
|
|
public string? LastEndpointUrl { get; private set; }
|
|
/// <summary>Gets the host-name-validation flag from the last CreateSessionAsync call (#816).</summary>
|
|
public bool? LastValidateServerHostName { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Optional gate that, when set, blocks <see cref="CreateSessionAsync" /> until completed.
|
|
/// Lets tests hold a failover loop in-flight to exercise re-entrancy.
|
|
/// </summary>
|
|
public TaskCompletionSource? CreateGate { get; set; }
|
|
|
|
/// <summary>Completed when a call observes <see cref="CreateGate" />.</summary>
|
|
public TaskCompletionSource CreateEntered { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
|
|
/// <summary>Gets the list of sessions created via CreateSessionAsync.</summary>
|
|
public IReadOnlyList<FakeSessionAdapter> CreatedSessions => _createdSessions;
|
|
|
|
/// <summary>Creates a session asynchronously (fake implementation).</summary>
|
|
/// <param name="config">The application configuration.</param>
|
|
/// <param name="endpoint">The endpoint description.</param>
|
|
/// <param name="sessionName">The session name.</param>
|
|
/// <param name="sessionTimeoutMs">The session timeout in milliseconds.</param>
|
|
/// <param name="identity">The user identity.</param>
|
|
/// <param name="validateServerHostName">The SDK checkDomain flag (#816).</param>
|
|
/// <param name="ct">Cancellation token.</param>
|
|
/// <returns>A task containing the session adapter.</returns>
|
|
public async Task<ISessionAdapter> CreateSessionAsync(
|
|
ApplicationConfiguration config, EndpointDescription endpoint, string sessionName,
|
|
uint sessionTimeoutMs, UserIdentity identity, bool validateServerHostName, CancellationToken ct)
|
|
{
|
|
CreateCallCount++;
|
|
LastEndpointUrl = endpoint.EndpointUrl ?? string.Empty;
|
|
LastValidateServerHostName = validateServerHostName;
|
|
|
|
// One-shot: the gate holds only the call that observed it, so a later call (e.g. a user's Connect while a
|
|
// failover is held) runs freely.
|
|
if (CreateGate is { } gate)
|
|
{
|
|
CreateGate = null;
|
|
CreateEntered.TrySetResult();
|
|
await gate.Task;
|
|
}
|
|
|
|
if (ThrowOnCreate)
|
|
throw new InvalidOperationException("FakeSessionFactory configured to fail.");
|
|
|
|
FakeSessionAdapter session;
|
|
if (_sessions.Count > 0)
|
|
session = _sessions.Dequeue();
|
|
else
|
|
session = new FakeSessionAdapter
|
|
{
|
|
EndpointUrl = endpoint.EndpointUrl ?? string.Empty,
|
|
ServerName = endpoint.Server?.ApplicationName.Text ?? "FakeServer",
|
|
SecurityMode = endpoint.SecurityMode.ToString(),
|
|
SecurityPolicyUri = endpoint.SecurityPolicyUri ?? string.Empty
|
|
};
|
|
|
|
// Ensure endpoint URL matches
|
|
session.EndpointUrl = endpoint.EndpointUrl ?? string.Empty;
|
|
_createdSessions.Add(session);
|
|
return session;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Enqueues a session adapter to be returned on the next call to CreateSessionAsync.
|
|
/// </summary>
|
|
/// <param name="session">The session adapter to enqueue.</param>
|
|
public void EnqueueSession(FakeSessionAdapter session)
|
|
{
|
|
_sessions.Enqueue(session);
|
|
}
|
|
} |