//go:build linux package host import ( "cmp" "context" "os" "path/filepath" "syscall" "testing" ) // TestCommandConfinesOnlyWithEgress guards the regression where a package that // declares no egress was still confined to a namespace holding loopback and // handed a proxy address nothing served, so it reached nothing: the host // connector could not open its SSH connection and reported a refused dial to // the relay instead. func TestCommandConfinesOnlyWithEgress(t *testing.T) { binary, err := os.Open("/proc/self/exe") if err != nil { t.Fatalf("open the binary: %v", err) } t.Cleanup(func() { _ = binary.Close() }) ctx := context.Background() t.Run("no egress runs on the host's network with no relay", func(t *testing.T) { cmd, gate, err := command(ctx, binary, nil) if err != nil { t.Fatalf("command: %v", err) } if gate != nil { t.Error("a package with no egress has nothing to relay") } if cmd.SysProcAttr != nil && cmd.SysProcAttr.Cloneflags&syscall.CLONE_NEWNET != 0 { t.Error("a package with no egress was confined to its own network namespace") } if cmd.Env != nil { t.Error("a package with no egress was handed a proxy environment") } }) t.Run("egress is confined behind a relay", func(t *testing.T) { cmd, gate, err := command(ctx, binary, []string{"example.com:443"}) if err != nil { t.Fatalf("command: %v", err) } if gate == nil { t.Fatal("a package with egress needs a gate to reach it through") } t.Cleanup(gate.close) if cmd.SysProcAttr == nil || cmd.SysProcAttr.Cloneflags&syscall.CLONE_NEWNET == 0 { t.Error("a package with egress was not confined to its own network namespace") } }) } // TestNamespaceLimitRefusesDisabledNamespaces guards fail-closed extension // startup: a kernel or container runtime that caps user or network namespaces // at zero must be reported before a fork the kernel would deny, so an operator // learns to configure a remote endpoint. Namespace support was detected only by // the failing fork, which surfaced as an opaque plugin start error. func TestNamespaceLimitRefusesDisabledNamespaces(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "max_user_namespaces") cases := []struct { name string content string path string wantErr bool }{ {name: "enabled", content: "255325\n"}, {name: "disabled", content: "0\n", wantErr: true}, {name: "unparseable", content: "yes\n", wantErr: true}, {name: "absent", path: filepath.Join(dir, "absent"), wantErr: true}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { if err := os.WriteFile(path, []byte(c.content), 0o600); err != nil { t.Fatal(err) } err := namespaceLimit(cmp.Or(c.path, path), "user namespaces") if (err != nil) != c.wantErr { t.Fatalf("namespaceLimit(%q) = %v, want error = %v", c.content, err, c.wantErr) } }) } }