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:
+58
@@ -0,0 +1,58 @@
|
||||
namespace ZB.MOM.WW.Secrets.Replicator.Grpc;
|
||||
|
||||
/// <summary>
|
||||
/// Configuration for the central-hosted secrets hub. Bound from a configuration section by
|
||||
/// <c>AddZbSecretsGrpcHub</c>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// The hub serves ciphertext to followers that authenticate with one shared bearer token. That
|
||||
/// token is the whole of the hub's authorization: there is no per-follower identity, because a
|
||||
/// follower's authority is uniform (read the manifest, read rows) and the pull-only contract gives
|
||||
/// it nothing else to be authorized for.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// It cannot itself be supplied as a <c>${secret:}</c> reference — resolving that reference is what
|
||||
/// the hub exists to make possible — so it comes from appsettings or the environment, the same way
|
||||
/// the family's mesh pre-shared keys do.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public sealed class SecretsGrpcHubServerOptions
|
||||
{
|
||||
/// <summary>The configuration section the hub is bound from unless a host overrides it.</summary>
|
||||
internal const string DefaultSectionPath = "Secrets:GrpcHub";
|
||||
|
||||
/// <summary>
|
||||
/// The shared bearer token a follower must present. Required — an unset token is not "open
|
||||
/// access", it is a hub that refuses every call (see <see cref="SecretsHubAuthInterceptor"/>).
|
||||
/// </summary>
|
||||
public string BearerToken { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// The configuration section this instance was bound from, used so failure messages name the
|
||||
/// key an operator actually has to set.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Deliberately <see langword="internal"/>: configuration binding only writes public settable
|
||||
/// properties, so a stray <c>SectionPath</c> key in an appsettings file cannot make the hub
|
||||
/// misreport which key it wants. The registration extension sets it.
|
||||
/// </remarks>
|
||||
internal string SectionPath { get; set; } = DefaultSectionPath;
|
||||
|
||||
/// <summary>
|
||||
/// Throws if the options are unusable. Called by the registration extension so a hub with no
|
||||
/// token fails at startup, rather than starting and rejecting every follower at runtime.
|
||||
/// </summary>
|
||||
/// <exception cref="InvalidOperationException">The bearer token is missing or blank.</exception>
|
||||
public void Validate()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(BearerToken))
|
||||
{
|
||||
// Names the key, never the value — there is no value here, and there never will be one
|
||||
// in an error message from this type.
|
||||
throw new InvalidOperationException(
|
||||
$"The gRPC secrets hub requires a shared bearer token ({SectionPath}:BearerToken). " +
|
||||
"Until it is set the hub refuses every call.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user