package daemon import ( "context" "io" "net" "net/netip" "strings" "testing" "time" "github.com/samcharles93/archie-core/internal/domain/workflow" ) // The relay is what makes a loopback State Store reachable from a container: // bytes the task's container writes to its address arrive at the target, and // no other peer on the bridge gets through. func TestStateStoreRelayForwards(t *testing.T) { t.Parallel() tests := []struct { name string admit []netip.Addr carried bool }{ {name: "the task container", admit: []netip.Addr{netip.MustParseAddr("127.0.0.1")}, carried: true}, {name: "another peer", admit: []netip.Addr{netip.MustParseAddr("127.0.0.2")}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { upstream := echoListener(t) relay, err := openStateStoreRelay(context.Background(), "127.0.0.1", upstream.Addr().String()) if err != nil { t.Fatalf("open relay: %v", err) } t.Cleanup(relay.Close) if relay.Addr() == upstream.Addr().String() { t.Fatalf("relay listens on the target's own address %s", relay.Addr()) } conn, err := (&net.Dialer{}).DialContext(context.Background(), "tcp", relay.Addr()) if err != nil { t.Fatalf("dial relay: %v", err) } defer func() { _ = conn.Close() }() relay.Admit(tt.admit) _ = conn.SetDeadline(time.Now().Add(5 * time.Second)) _, _ = conn.Write([]byte("ping")) got := make([]byte, 4) _, err = io.ReadFull(conn, got) if carried := err == nil && string(got) == "ping"; carried != tt.carried { t.Fatalf("carried = %v (read %q, %v), want %v", carried, got, err, tt.carried) } }) } } // The relay is per task, so releasing the task has to end the access it gave. func TestStateStoreRelayClosesWithTask(t *testing.T) { t.Parallel() upstream := echoListener(t) relay, err := openStateStoreRelay(context.Background(), "127.0.0.1", upstream.Addr().String()) if err != nil { t.Fatalf("open relay: %v", err) } addr := relay.Addr() relay.Close() if conn, err := (&net.Dialer{Timeout: time.Second}).DialContext(context.Background(), "tcp", addr); err == nil { _ = conn.Close() t.Fatalf("relay still accepts at %s after the task released it", addr) } } // An ordinary image task is handed the relay's address, never the loopback // target the daemon dialed, with the task-scoped credentials unchanged. func TestContainerEnvStateStore(t *testing.T) { t.Parallel() upstream := echoListener(t) relay, err := openStateStoreRelay(context.Background(), "127.0.0.1", upstream.Addr().String()) if err != nil { t.Fatalf("open relay: %v", err) } t.Cleanup(relay.Close) d := &Daemon{ ConnectedNATS: NATSEndpoint{URL: "nats://10.0.0.5:4222"}, ConnectedStateStore: StateStoreEndpoint{URL: upstream.Addr().String(), CA: []byte("ca-pem")}, } env := envValues(d.containerEnv(&workflow.Task{ID: 7}, "task-token", relay.Addr())) if got := env["STATE_STORE_URL"]; got != relay.Addr() { t.Fatalf("STATE_STORE_URL = %q, want the relay address %q", got, relay.Addr()) } if env["STATE_STORE_TOKEN"] != "task-token" { t.Fatalf("STATE_STORE_TOKEN = %q, want the task's own token", env["STATE_STORE_TOKEN"]) } if env["STATE_STORE_CA"] != "ca-pem" { t.Fatalf("STATE_STORE_CA = %q, want the CA passed through", env["STATE_STORE_CA"]) } if got := env["NATS_URL"]; got != "nats://10.0.0.5:4222" { t.Fatalf("NATS_URL = %q, want the connected broker", got) } } // Only a target the container cannot reach is relayed. An address already // configured for the containers' network is passed through untouched, and a // target the relay cannot forward is left alone rather than guessed at. func TestLoopbackStateStoreTarget(t *testing.T) { t.Parallel() cases := []struct { name string target string addr string loopback bool }{ {name: "loopback ipv4", target: "127.0.0.1:9090", addr: "127.0.0.1:9090", loopback: true}, {name: "loopback ipv6", target: "[::1]:9090", addr: "[::1]:9090", loopback: true}, {name: "localhost", target: "localhost:9090", addr: "localhost:9090", loopback: true}, {name: "grpc scheme form", target: "dns:///127.0.0.1:9090", addr: "127.0.0.1:9090", loopback: true}, {name: "host address", target: "10.0.0.5:9090"}, {name: "service name", target: "state-store:9090"}, {name: "unset", target: ""}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { t.Parallel() addr, loopback := loopbackStateStoreTarget(tc.target) if loopback != tc.loopback { t.Fatalf("loopbackStateStoreTarget(%q) loopback = %t, want %t", tc.target, loopback, tc.loopback) } if addr != tc.addr { t.Fatalf("loopbackStateStoreTarget(%q) addr = %q, want %q", tc.target, addr, tc.addr) } }) } } // echoListener accepts connections on loopback and echoes what it reads. func echoListener(t *testing.T) net.Listener { t.Helper() ln, err := (&net.ListenConfig{}).Listen(context.Background(), "tcp", "127.0.0.1:0") if err != nil { t.Fatalf("listen: %v", err) } t.Cleanup(func() { _ = ln.Close() }) go func() { for { conn, err := ln.Accept() if err != nil { return } go func() { defer func() { _ = conn.Close() }() _, _ = io.Copy(conn, conn) }() } }() return ln } func envValues(env []string) map[string]string { out := make(map[string]string, len(env)) for _, entry := range env { key, value, _ := strings.Cut(entry, "=") out[key] = value } return out }