f692f6afe4
`Security:Acl:Enabled` (default false) makes scoped `NodeAcl` grants decide data-plane authorization at the three seams that have one, and gives HistoryRead a check it never had. Read, Browse and Subscribe stay ungated — stage 6. The ACL decision REPLACES the server-wide role check rather than chaining with it. Chained, a grant would have to confer the role as well as pass the scope check, and only Cluster-scoped grants confer a role — so every narrow grant would have stayed inert and the flag would have looked inert too. The load-bearing test is an ALLOW: a session carrying no roles at all writing successfully on the strength of its grant. A deny-only test passes against a build where the ACL is never consulted, because the role gate denies too. Found en route: the principal never reached the session. The authenticator passed it as OpcUaUserAuthResult.DisplayName, but RoleCarryingUserIdentity derives DisplayName from the presented token — for a key-bearing client that is the static username `apikey`, identical fleet-wide. So the trie's matching key was dropped at the identity boundary, and the audit ClientUserId on a rejected write recorded `apikey` for every consuming system. Principal and ApiKeyId are now carried explicitly. Beyond the stage as sketched: - Revocation reaches an OPEN session. Every decision re-checks the authenticating key against the current snapshot; without it a key revoked by a deployment kept its authority until the client chose to reconnect, which for a machine-to-machine consumer can be indefinitely. - UserAuthorizationState is built per CALL, not per session. The per-session shape exists so an LDAP session can re-resolve membership that changed out of band; nothing can change out of band under API keys, so the generation stamp is the whole invalidation signal and a per-call record picks it up for free. IsStale/NeedsRefresh therefore cannot fire here and are documented as vestigial. - The trie is built inside DataPlaneAuthorityState.Apply, under the same generation as the snapshot, rather than by a second wiring at the publish actor. An uninstalled trie denies everything — indistinguishable from working enforcement. Deleting the install turns 13 of the 22 new tests red. - The deploy log reports what the BUILD dropped instead of re-deriving it. The old count agreed with the builder on one of its four drop reasons; a rule with a blank ScopeId, a retired scope kind, or a path not starting at a realm vanished with the log reading zero. Both IApiKeyStore implementations widened to the full grant set. The narrowing was redundant — ApiKeyInterimRoleMapper applies the same filter itself — and became a trap once scope was enforced. Pinned by asserting the roles are unchanged, with the two grants carrying different flags so it cannot pass by accident. Falsified by mutation: enforcement never engaging, the write seam ignoring the ACL, the realm dropped from the scope, the revocation check removed, and the trie never installed each turn the intended tests red and no others. Full solution suite green. Claude-Session: https://claude.ai/code/session_015p7wGqy3YpZNCpDzTpGMKo
72 lines
3.7 KiB
C#
72 lines
3.7 KiB
C#
using Opc.Ua;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.Security;
|
|
|
|
/// <summary>
|
|
/// A <see cref="UserIdentity"/> that additionally carries the LDAP-resolved roles
|
|
/// (<see cref="OpcUaUserAuthResult.Roles"/>) for the authenticated session. The SDK stores the
|
|
/// instance we assign to <c>ImpersonateEventArgs.Identity</c> by reference onto
|
|
/// <c>Session.Identity</c>/<c>Session.EffectiveIdentity</c> (no wrapping or copying), so a method
|
|
/// handler can later recover the roles via
|
|
/// <c>(context as ISessionOperationContext)?.UserIdentity as RoleCarryingUserIdentity</c> — the
|
|
/// seam the inbound alarm ack/shelve <c>AlarmAck</c> gate (T18) reads. This type only carries the
|
|
/// roles; it makes no auth decision.
|
|
/// </summary>
|
|
public sealed class RoleCarryingUserIdentity : UserIdentity
|
|
{
|
|
/// <summary>
|
|
/// Initializes a new instance, building the base identity from the same UserName token the
|
|
/// SDK decrypted and attaching the authenticator's resolved roles.
|
|
/// </summary>
|
|
/// <param name="token">The user identity token the SDK handed to the impersonation handler.</param>
|
|
/// <param name="roles">The roles resolved by the authenticator (e.g. <c>ReadOnly</c>, <c>AlarmAck</c>).</param>
|
|
public RoleCarryingUserIdentity(UserIdentityToken token, IReadOnlyList<string> roles)
|
|
: this(token, roles, principal: string.Empty, apiKeyId: null)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance carrying the ACL principal and the authenticating key id alongside
|
|
/// the roles (#520 stage 5).
|
|
/// </summary>
|
|
/// <param name="token">The user identity token the SDK handed to the impersonation handler.</param>
|
|
/// <param name="roles">The roles resolved by the authenticator.</param>
|
|
/// <param name="principal">The ACL principal this session authenticates as.</param>
|
|
/// <param name="apiKeyId">The public id of the key that authenticated, or null for anonymous.</param>
|
|
public RoleCarryingUserIdentity(
|
|
UserIdentityToken token, IReadOnlyList<string> roles, string principal, string? apiKeyId)
|
|
: base(token)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(roles);
|
|
// Freeze to a defensive copy so a caller mutating the source list can never retroactively
|
|
// alter this session's granted roles, and so the gate's per-ack `.Contains(...)` runs over a
|
|
// known-small frozen array.
|
|
Roles = [.. roles];
|
|
Principal = principal ?? string.Empty;
|
|
ApiKeyId = apiKeyId;
|
|
}
|
|
|
|
/// <summary>The roles the authenticator granted this session, used by downstream permission gates.</summary>
|
|
public IReadOnlyList<string> Roles { get; }
|
|
|
|
/// <summary>
|
|
/// The ACL principal this session authenticates as — the name <c>NodeAcl</c> rows key on, and what
|
|
/// the permission trie matches. Empty when the authenticator predates the principal model, which
|
|
/// the ACL gate treats as unauthorized.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// ⚠️ <b>This is NOT <see cref="UserIdentity.DisplayName"/>.</b> The base class derives that from
|
|
/// the presented token, so for a key-bearing client it is the static username <c>apikey</c> —
|
|
/// identical for every key in the fleet and useless as an identity. See
|
|
/// <see cref="OpcUaUserAuthResult.Principal"/>.
|
|
/// </remarks>
|
|
public string Principal { get; }
|
|
|
|
/// <summary>
|
|
/// Public id of the API key that authenticated this session, or null for anonymous. Lets an open
|
|
/// session be re-checked against a later deployment that revoked the key, instead of the key
|
|
/// remaining usable until the client happens to disconnect.
|
|
/// </summary>
|
|
public string? ApiKeyId { get; }
|
|
}
|