Files
archie/internal/infrastructure/kitrun/env.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

77 lines
2.4 KiB
Go

package kitrun
import (
"fmt"
"net"
"net/url"
"strconv"
"strings"
"github.com/samcharles93/archie-core/internal/infrastructure/egress"
)
// Endpoints are the host-reachable addresses a Kit task's worker needs,
// with the task's own credentials for them.
type Endpoints struct {
NATS string
NATSToken string
StateStore string
StateStoreToken string
StateStoreCA []byte
}
// WorkerEnv is the worker's entire environment in a Kit container: NATS and
// the State Store at the relay's fixed ports, and the task's credentials.
// Nothing else crosses, so no model provider key enters the container.
func WorkerEnv(e Endpoints, uid, gid int) ([]string, error) {
u, err := url.Parse(e.NATS)
if err != nil || u.Scheme == "" {
return nil, fmt.Errorf("nats url %q: want scheme://host:port", e.NATS)
}
env := []string{"NATS_URL=" + u.Scheme + "://" + relayAddr(egress.RelayNATSPort)}
if e.NATSToken != "" {
env = append(env, "NATS_TOKEN="+e.NATSToken)
}
if e.StateStore != "" {
env = append(env, "STATE_STORE_URL="+relayAddr(egress.RelayStateStorePort))
if e.StateStoreToken != "" {
env = append(env, "STATE_STORE_TOKEN="+e.StateStoreToken)
}
if len(e.StateStoreCA) > 0 {
env = append(env, "STATE_STORE_CA="+string(e.StateStoreCA))
}
}
return append(env, "WORKTREE_UID="+strconv.Itoa(uid), "WORKTREE_GID="+strconv.Itoa(gid)), nil
}
// relayTargets are the host:port addresses the relay forwards NATS and the
// State Store to.
func relayTargets(e Endpoints) (nats, stateStore string, err error) {
if strings.Contains(e.NATS, ",") {
return "", "", fmt.Errorf("nats url %q lists several servers; the relay forwards one", e.NATS)
}
u, err := url.Parse(e.NATS)
if err != nil || u.Host == "" {
return "", "", fmt.Errorf("nats url %q: want scheme://host:port", e.NATS)
}
stateStore = e.StateStore
if _, rest, ok := strings.Cut(stateStore, ":///"); ok {
stateStore = rest
}
return u.Host, stateStore, nil
}
func relayAddr(port int) string {
return net.JoinHostPort(egress.RelayAlias, strconv.Itoa(port))
}
// RelayCommand is the relay's archie-agent arguments, forwarding its fixed
// ports to the proxy and to the daemon's NATS and State Store.
func RelayCommand(proxy string, e Endpoints) ([]string, error) {
nats, stateStore, err := relayTargets(e)
if err != nil {
return nil, err
}
return egress.RelayArgs(proxy, nats, stateStore), nil
}