Files
archie/internal/app/archied/chat_service.go
T
sam 4c9c7cac55 feat(gateway): authenticate the gRPC contract and allow off-loopback listeners
The Gateway had no transport boundary: RunGateway refused any non-loopback
listen address and serveGateway registered no interceptor, so archie-ui was
colocation-only while the State Store already had a token-authenticated
equivalent.

gatewayrpc gains a gateway-token metadata credential with constant-time
comparison and both unary and stream interceptors on each side. Stream
coverage is required here and not in staterpc: the Gateway's Stream RPC is
server-streaming. Dial fails closed on a non-loopback target with no token,
mirroring staterpc.Dial. gatewayServerOpts applies the same rule server-side:
loopback stays insecure, non-loopback requires a token, malformed errors.

The daemon resolves [services.gateway].target_token then GATEWAY_TOKEN;
archie-ui passes the token its config projection already carried, so a
distributed dashboard is now possible.

Closes: archie-core-juqx
2026-09-09 00:27:03 +10:00

46 lines
1.8 KiB
Go

package archied
import (
"fmt"
"strings"
"google.golang.org/grpc"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/gateway"
"github.com/samcharles93/archie-core/internal/infrastructure/gatewayrpc"
"github.com/samcharles93/archie-core/internal/secret"
)
// gatewayResolvedToken returns the Bearer [REDACTED] a Gateway client
// presents: the explicit [services.gateway].target_token key, then the
// GATEWAY_TOKEN secret/env. The server side (RunGateway) resolves the same
// value, so a distributed consumer and the gateway agree without sharing
// any other configuration.
func gatewayResolvedToken(settings config.ServiceConnection, secrets *secret.Registry) string {
token := settings.TargetToken
if token == "" {
token = secrets.Getenv("GATEWAY_TOKEN")
}
return token
}
// composeChatContract dials the standalone archie-gateway, the sole owner of
// ChatContract, from the daemon's own configuration key. Token resolution is
// the daemon's (it owns the config keys and the secret registry); the dial
// itself and the fail-closed non-loopback rule belong to the transport, so
// they come from gatewayrpc.Dial.
func composeChatContract(settings config.ServiceConnection, secrets *secret.Registry, options ...grpc.DialOption) (gateway.ChatContract, func(), error) {
if strings.TrimSpace(settings.Target) == "" {
return nil, nil, fmt.Errorf("services.gateway.target is required")
}
// Assigned through a typed variable rather than returned directly: a
// direct forward would hand a failed dial's nil *Client back as a
// non-nil ChatContract interface.
client, cleanup, err := gatewayrpc.Dial(settings.Target, gatewayResolvedToken(settings, secrets), options...)
if err != nil {
return nil, nil, err
}
return client, cleanup, nil
}