fix(grpc-data): H1 peer cursors bind the caller's principal and the live serving incarnation (review 3)
The store used to build the expected key from its own fields, so the principal/incarnation check could never fail. The scope now supplies the caller's principal and reads the serving incarnation at save and at take, so an in-process failover on a still-open connection invalidates its tokens; a save for a non-owner principal is refused.
This commit is contained in:
@@ -9,7 +9,7 @@ namespace ZB.MOM.WW.OtOpcUa.OpcUaServer;
|
||||
/// <c>(realm, identifier)</c> and the hash of the request details.
|
||||
/// </summary>
|
||||
/// <param name="Principal">The authenticated principal of the connection.</param>
|
||||
/// <param name="ServingIncarnation">The serving incarnation the cursor was issued under.</param>
|
||||
/// <param name="ServingIncarnation">The serving incarnation CURRENT when the cursor was issued (or, on a take, now).</param>
|
||||
/// <param name="Epoch">The history (address-space) epoch.</param>
|
||||
/// <param name="Realm">The view's realm.</param>
|
||||
/// <param name="Identifier">The view's identifier.</param>
|
||||
@@ -20,14 +20,16 @@ internal readonly record struct PeerHistoryContinuationKey(
|
||||
/// <summary>
|
||||
/// The ORIGIN-side store for remote peers' HistoryRead continuation points (gRPC data history plan §3.2, phase H1;
|
||||
/// wired to the RPC by H2). One instance per (authenticated principal, connection incarnation), so no peer can ever
|
||||
/// reach another's cursors; unlike the SDK session store (<see cref="SessionHistoryContinuationStore"/>) there is no
|
||||
/// reach another's cursors (H2 must <see cref="Clear"/> it when the connection closes); unlike the SDK session store (<see cref="SessionHistoryContinuationStore"/>) there is no
|
||||
/// session to own the points, so this store owns lifetime itself.
|
||||
/// <list type="bullet">
|
||||
/// <item><b>Token:</b> a fresh random 16-byte id (<see cref="RandomNumberGenerator"/>). Unguessable, so no MAC is needed; it
|
||||
/// carries nothing — the state stays here.</item>
|
||||
/// <item><b>Binding:</b> each record binds the principal, serving incarnation, history epoch, view and details hash; a
|
||||
/// take whose expected <see cref="PeerHistoryContinuationKey"/> differs in ANY of them is a miss
|
||||
/// (<c>BadContinuationPointInvalid</c> at the port).</item>
|
||||
/// <item><b>Binding:</b> each record binds the CALLER's principal and the serving incarnation current at save (both
|
||||
/// supplied by the caller — <see cref="PeerHistoryContinuationScope"/> reads the incarnation live, so an in-process
|
||||
/// failover or role change on a still-open connection invalidates its tokens), the history epoch, the view and the
|
||||
/// details hash; a take whose expected <see cref="PeerHistoryContinuationKey"/> differs in ANY of them is a miss
|
||||
/// (<c>BadContinuationPointInvalid</c> at the port). A save for a principal other than the owner is refused.</item>
|
||||
/// <item><b>Single-use:</b> a take removes the record whether or not it matches, so a page lost in transit makes the
|
||||
/// client's resume fail rather than silently skipping a page, and a token can never be replayed.</item>
|
||||
/// <item><b>Idle expiry:</b> a record not taken within <see cref="IdleExpiry"/> (default 60 s, matching v1 browse
|
||||
@@ -52,21 +54,21 @@ internal sealed class PeerHistoryContinuationStore
|
||||
private readonly TimeProvider _clock;
|
||||
|
||||
/// <summary>Creates the store of one (principal, connection incarnation).</summary>
|
||||
/// <param name="principal">The authenticated principal.</param>
|
||||
/// <param name="servingIncarnation">This server's serving incarnation at connection time.</param>
|
||||
/// <param name="principal">The authenticated principal that owns the connection.</param>
|
||||
/// <param name="connectionIncarnation">The connection's own incarnation (identity only; never a binding).</param>
|
||||
/// <param name="capacity">The export limit on concurrent continuations (≥ 1).</param>
|
||||
/// <param name="idleExpiry">Idle expiry; null = <see cref="DefaultIdleExpiry"/>. Must be positive.</param>
|
||||
/// <param name="clock">Clock (tests inject a fake); null = system.</param>
|
||||
public PeerHistoryContinuationStore(
|
||||
string principal, string servingIncarnation, int capacity, TimeSpan? idleExpiry = null, TimeProvider? clock = null)
|
||||
string principal, string connectionIncarnation, int capacity, TimeSpan? idleExpiry = null, TimeProvider? clock = null)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(principal);
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(servingIncarnation);
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(connectionIncarnation);
|
||||
ArgumentOutOfRangeException.ThrowIfLessThan(capacity, 1);
|
||||
var idle = idleExpiry ?? DefaultIdleExpiry;
|
||||
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(idle, TimeSpan.Zero, nameof(idleExpiry));
|
||||
Principal = principal;
|
||||
ServingIncarnation = servingIncarnation;
|
||||
ConnectionIncarnation = connectionIncarnation;
|
||||
Capacity = capacity;
|
||||
IdleExpiry = idle;
|
||||
_clock = clock ?? TimeProvider.System;
|
||||
@@ -75,8 +77,8 @@ internal sealed class PeerHistoryContinuationStore
|
||||
/// <summary>The principal this instance serves.</summary>
|
||||
public string Principal { get; }
|
||||
|
||||
/// <summary>The connection's serving incarnation.</summary>
|
||||
public string ServingIncarnation { get; }
|
||||
/// <summary>The connection's incarnation (which connection this store belongs to).</summary>
|
||||
public string ConnectionIncarnation { get; }
|
||||
|
||||
/// <summary>The most live records.</summary>
|
||||
public int Capacity { get; }
|
||||
@@ -98,23 +100,30 @@ internal sealed class PeerHistoryContinuationStore
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stores <paramref name="state"/> under a fresh token, bound to this instance's principal and incarnation, the
|
||||
/// state's own epoch and view (<see cref="HistoryContinuationState.BoundRealm"/> /
|
||||
/// <see cref="HistoryContinuationState.BoundIdentifier"/>, set by the port) and <paramref name="detailsHash"/>.
|
||||
/// Stores <paramref name="state"/> under a fresh token, bound to the calling <paramref name="principal"/>, the
|
||||
/// <paramref name="servingIncarnation"/> current now, the state's own epoch and view
|
||||
/// (<see cref="HistoryContinuationState.BoundRealm"/> / <see cref="HistoryContinuationState.BoundIdentifier"/>, set by
|
||||
/// the port) and <paramref name="detailsHash"/>.
|
||||
/// </summary>
|
||||
/// <param name="state">The bound resume state.</param>
|
||||
/// <param name="principal">The caller's authenticated principal; must be this store's owner.</param>
|
||||
/// <param name="servingIncarnation">The serving incarnation current at save.</param>
|
||||
/// <param name="detailsHash">The request's details hash.</param>
|
||||
/// <returns>The 16-byte token.</returns>
|
||||
public byte[] Save(HistoryContinuationState state, string detailsHash)
|
||||
/// <exception cref="ArgumentException">An unbound state, or a principal other than the owner.</exception>
|
||||
public byte[] Save(HistoryContinuationState state, string principal, string servingIncarnation, string detailsHash)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(state);
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(servingIncarnation);
|
||||
ArgumentNullException.ThrowIfNull(detailsHash);
|
||||
if (state.BoundIdentifier is null)
|
||||
throw new ArgumentException("A peer continuation must be bound to its view.", nameof(state));
|
||||
if (!string.Equals(principal, Principal, StringComparison.Ordinal))
|
||||
throw new ArgumentException("A peer continuation store holds only its owning principal's cursors.", nameof(principal));
|
||||
|
||||
var token = RandomNumberGenerator.GetBytes(TokenLength);
|
||||
var key = new PeerHistoryContinuationKey(
|
||||
Principal, ServingIncarnation, state.AddressSpaceEpoch, state.BoundRealm, state.BoundIdentifier, detailsHash);
|
||||
principal, servingIncarnation, state.AddressSpaceEpoch, state.BoundRealm, state.BoundIdentifier, detailsHash);
|
||||
lock (_gate)
|
||||
{
|
||||
var now = _clock.GetTimestamp();
|
||||
@@ -187,19 +196,24 @@ internal sealed class PeerHistoryContinuationStore
|
||||
|
||||
/// <summary>
|
||||
/// The port's continuation scope for one remote peer: saves and takes through that peer's
|
||||
/// <see cref="PeerHistoryContinuationStore"/>, binding the request's details hash and this instance's principal and
|
||||
/// incarnation on top of the view and epoch the port itself re-checks.
|
||||
/// <see cref="PeerHistoryContinuationStore"/>, binding the caller's principal, the serving incarnation CURRENT at the
|
||||
/// save and at the take (read live from <paramref name="currentServingIncarnation"/>, so an in-process failover or role
|
||||
/// change on a still-open connection invalidates its tokens), and the request's details hash, on top of the view and
|
||||
/// epoch the port itself re-checks.
|
||||
/// </summary>
|
||||
/// <param name="store">The (principal, incarnation) store.</param>
|
||||
internal sealed class PeerHistoryContinuationScope(PeerHistoryContinuationStore store) : IHistoryContinuationScope
|
||||
/// <param name="store">The (principal, connection) store.</param>
|
||||
/// <param name="principal">The caller's authenticated principal.</param>
|
||||
/// <param name="currentServingIncarnation">Reads this server's serving incarnation now.</param>
|
||||
internal sealed class PeerHistoryContinuationScope(
|
||||
PeerHistoryContinuationStore store, string principal, Func<string> currentServingIncarnation) : IHistoryContinuationScope
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public byte[]? Save(HistoryContinuationState state, HistoryContinuationBinding binding) =>
|
||||
store.Save(state, binding.Details?.ComputeDetailsHash() ?? string.Empty);
|
||||
store.Save(state, principal, currentServingIncarnation(), binding.Details?.ComputeDetailsHash() ?? string.Empty);
|
||||
|
||||
/// <inheritdoc />
|
||||
public HistoryContinuationState? TryTake(byte[] continuationPoint, HistoryContinuationBinding binding) =>
|
||||
store.TryTake(continuationPoint, new PeerHistoryContinuationKey(
|
||||
store.Principal, store.ServingIncarnation, binding.Epoch, binding.Realm, binding.Identifier,
|
||||
principal, currentServingIncarnation(), binding.Epoch, binding.Realm, binding.Identifier,
|
||||
binding.Details?.ComputeDetailsHash() ?? string.Empty));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user