fix(acl): place opaque-id UNS folders, virtual tags and scripted conditions for every operation (#808)

UNS Area/Line/Equipment folders are minted under their opaque entity ids
(s=UA-/UL-/EQ-...), virtual tags under s=EQ-.../Name and scripted conditions
(+ #629 children) under s=<ScriptedAlarmId>[/...], while every UNS grant is
planted at a NAME path. DataPlaneAuthorizer split those ids, so with
Security:Acl:Enabled a narrow (Area/Line/Equipment) grant could not Browse,
Read, subscribe or HistoryRead them, and Server-object delivery never reached
it — so a narrow key never held the EventId Ack/Confirm/AddComment need.

- The generation carries UnsFolderScopes (folderId -> path + id chain), built
  by DeploymentArtifact from the same AclScopePathResolver as the grants; ids
  two entity kinds share are dropped; plumbed through IDataPlaneAuthoritySink.
- OtOpcUaNodeManager.ResolveUnsAclSubject reports live facts (folder id
  ancestry / scripted owner binding); attached by the Authorizer setter.
- Evaluate places the subject against the CURRENT generation before the
  decision cache, which now caches the walk keyed on the resolved scope.
  Folders require live chain == generation chain (a moved equipment is denied
  under old and new line until the rebuild). Owned nodes (vtags, conditions,
  children) get owner path + a reserved segment, so Tag grants and their
  Browse transit never expose them. Conditions share TryPlaceScriptedCondition
  with the #797 command gate.
- Server-object visibility of a scripted condition is decided on the
  condition alone, so a moved alarm cannot reach its old owner there.

Tests: UnsFolderScopeWireTests (opaque ids, end-to-end browse -> EventId ->
Acknowledge on an Equipment grant; sibling denied; Tag-only grant; moved alarm
and moved equipment stale windows). Rig tests now install folder scopes
explicitly (ServerEventRig.FolderScopes); the #797 recreate test deliberately
asserts the moved alarm is invisible to the Cluster observer in the window.
This commit is contained in:
Joseph Doherty
2026-11-10 23:12:46 -05:00
parent 308b1b3b56
commit af5c9744b7
15 changed files with 1095 additions and 60 deletions
@@ -633,7 +633,15 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
public DataPlaneAuthorizer? Authorizer
{
get => _authorizer;
set => _authorizer = value;
set
{
// Gitea #808: the authorizer places UNS folders, the nodes they own and scripted conditions through THIS
// manager's live address space. Attached here — the one place an authorizer meets its node manager — so
// there is no second wiring that could silently not run (an unattached resolver splits opaque ids,
// which only ever DENIES narrow grants, so a miss would read as working enforcement).
if (value is not null) value.UnsSubjectResolver = ResolveUnsAclSubject;
_authorizer = value;
}
}
private volatile DataPlaneAuthorizer? _authorizer;
@@ -4203,6 +4211,122 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
!OwnsNamespace(nodeId)
|| _authorizer!.IsAllowedCached(identity, operation, nodeId, RealmOf(nodeId));
/// <summary>Guard against a corrupt (cyclic) parent chain in <see cref="ResolveUnsAclSubject"/>.</summary>
private const int MaxUnsAncestry = 64;
/// <summary>
/// Gitea #808 — reports how the UNS-namespace node <paramref name="identifier"/> is placed in the LIVE address
/// space, for <see cref="Security.DataPlaneAuthorizer"/> to turn into a scope against the current generation.
/// </summary>
/// <remarks>
/// <para>
/// Three shapes share the UNS namespace, and only one of them has a path for an identifier:
/// </para>
/// <list type="bullet">
/// <item><b>UNS reference variables</b> — <c>s=Area/Line/Equipment/EffectiveName</c>, NAMES: the id IS the
/// ACL path (<see cref="UnsAclSubjectKind.Path"/>), so a <c>Tag</c> grant keeps matching it.</item>
/// <item><b>Hierarchy folders and what they own</b> — Area / Line / Equipment folders under their opaque
/// entity ids (<c>s=UA-…</c>/<c>s=UL-…</c>/<c>s=EQ-…</c>), and an equipment's virtual tags and their
/// sub-folders (<c>s=EQ-…/Name</c>): <see cref="UnsAclSubjectKind.Folder"/>, with the live id ancestry.</item>
/// <item><b>Scripted conditions and their children</b> — <c>s=&lt;ScriptedAlarmId&gt;[/…]</c>:
/// <see cref="UnsAclSubjectKind.ScriptedCondition"/>, with the exact live instance's owner binding (#797).</item>
/// </list>
/// <para>
/// <b>A folder-owned node is recognised by an id rule that can only NARROW, never widen.</b> A node is folder-
/// owned when its first id segment is the id of its NEAREST live hierarchy-folder ancestor (a folder whose id
/// carries no <c>/</c> — Area/Line/Equipment ids are <c>^[A-Za-z0-9_-]+$</c>; a virtual-tag sub-folder's id
/// always does). A reference variable's first segment is its Area's NAME, so the rule can only misfire if an
/// Area is named exactly like the referencing equipment's own id — and then the variable is authorized at that
/// very equipment (its real parent), losing only its Tag-grant match: fail closed.
/// </para>
/// <para>
/// Lock-free except for the scripted-condition branch (the native flag and owner binding are guarded by
/// <c>Lock</c>, which is reentrant): the folder and variable maps are concurrent and a node's
/// <see cref="NodeState.Parent"/> is a reference read. Every folder on the walk must be the instance the
/// folder map holds now, so a node torn down by a rebuild cannot place itself through a dead ancestry.
/// </para>
/// </remarks>
/// <param name="identifier">The node's <c>s=</c> identifier in the UNS namespace.</param>
/// <returns>The placement facts; <see cref="UnsAclSubject.Path"/> when the identifier is its own path.</returns>
internal UnsAclSubject ResolveUnsAclSubject(string identifier)
{
if (string.IsNullOrEmpty(identifier)) return UnsAclSubject.Path;
var key = MapKey(AddressSpaceRealm.Uns, identifier);
NodeState? node = _folders.TryGetValue(key, out var folder)
? folder
: _variables.TryGetValue(key, out var variable) ? variable : null;
var slash = identifier.IndexOf('/');
var firstSegment = slash < 0 ? identifier.AsSpan() : identifier.AsSpan(0, slash);
if (node is null)
{
// Not a folder or variable: a scripted condition (s=<id>) or one of its instance-scoped children
// (s=<id>/<browse path>, #629). Anything else is an unknown id and keeps the split.
var conditionId = slash < 0 ? identifier : identifier[..slash];
var conditionKey = MapKey(AddressSpaceRealm.Uns, conditionId);
if (!_alarmConditions.TryGetValue(conditionKey, out var condition)) return UnsAclSubject.Path;
lock (Lock)
{
if (_nativeAlarmNodeIds.Contains(conditionKey)) return UnsAclSubject.Path;
var owner = _scriptedAlarmOwners.TryGetValue(conditionKey, out var binding)
&& ReferenceEquals(binding.Alarm, condition)
&& _alarmConditions.TryGetValue(conditionKey, out var live)
&& ReferenceEquals(live, condition)
? binding.EquipmentId
: null;
return UnsAclSubject.ScriptedCondition(conditionId, owner);
}
}
// The nearest live hierarchy folder at or above the node.
FolderState? owning = null;
var current = ReferenceEquals(node, folder) ? node : ParentOf(node);
for (var depth = 0; current is not null && depth < MaxUnsAncestry; depth++, current = ParentOf(current))
{
if (LiveUnsFolderId(current) is not { } id) break;
if (id.Contains('/')) continue;
owning = (FolderState)current;
break;
}
if (owning is null || !firstSegment.SequenceEqual(owning.NodeId.IdentifierAsString)) return UnsAclSubject.Path;
// Its live ancestry, root-first: every live hierarchy folder up to the first non-UNS-folder (the root).
var chain = new List<string>(3);
current = owning;
for (var depth = 0; current is not null && depth < MaxUnsAncestry; depth++, current = ParentOf(current))
{
if (LiveUnsFolderId(current) is not { } id || id.Contains('/')) break;
chain.Add(id);
}
chain.Reverse();
return UnsAclSubject.Folder(chain, isFolderItself: ReferenceEquals(node, owning));
}
/// <summary>The node's parent in the instance tree, or null for a type/root node.</summary>
/// <param name="node">The node.</param>
/// <returns>Its parent.</returns>
private static NodeState? ParentOf(NodeState node) => (node as BaseInstanceState)?.Parent;
/// <summary>
/// The id of <paramref name="node"/> when it is a UNS folder this manager currently holds — the exact instance
/// the folder map holds, not a torn-down one — else null.
/// </summary>
/// <param name="node">A node on an ancestry walk.</param>
/// <returns>The folder's <c>s=</c> id, or null.</returns>
private string? LiveUnsFolderId(NodeState node) =>
node is FolderState f
&& f.NodeId.NamespaceIndex == NamespaceIndexForRealm(AddressSpaceRealm.Uns)
&& f.NodeId.TryGetValue(out string id)
&& _folders.TryGetValue(MapKey(AddressSpaceRealm.Uns, id), out var live)
&& ReferenceEquals(live, f)
? id
: null;
// Denied slots are answered and marked ReadValueId.Processed before the base
// runs, so a refused read never resolves the node or samples its value. Every attribute of a node
// is gated by the single Read permission rather than only Value: the finer split
@@ -5288,6 +5412,21 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
lock (Lock)
{
// Gitea #808: a SCRIPTED condition is admitted by its own node alone. The authorizer places it at its
// owning equipment (#797 owner binding + this generation's deployed owner), so every grant its equipment
// folder would admit admits the condition too — the folders add nothing in steady state. What they WOULD
// add is the stale window: a moved alarm still hanging under its old equipment's folder (generation
// installed, address space not yet rebuilt, or the apply failed) would keep reaching the old owner's
// sessions through that folder while its node, its children and its commands are denied to them.
var key = MapKey(condition.NodeId);
if (condition.NodeId.NamespaceIndex == NamespaceIndexForRealm(AddressSpaceRealm.Uns)
&& _alarmConditions.TryGetValue(key, out var live)
&& ReferenceEquals(live, condition)
&& !_nativeAlarmNodeIds.Contains(key))
{
return scopes;
}
for (var ancestor = (condition as BaseInstanceState)?.Parent;
ancestor is not null;
ancestor = (ancestor as BaseInstanceState)?.Parent)