Files
archie/internal/infrastructure/gatewayrpc/dial.go
T
sam cf3455ef2e feat(rpc): mTLS between services from operator-issued certificates
Replace the service bearer tokens with [tls] ca_file and cert_dir. Caller
identity comes from the verified certificate, not metadata. Without TLS,
services listen and dial on loopback only. Task containers get only the CA
and keep their grant.
2026-10-16 02:37:51 +11:00

35 lines
1007 B
Go

package gatewayrpc
import (
"fmt"
"strings"
"google.golang.org/grpc"
"github.com/samcharles93/archie-core/internal/domain/presence"
"github.com/samcharles93/archie-core/internal/infrastructure/rpctls"
)
// Dial returns a Gateway chat client for target, authenticated by id. The
// cleanup closes the connection.
func Dial(target string, id *rpctls.Identity, options ...grpc.DialOption) (*Client, func(), error) {
target = strings.TrimSpace(target)
if target == "" {
return nil, nil, fmt.Errorf("gateway target is required")
}
creds, err := id.DialOption(target, presence.Gateway)
if err != nil {
return nil, nil, fmt.Errorf("gateway: %w", err)
}
opts := []grpc.DialOption{
creds,
grpc.WithUnaryInterceptor(unaryPrincipal),
grpc.WithStreamInterceptor(streamPrincipal),
}
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
}