The task session carries containers.egress_hosts and the profile's remote MCP hosts, and the container gets ProxyEnv with the relay exempt from the proxy so the State Store and NATS dials bypass it. A host the session lists exactly may resolve to a private address; wildcard and any-host matches may not. egress_hosts moves from openshell to containers, as it applies to both runtimes.
201 lines
7.5 KiB
Go
201 lines
7.5 KiB
Go
package egress
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/moby/moby/api/types/container"
|
|
"github.com/moby/moby/api/types/network"
|
|
"github.com/moby/moby/client"
|
|
)
|
|
|
|
const (
|
|
// RelayAlias is the name a sandbox container reaches its relay by.
|
|
RelayAlias = "archie-egress"
|
|
// RelayPort is the port the relay forwards to the egress proxy.
|
|
RelayPort = 3128
|
|
// RelayNATSPort and RelayStateStorePort are the ports the relay forwards
|
|
// to NATS and the State Store, for the worker in a sandbox container.
|
|
RelayNATSPort = 4222
|
|
RelayStateStorePort = 50051
|
|
|
|
relayLabel = "archie-egress-relay"
|
|
sandboxLabel = "archie-egress-sandbox"
|
|
)
|
|
|
|
// RelaySpec is how the relay container is started. Network is the
|
|
// host-reachable network it joins to reach the proxy, NATS and the State
|
|
// Store; Entrypoint and Cmd run archie-agent relay forwarding to them.
|
|
type RelaySpec struct {
|
|
Image string
|
|
Entrypoint []string
|
|
Cmd []string
|
|
Network string
|
|
// Binds are host paths in Docker's "src:dst[:opts]" form. The relay
|
|
// needs none when its image carries archie-agent.
|
|
Binds []string
|
|
}
|
|
|
|
// Networks provisions sandbox networks: internal bridges on which the host
|
|
// holds no address, so the only reachable endpoint is the relay. Every
|
|
// other host service, including ones listening on all interfaces, is out of
|
|
// reach.
|
|
type Networks struct {
|
|
cli *client.Client
|
|
// relayName is the relay container's name. It also scopes the labels
|
|
// Sweep prunes by, so two instances on one host never remove each
|
|
// other's networks.
|
|
relayName string
|
|
}
|
|
|
|
// NewNetworks is the sandbox provisioner for the relay named relayName. The
|
|
// relay itself is started by Start, once the addresses it forwards to exist.
|
|
func NewNetworks(cli *client.Client, relayName string) *Networks {
|
|
return &Networks{cli: cli, relayName: relayName}
|
|
}
|
|
|
|
// Sweep removes the relay and every sandbox network a previous daemon left
|
|
// behind. Work interrupted by a restart is requeued, so no live sandbox
|
|
// depends on them. It removes what it can and reports the rest.
|
|
func (n *Networks) Sweep(ctx context.Context) error {
|
|
errs := []error{n.removeRelay(ctx)}
|
|
list, err := n.cli.NetworkList(ctx, client.NetworkListOptions{Filters: client.Filters{}.Add("label", sandboxLabel+"="+n.relayName)})
|
|
if err != nil {
|
|
return errors.Join(append(errs, fmt.Errorf("list sandbox networks: %w", err))...)
|
|
}
|
|
for _, stale := range list.Items {
|
|
errs = append(errs, n.Remove(ctx, stale.ID))
|
|
}
|
|
return errors.Join(errs...)
|
|
}
|
|
|
|
// Start starts a fresh relay, replacing any left running.
|
|
func (n *Networks) Start(ctx context.Context, relay RelaySpec) error {
|
|
if err := n.removeRelay(ctx); err != nil {
|
|
return err
|
|
}
|
|
created, err := n.cli.ContainerCreate(ctx, client.ContainerCreateOptions{
|
|
Name: n.relayName,
|
|
Config: &container.Config{
|
|
Image: relay.Image,
|
|
Entrypoint: relay.Entrypoint,
|
|
Cmd: relay.Cmd,
|
|
Labels: map[string]string{relayLabel: n.relayName},
|
|
},
|
|
HostConfig: &container.HostConfig{
|
|
NetworkMode: container.NetworkMode(relay.Network),
|
|
Binds: relay.Binds,
|
|
RestartPolicy: container.RestartPolicy{Name: container.RestartPolicyUnlessStopped},
|
|
},
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("create egress relay: %w", err)
|
|
}
|
|
if _, err := n.cli.ContainerStart(ctx, created.ID, client.ContainerStartOptions{}); err != nil {
|
|
return errors.Join(fmt.Errorf("start egress relay: %w", err), n.removeRelay(context.WithoutCancel(ctx)))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Close removes the relay. Sandbox networks are removed by their owners.
|
|
func (n *Networks) Close(ctx context.Context) error { return n.removeRelay(ctx) }
|
|
|
|
func (n *Networks) removeRelay(ctx context.Context) error {
|
|
list, err := n.cli.ContainerList(ctx, client.ContainerListOptions{
|
|
All: true,
|
|
Filters: client.Filters{}.Add("label", relayLabel+"="+n.relayName),
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("list egress relays: %w", err)
|
|
}
|
|
for _, c := range list.Items {
|
|
if _, err := n.cli.ContainerRemove(ctx, c.ID, client.ContainerRemoveOptions{Force: true}); err != nil {
|
|
return fmt.Errorf("remove egress relay: %w", err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Create makes one sandbox's network and attaches the relay to it.
|
|
func (n *Networks) Create(ctx context.Context, name string) error {
|
|
if _, err := n.cli.NetworkCreate(ctx, name, client.NetworkCreateOptions{
|
|
Driver: "bridge",
|
|
Internal: true,
|
|
// With no host address on the bridge, nothing on the host is
|
|
// routable from the sandbox; internal alone still exposes every
|
|
// host service through the gateway.
|
|
Options: map[string]string{"com.docker.network.bridge.inhibit_ipv4": "true"},
|
|
Labels: map[string]string{sandboxLabel: n.relayName},
|
|
}); err != nil {
|
|
return fmt.Errorf("create sandbox network %s: %w", name, err)
|
|
}
|
|
if _, err := n.cli.NetworkConnect(ctx, name, client.NetworkConnectOptions{
|
|
Container: n.relayName,
|
|
EndpointConfig: &network.EndpointSettings{Aliases: []string{RelayAlias}},
|
|
}); err != nil {
|
|
return errors.Join(fmt.Errorf("attach egress relay to %s: %w", name, err), n.Remove(context.WithoutCancel(ctx), name))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Remove deletes a sandbox network. Every endpoint still on it is detached
|
|
// first: the task container may not be gone yet when the max-uptime reaper
|
|
// and Release race, and an attached endpoint would otherwise block removal
|
|
// and leak the network's subnet.
|
|
func (n *Networks) Remove(ctx context.Context, name string) error {
|
|
inspected, err := n.cli.NetworkInspect(ctx, name, client.NetworkInspectOptions{})
|
|
if err != nil {
|
|
return fmt.Errorf("inspect sandbox network %s: %w", name, err)
|
|
}
|
|
var errs []error
|
|
for id := range inspected.Network.Containers {
|
|
if _, err := n.cli.NetworkDisconnect(ctx, name, client.NetworkDisconnectOptions{Container: id, Force: true}); err != nil {
|
|
errs = append(errs, fmt.Errorf("detach %s from sandbox network %s: %w", id, name, err))
|
|
}
|
|
}
|
|
if _, err := n.cli.NetworkRemove(ctx, name, client.NetworkRemoveOptions{}); err != nil {
|
|
return errors.Join(append(errs, fmt.Errorf("remove sandbox network %s: %w", name, err))...)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ProxyEnv is the environment a sandbox container needs to send its egress
|
|
// through the relay under session token and to trust the daemon CA mounted
|
|
// at caPath. Both spellings are set because tools disagree on case. The relay
|
|
// itself is exempt, so the State Store dial (grpc-go honours HTTPS_PROXY) and
|
|
// NATS reach their relay ports directly.
|
|
func ProxyEnv(token, caPath string) []string {
|
|
proxy := (&url.URL{
|
|
Scheme: "http",
|
|
User: url.UserPassword(proxyUser, token),
|
|
Host: net.JoinHostPort(RelayAlias, strconv.Itoa(RelayPort)),
|
|
}).String()
|
|
noProxy := RelayAlias + ",localhost,127.0.0.1"
|
|
env := []string{"NO_PROXY=" + noProxy, "no_proxy=" + noProxy}
|
|
for _, name := range []string{"HTTPS_PROXY", "HTTP_PROXY", "ALL_PROXY"} {
|
|
env = append(env, name+"="+proxy, strings.ToLower(name)+"="+proxy)
|
|
}
|
|
for _, name := range []string{"SSL_CERT_FILE", "NODE_EXTRA_CA_CERTS", "REQUESTS_CA_BUNDLE", "CURL_CA_BUNDLE"} {
|
|
env = append(env, name+"="+caPath)
|
|
}
|
|
return env
|
|
}
|
|
|
|
// RelayArgs is archie-agent relay's arguments forwarding the relay's fixed
|
|
// ports to the proxy, NATS and the State Store at their host-reachable
|
|
// addresses. An empty address is not forwarded.
|
|
func RelayArgs(proxy, nats, stateStore string) []string {
|
|
args := []string{"relay"}
|
|
for port, target := range map[int]string{RelayPort: proxy, RelayNATSPort: nats, RelayStateStorePort: stateStore} {
|
|
if target != "" {
|
|
args = append(args, "-forward", ":"+strconv.Itoa(port)+"="+target)
|
|
}
|
|
}
|
|
return args
|
|
}
|