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:
6 files changed
+262
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user