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; /// /// Derives the interim server-wide role set for an authenticated principal from its /// grants. /// /// /// /// Why this exists at all. The two live authorization gates — /// on Value writes and /// 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. /// /// /// ⚠️ Only grants confer a role. This is deliberate, /// and it under-grants. 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. /// /// /// 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. /// /// public static class ApiKeyInterimRoleMapper { /// /// Maps a principal's cluster-scoped grants to the server-wide data-plane roles. /// /// Candidate ACL rows. Rows for other principals are ignored. /// The authenticated principal. /// The distinct role names to attach to the session identity; never null. public static IReadOnlyList Map(IEnumerable grants, string principal) { ArgumentNullException.ThrowIfNull(grants); if (string.IsNullOrWhiteSpace(principal)) return Array.Empty(); 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(3); 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); // HistoryUpdate has its own role: WriteOperate (live writes) never implies authority to rewrite // recorded history, so the role is conferred by the HistoryUpdate flag alone. if (effective.HasFlag(NodePermissions.HistoryUpdate)) roles.Add(OpcUaDataPlaneRoles.HistoryUpdate); return roles; } }