From 0da70ca8f41962d797f39d0e851bedf92ac2b7b0 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 15 Sep 2026 10:18:25 +1000 Subject: [PATCH] refactor(staterpc): delete the superseded single-token auth interceptor UnaryTokenInterceptor/TokenValidator/checkToken (auth.go) and constantTimeTokenValidator (state_store.go) had no production caller: server auth on the bridge-address listener is wired through TaskGrants.UnaryInterceptor/StreamInterceptor, which replaced this all-or-nothing token check with per-task scoped grants. Removed the dead cluster and its now-pointless tests; TaskGrants' own auth rejection paths are already covered in task_grants_test.go. --- internal/app/archied/state_store.go | 11 ----- internal/app/archied/state_store_test.go | 13 ------ internal/infrastructure/staterpc/auth.go | 36 --------------- .../staterpc/conformance_test.go | 44 ------------------- 4 files changed, 104 deletions(-) diff --git a/internal/app/archied/state_store.go b/internal/app/archied/state_store.go index e91969a5c..fe4b129f1 100644 --- a/internal/app/archied/state_store.go +++ b/internal/app/archied/state_store.go @@ -11,7 +11,6 @@ package archied import ( "context" - "crypto/subtle" "encoding/json" "fmt" "net" @@ -222,13 +221,3 @@ func stateStoreServerOpts(listen, token string, grants *staterpc.TaskGrants) (op grpc.ChainStreamInterceptor(grants.StreamInterceptor(token)), }, false, nil } - -// constantTimeTokenValidator returns a staterpc.TokenValidator that accepts -// exactly the configured token, compared in constant time so a timing side -// channel cannot leak how many bytes matched (the standalone server's -// equivalent of the earlier in-process interceptor's token check). -func constantTimeTokenValidator(token string) staterpc.TokenValidator { - return func(candidate string) bool { - return subtle.ConstantTimeCompare([]byte(token), []byte(candidate)) == 1 - } -} diff --git a/internal/app/archied/state_store_test.go b/internal/app/archied/state_store_test.go index b743fa333..2d922ed30 100644 --- a/internal/app/archied/state_store_test.go +++ b/internal/app/archied/state_store_test.go @@ -50,19 +50,6 @@ func TestStateStoreServerOptsMalformedListen(t *testing.T) { } } -func TestConstantTimeTokenValidator(t *testing.T) { - validate := constantTimeTokenValidator("correct-token") - if !validate("correct-token") { - t.Fatal("validator should accept the configured token") - } - if validate("wrong-token") { - t.Fatal("validator should reject a different token") - } - if validate("") { - t.Fatal("validator should reject an empty token") - } -} - // TestServeStateStoreServesContract verifies the standalone composition's // serveStateStore + stateStoreDeps wiring actually serves the StateStore gRPC // contract over a real loopback listener: the same service .4.2 serves diff --git a/internal/infrastructure/staterpc/auth.go b/internal/infrastructure/staterpc/auth.go index fc7084713..041b404e0 100644 --- a/internal/infrastructure/staterpc/auth.go +++ b/internal/infrastructure/staterpc/auth.go @@ -4,9 +4,7 @@ import ( "context" "google.golang.org/grpc" - "google.golang.org/grpc/codes" "google.golang.org/grpc/metadata" - "google.golang.org/grpc/status" ) // tokenMetadataKey is the gRPC metadata key the bearer token travels in. @@ -14,40 +12,6 @@ import ( // lifecycle note. const tokenMetadataKey = "state-store-token" -// TokenValidator reports whether token is a currently-issued, unexpired -// bearer token for the State Store's bridge-address listener topology. The -// caller (the daemon) owns the issued-token set; this package only enforces -// the check server-side and attaches the token client-side. -type TokenValidator func(token string) bool - -// UnaryTokenInterceptor validates the bearer token carried in each unary -// call's metadata, returning codes.Unauthenticated on a missing, unknown, or -// expired token. Required on the bridge-address (agent-consumed) listener -// topology per §9; not used on the loopback-only, daemon-only topology. -func UnaryTokenInterceptor(validate TokenValidator) grpc.UnaryServerInterceptor { - return func(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) { - if err := checkToken(ctx, validate); err != nil { - return nil, err - } - return handler(ctx, req) - } -} - -func checkToken(ctx context.Context, validate TokenValidator) error { - md, ok := metadata.FromIncomingContext(ctx) - if !ok { - return status.Error(codes.Unauthenticated, "missing state store token") - } - values := md.Get(tokenMetadataKey) - if len(values) == 0 || values[0] == "" { - return status.Error(codes.Unauthenticated, "missing state store token") - } - if !validate(values[0]) { - return status.Error(codes.Unauthenticated, "invalid state store token") - } - return nil -} - // UnaryClientTokenInterceptor attaches token to every outgoing call's // metadata under tokenMetadataKey. One token covers a task's container // lifetime (§9's per-incumbence lifecycle) so it does not need to be diff --git a/internal/infrastructure/staterpc/conformance_test.go b/internal/infrastructure/staterpc/conformance_test.go index bd5adabb7..4c397b776 100644 --- a/internal/infrastructure/staterpc/conformance_test.go +++ b/internal/infrastructure/staterpc/conformance_test.go @@ -263,50 +263,6 @@ func TestStateStoreConformance(t *testing.T) { } } -// TestTokenInterceptorRejectsMissingOrInvalidToken exercises the gRPC -// interceptor the bridge-address (agent-consumed) listener topology -// requires: missing or unrecognised tokens fail closed with -// codes.Unauthenticated, per §9's token lifecycle. -func TestTokenInterceptorRejectsMissingOrInvalidToken(t *testing.T) { - const validToken = "task-42-token" - validate := func(token string) bool { return token == validToken } - - listener := bufconn.Listen(1 << 20) - local := store.OpenTest(t) - server := grpc.NewServer(grpc.UnaryInterceptor(UnaryTokenInterceptor(validate))) - RegisterServer(server, Deps{Tasks: local}) - go func() { _ = server.Serve(listener) }() - t.Cleanup(func() { server.Stop(); _ = listener.Close() }) - - dial := func(t *testing.T, token string) contract { - t.Helper() - opts := []grpc.DialOption{ - grpc.WithTransportCredentials(insecure.NewCredentials()), - grpc.WithContextDialer(func(ctx context.Context, _ string) (net.Conn, error) { return listener.DialContext(ctx) }), - } - if token != "" { - opts = append(opts, grpc.WithUnaryInterceptor(UnaryClientTokenInterceptor(token))) - } - conn, err := grpc.NewClient("passthrough:///state", opts...) - if err != nil { - t.Fatal(err) - } - t.Cleanup(func() { _ = conn.Close() }) - return NewClient(conn) - } - - ctx := t.Context() - if _, err := dial(t, "").StatusCounts(ctx); err == nil { - t.Fatal("missing token accepted") - } - if _, err := dial(t, "wrong-token").StatusCounts(ctx); err == nil { - t.Fatal("invalid token accepted") - } - if _, err := dial(t, validToken).StatusCounts(ctx); err != nil { - t.Fatalf("valid token rejected: %v", err) - } -} - // TestConfigSnapshotContract drives the published configuration projection // through both the local store and the gRPC client. The document is opaque to // this hop, so the test's whole claim is that it arrives unchanged: the UI