feat(acl): API-key identity replaces LDAP on the OPC UA data plane (#520)

Stage 2a. Clients present the static username "apikey" with an
otua_<keyId>_<secret> credential as the password; the key resolves to a
principal. LDAP registrations are deliberately RETAINED for AdminUI login —
only IOpcUaUserAuthenticator moves, which is what D2 asked for.

The load-bearing decision here is what roles a key gets. The two live gates
(WriteOperate on Value writes, AlarmAck on Part 9 methods) check a SERVER-WIDE
role set, so the cutover has to keep producing one or it would lock every client
out of writes long before per-node evaluation lands in stage 5.

ApiKeyInterimRoleMapper therefore maps ACL grants to roles, but ONLY
cluster-scoped ones. The obvious version — union the permission flags across all
of a principal's grants — would let a grant authored narrowly ("WriteOperate on
Equipment X") confer server-wide write authority, because nothing in the pipeline
can enforce scope yet. That fails in the dangerous direction: the operator's
narrow intent silently becomes broad. Restricting to cluster scope under-grants
instead, which is no worse than today (where every ACL row does nothing) and can
never exceed what was authored. Falsified: deleting the scope check reds exactly
A_narrower_scoped_write_grant_confers_nothing_yet and nothing else.

Verification is one indexed lookup plus one HMAC, with no network I/O at all.
That matters because the SDK raises ImpersonateUser synchronously while holding a
SessionManager-wide event lock, so this path does not stall one request — it
serializes every session activation server-wide. It is the main practical gain
over the LDAP bind it replaces.

Every rejection returns one opaque message so the server cannot be used as an
oracle for whether a key id exists or which half of a credential was wrong; a
test asserts the failure modes really are indistinguishable AND that a valid key
still succeeds, so it cannot pass by denying everything.

Caught while wiring: OtOpcUaServerHostedService takes IOpcUaUserAuthenticator as
a REQUIRED ctor parameter, so leaving a driver-only node unregistered is not a
quiet fallback to deny-all — it fails DI and the node does not boot.
NullOpcUaUserAuthenticator is now registered explicitly there. A driver-only node
has no ConfigDb since Phase 4 and so has no key source until the artifact-backed
store lands in stage 4; it denies UserName tokens until then, which is the safe
direction, and anonymous sessions are unaffected.

LdapOpcUaUserAuthenticator is now registered nowhere and is dead code. Deleting
it plus LdapOpcUaUserAuthenticatorTests / LdapAuthResilienceTests /
LdapGlauthOutageLiveTests is stage 2b, kept separate because those cover a
live-gated resilience feature (hard wall-clock bound, bounded concurrency,
directory-outage circuit) that exists precisely BECAUSE the LDAP bind ran inline
on session activation — a constraint the key path does not have. That deserves
its own commit, not a footnote in this one.

Anonymous-as-a-grantable-principal (D3) is also stage 2b.

Host 37, Security 89, OpcUaServer 364 green.

Claude-Session: https://claude.ai/code/session_015p7wGqy3YpZNCpDzTpGMKo
This commit is contained in:
Joseph Doherty
2026-07-31 12:57:51 -04:00
parent f88eb49bd1
commit 0329fa4d4b
7 changed files with 558 additions and 1 deletions
@@ -0,0 +1,67 @@
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
using ZB.MOM.WW.OtOpcUa.OpcUaServer.Security;
namespace ZB.MOM.WW.OtOpcUa.Host.OpcUa;
/// <summary>
/// Derives the interim server-wide role set for an authenticated principal from its
/// <see cref="NodeAcl"/> grants (#520, stage 2).
/// </summary>
/// <remarks>
/// <para>
/// <b>Why this exists at all.</b> The two live authorization gates —
/// <see cref="OpcUaDataPlaneRoles.WriteOperate"/> on Value writes and
/// <see cref="OpcUaDataPlaneRoles.AlarmAck"/> on the Part 9 alarm methods — check a server-wide
/// role set on the session identity. Replacing LDAP with API keys therefore has to keep
/// producing that role set, or the cutover would lock every client out of writes and alarm acks
/// long before per-node ACL evaluation lands in stage 5.
/// </para>
/// <para>
/// ⚠️ <b>Only <see cref="NodeAclScopeKind.Cluster"/> grants confer a role. This is deliberate,
/// and it under-grants.</b> The obvious implementation — union the permission flags across all of
/// a principal's grants — would let a grant authored narrowly ("WriteOperate on Equipment X")
/// confer the server-wide WriteOperate role, because nothing here can enforce scope yet. That
/// fails in the dangerous direction: the operator's narrow intent silently becomes broad
/// authority. Restricting to cluster-scoped grants means a narrow grant does nothing until
/// stage 5 can honour its scope, which is no worse than today (where every ACL row does
/// nothing) and never exceeds what was authored.
/// </para>
/// <para>
/// Scope-narrower-than-cluster grants are not an error and are not logged as one — they are
/// simply inert until enforcement lands. The AdminUI banner is where an operator is told that.
/// </para>
/// </remarks>
public static class ApiKeyInterimRoleMapper
{
/// <summary>
/// Maps a principal's cluster-scoped grants to the server-wide data-plane roles.
/// </summary>
/// <param name="grants">Candidate ACL rows. Rows for other principals are ignored.</param>
/// <param name="principal">The authenticated principal.</param>
/// <returns>The distinct role names to attach to the session identity; never null.</returns>
public static IReadOnlyList<string> Map(IEnumerable<NodeAcl> grants, string principal)
{
ArgumentNullException.ThrowIfNull(grants);
if (string.IsNullOrWhiteSpace(principal)) return Array.Empty<string>();
var effective = NodePermissions.None;
foreach (var grant in grants)
{
if (grant.ScopeKind != NodeAclScopeKind.Cluster) continue;
if (!string.Equals(grant.Principal, principal, StringComparison.OrdinalIgnoreCase)) continue;
effective |= grant.PermissionFlags;
}
var roles = new List<string>(2);
if (effective.HasFlag(NodePermissions.WriteOperate)) roles.Add(OpcUaDataPlaneRoles.WriteOperate);
// AlarmAcknowledge is the flag an operator authors; AlarmAck is the role the Part 9 method gates
// check. The gate covers acknowledge/confirm/shelve as one role, so Confirm/Shelve do not map
// separately — mapping them would imply a granularity the gate does not have.
if (effective.HasFlag(NodePermissions.AlarmAcknowledge)) roles.Add(OpcUaDataPlaneRoles.AlarmAck);
return roles;
}
}