diff --git a/docs/DataConnections.md b/docs/DataConnections.md index 3bb941db9..b9b089d23 100644 --- a/docs/DataConnections.md +++ b/docs/DataConnections.md @@ -98,22 +98,32 @@ HistoryRead of such a node, the consumer first authorizes the caller against the (local grant + mapping bit, everything but the lease). A caller without the grant is still `BadUserAccessDenied` and nothing is sent to the origin. A granted caller gets the same exact `GetMetadata` renewal as above, for the operation it is performing (Read / HistoryRead bit, not -Subscribe), one per node at a time, bounded by the unary budget, with a 250 ms back-off after a -failure: +Subscribe), one per node and operation at a time, with a 250 ms back-off after a failure. On-demand +renewals never touch the unary budget (cold reads, writes) or the browse/subscription-renewal fetch +slots: they queue (at most 64) for their own pool of `max(1, maxConcurrentFetches / 2)` origin calls, +reserve 256 KiB each, and have 3 s end to end, so a batch of N lapsed nodes renews all N: - **Value Read** answers `BadWaitingForInitialData` (as a cold read does) while the renewal runs off the request thread; once it commits, a read is scheduled so the client's retry is served from the fresh sample. `BadResourceUnavailable` while a renewal cannot be started (capacity, back-off). A non-Value attribute of a lapsed node answers `BadResourceUnavailable` while it renews. -- **HistoryRead** renews within the request (one wait per batch, at most 5 s, no node-manager `Lock` - held) and is then served; a renewal that cannot complete (origin unreachable) answers - `BadResourceUnavailable`, never `BadUserAccessDenied`. +- **HistoryRead** renews within the request and is then served. The wait (one per batch, at most 4 s + and never past `HistoryReadDeadline`, no node-manager `Lock` held, the request's cancellation + honoured) takes one of the non-local history batch slots first. A batch that finds none free does + not wait. A renewal that cannot complete (origin unreachable or silent, no slot) answers + `BadResourceUnavailable`. A renewal that succeeded while the node manager's ACL veto still refuses + answers `BadUserAccessDenied`. - **Browse:** a cached page past half its lease is served as is *and* refreshed in the background (one refresh per page key), so a re-browse renews the leases of the nodes it lists. An origin that stopped granting the operation fails the renewal; the consumer then keeps answering the retryable status (it cannot tell that from an unreachable origin). +Not covered yet: a **Write** or **CreateMonitoredItems** on a lapsed node is still refused +`BadUserAccessDenied` (neither starts a renewal; a re-browse, a Read or a HistoryRead of the node +renews it, after which they succeed). A lease that was revoked (`Invalidate`), a removed mapping and a +retired node are refusals, not lapses, and are never renewed. + An authorized logical monitored-item request can survive a physical stream expiry and seek a new pinned binding with bounded retries. The old binding and any queued sample evidence remain tied to their original authority and serving generation. The same item needs a fresh, complete baseline diff --git a/docs/plans/evidence/grpc-data-ext/fu922.md b/docs/plans/evidence/grpc-data-ext/fu922.md index a763a0c1b..e7a24916c 100644 --- a/docs/plans/evidence/grpc-data-ext/fu922.md +++ b/docs/plans/evidence/grpc-data-ext/fu922.md @@ -68,3 +68,21 @@ the fresh-lease control and both idle legs (20 s and 40 s, i.e. past the lease) the next read, with **no `BadUserAccessDenied` anywhere**. With the CLI's default `--max-age 0`, every projected read (lapsed or not) stays `BadWaitingForInitialData`, because the v1 Read path never serves `maxAge = 0` from cache. That is separate from this fix and is filed as a follow-up. `rig.py down` ⇒ 0 containers / volumes / networks / images. + +## Security review round 1 (no auth bypass; two availability findings + lows) — fixes + +Merged local master first (incl. #916/#917, DataConnections), clean, build 0 errors. + +| # | Finding | Resolution | Red/green | +|---|---|---|---| +| 1 | A renewal reserved `MaxUnaryBytes` in the unary budget: (A) a batch of N lapsed nodes renewed only the first, (B) while one ran, every cold read / write / subscription renewal was refused | renewals left the unary budget: own resolver pool `max(1, maxConcurrentFetches/2)` (never `_fetchSlots`), a bounded queue (64), 256 KiB each, 3 s end to end including the pool wait | `A_read_batch_of_several_lapsed_nodes_renews_every_node…`, `A_history_batch_of_several_lapsed_nodes…`, `A_renewal_in_flight_does_not_crowd_out_a_cold_read…`: all **red** on the pre-review fix, green now | +| 2 | `AuthorizeHistoryRead` waited 5 s before the history batch limiter and ignored the request token, so concurrent HistoryReads against a slow origin pinned SDK threads past the 16-batch cap. The wait also equalled the renewal budget | the wait first takes a non-local history slot with `Wait(0)` (none free ⇒ no wait, retryable), honours `OperationContext.CancellationToken`, and lasts 4 s (≤ `HistoryReadDeadline`) against a 3 s renewal | `Concurrent_history_reads_against_a_slow_origin…`: 24 reads against a silent origin, ≥ 24 − RemoteSlots answer in < 2.5 s, all `BadResourceUnavailable`, origin untouched, Good after release. **Red** on the pre-review fix (all 24 waited ~5 s) | +| 3 | Two authorities (policy classifies, `DataPlaneAuthorizer` vetoes) | a renewal that completed `true` and is still vetoed ⇒ `BadUserAccessDenied` | by construction; the grant-revoked test below covers the revoked case | +| 4 | Renewals keyed by node only, though each requests its operation's bit | keyed by (realm, id, operation), back-off too | — | +| 5 | Back-off map cleared wholesale at 4096; browse refresh deduped per (caller, MaxResults) | expired entries are evicted first, otherwise the one expiring first; one background refresh per parent node | — | +| 6 | Write / CreateMonitoredItems on a lapsed node | documented in `docs/DataConnections.md` as not covered (still `BadUserAccessDenied` until a read/browse renews); reported as a follow-up | — | + +New safety controls, green on both the pre-review fix and now: an `Invalidate`d lease ⇒ `BadUserAccessDenied`, no renewal, no origin +call. A grant revoked while the renewal is parked at the origin ⇒ nothing served, the renewal never commits, then +`BadUserAccessDenied`. A mapping removed while renewing ⇒ not revived, not answered as a lapse. 13 #922 tests, 5 consecutive green +runs. diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataLeaseLapseReviewTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataLeaseLapseReviewTests.cs index ccc83ed68..97d3b5240 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataLeaseLapseReviewTests.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataLeaseLapseReviewTests.cs @@ -3,6 +3,7 @@ using Opc.Ua.Client; using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections; +using ZB.MOM.WW.OtOpcUa.OpcUaServer; using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections; using DataBrowseRequest = ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections.BrowseRequest; using DataReadRequest = ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections.ReadRequest; @@ -172,6 +173,7 @@ public sealed partial class GrpcDataConfiguredDiscoveryWireTests await using var originServer = await HistoryOrigin.StartAsync(7); using var origin = new LocalDataFixture(servingGeneration: 1, history: originServer.Port); var connections = new GatedConnections(origin); + OtOpcUaNodeManager? manager = null; await WithComposedServer(origin, origin.Raw, async (session, _, _, _, _, _, anchor) => { var client = (SessionClient)session; @@ -179,14 +181,25 @@ public sealed partial class GrpcDataConfiguredDiscoveryWireTests origin.Clock.Advance(TimeSpan.FromSeconds(16)); connections.HoldMetadataFor = anchor.IdentifierAsString; // the origin never answers the renewal until released var clock = System.Diagnostics.Stopwatch.StartNew(); - var reads = Enumerable.Range(0, 24).Select(_ => RawHistory(client, anchor, 3, default)).ToArray(); + async Task<(Opc.Ua.HistoryReadResult Result, TimeSpan Took)> Timed() + { + var each = System.Diagnostics.Stopwatch.StartNew(); + var result = await RawHistory(client, anchor, 3, default); + return (result, each.Elapsed); + } + var reads = Enumerable.Range(0, 24).Select(_ => Timed()).ToArray(); var all = Task.WhenAll(reads); // Every waiter is bounded (a non-local history slot, then at most the lapse wait) — none is pinned until the origin // answers, which it never does while held. (await Task.WhenAny(all, Task.Delay(TimeSpan.FromSeconds(30), TestContext.Current.CancellationToken))).ShouldBe(all, "every concurrent HistoryRead returned while the origin was still silent"); clock.Elapsed.ShouldBeLessThan(TimeSpan.FromSeconds(30)); - var statuses = all.Result.Select(r => r.StatusCode.Code).ToArray(); + var statuses = all.Result.Select(r => r.Result.StatusCode.Code).ToArray(); + // Waiters are capped by the non-local history slots: the reads that found none answer at once instead of each pinning + // an SDK thread for the whole lapse wait (the pre-review code made all 24 wait it out). + var slots = OriginHistoryPort.RemoteSlots(manager!.MaxConcurrentHistoryReadBatches); + all.Result.Count(r => r.Took < TimeSpan.FromSeconds(2.5)).ShouldBeGreaterThanOrEqualTo(24 - slots, + string.Join(", ", all.Result.Select(r => r.Took.TotalMilliseconds.ToString("0")))); statuses.ShouldAllBe(code => code == StatusCodes.BadResourceUnavailable.Code, "granted + lapsed + silent origin ⇒ retryable"); originServer.Source.Calls.ShouldBe(0); @@ -200,7 +213,7 @@ public sealed partial class GrpcDataConfiguredDiscoveryWireTests await Task.Delay(50, TestContext.Current.CancellationToken); } StatusCode.IsGood(served!.StatusCode).ShouldBeTrue(served.StatusCode.ToString()); - }, localPermissions: 15, allowHistory: true, connectionSource: connections); + }, localPermissions: 15, allowHistory: true, connectionSource: connections, onHistory: (_, nm) => manager = nm); } /// One persistent origin connection whose metadata call for one node can be parked; counts browse/metadata/read calls.