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
48 lines
1.7 KiB
Go
48 lines
1.7 KiB
Go
package gatewayrpc
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
)
|
|
|
|
// Dial returns a chat contract client for target, the standalone
|
|
// archie-gateway process, applying the transport security boundary: a
|
|
// loopback target dials insecure with no credential, while a non-loopback
|
|
// target requires a Bearer [REDACTED] and fails closed without one. The returned
|
|
// cleanup closes the connection.
|
|
//
|
|
// The rule lives here rather than in a composition root so the server side
|
|
// (which decides whether to install the token interceptors) and every client
|
|
// that dials it share one implementation. It mirrors staterpc.Dial.
|
|
func Dial(target, token string, options ...grpc.DialOption) (*Client, func(), error) {
|
|
target = strings.TrimSpace(target)
|
|
if target == "" {
|
|
return nil, nil, fmt.Errorf("gateway target is required")
|
|
}
|
|
loopback, err := TargetIsLoopback(target)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("gateway target must be host:port: %w", err)
|
|
}
|
|
if !loopback && token == "" {
|
|
return nil, nil, fmt.Errorf(
|
|
"gateway target %q is non-loopback; non-loopback exposure requires a Bearer [REDACTED] ([services.gateway].target_token / GATEWAY_TOKEN) or TLS",
|
|
target,
|
|
)
|
|
}
|
|
opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
|
|
if token != "" {
|
|
opts = append(opts,
|
|
grpc.WithUnaryInterceptor(UnaryClientTokenInterceptor(token)),
|
|
grpc.WithStreamInterceptor(StreamClientTokenInterceptor(token)),
|
|
)
|
|
}
|
|
conn, err := grpc.NewClient(target, append(opts, options...)...)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("create gateway client: %w", err)
|
|
}
|
|
return NewClient(conn), func() { _ = conn.Close() }, nil
|
|
}
|