diff --git a/internal/oauth/env.go b/internal/oauth/env.go index 34fa329973..9b79217fde 100644 --- a/internal/oauth/env.go +++ b/internal/oauth/env.go @@ -6,6 +6,7 @@ import ( "io" "os" "os/exec" + "path/filepath" "runtime" "strings" ) @@ -48,19 +49,26 @@ func openBrowser(url string) error { return nil } -// isRunningInDocker reports whether the process is running inside a Docker (or -// containerd) container. Detection relies on Linux-specific paths and is always -// false elsewhere. It is used only to skip a PKCE flow that cannot work: a -// random callback port inside a container cannot be reached from the host -// browser, so we go straight to device flow in that case. +// isRunningInDocker reports whether the process is running inside a Docker, +// containerd, or Podman container. Detection relies on Linux-specific paths and +// is always false elsewhere. The result decides whether to skip a random-port +// PKCE flow and whether to bind the callback listener to all interfaces so a +// fixed published port can reach it. func isRunningInDocker() bool { if runtime.GOOS != "linux" { return false } - if _, err := os.Stat("/.dockerenv"); err == nil { + return containerMarkersPresent("/") +} + +func containerMarkersPresent(root string) bool { + if _, err := os.Stat(filepath.Join(root, ".dockerenv")); err == nil { + return true + } + if _, err := os.Stat(filepath.Join(root, "run", ".containerenv")); err == nil { return true } - if data, err := os.ReadFile("/proc/1/cgroup"); err == nil { + if data, err := os.ReadFile(filepath.Join(root, "proc", "1", "cgroup")); err == nil { s := string(data) if strings.Contains(s, "docker") || strings.Contains(s, "containerd") { return true diff --git a/internal/oauth/env_test.go b/internal/oauth/env_test.go new file mode 100644 index 0000000000..f51ea84e7f --- /dev/null +++ b/internal/oauth/env_test.go @@ -0,0 +1,60 @@ +package oauth + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestContainerMarkersPresent(t *testing.T) { + tests := []struct { + name string + files map[string]string + present bool + }{ + { + name: "no markers", + }, + { + name: "Docker environment marker", + files: map[string]string{".dockerenv": ""}, + present: true, + }, + { + name: "Podman environment marker", + files: map[string]string{filepath.Join("run", ".containerenv"): ""}, + present: true, + }, + { + name: "Docker cgroup", + files: map[string]string{filepath.Join("proc", "1", "cgroup"): "12:devices:/docker/container-id\n"}, + present: true, + }, + { + name: "containerd cgroup", + files: map[string]string{filepath.Join("proc", "1", "cgroup"): "0::/containerd/container-id\n"}, + present: true, + }, + { + name: "cgroup v2 root", + files: map[string]string{filepath.Join("proc", "1", "cgroup"): "0::/\n"}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + root := t.TempDir() + for name, contents := range tt.files { + path := filepath.Join(root, name) + require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755)) + require.NoError(t, os.WriteFile(path, []byte(contents), 0o600)) + require.FileExists(t, path) + } + + assert.Equal(t, tt.present, containerMarkersPresent(root)) + }) + } +}