diff --git a/internal/app/archied/bootstrap.go b/internal/app/archied/bootstrap.go index caebeb8b4..2b66ca1e0 100644 --- a/internal/app/archied/bootstrap.go +++ b/internal/app/archied/bootstrap.go @@ -11,6 +11,7 @@ import ( "errors" "fmt" "log/slog" + "net" "net/url" "os" "os/signal" @@ -473,14 +474,38 @@ func (b *boot) startCallout(ctx context.Context, url, token string) error { return nil } +// natsHost is the address the embedded broker binds. OpenShell sandboxes run +// on the host network and reach the broker by the services host name, so it +// binds the address that name resolves to; Docker containers reach it on the +// bridge gateway. +func natsHost(ctx context.Context, cfg config.Config, pool *container.Pool) (string, error) { + shell := cfg.Containers.OpenShell + if cfg.Containers.Runtime == config.RuntimeOpenShell && shell.ServicesHost != "" { + addrs, err := net.DefaultResolver.LookupHost(ctx, shell.ServicesHost) + if err != nil || len(addrs) == 0 { + return "", fmt.Errorf("embedded nats: resolve services host %s: %w", shell.ServicesHost, err) + } + for _, a := range addrs { + if ip := net.ParseIP(a); ip != nil && ip.To4() != nil { + return a, nil + } + } + return addrs[0], nil + } + if pool != nil { + return pool.HostGateway(), nil + } + return "", nil +} + func (b *boot) startEmbeddedNATS(ctx context.Context) (string, string, error) { cfg, log := b.cfg, b.log if cfg.StateDir == "" { return "", "", errors.New("embedded nats: state_dir is required") } - host := "" - if b.containerPool != nil { - host = b.containerPool.HostGateway() + host, err := natsHost(ctx, cfg, b.containerPool) + if err != nil { + return "", "", err } // A restarted broker keeps the previous endpoint, so the Gateway and // running agents reconnect to it instead of a broker that no longer exists. diff --git a/internal/infrastructure/openshell/runtime.go b/internal/infrastructure/openshell/runtime.go index e5163a253..a5ad92433 100644 --- a/internal/infrastructure/openshell/runtime.go +++ b/internal/infrastructure/openshell/runtime.go @@ -40,10 +40,11 @@ const anyBinary = "/**" // writable are container paths tools write to inside the sandbox. They are // the container's own layer, so writing them exposes nothing on the host. -var writable = []string{"/tmp", "/var/tmp", "/root", "/home"} +var writable = []string{"/tmp", "/var/tmp", "/root", "/home", "/dev/null"} -// readOnly are the system paths the agent and its tools read. -var readOnly = []string{"/usr", "/lib", "/lib64", "/bin", "/sbin", "/etc", "/opt"} +// readOnly are the system paths the agent and its tools read. git and the +// shell need /dev/null and /proc. +var readOnly = []string{"/usr", "/lib", "/lib64", "/bin", "/sbin", "/etc", "/opt", "/proc", "/dev/urandom"} // Options configure a Runtime. type Options struct {