Files
lmxopcua/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/Security/UnsAclSubject.cs
T
Joseph Doherty e39a125069 fix(acl): close the #808 review findings — reference variables, owner folder chain, NUL keys, unknown ids (#808)
Review of af5c9744:
1. UNS reference variables (s=Area/Line/Equip/Eff) of a moved or renamed
   equipment stayed readable/writable under the OLD names' grants: they now
   resolve as ReferenceVariable and are authorized at their split path only
   while it equals their live equipment folder's placement (live chain ==
   generation chain) + one name; otherwise denied (Read and Write).
2. TryPlaceScriptedCondition now also requires the owner folder's live chain
   to match the generation (AuthorizeScriptedCondition gains ownerFolderChain),
   so a moved equipment's alarm/children/commands follow its folder.
3. Identifiers containing NUL are denied before the decision cache, keeping
   client ids disjoint from the NUL-prefixed resolved-scope keys (a client
   could otherwise poison a real folder's cached verdict for its key shard).
4. An id no live node holds is Unknown: placed at [\0unknown] so only
   Cluster / Namespace-uns grants decide it (a removed opaque id can no longer
   split onto a same-named narrow grant; narrow grantees get no NodeDeleted for
   removed UNS folders/vtags/conditions — documented); 4-segment
   reference-shaped ids keep their split.
5. A wire test drives placements from DeploymentArtifact.ParseDataPlaneAuthority.

Baseline updates: #797 owner tests (unit + wire) and Sdk2RepeatedAlarmAuthority
now install folder scopes; Sdk2HistoryAuthority's UNS alias used the RAW path
as its UNS id (never minted in production) and now carries EqA/HistoryAlias.
Each new guard was falsified (reverting it fails its test).
2026-11-10 23:44:11 -05:00

121 lines
6.4 KiB
C#

namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.Security;
/// <summary>How a UNS-namespace node is to be placed in the ACL scope tree (Gitea #808).</summary>
public enum UnsAclSubjectKind
{
/// <summary>
/// The node's <c>s=</c> identifier IS its ACL path and nothing live vouches for it — a UNS node with no live
/// hierarchy-folder ancestor (only test rigs mint those). Split as before.
/// </summary>
Path,
/// <summary>
/// No live node holds the identifier (removed, never deployed, or a client probing). Only a realm-wide grant
/// may decide it — except a reference-variable-shaped id (exactly four segments), which keeps its split so a
/// removed reference variable's NodeDeleted still reaches the grantees of that name path.
/// </summary>
Unknown,
/// <summary>
/// A UNS reference variable (<c>s=Area/Line/Equipment/EffectiveName</c>) hanging under a live equipment folder:
/// authorized at its split path, but only while that path equals the equipment's placement in the current
/// generation plus one effective-name segment (a moved or renamed equipment's variables are denied until the
/// address space re-points them).
/// </summary>
ReferenceVariable,
/// <summary>
/// A UNS Area / Line / Equipment folder minted under its opaque entity id, or a node owned by one
/// (a virtual tag <c>EQ-…/Name</c> or its sub-folder): placed at the folder's path in the current generation.
/// </summary>
Folder,
/// <summary>
/// A scripted alarm condition (<c>s=&lt;ScriptedAlarmId&gt;</c>) or one of its instance-scoped children
/// (<c>s=&lt;ScriptedAlarmId&gt;/…</c>): placed at its owning equipment, as the #797 command gate does.
/// </summary>
ScriptedCondition,
}
/// <summary>
/// The structural facts the node manager reports about one UNS-namespace node, read from the LIVE address
/// space at decision time. The authorizer turns them into a scope against the CURRENT authority generation —
/// the node manager never computes a scope itself, so placement and grants always come from one generation.
/// </summary>
/// <remarks>
/// <para>
/// <b>Why the identifier cannot simply be split.</b> UNS folders are minted under opaque entity ids
/// (<c>s=EQ-1a2b…</c>) and scripted conditions under their opaque <c>ScriptedAlarmId</c>, while every
/// UNS grant is planted at a NAME path (<c>[uns, Area, Line, Equipment]</c>). Splitting those ids can
/// never reach a narrow grant (and a one-segment split could coincide with an unrelated Area's name).
/// </para>
/// <para>
/// A <see cref="UnsAclSubjectKind.Folder"/> subject carries the folder's live id ancestry so the authorizer
/// can require it to equal the generation's; a <see cref="UnsAclSubjectKind.ScriptedCondition"/> subject
/// carries the live owner binding (null when unbound ⇒ denied under every grant).
/// </para>
/// </remarks>
public readonly record struct UnsAclSubject
{
/// <summary>The identifier is the ACL path — the pre-#808 behaviour.</summary>
public static readonly UnsAclSubject Path = new() { Kind = UnsAclSubjectKind.Path };
/// <summary>No live node holds the identifier.</summary>
public static readonly UnsAclSubject Unknown = new() { Kind = UnsAclSubjectKind.Unknown };
/// <summary>Which placement rule applies.</summary>
public UnsAclSubjectKind Kind { get; init; }
/// <summary>
/// The owning folder's LIVE id ancestry, root-first, ending at the owning folder itself
/// (e.g. <c>[UA-…, UL-…, EQ-…]</c>): for <see cref="UnsAclSubjectKind.Folder"/> and
/// <see cref="UnsAclSubjectKind.ReferenceVariable"/> the folder at or above the node; for
/// <see cref="UnsAclSubjectKind.ScriptedCondition"/> the folder the bound condition hangs under (null when
/// unbound).
/// </summary>
public IReadOnlyList<string>? FolderChain { get; init; }
/// <summary>
/// For <see cref="UnsAclSubjectKind.Folder"/>: true when the node IS the folder; false when it is a node the
/// folder owns (a virtual tag, its sub-folder).
/// </summary>
public bool IsFolderItself { get; init; }
/// <summary>For <see cref="UnsAclSubjectKind.ScriptedCondition"/>: the condition's <c>ScriptedAlarmId</c>.</summary>
public string? AlarmId { get; init; }
/// <summary>
/// For <see cref="UnsAclSubjectKind.ScriptedCondition"/>: the <c>EquipmentId</c> the LIVE condition instance
/// is bound to, or null when the node manager holds no binding for it.
/// </summary>
public string? OwnerEquipmentId { get; init; }
/// <summary>A UNS folder (or a node it owns) with its live id ancestry.</summary>
/// <param name="chain">Root-first live ancestry ending at the owning folder.</param>
/// <param name="isFolderItself">Whether the node is the folder itself.</param>
/// <returns>The subject.</returns>
public static UnsAclSubject Folder(IReadOnlyList<string> chain, bool isFolderItself) =>
new() { Kind = UnsAclSubjectKind.Folder, FolderChain = chain, IsFolderItself = isFolderItself };
/// <summary>A scripted condition (or one of its children) and its live owner binding.</summary>
/// <param name="alarmId">The condition's <c>ScriptedAlarmId</c>.</param>
/// <param name="ownerEquipmentId">The live owner binding, or null when unbound.</param>
/// <param name="ownerFolderChain">The live id ancestry of the folder the condition hangs under, or null.</param>
/// <returns>The subject.</returns>
public static UnsAclSubject ScriptedCondition(
string alarmId, string? ownerEquipmentId, IReadOnlyList<string>? ownerFolderChain) =>
new()
{
Kind = UnsAclSubjectKind.ScriptedCondition,
AlarmId = alarmId,
OwnerEquipmentId = ownerEquipmentId,
FolderChain = ownerFolderChain,
};
/// <summary>A UNS reference variable under a live equipment folder.</summary>
/// <param name="equipmentChain">Root-first live ancestry ending at the variable's equipment folder.</param>
/// <returns>The subject.</returns>
public static UnsAclSubject ReferenceVariable(IReadOnlyList<string> equipmentChain) =>
new() { Kind = UnsAclSubjectKind.ReferenceVariable, FolderChain = equipmentChain };
}