| 1 | package containment |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strconv" |
| 9 | "sync" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/runner/egress" |
| 12 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 13 | ) |
| 14 | |
| 15 | func (m *Manager) openEgress(ctx context.Context, l provider.Launch, dir string, p *mounts) (string, error) { |
| 16 | // Check jobs have no provider and retain a completely disconnected namespace. |
| 17 | if l.Selection.Provider == "" { |
| 18 | return "", nil |
| 19 | } |
| 20 | if l.Binding.DedicatedHome && !l.Binding.VerifiedDedicatedHome() { |
| 21 | return "", errMount |
| 22 | } |
| 23 | contract := provider.NetworkContract{Provider: l.Selection.Provider, Harness: l.Policy.Harness()} |
| 24 | for _, c := range m.cfg.ProviderNetworks { |
| 25 | if c.Provider == contract.Provider && c.Harness == contract.Harness { |
| 26 | contract = c |
| 27 | } |
| 28 | } |
| 29 | policy, e := egress.ProviderPolicy(contract, l.Binding.VerifiedDedicatedHome()) |
| 30 | if l.Policy.IsSignIn() { |
| 31 | // Claude's sign-in reaches its sign-in and API hosts, nothing |
| 32 | // else (provider.SignInHosts). |
| 33 | policy, e = egress.SignInPolicy(contract) |
| 34 | } |
| 35 | if e != nil { |
| 36 | return "", e |
| 37 | } |
| 38 | if e = l.Policy.ValidateNetwork(policy.Provider, policy.Hosts); e != nil { |
| 39 | return "", e |
| 40 | } |
| 41 | hash, e := policy.Hash() |
| 42 | if e != nil { |
| 43 | return "", e |
| 44 | } |
| 45 | private := filepath.Join(dir, "egress") |
| 46 | if e = os.Mkdir(private, 0700); e != nil { |
| 47 | return "", e |
| 48 | } |
| 49 | proxy, e := egress.Open(ctx, private, policy) |
| 50 | if e != nil { |
| 51 | return "", e |
| 52 | } |
| 53 | ok := false |
| 54 | defer func() { |
| 55 | if !ok { |
| 56 | proxy.Close() |
| 57 | } |
| 58 | }() |
| 59 | f, e := pin(proxy.SocketPath()) |
| 60 | if e != nil { |
| 61 | return "", e |
| 62 | } |
| 63 | info, e := f.Stat() |
| 64 | if e != nil || info.Mode()&os.ModeSocket == 0 || info.Mode().Perm() != 0600 { |
| 65 | f.Close() |
| 66 | return "", errMount |
| 67 | } |
| 68 | // This is the sole socket import. The parent directory and other host sockets |
| 69 | // remain absent; the retained O_PATH descriptor pins this exact endpoint. |
| 70 | p.args = append(p.args, "--ro-bind-fd", strconv.Itoa(6+len(p.files)), egress.NamespaceSocket) |
| 71 | p.files = append(p.files, f) |
| 72 | for _, key := range []string{"HTTPS_PROXY", "HTTP_PROXY", "https_proxy", "http_proxy"} { |
| 73 | p.args = append(p.args, "--setenv", key, egress.ProxyURL) |
| 74 | } |
| 75 | p.proxy = proxy |
| 76 | ok = true |
| 77 | return hash, nil |
| 78 | } |
| 79 | |
| 80 | // The relay runs in PID 1's Go process. It cannot select a host socket or remote |
| 81 | // address, and starts before the harness. No subprocess or external binary is used. |
| 82 | type namespaceRelay struct { |
| 83 | listener net.Listener |
| 84 | failed chan struct{} |
| 85 | once sync.Once |
| 86 | mu sync.Mutex |
| 87 | closed bool |
| 88 | connections map[net.Conn]struct{} |
| 89 | wg sync.WaitGroup |
| 90 | } |
| 91 | |
| 92 | func startRelay() (*namespaceRelay, error) { |
| 93 | ln, e := net.Listen("tcp4", egress.RelayAddress) |
| 94 | if e != nil { |
| 95 | return nil, e |
| 96 | } |
| 97 | r := &namespaceRelay{listener: ln, failed: make(chan struct{}), connections: map[net.Conn]struct{}{}} |
| 98 | r.wg.Add(1) |
| 99 | go r.serve() |
| 100 | return r, nil |
| 101 | } |
| 102 | func (r *namespaceRelay) shutdown() { |
| 103 | r.once.Do(func() { |
| 104 | r.mu.Lock() |
| 105 | r.closed = true |
| 106 | r.listener.Close() |
| 107 | for c := range r.connections { |
| 108 | c.Close() |
| 109 | } |
| 110 | r.mu.Unlock() |
| 111 | close(r.failed) |
| 112 | }) |
| 113 | } |
| 114 | func (r *namespaceRelay) close() { r.shutdown(); r.wg.Wait() } |
| 115 | func (r *namespaceRelay) serve() { |
| 116 | defer r.wg.Done() |
| 117 | defer r.shutdown() |
| 118 | slots := make(chan struct{}, egress.MaxTunnels) |
| 119 | for { |
| 120 | c, e := r.listener.Accept() |
| 121 | if e != nil { |
| 122 | return |
| 123 | } |
| 124 | select { |
| 125 | case slots <- struct{}{}: |
| 126 | default: |
| 127 | c.Close() |
| 128 | continue |
| 129 | } |
| 130 | if !r.add(c) { |
| 131 | c.Close() |
| 132 | <-slots |
| 133 | return |
| 134 | } |
| 135 | r.wg.Add(1) |
| 136 | go func() { |
| 137 | defer r.wg.Done() |
| 138 | defer func() { <-slots; r.remove(c) }() |
| 139 | upstream, e := net.DialTimeout("unix", egress.NamespaceSocket, egress.ConnectTimeout) |
| 140 | if e != nil { |
| 141 | r.shutdown() |
| 142 | return |
| 143 | } |
| 144 | if !r.add(upstream) { |
| 145 | upstream.Close() |
| 146 | return |
| 147 | } |
| 148 | defer r.remove(upstream) |
| 149 | egress.Tunnel(c, upstream, egress.IdleTimeout) |
| 150 | }() |
| 151 | } |
| 152 | } |
| 153 | func (r *namespaceRelay) add(c net.Conn) bool { |
| 154 | r.mu.Lock() |
| 155 | defer r.mu.Unlock() |
| 156 | if r.closed { |
| 157 | return false |
| 158 | } |
| 159 | r.connections[c] = struct{}{} |
| 160 | return true |
| 161 | } |
| 162 | func (r *namespaceRelay) remove(c net.Conn) { |
| 163 | c.Close() |
| 164 | r.mu.Lock() |
| 165 | delete(r.connections, c) |
| 166 | r.mu.Unlock() |
| 167 | } |
| 168 | |