7482a88888
#838 review of 91e28e87:
1. Ghost events. The deferred report ran the SDK walk off Lock with no
liveness check, so an event waiting behind a full channel when a deploy
removed its condition still reached i=2253 subscribers after the NodeDeleted
(a ghost Retain alarm for ScadaBridge) and folder items. Delivery now walks
the SDK path itself (OnReportEvent, OnReportEventAsync, inverse notifiers,
then the Parent as BaseInstanceState does) re-checking before EVERY hop that
the condition is the live instance; OnConditionReportEvent checks the same,
which also stops a non-live scripted condition being admitted through its
old equipment folder (#808). A guard test pins that only NodeState and
BaseInstanceState define the walk. New wire test removes the condition in
the same Lock hold as a flood; the flag-off variant fails without the checks.
2. Back-pressure. Deferral is per condition (one condition's events stay
ordered; others are not held up). Overflow policy: coalesce, newest wins --
past 1024 waiting per condition or 65,536 total, the oldest not-yet-started
event is discarded (a condition event carries full state). Warning on
entering deferral (<=1/min), Information on draining, meters
otopcua.opcua.condition_events.deferred / .discarded{reason}.
3. Verified by decompiling SDK 2.0.1: the notifier list is snapshotted under the
node's own lock, MonitoredNode2.Dispose completes its writer (pending writes
fail with ChannelClosedException, swallowed), and the consumer catches
per-event exceptions -- no fix needed; documented.
4. ConditionRefresh does not flush deferrals; documented as a residual.
#844 review: DescribeCredential canonicalises the key id (trim, lowercase;
non-hex ids fold into one identity) and the username, since ConfigDbApiKeyStore
matches with SQL Server's case-insensitive, trailing-space-blind `=`.
218 lines
11 KiB
C#
218 lines
11 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
// Explicit: an unqualified Configuration.* inside this namespace binds to Host.Configuration, not to
|
|
// the ZB.MOM.WW.OtOpcUa.Configuration assembly.
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Authorization;
|
|
using ZB.MOM.WW.OtOpcUa.OpcUaServer.Security;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Host.OpcUa;
|
|
|
|
/// <summary>
|
|
/// Production <see cref="IOpcUaUserAuthenticator"/> for the machine-to-machine data plane:
|
|
/// the client presents the static username <c>apikey</c> with an
|
|
/// <c>otua_<keyId>_<secret></c> credential as the password. Replaces the LDAP adapter.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <para>
|
|
/// <b>This runs inline on session activation.</b> The SDK raises <c>ImpersonateUser</c>
|
|
/// synchronously while holding a SessionManager-wide event lock, so a slow authenticator does not
|
|
/// stall one request — it serializes every session activation server-wide. That is why
|
|
/// verification is a single indexed lookup plus one HMAC, with no directory call and no retry —
|
|
/// the main practical advantage of keys over the LDAP bind this replaces.
|
|
/// </para>
|
|
/// <para>
|
|
/// ⚠️ <b>"No network I/O" is TRUE ONLY ON A DRIVER-ONLY NODE</b> (arch-review 03/S18). The claim
|
|
/// describes <see cref="ArtifactApiKeyStore"/>, which is two dictionary probes on an in-memory
|
|
/// snapshot. On an <b>admin or fused</b> node <see cref="IApiKeyStore"/> is
|
|
/// <see cref="ConfigDbApiKeyStore"/>, and a UserName activation costs <b>two</b> EF round trips
|
|
/// (the key row, then the principal's <c>NodeAcl</c> rows) — inside this same lock. What bounds
|
|
/// that is <see cref="BoundedApiKeyStore"/>, which wraps it in production; the deadline it applies
|
|
/// surfaces here as an ordinary throw and is denied by the catch-all below, like any other store
|
|
/// failure.
|
|
/// </para>
|
|
/// <para>
|
|
/// <b>Fail closed, and say little.</b> Every rejection returns the same opaque message. The
|
|
/// reason is logged server-side but never returned, so a caller cannot distinguish "no such key"
|
|
/// from "wrong secret" from "key disabled" and use the server as an oracle.
|
|
/// </para>
|
|
/// </remarks>
|
|
public sealed class ApiKeyOpcUaUserAuthenticator : IOpcUaUserAuthenticator
|
|
{
|
|
/// <summary>
|
|
/// Returned for every failure mode, deliberately uninformative. Public so a test can assert that
|
|
/// the failure modes really are indistinguishable, rather than restating the literal and passing
|
|
/// even if the implementation started differentiating them.
|
|
/// </summary>
|
|
public const string DenyMessage = "Invalid API key.";
|
|
|
|
private readonly IApiKeyStore _store;
|
|
private readonly IOptions<ApiKeyOptions> _options;
|
|
private readonly ILogger<ApiKeyOpcUaUserAuthenticator> _log;
|
|
private readonly TimeProvider _time;
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="ApiKeyOpcUaUserAuthenticator"/> class.</summary>
|
|
/// <param name="store">The API key source.</param>
|
|
/// <param name="options">The <c>Security:ApiKeys</c> options.</param>
|
|
/// <param name="log">The logger.</param>
|
|
/// <param name="time">Time provider, injected so expiry is testable.</param>
|
|
public ApiKeyOpcUaUserAuthenticator(
|
|
IApiKeyStore store,
|
|
IOptions<ApiKeyOptions> options,
|
|
ILogger<ApiKeyOpcUaUserAuthenticator> log,
|
|
TimeProvider? time = null)
|
|
{
|
|
_store = store;
|
|
_options = options;
|
|
_log = log;
|
|
_time = time ?? TimeProvider.System;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task<OpcUaUserAuthResult> AuthenticateUserNameAsync(string username, string password, CancellationToken ct) =>
|
|
AuthenticateUserNameAsync(username, password, remoteEndpoint: null, ct);
|
|
|
|
/// <inheritdoc />
|
|
// The client's address rides into the store so BoundedApiKeyStore can charge a miss to that client's own
|
|
// budget before the process-wide one (lmxopcua#751).
|
|
public async Task<OpcUaUserAuthResult> AuthenticateUserNameAsync(
|
|
string username, string password, System.Net.IPEndPoint? remoteEndpoint, CancellationToken ct)
|
|
{
|
|
var opts = _options.Value;
|
|
|
|
// The username carries no information, but check it rather than ignore it: a client sending the
|
|
// wrong username gets an explicit denial it can diagnose, instead of "any username works".
|
|
if (!string.Equals(username, opts.Username, StringComparison.Ordinal))
|
|
{
|
|
_log.LogInformation(
|
|
"ApiKeyAuth: denied — username {Presented} is not the configured {Expected}",
|
|
username, opts.Username);
|
|
return OpcUaUserAuthResult.Deny(DenyMessage);
|
|
}
|
|
|
|
if (!ApiKeySecret.TryParse(password, out var apiKeyId, out var secret))
|
|
{
|
|
_log.LogInformation("ApiKeyAuth: denied — credential is not a well-formed otua_ key");
|
|
return OpcUaUserAuthResult.Deny(DenyMessage);
|
|
}
|
|
|
|
var now = _time.GetUtcNow().UtcDateTime;
|
|
|
|
ApiKeyLookup? lookup;
|
|
try
|
|
{
|
|
lookup = await _store.FindAsync(apiKeyId, now, remoteEndpoint?.Address, ct).ConfigureAwait(false);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// The contract forbids throwing: an exception here escapes into the SDK's session-activation
|
|
// path and surfaces as BadInternalError. A store outage denies, it does not crash.
|
|
// lmxopcua#845: a store timeout, miss-budget refusal or outage says nothing about the credential, so it
|
|
// is reported Transient and the failed-credential lockout does not count it (a ConfigDb stall must not
|
|
// lock every keyed client out). The client still sees the same opaque denial.
|
|
_log.LogError(ex, "ApiKeyAuth: key store threw for key id {ApiKeyId}", apiKeyId);
|
|
return OpcUaUserAuthResult.Unavailable(DenyMessage);
|
|
}
|
|
|
|
if (lookup is null)
|
|
{
|
|
_log.LogInformation("ApiKeyAuth: denied — no enabled, unexpired key with id {ApiKeyId}", apiKeyId);
|
|
return OpcUaUserAuthResult.Deny(DenyMessage);
|
|
}
|
|
|
|
bool verified;
|
|
try
|
|
{
|
|
verified = ApiKeySecret.Verify(secret, opts.Pepper, lookup.Key.SecretHash);
|
|
}
|
|
catch (ArgumentException ex)
|
|
{
|
|
// Thrown only when the pepper is unset. This is a deployment error, not a bad credential —
|
|
// log it loudly, because otherwise it presents as "every key is rejected" with no clue why.
|
|
_log.LogError(ex,
|
|
"ApiKeyAuth: {Section}:Pepper is not configured — every key will be rejected until it is",
|
|
ApiKeyOptions.SectionName);
|
|
return OpcUaUserAuthResult.Unavailable(DenyMessage); // a deployment error, not a bad credential (#845)
|
|
}
|
|
|
|
if (!verified)
|
|
{
|
|
_log.LogInformation("ApiKeyAuth: denied — secret mismatch for key id {ApiKeyId}", apiKeyId);
|
|
return OpcUaUserAuthResult.Deny(DenyMessage);
|
|
}
|
|
|
|
if (string.Equals(lookup.Key.Principal, OpcUaPrincipals.Anonymous, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
// Defence in depth against a key authored with the reserved principal (authoring blocks it).
|
|
// Allowing it would let a credentialled client silently inherit whatever anonymous was
|
|
// granted, and vice versa — two identities that are meant to be distinct sharing one grant.
|
|
_log.LogError(
|
|
"ApiKeyAuth: denied — key {ApiKeyId} uses the reserved principal '{Principal}'",
|
|
apiKeyId, OpcUaPrincipals.Anonymous);
|
|
return OpcUaUserAuthResult.Deny(DenyMessage);
|
|
}
|
|
|
|
var roles = ApiKeyInterimRoleMapper.Map(lookup.Grants, lookup.Key.Principal);
|
|
|
|
_log.LogInformation(
|
|
"ApiKeyAuth: granted key {Name} ({ApiKeyId}) as principal {Principal} with roles [{Roles}]",
|
|
lookup.Key.Name, apiKeyId, lookup.Key.Principal, string.Join(",", roles));
|
|
|
|
// AllowKey, not Allow: the key id rides along so an open session can be re-checked against a later
|
|
// deployment that revoked this key. Without it a revoked key keeps working until the client
|
|
// happens to disconnect, which on a machine-to-machine data plane can be indefinitely.
|
|
return OpcUaUserAuthResult.AllowKey(lookup.Key.Principal, roles, apiKeyId);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
// lmxopcua#844 — the key id is public (it is the lookup index, never the secret), so guesses at different keys
|
|
// are counted apart and a client's own successful key cannot clear the count of a key it is guessing at.
|
|
// A credential that is not a well-formed key falls back to the username (all such guesses share one count).
|
|
//
|
|
// #844 review: the identity is CANONICAL, never the raw presented id. ConfigDbApiKeyStore matches the id with SQL
|
|
// Server `=` under a case-insensitive collation that also ignores trailing spaces, so `otua_abc…`, `otua_ABC…` and
|
|
// `otua_abc… ` reach the same key row — as separate lockout identities they would each get a fresh per-credential
|
|
// count. Minted ids are lowercase hex (ApiKeySecret.Generate), so: trim, lowercase, and fold every id outside that
|
|
// charset (which no real key can carry) into one shared identity. The username is folded the same way. Merging
|
|
// only ever makes the per-credential count stricter; the per-client cap is unaffected.
|
|
public string DescribeCredential(string username, string password)
|
|
{
|
|
if (!ApiKeySecret.TryParse(password, out var apiKeyId, out _))
|
|
return "user:" + (username ?? string.Empty).Trim().ToLowerInvariant();
|
|
|
|
var canonical = apiKeyId.Trim().ToLowerInvariant();
|
|
return canonical.Length > 0 && canonical.All(char.IsAsciiHexDigitLower)
|
|
? "key:" + canonical
|
|
: "key:<not-a-minted-id>";
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
// Resolves the <c>anonymous</c> principal's grants through the same mapper a key's principal
|
|
// goes through, so anonymous is authorized by ordinary ACL rows rather than by a special case.
|
|
// Whether anonymous may connect at all is the host's decision
|
|
// (<c>OpcUa:AllowAnonymous</c>) and is settled before this is called.
|
|
public async Task<OpcUaUserAuthResult> AuthenticateAnonymousAsync(CancellationToken ct)
|
|
{
|
|
IReadOnlyList<NodeAcl> grants;
|
|
try
|
|
{
|
|
grants = await _store.FindGrantsAsync(OpcUaPrincipals.Anonymous, ct).ConfigureAwait(false);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Deny rather than admit-with-no-roles. Admitting would be the previous behaviour, and doing
|
|
// it precisely when we could not read what anonymous is allowed to do is the wrong default:
|
|
// once reads are gated a no-roles admission stops being harmless.
|
|
_log.LogError(ex, "ApiKeyAuth: key store threw resolving anonymous grants");
|
|
return OpcUaUserAuthResult.Deny("Anonymous access is unavailable.");
|
|
}
|
|
|
|
var roles = ApiKeyInterimRoleMapper.Map(grants, OpcUaPrincipals.Anonymous);
|
|
_log.LogInformation(
|
|
"ApiKeyAuth: anonymous admitted with roles [{Roles}]",
|
|
roles.Count == 0 ? "none" : string.Join(",", roles));
|
|
|
|
return OpcUaUserAuthResult.Allow(OpcUaPrincipals.Anonymous, roles);
|
|
}
|
|
}
|