caelum0x a515e13dab feat(ledger-core): enforce tenant-scoped authorization on ledger-core's own gRPC surface
docs/THREAT-MODEL.md names this "the biggest one" sub-gap of item 1:
cli/tui/agent (and gateway's own forwarding, card-issuance,
webhook-ingestion, tenant-registry) all dial ledger-core directly,
bypassing gateway's internal/tenantauth entirely, so any operator or
compromised caller could act as any tenant_id against the system's
actual source of truth regardless of what gateway enforces upstream.

Add services/ledger-core/src/tenantauth.rs (authorize_tenant /
authorize_operator, checking x-api-key / authorization: Bearer headers
against a new src/authclient.rs that dials Auth's VerifyAPIKey/
VerifyJWT directly) and call it from every LedgerCore RPC handler.
RATION_LEDGER_REQUIRE_TENANT_AUTH defaults to true; ledger-core refuses
to start with it on but no AUTH_GRPC_ADDR configured.

Every direct ledger-core caller now mints and presents its own
operator-scoped credential: cli/tui/agent (reusing their existing
gateway credential), and new internal/authclient packages for gateway,
card-issuance, webhook-ingestion and tenant-registry.

Verified end-to-end against the live demo stack (seed, simulate-deposit,
status, and the agent's poll/bridge-funding loop all succeed with the
new enforcement on; zero Unauthenticated/PermissionDenied anywhere in
any service's logs).
2026-10-23 18:33:33 +03:00

36 lines
1.5 KiB
Go

package authclient
import (
"context"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
// bearerHeader must match services/gateway/internal/tenantauth's
// authorizationHeader constant (and services/ledger-core/src/
// tenantauth.rs's AUTHORIZATION_HEADER) exactly — all three sides of
// the same credential exchange are defined independently but must
// agree on the wire.
const bearerHeader = "authorization"
// UnaryClientInterceptor attaches "authorization: Bearer <token>" to
// every outgoing unary RPC's context — see MintOperatorJWT's doc
// comment for why gateway needs this on its own outgoing ledger-core
// calls, not just incoming ones.
func UnaryClientInterceptor(token string) grpc.UnaryClientInterceptor {
return func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
ctx = metadata.AppendToOutgoingContext(ctx, bearerHeader, "Bearer "+token)
return invoker(ctx, method, req, reply, cc, opts...)
}
}
// StreamClientInterceptor is UnaryClientInterceptor's counterpart for
// streaming RPCs.
func StreamClientInterceptor(token string) grpc.StreamClientInterceptor {
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
ctx = metadata.AppendToOutgoingContext(ctx, bearerHeader, "Bearer "+token)
return streamer(ctx, desc, cc, method, opts...)
}
}