feat(egress): relay forwarding a sandbox network to the egress proxy

archie-agent relay listens on the sandbox's isolated network and joins
every connection to one fixed target, the proxy. The relay holds no
secrets and can reach nothing else, so the host needs no address on a
sandbox network at all.
This commit is contained in:
sam committed 2026-09-26 02:33:41 +10:00
1 parent d1e5c628d0
commit c98c9dcc22
6 files changed
+262

No files matched your search

+1
View File
@@ -53,6 +53,7 @@ func TestCommandBoundary(t *testing.T) {
"run": true,
"runCommand": true,
"natsConnectionSettings": true,
"runRelay": true,
}
for _, declaration := range file.Decls {
function, ok := declaration.(*ast.FuncDecl)
+32
View File
@@ -26,6 +26,9 @@ func run() int {
}
func runCommand(args []string, getenv func(string) string, stderr io.Writer, runWorker workerRunner) int {
if len(args) > 0 && args[0] == "relay" {
return runRelay(args[1:], stderr, agentworker.RunRelay)
}
flags := flag.NewFlagSet("archie-agent", flag.ContinueOnError)
flags.SetOutput(stderr)
natsURLFlag := flags.String("nats-url", "", "NATS server URL (defaults to NATS_URL)")
@@ -81,3 +84,32 @@ func natsConnectionSettings(flagURL string, getenv func(string) string) (url, to
}
return url, getenv("NATS_TOKEN")
}
type relayServer func(ctx context.Context, listen, target string) error
// runRelay is the sandbox egress relay: the container on both a sandbox's
// isolated network and the host-reachable one, forwarding to the proxy.
func runRelay(args []string, stderr io.Writer, serve relayServer) int {
flags := flag.NewFlagSet("archie-agent relay", flag.ContinueOnError)
flags.SetOutput(stderr)
listen := flags.String("listen", ":3128", "address sandbox containers connect to")
target := flags.String("to", "", "the egress proxy address every connection is forwarded to")
if err := flags.Parse(args); err != nil {
if errors.Is(err, flag.ErrHelp) {
return 0
}
return 2
}
if *target == "" {
fmt.Fprintln(stderr, "error: -to is required")
flags.Usage()
return 1
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
if err := serve(ctx, *listen, *target); err != nil {
fmt.Fprintln(stderr, "error:", err)
return 1
}
return 0
}
+43
View File
@@ -0,0 +1,43 @@
package main
import (
"bytes"
"context"
"strings"
"testing"
)
func TestRunRelay(t *testing.T) {
t.Run("-to is required", func(t *testing.T) {
var stderr bytes.Buffer
called := false
serve := func(context.Context, string, string) error { called = true; return nil }
if code := runRelay([]string{"-listen", "127.0.0.1:0"}, &stderr, serve); code != 1 {
t.Fatalf("exit %d, want 1", code)
}
if called || !strings.Contains(stderr.String(), "-to is required") {
t.Fatalf("served=%v stderr=%q", called, stderr.String())
}
})
t.Run("serves the named target on the listen address", func(t *testing.T) {
var gotTarget, gotAddr string
serve := func(_ context.Context, listen, target string) error {
gotTarget, gotAddr = target, listen
return nil
}
if code := runRelay([]string{"-listen", "127.0.0.1:0", "-to", "172.17.0.1:3129"}, &bytes.Buffer{}, serve); code != 0 {
t.Fatalf("exit %d, want 0", code)
}
if gotTarget != "172.17.0.1:3129" || gotAddr != "127.0.0.1:0" {
t.Fatalf("served %q on %q", gotTarget, gotAddr)
}
})
}
func TestRelaySubcommandRoutes(t *testing.T) {
var stderr bytes.Buffer
code := runCommand([]string{"relay"}, func(string) string { return "" }, &stderr, nil)
if code != 1 || !strings.Contains(stderr.String(), "-to is required") {
t.Fatalf("relay subcommand exit %d stderr %q; want the relay's own usage error", code, stderr.String())
}
}
+18
View File
@@ -0,0 +1,18 @@
package agentworker
import (
"context"
"net"
"github.com/samcharles93/archie-core/internal/infrastructure/egress/relay"
)
// RunRelay listens on listen and forwards every connection to target, the
// egress proxy, until ctx is cancelled.
func RunRelay(ctx context.Context, listen, target string) error {
ln, err := (&net.ListenConfig{}).Listen(ctx, "tcp", listen)
if err != nil {
return err
}
return relay.Serve(ctx, ln, target)
}
@@ -0,0 +1,70 @@
// Package relay forwards a sandbox's proxy connections to archie's egress
// proxy. It runs in the one container that is on both a sandbox's isolated
// network and the host-reachable network, and it can reach exactly one
// address: it moves bytes to its fixed target and nothing else.
package relay
import (
"context"
"errors"
"io"
"net"
"sync"
"time"
)
const dialTimeout = 10 * time.Second
// Serve accepts connections on ln and joins each to a new connection to
// target until ctx is cancelled.
func Serve(ctx context.Context, ln net.Listener, target string) error {
stop := context.AfterFunc(ctx, func() { _ = ln.Close() })
defer stop()
var wg sync.WaitGroup
defer wg.Wait()
for {
client, err := ln.Accept()
if err != nil {
if ctx.Err() != nil {
return nil
}
return err
}
wg.Add(1)
go func() {
defer wg.Done()
join(ctx, client, target)
}()
}
}
func join(ctx context.Context, client net.Conn, target string) {
defer client.Close()
upstream, err := (&net.Dialer{Timeout: dialTimeout}).DialContext(ctx, "tcp", target)
if err != nil {
return
}
defer upstream.Close()
stop := context.AfterFunc(ctx, func() {
_ = client.Close()
_ = upstream.Close()
})
defer stop()
done := make(chan struct{}, 2)
go pipe(upstream, client, done)
go pipe(client, upstream, done)
<-done
<-done
}
// pipe copies src to dst, then half-closes dst so the far side sees EOF
// while the other direction keeps flowing.
func pipe(dst, src net.Conn, done chan<- struct{}) {
_, err := io.Copy(dst, src)
if tcp, ok := dst.(*net.TCPConn); ok && (err == nil || errors.Is(err, net.ErrClosed)) {
_ = tcp.CloseWrite()
} else {
_ = dst.Close()
}
done <- struct{}{}
}
@@ -0,0 +1,98 @@
package relay
import (
"bufio"
"context"
"errors"
"io"
"net"
"testing"
"time"
)
func listen(t *testing.T) net.Listener {
t.Helper()
ln, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = ln.Close() })
return ln
}
func echoServer(t *testing.T) string {
t.Helper()
ln := listen(t)
go func() {
for {
c, err := ln.Accept()
if err != nil {
return
}
go func() {
defer c.Close()
_, _ = io.Copy(c, c)
}()
}
}()
return ln.Addr().String()
}
func startRelay(t *testing.T, target string) (string, context.CancelFunc, <-chan error) {
t.Helper()
ln := listen(t)
ctx, cancel := context.WithCancel(t.Context())
done := make(chan error, 1)
go func() { done <- Serve(ctx, ln, target) }()
return ln.Addr().String(), cancel, done
}
func TestRelayForwardsBothWays(t *testing.T) {
addr, cancel, _ := startRelay(t, echoServer(t))
defer cancel()
c, err := (&net.Dialer{}).DialContext(t.Context(), "tcp", addr)
if err != nil {
t.Fatal(err)
}
defer c.Close()
_ = c.SetDeadline(time.Now().Add(5 * time.Second))
for _, line := range []string{"first\n", "second\n"} {
if _, err := io.WriteString(c, line); err != nil {
t.Fatal(err)
}
got, err := bufio.NewReader(c).ReadString('\n')
if err != nil || got != line {
t.Fatalf("echo %q, want %q (%v)", got, line, err)
}
}
}
func TestRelayClosesTheClientWhenTheTargetIsDown(t *testing.T) {
dead := listen(t)
target := dead.Addr().String()
_ = dead.Close()
addr, cancel, _ := startRelay(t, target)
defer cancel()
c, err := (&net.Dialer{}).DialContext(t.Context(), "tcp", addr)
if err != nil {
t.Fatal(err)
}
defer c.Close()
_ = c.SetReadDeadline(time.Now().Add(5 * time.Second))
if _, err := c.Read(make([]byte, 1)); !errors.Is(err, io.EOF) {
t.Fatalf("read from a relay with no target: %v, want EOF", err)
}
}
func TestRelayStopsOnCancel(t *testing.T) {
_, cancel, done := startRelay(t, echoServer(t))
cancel()
select {
case err := <-done:
if err != nil {
t.Fatalf("Serve returned %v after cancel, want nil", err)
}
case <-time.After(5 * time.Second):
t.Fatal("Serve did not return after its context was cancelled")
}
}