Task containers log in with their run credential on an external broker and reach only their own subjects, decided by the embedded broker's authenticator. The broker token is never put in a container environment.
78 lines
2.4 KiB
Go
78 lines
2.4 KiB
Go
package egress
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// Endpoints are the host-reachable addresses a sandboxed task's worker
|
|
// needs, with the task's own credentials for them.
|
|
type Endpoints struct {
|
|
NATS string
|
|
StateStore string
|
|
StateStoreToken string
|
|
StateStoreCA []byte
|
|
}
|
|
|
|
// WorkerEnv is the worker's network environment in a Docker sandbox: 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) ([]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)
|
|
}
|
|
return DirectWorkerEnv(u.Scheme+"://"+relayAddr(RelayNATSPort), relayAddr(RelayStateStorePort), e), nil
|
|
}
|
|
|
|
// DirectWorkerEnv is the worker's network environment when it reaches NATS
|
|
// at nats and the State Store at stateStore itself, with the task's
|
|
// credentials from e. The State Store is left out when e names none.
|
|
func DirectWorkerEnv(nats, stateStore string, e Endpoints) []string {
|
|
env := []string{"NATS_URL=" + nats}
|
|
if e.StateStore != "" {
|
|
env = append(env, "STATE_STORE_URL="+stateStore)
|
|
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 env
|
|
}
|
|
|
|
// 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(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 RelayArgs(proxy, nats, stateStore), nil
|
|
}
|