fix(grpc-data): removal admission keyed on the credential, lazy + bounded, no stale verdict write-back (#936 review r1)
S3: the SDK installs a new identity object on every ActivateSession (decompiled Opc.Ua.Server 2.0.1: Session.UpdateUserIdentity assigns EffectiveIdentity; MonitoredItem/Subscription.EffectiveIdentity read the current session's), so an object-keyed admission forgot a re-activated or transferred session. Admission is now keyed on RoleCarryingUserIdentity.AdmissionKey (principal + api key id), and a removal also tells an item that passes the current gate (uncached). S4: the per-condition record is a lazily allocated locked HashSet<string> (no ConditionalWeakTable per condition), one entry per credential, capped at 1024, dropped after the removal's RefreshRequired is decided. N4: Verdict writes back only while that instance is still live. Tests: OI re-activation (red on revert), unit S3/S4 + N4 (red on revert); docs. Also DataConnections.md for #933 r1 S1/S2.
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Opc.Ua;
|
||||
using Opc.Ua.Server;
|
||||
@@ -57,15 +56,48 @@ internal sealed class ImportedCondition(AlarmConditionState alarm, string connec
|
||||
public bool IsNative { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// #936: every identity that passed the §5.4 gate for THIS condition instance (each delivery, refresh replay and
|
||||
/// Read/Browse of it is gated, so any client that may hold it is here). Weak keys: a closed session's identity is
|
||||
/// collected with it. Lets the removal RefreshRequired reach the holders even after the mapping that admitted them
|
||||
/// left the deployed set — the current gate then admits nobody.
|
||||
/// #936: the credentials (<see cref="RoleCarryingUserIdentity.AdmissionKey"/>: principal + key id) that passed the §5.4
|
||||
/// gate for THIS condition instance — each delivery, refresh replay and Read/Browse of it is gated, so any client that
|
||||
/// may hold it is here. Lets the removal RefreshRequired reach the holders even after the mapping that admitted them
|
||||
/// left the deployed set (the current gate then admits nobody).
|
||||
/// Review r1 S3: keyed on the credential, not the identity OBJECT — the SDK installs a new identity object on every
|
||||
/// ActivateSession (verified: <c>Session.UpdateUserIdentity</c> assigns <c>EffectiveIdentity</c>, and
|
||||
/// <c>MonitoredItem.EffectiveIdentity</c> reads it through the subscription's CURRENT session), so a re-activated or
|
||||
/// transferred session would otherwise be forgotten.
|
||||
/// Review r1 S4: allocated on the first admission only (most conditions are never admitted to anyone), a plain locked
|
||||
/// set of strings shared with the identity — no finalizable containers, no dependent handles. Bounded: one entry per
|
||||
/// distinct credential admitted (≤ deployed API keys + anonymous; NOT per session — a Server-object ConditionRefresh by
|
||||
/// S sessions of K keys adds ≤ C × K entries), capped at <see cref="MaxAdmittedCredentials"/>; past the cap a holder
|
||||
/// falls back to the current gate at removal. Dropped when the condition is removed.
|
||||
/// </summary>
|
||||
private readonly ConditionalWeakTable<object, object> _admitted = new();
|
||||
private static readonly object AdmittedMark = new();
|
||||
public void Admit(object identity) => _admitted.AddOrUpdate(identity, AdmittedMark);
|
||||
public bool WasAdmitted(object identity) => _admitted.TryGetValue(identity, out _);
|
||||
private HashSet<string>? _admitted;
|
||||
|
||||
/// <summary>#936 review r1 S4: the per-condition cap on recorded credentials (far above any real key count).</summary>
|
||||
internal const int MaxAdmittedCredentials = 1024;
|
||||
|
||||
public void Admit(RoleCarryingUserIdentity identity)
|
||||
{
|
||||
var set = Volatile.Read(ref _admitted);
|
||||
if (set is null)
|
||||
{
|
||||
var fresh = new HashSet<string>(StringComparer.Ordinal);
|
||||
set = Interlocked.CompareExchange(ref _admitted, fresh, null) ?? fresh;
|
||||
}
|
||||
lock (set) if (set.Count < MaxAdmittedCredentials) set.Add(identity.AdmissionKey);
|
||||
}
|
||||
|
||||
public bool WasAdmitted(object? identity)
|
||||
{
|
||||
if (identity is not RoleCarryingUserIdentity caller || Volatile.Read(ref _admitted) is not { } set) return false;
|
||||
lock (set) return set.Contains(caller.AdmissionKey);
|
||||
}
|
||||
|
||||
/// <summary>Test/diagnostic observation: credentials recorded (0 when never admitted — the set is not allocated).</summary>
|
||||
internal int AdmittedCount { get { var set = Volatile.Read(ref _admitted); if (set is null) return 0; lock (set) return set.Count; } }
|
||||
internal bool AdmittedAllocated => Volatile.Read(ref _admitted) is not null;
|
||||
|
||||
/// <summary>#936 review r1 S4: the removal's RefreshRequired has been decided — the record goes with the instance.</summary>
|
||||
public void ForgetAdmitted() => Volatile.Write(ref _admitted, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -156,6 +188,10 @@ internal sealed class ProjectedConditionRegistry
|
||||
: credentialExpires is { } expires && expires < now + PositiveVerdictTtl ? expires : now + PositiveVerdictTtl;
|
||||
lock (_verdictGate)
|
||||
{
|
||||
// #936 review r1 N4: computed off the gate — a Remove may have purged this NodeId's verdicts meanwhile. Written back
|
||||
// only while THIS instance is still the live one (Remove drops it from _byInstance before purging under this gate),
|
||||
// so a same-NodeId re-creation never inherits a verdict computed for the removed instance.
|
||||
if (!_byInstance.ContainsKey(condition.Alarm)) return allowed;
|
||||
if (_verdicts.Count >= 65_536) _verdicts.Clear(); // bounded: a flood of identities only costs recomputation
|
||||
_verdicts[key] = (generation, condition.PlacementVersion, allowed, validUntil);
|
||||
}
|
||||
@@ -236,6 +272,10 @@ public sealed partial class OtOpcUaNodeManager : IProjectedConditionSink
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>#936 review r1 S3: the current §5.4 gate, uncached (a removed instance's removal decision).</summary>
|
||||
private bool ImportedVisibleNow(object identity, ImportedCondition condition) =>
|
||||
ProjectedConditionAccess is { } access && identity is RoleCarryingUserIdentity caller && access.CanSee(caller, condition.Placements);
|
||||
|
||||
/// <summary>The marker a queued imported event carries: re-checked at every Publish and Republish (§5.4).</summary>
|
||||
private ProjectedEventMarker ImportedMarker(IEventMonitoredItem item, ImportedCondition condition)
|
||||
{
|
||||
@@ -354,7 +394,11 @@ public sealed partial class OtOpcUaNodeManager : IProjectedConditionSink
|
||||
// §5.2 "removal + RefreshRequired": a client holding a removed (e.g. Active/Retain) condition is told to refresh, after
|
||||
// which it no longer lists it. Outside Lock. #936: decided by who was ADMITTED to the condition, not by the current
|
||||
// mapping set — a removal caused by the mapping (or its connection) leaving the deployed set must still tell them.
|
||||
if (removed.Count > 0) QueueRefreshRequired(targets, removed, removal: true);
|
||||
if (removed.Count > 0)
|
||||
{
|
||||
QueueRefreshRequired(targets, removed, removal: true);
|
||||
foreach (var c in removed) c.ForgetAdmitted();
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -438,8 +482,10 @@ public sealed partial class OtOpcUaNodeManager : IProjectedConditionSink
|
||||
/// </summary>
|
||||
private void QueueRefreshRequired(RefreshTargets targets, IReadOnlyList<ImportedCondition> conditions, bool removal = false)
|
||||
{
|
||||
// #936 review r1 S3: admitted under this credential, OR passes the current gate now (a holder past the per-condition
|
||||
// cap, or anything the record missed) — uncached, so no verdict is written back for the removed instance.
|
||||
Func<IEventMonitoredItem, ImportedCondition, bool> holds = removal
|
||||
? (item, c) => item.EffectiveIdentity is { } identity && c.WasAdmitted(identity)
|
||||
? (item, c) => item.EffectiveIdentity is { } identity && (c.WasAdmitted(identity) || ImportedVisibleNow(identity, c))
|
||||
: ImportedVisibleTo;
|
||||
var send = new List<IEventMonitoredItem>();
|
||||
foreach (var item in targets.ServerItems)
|
||||
|
||||
@@ -52,6 +52,7 @@ public sealed class RoleCarryingUserIdentity : RoleBasedIdentity
|
||||
Roles = [.. roles];
|
||||
Principal = principal ?? string.Empty;
|
||||
ApiKeyId = apiKeyId;
|
||||
AdmissionKey = Principal + "\u0000" + (apiKeyId ?? string.Empty);
|
||||
CredentialVersion = credentialVersion;
|
||||
CredentialExpiresUtc = credentialExpiresUtc;
|
||||
}
|
||||
@@ -84,6 +85,13 @@ public sealed class RoleCarryingUserIdentity : RoleBasedIdentity
|
||||
/// </summary>
|
||||
public string? ApiKeyId { get; }
|
||||
|
||||
/// <summary>
|
||||
/// #936 review r1 S3: what stays the same across this credential's activations — principal + key id. The SDK builds a NEW
|
||||
/// identity object on every ActivateSession (re-activation on a new channel, a transferred subscription), so anything
|
||||
/// keyed on the object forgets the session. Sessions sharing one key share its grants, so they share this key.
|
||||
/// </summary>
|
||||
internal string AdmissionKey { get; }
|
||||
|
||||
/// <summary>Authenticated credential version captured at activation, never the secret.</summary>
|
||||
public string? CredentialVersion { get; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user