Files
scadaproj/ZB.MOM.WW.Secrets/src/ZB.MOM.WW.Secrets.Replicator.Grpc/SecretsHubService.cs
T
Joseph Doherty 353af72265 fix(secrets): Grpc hub server review follow-ups — GetSecrets batch cap, DI single-registration guard + tests
Five review items on the Task 2 server half.

1. GetSecrets now enforces a server-side cap on request.Names, refusing an
   over-cap batch with ResourceExhausted BEFORE any store read runs. The
   well-behaved follower chunks at 500, but client discipline is not a server
   defence — the family precedent (HistorianGateway's max_values) is that the
   server declines regardless. The new SecretsGrpcHubServerOptions.
   MaxNamesPerRequest defaults to 1000, comfortably above that chunk so a
   compliant sweep never meets it, and Validate() refuses a non-positive value
   at startup rather than letting it read at runtime as a broken follower. The
   refusal states the cap and the received count — neither is secret, and an
   operator cannot resize a batch without them — and never a name. The cap sits
   behind the interceptor, so it is defence in depth against a misbehaving
   token holder, not an anonymous-DoS control.

2. AddZbSecretsGrpcHub now refuses a second registration. The two calls do not
   compose: options binding MERGES (a key absent from the second section leaves
   the first section's value standing) while the SectionPath stamp is
   last-one-wins, so a hub registered twice could serve a token bound from one
   section while every failure message named the other. Detected via a private
   marker registration. The extension also gains real DI coverage — the intended
   single-call path asserted for token/SectionPath coherence through a built
   provider, a custom section path stamped end to end, a missing token throwing
   from the registration call itself, and the duplicate call throwing.

3. Client-streaming and duplex-streaming denial tests, so all four gated handler
   shapes are pinned rather than only unary and server-streaming, plus a
   valid-token test that reaches all three streaming continuations.

4. A cancellation test: GetSecrets hands the ServerCallContext token through to
   the store, pinned against a fake that records it.

5. The proto's service comment stated the ISecretReplicationHub mapping in prose
   that no longer matched the RPC name; it now says GetManifestAsync ->
   GetManifest and GetManyAsync -> GetSecrets explicitly. Comment-only — no
   message or rpc definition is touched. The GetSecrets rpc comment also notes
   the new RESOURCE_EXHAUSTED cap, which a client author has to know about.

Grpc server suite 50 -> 73 tests; full solution suite green, 0 new warnings.

Claude-Session: https://claude.ai/code/session_014WNM4vjoVksyyBraTXSZE1
2026-08-07 06:27:16 -04:00

175 lines
7.6 KiB
C#

using Grpc.Core;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using ZB.MOM.WW.Secrets.Abstractions;
using ZB.MOM.WW.Secrets.Replicator.Grpc.V1;
using ZB.MOM.WW.Secrets.Replicator.Grpc.Wire;
namespace ZB.MOM.WW.Secrets.Replicator.Grpc;
/// <summary>
/// The central-hosted secrets hub: adapts the node's own <see cref="ISecretStore"/> onto the
/// <c>secrets_hub.v1</c> contract so site nodes can pull from it.
/// </summary>
/// <remarks>
/// <para>
/// <b>Read-only by construction.</b> It touches exactly two store members —
/// <see cref="ISecretStore.GetManifestAsync"/> and <see cref="ISecretStore.GetAsync"/> — mirroring
/// the read half of <c>ISecretReplicationHub</c>. The proto exposes no write RPC, so there is no
/// path by which a follower can put a row into central, whatever its credentials allow.
/// </para>
/// <para>
/// Everything it returns is ciphertext or metadata about ciphertext. The KEK never travels: a row
/// carries the KEK's identifier and the DEK wrapped by it, and every node in a replicating
/// deployment is configured with the same KEK out of band.
/// </para>
/// </remarks>
public sealed class SecretsHubService : SecretsHub.SecretsHubBase
{
private readonly ISecretStore _store;
private readonly ILogger<SecretsHubService> _logger;
private readonly int _maxNamesPerRequest;
/// <summary>Creates the hub service over the node's secret store.</summary>
/// <param name="store">The store to serve. Only its read members are used.</param>
/// <param name="options">The hub options, read for the per-request name cap.</param>
/// <param name="logger">Diagnostics sink.</param>
public SecretsHubService(
ISecretStore store,
IOptions<SecretsGrpcHubServerOptions> options,
ILogger<SecretsHubService> logger)
{
ArgumentNullException.ThrowIfNull(options);
_store = store ?? throw new ArgumentNullException(nameof(store));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
// The registration extension validates the cap, so a non-positive value here means the
// options were built some other way. Falling back to the default is the safe direction: the
// alternative reading of "0" is a hub with no cap at all, which is the thing the cap exists
// to prevent.
int configured = options.Value.MaxNamesPerRequest;
_maxNamesPerRequest = configured > 0
? configured
: SecretsGrpcHubServerOptions.DefaultMaxNamesPerRequest;
}
/// <summary>
/// Returns the hub's full manifest, <b>tombstones included</b> — a follower converges on a
/// delete by seeing the tombstone, so omitting them would make deletes unpropagatable.
/// </summary>
/// <param name="request">The (empty) manifest request.</param>
/// <param name="context">The call context.</param>
/// <returns>Every manifest entry the store holds.</returns>
public override async Task<GetManifestResponse> GetManifest(
GetManifestRequest request, ServerCallContext context)
{
ArgumentNullException.ThrowIfNull(context);
IReadOnlyList<SecretManifestEntry> entries =
await _store.GetManifestAsync(context.CancellationToken).ConfigureAwait(false);
var response = new GetManifestResponse();
foreach (SecretManifestEntry entry in entries)
{
response.Entries.Add(DomainToWire.ToWire(entry));
}
_logger.LogDebug("Secrets hub served a manifest of {EntryCount} entries.", response.Entries.Count);
return response;
}
/// <summary>
/// Fetches the named encrypted rows in bulk.
/// </summary>
/// <remarks>
/// <para>
/// A requested name the hub does not hold is <b>omitted</b> from the response, and an unparseable
/// name is <b>skipped</b> — neither is an error. Both describe the same ordinary situation: the
/// follower is acting on a manifest that was true a moment ago. Failing the whole call would turn
/// a routine race into a stalled sweep, and would let one bad name deny the follower every other
/// row in the batch.
/// </para>
/// <para>
/// A request carrying more than <see cref="SecretsGrpcHubServerOptions.MaxNamesPerRequest"/>
/// names <b>is</b> refused, before any read runs. That is not the stale-manifest case: it is a
/// caller asking the hub to walk its store an unbounded number of times inside one call, and the
/// server declines independently of whatever chunking the client believes it is doing.
/// </para>
/// </remarks>
/// <param name="request">The names to fetch.</param>
/// <param name="context">The call context.</param>
/// <returns>One row per distinct name the hub holds.</returns>
/// <exception cref="RpcException">The request carries more names than the configured cap.</exception>
public override async Task<GetSecretsResponse> GetSecrets(
GetSecretsRequest request, ServerCallContext context)
{
ArgumentNullException.ThrowIfNull(request);
ArgumentNullException.ThrowIfNull(context);
if (request.Names.Count > _maxNamesPerRequest)
{
// The cap and the count are both safe to state — neither is secret, and an operator
// cannot fix a batch that is too large without knowing by how much. The names are NOT
// stated: they are the one part of this request that identifies secrets.
_logger.LogWarning(
"Secrets hub refused a GetSecrets request of {RequestedCount} name(s); the cap is " +
"{MaxNamesPerRequest}.",
request.Names.Count,
_maxNamesPerRequest);
throw new RpcException(new Status(
StatusCode.ResourceExhausted,
$"GetSecrets accepts at most {_maxNamesPerRequest} names per request; this one " +
$"carried {request.Names.Count}. Split the fetch into smaller batches."));
}
var response = new GetSecretsResponse();
var seen = new HashSet<string>(StringComparer.Ordinal);
int skipped = 0;
foreach (string requested in request.Names)
{
SecretName name;
try
{
// Re-validated server-side: the wire type is a plain string, so this is where a
// path-traversing or otherwise malformed name is stopped — before it reaches a store
// that may treat the name as a path segment.
name = new SecretName(requested);
}
catch (ArgumentException)
{
skipped++;
continue;
}
// Normalization happens in the SecretName constructor, so de-duplication has to key off
// the normalized value — otherwise "Db/Central" and "db/central" would each cost a read
// and each return the same row.
if (!seen.Add(name.Value))
{
continue;
}
StoredSecret? row = await _store.GetAsync(name, context.CancellationToken).ConfigureAwait(false);
if (row is null)
{
continue;
}
response.Secrets.Add(DomainToWire.ToWire(row));
}
if (skipped > 0)
{
// The count, never the values: the names are caller-supplied text and this is a log.
_logger.LogWarning(
"Secrets hub skipped {SkippedCount} unparseable name(s) in a GetSecrets request.", skipped);
}
_logger.LogDebug("Secrets hub served {RowCount} row(s).", response.Secrets.Count);
return response;
}
}