Confining it was pointless: the relay the namespace exists for is only started when egress is non-empty, so the package got loopback and a proxy address nothing served. Any client honouring the proxy variables then reported a dial refused at the relay rather than reaching the host it was configured for. Fixes #2152.
55 lines
1.6 KiB
Go
55 lines
1.6 KiB
Go
//go:build linux
|
|
|
|
package host
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"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")
|
|
}
|
|
})
|
|
}
|