feat(secrets): Grpc hub server — SecretsHubService + fail-closed bearer interceptor + DI
The server half of the central-hosted pull-only secrets hub. SecretsHubService adapts the node's own ISecretStore onto secrets_hub.v1 using exactly two read members (GetManifestAsync, GetAsync) — the proto exposes no write RPC, so a follower has no path to put a row into central whatever its credentials allow. GetSecrets omits a name the hub does not hold and skips one that will not parse as a SecretName; neither is an error, because both describe a follower acting on a manifest that was true a moment ago. Failing the call would stall the sweep and let one bad name deny the follower every other row in the batch. Names are re-validated server-side (the wire type is a plain string) and de-duplicated on the normalized value. DomainToWire mirrors the Akka replicator's SecretRowDto discipline exactly: round-trip "O" timestamps so a DateTimeOffset's offset and ticks survive the last-writer-wins equality compare, the content type as its enum MEMBER NAME so an unknown value is rejected rather than decoded to the zero member, and nullable fields left absent rather than written as "" so null and empty stay distinct. SecretsHubAuthInterceptor fails closed: with no token configured every call is denied and one warning names the exact configuration key — never a token value, presented or configured, at any level. Denial is uniform (one Status, one detail, no "why") so a caller gets no oracle. Comparison is constant-time over UTF-8 bytes. All four handler shapes are gated, not just unary, so a future RPC cannot arrive unauthenticated by omission; more than one Authorization header is refused as a smuggling shape. The type keeps EXACTLY ONE public constructor — Grpc's activator silently stops invoking an interceptor it cannot construct unambiguously, and the failure mode is an auth gate that quietly does not run — so a test pins the count. Tests: 50, red-first (compile-red, then a fail-closed mutation verified 5 of them bite). Full Secrets suite 328 passed / 0 failed / 15 live-gated skips, 0 new warnings. Claude-Session: https://claude.ai/code/session_014WNM4vjoVksyyBraTXSZE1
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
using Grpc.Core;
|
||||
using Microsoft.Extensions.Logging;
|
||||
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;
|
||||
|
||||
/// <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="logger">Diagnostics sink.</param>
|
||||
public SecretsHubService(ISecretStore store, ILogger<SecretsHubService> logger)
|
||||
{
|
||||
_store = store ?? throw new ArgumentNullException(nameof(store));
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
|
||||
/// <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>
|
||||
/// 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.
|
||||
/// </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>
|
||||
public override async Task<GetSecretsResponse> GetSecrets(
|
||||
GetSecretsRequest request, ServerCallContext context)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(request);
|
||||
ArgumentNullException.ThrowIfNull(context);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user