mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-18 14:43:08 +08:00
b97fd8d4b5
# Description Fix the four blockers that prevented the OPC 10000-21 onboarding registrar and client from working end to end: - generate every `ModelDesign` target independently, including multiple models in one directory, while preserving stable identifier CSV matching and cross-model dependencies; - ship the generated `Opc.Ua.Onboarding` constants, node states, encodeables, and typed clients from `Opc.Ua.Gds.Common`; - correct the standard `EncodedTicket` datatype to derive from `ByteString`; - align the new 2.0 `OnboardingClient` API with the generated `ArrayOf<ByteString>` / `ArrayOf<StatusCode>` contract; - make `BindToTicketStore` fully asynchronous, cancellation-aware, and assign the declared output slot instead of appending a second output; - add generated-model, client fallback, server binding, and in-process end-to-end regressions. The change also adds `samples/Gds/OnboardingRegistrar`, `samples/Gds/OnboardingClient`, and `run-onboarding-demo.ps1`. The demo loads the generated GDS and Onboarding models, restricts ticket administration to an ephemeral `RegistrarAdmin` user, proves anonymous access is denied, and completes a secure register/unregister round trip. The History CI follow-up rejects negative and non-finite processed-history intervals before provider dispatch and enforces the buffered aggregate output cap while calculator results are flushed. This prevents malformed requests from running until the client timeout, exhausting memory, delaying server teardown, and causing later alarm-event tests to fail. ## Related Issues - Fixes #4378 - Unblocks OPCFoundation/UA-.NETStandard-Samples#819 - Tracks the canonical model correction in OPCFoundation/UA-Nodeset#122 ## Validation - Full `net10.0` test projects: - `Opc.Ua.SourceGeneration.Core.Tests`: 3803 passed, 8 skipped - `Opc.Ua.SourceGeneration.Tests`: 143 passed - `Opc.Ua.Gds.Tests`: 1114 passed, 42 skipped - `Opc.Ua.History.Tests` with the CI coverage settings: 506 passed, 27 skipped - `HistorianDispatcher` tests: 88 passed on `net10.0`. - Invalid processed-history interval regressions passed on `net10.0` and `net48`. - Focused onboarding/source-generation regressions passed on `net48`. - `dotnet build UA.slnx -p:CustomTestTarget=net10.0 --no-restore` succeeded; the reported analyzer warnings are pre-existing in unchanged XRegistry/WoT files. - Packed all three GDS packages and verified the packaged `Opc.Ua.Gds.Common` assembly contains the generated Onboarding model surface. - `pwsh samples/Gds/run-onboarding-demo.ps1` verified anonymous denial and the authenticated secure ticket lifecycle. ## Checklist - [ ] I have signed the [CLA](https://opcfoundation.org/license/cla/ContributorLicenseAgreementv1.0.pdf) and read the [CONTRIBUTING](https://github.com/OPCFoundation/UA-.NETStandard/blob/master/CONTRIBUTING.md) doc. - [x] I have added tests that prove my fix is effective or that my feature works and increased code coverage. - [x] I have added all necessary documentation. - [x] I have verified that my changes do not introduce (new) build or analyzer warnings. - [ ] I ran **all** tests locally using the **UA.slnx** solution against at least .net **framework** and .net **10**, and all passed. - [ ] I fixed **all** failing and flaky tests in the CI pipelines and **all** CodeQL warnings. - [x] I have addressed **all** PR feedback received. --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 2d2c52c7-7040-4bd6-98de-aaf82ba7846b
226 lines
8.4 KiB
C#
226 lines
8.4 KiB
C#
/* ========================================================================
|
|
* Copyright (c) 2005-2026 The OPC Foundation, Inc. All rights reserved.
|
|
*
|
|
* OPC Foundation MIT License 1.00
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person
|
|
* obtaining a copy of this software and associated documentation
|
|
* files (the "Software"), to deal in the Software without
|
|
* restriction, including without limitation the rights to use,
|
|
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the
|
|
* Software is furnished to do so, subject to the following
|
|
* conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be
|
|
* included in all copies or substantial portions of the Software.
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
|
*
|
|
* The complete license agreement can be found here:
|
|
* http://opcfoundation.org/License/MIT/1.00/
|
|
* ======================================================================*/
|
|
|
|
using System;
|
|
using System.IO;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using Opc.Ua;
|
|
using Opc.Ua.Client;
|
|
using Opc.Ua.Gds.Client;
|
|
using Opc.Ua.Identity;
|
|
|
|
try
|
|
{
|
|
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
|
|
builder.Logging.ClearProviders();
|
|
builder.Logging.AddConsole();
|
|
builder.Logging.SetMinimumLevel(LogLevel.Error);
|
|
|
|
string endpoint = builder.Configuration["endpoint"] ??
|
|
"opc.tcp://localhost:62560/OnboardingRegistrar";
|
|
string pkiRoot = builder.Configuration["pkiRoot"] ??
|
|
Path.Combine(
|
|
Path.GetTempPath(),
|
|
"opcua-onboarding-demo",
|
|
"client-pki");
|
|
bool useAnonymous = bool.TryParse(
|
|
builder.Configuration["anonymous"],
|
|
out bool anonymous) && anonymous;
|
|
IClientIdentityProvider? identityProvider = null;
|
|
if (!useAnonymous)
|
|
{
|
|
string userName = GetRequiredEnvironmentVariable("ONBOARDING_DEMO_USER");
|
|
string password = GetRequiredEnvironmentVariable("ONBOARDING_DEMO_PASSWORD");
|
|
var passwordStore = new InMemorySecretStore();
|
|
var passwordId = new SecretIdentifier(
|
|
"onboarding-demo-password",
|
|
passwordStore.StoreType);
|
|
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
|
|
try
|
|
{
|
|
await passwordStore.SetAsync(
|
|
passwordId,
|
|
passwordBytes,
|
|
CancellationToken.None)
|
|
.ConfigureAwait(false);
|
|
}
|
|
finally
|
|
{
|
|
CryptographicOperations.ZeroMemory(passwordBytes);
|
|
}
|
|
identityProvider = new UserNamePasswordIdentityProvider(
|
|
userName,
|
|
new SecretRegistry(passwordStore),
|
|
passwordId);
|
|
}
|
|
|
|
IOpcUaClientBuilder clientBuilder = builder.Services
|
|
.AddOpcUa()
|
|
.AddClient(options =>
|
|
{
|
|
options.ApplicationName = "OnboardingClient";
|
|
options.ApplicationUri =
|
|
"urn:localhost:OPCFoundation:OnboardingClient";
|
|
options.ProductUri =
|
|
"uri:opcfoundation.org:UA-.NETStandard:OnboardingClient";
|
|
options.PkiRoot = pkiRoot;
|
|
options.AutoAcceptUntrustedCertificates = true;
|
|
options.RejectSHA1SignedCertificates = true;
|
|
options.MinimumCertificateKeySize = 2048;
|
|
options.Session = new ManagedSessionOptions
|
|
{
|
|
SessionName = "OnboardingClient",
|
|
SessionTimeout = TimeSpan.FromSeconds(60)
|
|
};
|
|
})
|
|
.AddDiscoveryAndConnect(options =>
|
|
{
|
|
options.DiscoveryUrl = endpoint;
|
|
options.SecurityMode = MessageSecurityMode.SignAndEncrypt;
|
|
options.SecurityPolicyUri = SecurityPolicies.Basic256Sha256;
|
|
});
|
|
if (identityProvider != null)
|
|
{
|
|
clientBuilder.AddIdentityProvider(identity => identity.Add(identityProvider));
|
|
}
|
|
clientBuilder
|
|
.AddGdsClient()
|
|
.AddOnboardingClient();
|
|
|
|
using IHost host = builder.Build();
|
|
await host.StartAsync(CancellationToken.None).ConfigureAwait(false);
|
|
try
|
|
{
|
|
Func<CancellationToken, Task<ManagedSession>> connect =
|
|
host.Services.GetRequiredService<
|
|
Func<CancellationToken, Task<ManagedSession>>>();
|
|
ManagedSession session = await connect(CancellationToken.None)
|
|
.ConfigureAwait(false);
|
|
await using (session.ConfigureAwait(false))
|
|
{
|
|
Console.WriteLine("Server namespaces:");
|
|
for (int i = 0; i < session.NamespaceUris.Count; i++)
|
|
{
|
|
Console.WriteLine($" ns={i}: {session.NamespaceUris.GetString((uint)i)}");
|
|
}
|
|
NodeId registrarId = ExpandedNodeId.ToNodeId(
|
|
Opc.Ua.Onboarding.ObjectIds.DeviceRegistrar_Administration,
|
|
session.NamespaceUris);
|
|
if (registrarId.IsNull)
|
|
{
|
|
throw new ServiceResultException(
|
|
StatusCodes.BadNodeIdUnknown,
|
|
"The server did not publish the OPC 10000-21 namespace.");
|
|
}
|
|
|
|
Func<NodeId, CancellationToken, ValueTask<OnboardingClient>>
|
|
createOnboardingClient = host.Services.GetRequiredService<
|
|
Func<NodeId, CancellationToken, ValueTask<OnboardingClient>>>();
|
|
OnboardingClient onboarding = await createOnboardingClient(
|
|
registrarId,
|
|
CancellationToken.None)
|
|
.ConfigureAwait(false);
|
|
ArrayOf<ByteString> tickets =
|
|
[
|
|
new ByteString(Encoding.UTF8.GetBytes("demo-ticket-one")),
|
|
new ByteString(Encoding.UTF8.GetBytes("demo-ticket-two"))
|
|
];
|
|
|
|
ArrayOf<StatusCode> registered = await onboarding
|
|
.RegisterTicketsAsync(tickets)
|
|
.ConfigureAwait(false);
|
|
RequireStatus(registered, 0, StatusCodes.Good, "register ticket one");
|
|
RequireStatus(registered, 1, StatusCodes.Good, "register ticket two");
|
|
Console.WriteLine(
|
|
$"REGISTER {registered[0]} {registered[1]}");
|
|
|
|
ArrayOf<StatusCode> removed = await onboarding
|
|
.UnregisterTicketsAsync([tickets[0]])
|
|
.ConfigureAwait(false);
|
|
RequireStatus(removed, 0, StatusCodes.Good, "unregister ticket one");
|
|
Console.WriteLine($"UNREGISTER {removed[0]}");
|
|
|
|
ArrayOf<StatusCode> removedAgain = await onboarding
|
|
.UnregisterTicketsAsync([tickets[0]])
|
|
.ConfigureAwait(false);
|
|
RequireStatus(
|
|
removedAgain,
|
|
0,
|
|
StatusCodes.BadNotFound,
|
|
"unregister ticket one again");
|
|
Console.WriteLine($"UNREGISTER_AGAIN {removedAgain[0]}");
|
|
Console.WriteLine("ONBOARDING_DEMO_OK");
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
await host.StopAsync(CancellationToken.None).ConfigureAwait(false);
|
|
}
|
|
return 0;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.Error.WriteLine(ex);
|
|
return 1;
|
|
}
|
|
|
|
static string GetRequiredEnvironmentVariable(string name)
|
|
{
|
|
string? value = Environment.GetEnvironmentVariable(name);
|
|
if (string.IsNullOrWhiteSpace(value))
|
|
{
|
|
throw new InvalidOperationException(
|
|
$"Set the {name} environment variable before starting the demo.");
|
|
}
|
|
return value;
|
|
}
|
|
|
|
static void RequireStatus(
|
|
ArrayOf<StatusCode> statuses,
|
|
int index,
|
|
StatusCode expected,
|
|
string operation)
|
|
{
|
|
if (statuses.Count <= index || statuses[index] != expected)
|
|
{
|
|
StatusCode actual = statuses.Count > index
|
|
? statuses[index]
|
|
: StatusCodes.BadNoData;
|
|
throw new ServiceResultException(
|
|
StatusCodes.BadUnexpectedError,
|
|
$"{operation} returned {actual}; expected {expected}.");
|
|
}
|
|
}
|