fix(acl): authorize scripted alarm commands at their owning equipment's scope (#797)

Scripted condition NodeIds are opaque ScriptedAlarmIds, so splitting them as a
topology path could never match an Equipment grant: a narrow equipment grant was
denied while a Cluster grant passed.

- OtOpcUaNodeManager binds each LIVE scripted condition instance to the
  EquipmentId it was materialised under (only when it hangs under that folder);
  dropped on remove / drop-recreate / equipment-subtree removal / rebuild; a
  same-id re-materialise under another equipment re-parents and re-binds.
- DataPlaneAuthoritySnapshot carries EquipmentScopePaths (EquipmentId ->
  [uns, Area, Line, Equipment]) decoded from the artifact by the same
  AclScopePathResolver call that plants Equipment grants.
- DataPlaneAuthorizer.AuthorizeScriptedCondition re-reads key, grant and
  generation per call (uncached) and evaluates at the owner path; unbound,
  replaced or unplaceable owners deny under every grant. No id-split or
  realm-root fallback.
- Native conditions (RawPath) and the ACL-off role gate are unchanged.
  NodeIds and AlarmCommand.AlarmId are unchanged.

Sdk2OpaqueScriptedScopeBaselineTests deliberately flipped: it pinned the denial
and now pins owner-grant allow / sibling deny / cluster allow. Follow-up #808
(folder ids + Server-object event visibility for narrow UNS grants).
This commit is contained in:
Joseph Doherty
2026-11-10 21:23:57 -05:00
parent b36e688f0a
commit 3d1b86b451
16 changed files with 1511 additions and 37 deletions
@@ -34,7 +34,8 @@ public sealed class DataPlaneAuthoritySnapshot
IReadOnlyDictionary<AclScopeRef, IReadOnlyList<NodeAclPath>> scopePaths,
IReadOnlyList<PermissionTrieBuildDiagnostic> droppedGrants,
IReadOnlyDictionary<string, ApiKey> keysById,
IReadOnlyDictionary<string, IReadOnlyList<NodeAcl>> grantsByPrincipal)
IReadOnlyDictionary<string, IReadOnlyList<NodeAcl>> grantsByPrincipal,
IReadOnlyDictionary<string, NodeAclPath> equipmentScopePaths)
{
ClusterId = clusterId;
RevisionHash = revisionHash;
@@ -45,11 +46,64 @@ public sealed class DataPlaneAuthoritySnapshot
DroppedGrants = droppedGrants;
_keysById = keysById;
_grantsByPrincipal = grantsByPrincipal;
EquipmentScopePaths = equipmentScopePaths;
}
private readonly IReadOnlyDictionary<string, ApiKey> _keysById;
private readonly IReadOnlyDictionary<string, IReadOnlyList<NodeAcl>> _grantsByPrincipal;
/// <summary>
/// <c>EquipmentId → its UNS ACL path</c> (<c>[uns, Area, Line, Equipment]</c>) for every equipment in
/// the artifact this generation was built from, resolved by the same <see cref="AclScopePathResolver"/>
/// rule that places an <c>Equipment</c>-scoped grant. Keyed <c>OrdinalIgnoreCase</c> (identifier rule,
/// see <see cref="AclScopeRef"/>).
/// </summary>
/// <remarks>
/// <para>
/// <b>Why this exists (Gitea #797).</b> A scripted alarm's condition NodeId is its opaque
/// <c>ScriptedAlarmId</c> (e.g. <c>scripted-eqa-hi</c>), and its equipment folder's NodeId is the
/// opaque <c>EquipmentId</c> — neither is a topology path, so splitting the NodeId (what
/// <c>DataPlaneAuthorizer</c> does for every other node) can never reach the owning equipment's
/// grant. The node manager instead binds each LIVE scripted condition to its owning
/// <c>EquipmentId</c>, and the authorizer resolves that id to a path HERE, against the same
/// generation the grants and the trie came from — so an equipment rename or move that a deploy
/// carries is seen by grants and by owner paths together, never one ahead of the other.
/// </para>
/// <para>
/// Not a grant table and never consulted by the trie build: an entry confers nothing; it only says
/// where in the UNS tree a given equipment sits. An equipment absent here (deleted, line-less,
/// dangling) has no owner path and its scripted conditions are denied under enforcement.
/// </para>
/// </remarks>
public IReadOnlyDictionary<string, NodeAclPath> EquipmentScopePaths { get; }
/// <summary>
/// The owning equipment's path WITHIN the UNS realm (the realm segment stripped), for a scripted
/// condition bound to <paramref name="equipmentId"/>. False when this generation knows no such
/// equipment or its recorded path is not a UNS path — the caller must deny.
/// </summary>
/// <param name="equipmentId">The owning equipment's logical id.</param>
/// <param name="unsSegments">The path below the realm root, e.g. <c>[Area, Line, Equipment]</c>.</param>
/// <returns><see langword="true"/> when an owner path is known.</returns>
public bool TryGetEquipmentUnsSegments(string equipmentId, out IReadOnlyList<string> unsSegments)
{
unsSegments = [];
if (string.IsNullOrEmpty(equipmentId)) return false;
if (!EquipmentScopePaths.TryGetValue(equipmentId, out var path)) return false;
// Shape-checked rather than trusted: a path that does not start at the UNS realm, or that names the
// realm alone, describes no equipment. Accepting a bare [uns] would evaluate at the realm root — a
// parent fallback this binding exists to rule out.
if (path.Segments.Count < 2
|| !string.Equals(path.Segments[0], NodeAclRealms.Uns, StringComparison.Ordinal))
{
return false;
}
unsSegments = path.Segments.Skip(1).ToArray();
return true;
}
/// <summary>
/// The cluster this node belongs to per the artifact's <c>ClusterNode</c> row, or null on a
/// single-cluster / legacy artifact where no scoping applies.
@@ -127,6 +181,8 @@ public sealed class DataPlaneAuthoritySnapshot
/// <param name="grants">The ACL grants for this node's cluster.</param>
/// <param name="scopePaths">Where each grant's scope sits in the address space.</param>
/// <param name="droppedGrants">Grants the trie build discarded, or empty when no trie was built.</param>
/// <param name="equipmentScopePaths">Every equipment's UNS ACL path (see <see cref="EquipmentScopePaths"/>);
/// null ⇒ none, which denies every scripted-condition command under enforcement.</param>
/// <returns>The built snapshot.</returns>
public static DataPlaneAuthoritySnapshot Create(
string? clusterId,
@@ -135,7 +191,8 @@ public sealed class DataPlaneAuthoritySnapshot
IReadOnlyList<ApiKey> keys,
IReadOnlyList<NodeAcl> grants,
IReadOnlyDictionary<AclScopeRef, IReadOnlyList<NodeAclPath>> scopePaths,
IReadOnlyList<PermissionTrieBuildDiagnostic>? droppedGrants = null)
IReadOnlyList<PermissionTrieBuildDiagnostic>? droppedGrants = null,
IReadOnlyDictionary<string, NodeAclPath>? equipmentScopePaths = null)
{
ArgumentNullException.ThrowIfNull(revisionHash);
ArgumentNullException.ThrowIfNull(keys);
@@ -169,8 +226,22 @@ public sealed class DataPlaneAuthoritySnapshot
kv => (IReadOnlyList<NodeAcl>)kv.Value,
StringComparer.OrdinalIgnoreCase);
// Re-keyed under the identifier comparer here rather than trusted from the caller, the same reason
// AclScopePathResolver re-keys its maps: the binding side (the node manager's EquipmentId) and this
// side must not be able to disagree on case.
var equipment = new Dictionary<string, NodeAclPath>(StringComparer.OrdinalIgnoreCase);
if (equipmentScopePaths is not null)
{
foreach (var (equipmentId, path) in equipmentScopePaths)
{
if (string.IsNullOrWhiteSpace(equipmentId) || path is null) continue;
equipment[equipmentId] = path;
}
}
return new DataPlaneAuthoritySnapshot(
clusterId, revisionHash, generationId, keys, grants, scopePaths, droppedGrants ?? [], byId, frozen);
clusterId, revisionHash, generationId, keys, grants, scopePaths, droppedGrants ?? [], byId, frozen,
equipment);
}
}
@@ -236,13 +307,16 @@ public interface IDataPlaneAuthoritySink
/// <param name="keys">The API keys the artifact carries.</param>
/// <param name="grants">The ACL grants for this node's cluster.</param>
/// <param name="scopePaths">Where each grant's scope sits in the address space.</param>
/// <param name="equipmentScopePaths">Every equipment's UNS ACL path, resolved from the same artifact —
/// the owner lookup for scripted alarm conditions (Gitea #797). Null ⇒ none.</param>
/// <returns>The installed snapshot.</returns>
DataPlaneAuthoritySnapshot Apply(
string? clusterId,
string revisionHash,
IReadOnlyList<ApiKey> keys,
IReadOnlyList<NodeAcl> grants,
IReadOnlyDictionary<AclScopeRef, IReadOnlyList<NodeAclPath>> scopePaths);
IReadOnlyDictionary<AclScopeRef, IReadOnlyList<NodeAclPath>> scopePaths,
IReadOnlyDictionary<string, NodeAclPath>? equipmentScopePaths = null);
}
/// <summary>
@@ -290,7 +364,8 @@ public sealed class DataPlaneAuthorityState : IDataPlaneAuthoritySource, IDataPl
string revisionHash,
IReadOnlyList<ApiKey> keys,
IReadOnlyList<NodeAcl> grants,
IReadOnlyDictionary<AclScopeRef, IReadOnlyList<NodeAclPath>> scopePaths)
IReadOnlyDictionary<AclScopeRef, IReadOnlyList<NodeAclPath>> scopePaths,
IReadOnlyDictionary<string, NodeAclPath>? equipmentScopePaths = null)
{
ArgumentNullException.ThrowIfNull(revisionHash);
@@ -323,7 +398,7 @@ public sealed class DataPlaneAuthorityState : IDataPlaneAuthoritySource, IDataPl
var dropped = BuildAndInstallTrie(clusterId, generation, grants, scopePaths);
published = DataPlaneAuthoritySnapshot.Create(
clusterId, revisionHash, generation, keys, grants, scopePaths, dropped);
clusterId, revisionHash, generation, keys, grants, scopePaths, dropped, equipmentScopePaths);
Volatile.Write(ref _current, published);
}