* feat(state-store): generalize storerpc as the State Store gRPC contract Implements archie-core-8cda.4.2 against docs/prds/state-store-contract.md rev. 2c: generate protobuf/gRPC client and server adapters for every ratified store contract (proto/state/v1/state.proto, 40 RPCs, one StateStoreService), add internal/infrastructure/staterpc with a bufconn conformance suite exercising both local and remote adapters, serve the service in-process (bridge address + mandatory per-task bearer token when agent containers exist, loopback-only otherwise), and flip archie-agent's workflow.Store to a long-lived gRPC client when the daemon injects STATE_STORE_URL/STATE_STORE_TOKEN (falling back to the legacy NATS storerpc path otherwise -- one agent process is never both). Also drops the *sql.Tx parameter from store.BindingDispatcher.RecordDispatch (a required wire-blocker fix: a transaction cannot cross a gRPC boundary, and production always passed nil). fix(store): drop RecordDispatch's unusable *sql.Tx parameter feat(config): add services.state contract-adapter selector * fix(state-store): apply no-mistakes review decisions - Drop StreamTokenInterceptor (unused; the State Store service has no streaming RPCs and nothing calls it). - Make StateStoreTokens.Validate compare tokens in constant time (crypto/subtle) per the auto-fix hardening.ing finding. - Amend config.example.toml / config.Services.State doc text so it frames [services.state].target as the ratified surface consumed by the upcoming standalone archie-state-store binary (.4.3), not active today.
61 lines
2.4 KiB
Go
61 lines
2.4 KiB
Go
package staterpc
|
|
|
|
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.
|
|
// Metadata, never a URL, per docs/prds/state-store-contract.md §9's token
|
|
// 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
|
|
// refreshed mid-connection. Used by the agent's long-lived client
|
|
// connection to the State Store's bridge-address listener.
|
|
func UnaryClientTokenInterceptor(token string) grpc.UnaryClientInterceptor {
|
|
return func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
|
|
return invoker(metadata.AppendToOutgoingContext(ctx, tokenMetadataKey, token), method, req, reply, cc, opts...)
|
|
}
|
|
}
|