152 lines
4.3 KiB
Go
152 lines
4.3 KiB
Go
package host
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"errors"
|
|
"fmt"
|
|
"maps"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
)
|
|
|
|
// RemoteTLS holds the mutual-TLS material used to authenticate remote extension
|
|
// gRPC endpoints.
|
|
type RemoteTLS struct {
|
|
CAFile string `toml:"ca_file"`
|
|
CertFile string `toml:"cert_file"`
|
|
KeyFile string `toml:"key_file"`
|
|
}
|
|
|
|
// RemoteEndpoint names a pre-deployed gRPC extension service (sidecar socket
|
|
// or mTLS pod address).
|
|
type RemoteEndpoint struct {
|
|
Address string `toml:"address"`
|
|
ServerName string `toml:"server_name"`
|
|
}
|
|
|
|
// RemoteConfig controls remote gRPC extension routing.
|
|
type RemoteConfig struct {
|
|
RequireRemote bool
|
|
TLS RemoteTLS
|
|
Endpoints map[string]RemoteEndpoint
|
|
}
|
|
|
|
func validateRemoteConfig(cfg RemoteConfig) (*tls.Config, map[string]RemoteEndpoint, error) {
|
|
endpoints := make(map[string]RemoteEndpoint, len(cfg.Endpoints))
|
|
needTLS := false
|
|
for k, ep := range cfg.Endpoints {
|
|
key := strings.TrimSpace(k)
|
|
addr := strings.TrimSpace(ep.Address)
|
|
if key == "" || addr == "" {
|
|
return nil, nil, fmt.Errorf("remote endpoint %q: address is required", k)
|
|
}
|
|
if strings.HasPrefix(addr, "unix://") {
|
|
if err := checkUnixAddress(addr); err != nil {
|
|
return nil, nil, fmt.Errorf("remote endpoint %q: %w", key, err)
|
|
}
|
|
} else {
|
|
if _, _, err := net.SplitHostPort(addr); err != nil {
|
|
return nil, nil, fmt.Errorf("remote endpoint %q: address must be host:port or unix://path: %w", key, err)
|
|
}
|
|
needTLS = true
|
|
}
|
|
endpoints[key] = RemoteEndpoint{Address: addr, ServerName: strings.TrimSpace(ep.ServerName)}
|
|
}
|
|
var tlsCfg *tls.Config
|
|
hasAnyTLS := cfg.TLS.CAFile != "" || cfg.TLS.CertFile != "" || cfg.TLS.KeyFile != ""
|
|
if needTLS || hasAnyTLS {
|
|
var err error
|
|
tlsCfg, err = loadRemoteTLS(cfg.TLS)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
}
|
|
return tlsCfg, endpoints, nil
|
|
}
|
|
|
|
func checkUnixAddress(addr string) error {
|
|
sock := strings.TrimPrefix(addr, "unix://")
|
|
if !strings.HasPrefix(sock, "/") {
|
|
return errors.New("unix socket path must be absolute")
|
|
}
|
|
clean := filepath.Clean(sock)
|
|
if clean != sock {
|
|
return fmt.Errorf("unix socket path %q must be clean", sock)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func loadRemoteTLS(cfg RemoteTLS) (*tls.Config, error) {
|
|
if cfg.CAFile == "" || cfg.CertFile == "" || cfg.KeyFile == "" {
|
|
return nil, errors.New("remote mTLS requires ca_file, cert_file, and key_file")
|
|
}
|
|
caPEM, err := os.ReadFile(cfg.CAFile) //nolint:gosec // operator-configured CA path
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read remote CA %s: %w", cfg.CAFile, err)
|
|
}
|
|
pool := x509.NewCertPool()
|
|
if !pool.AppendCertsFromPEM(caPEM) {
|
|
return nil, fmt.Errorf("parse remote CA %s: no certificates found", cfg.CAFile)
|
|
}
|
|
cert, err := tls.LoadX509KeyPair(cfg.CertFile, cfg.KeyFile)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("load remote client keypair: %w", err)
|
|
}
|
|
return &tls.Config{
|
|
RootCAs: pool,
|
|
Certificates: []tls.Certificate{cert},
|
|
MinVersion: tls.VersionTLS13,
|
|
}, nil
|
|
}
|
|
|
|
func dialRemote(ep RemoteEndpoint, baseTLS *tls.Config) (*grpc.ClientConn, error) {
|
|
if strings.HasPrefix(ep.Address, "unix://") {
|
|
creds := insecure.NewCredentials()
|
|
if baseTLS != nil && ep.ServerName != "" {
|
|
cfg := baseTLS.Clone()
|
|
cfg.ServerName = ep.ServerName
|
|
creds = credentials.NewTLS(cfg)
|
|
}
|
|
return grpc.NewClient("unix://"+strings.TrimPrefix(ep.Address, "unix://"), grpc.WithTransportCredentials(creds))
|
|
}
|
|
if baseTLS == nil {
|
|
return nil, fmt.Errorf("remote endpoint %q requires mTLS credentials", ep.Address)
|
|
}
|
|
cfg := baseTLS.Clone()
|
|
if ep.ServerName != "" {
|
|
cfg.ServerName = ep.ServerName
|
|
} else if host, _, err := net.SplitHostPort(ep.Address); err == nil {
|
|
cfg.ServerName = host
|
|
}
|
|
return grpc.NewClient(ep.Address, grpc.WithTransportCredentials(credentials.NewTLS(cfg)))
|
|
}
|
|
|
|
func (h *Host) lookupRemote(spec Spec) (RemoteEndpoint, bool) {
|
|
h.mu.Lock()
|
|
defer h.mu.Unlock()
|
|
if spec.Key != "" {
|
|
if ep, ok := h.remotes[spec.Key]; ok {
|
|
return ep, true
|
|
}
|
|
}
|
|
if spec.Name != "" {
|
|
if ep, ok := h.remotes[spec.Name]; ok {
|
|
return ep, true
|
|
}
|
|
}
|
|
return RemoteEndpoint{}, false
|
|
}
|
|
|
|
func cloneEndpoints(in map[string]RemoteEndpoint) map[string]RemoteEndpoint {
|
|
out := make(map[string]RemoteEndpoint, len(in))
|
|
maps.Copy(out, in)
|
|
return out
|
|
}
|