diff --git a/internal/oauth/env.go b/internal/oauth/env.go index 34fa329973..91102ddd53 100644 --- a/internal/oauth/env.go +++ b/internal/oauth/env.go @@ -16,6 +16,13 @@ import ( // from a browser on another machine (e.g. a remote SSH session). var errNoDisplay = errors.New("no display server detected") +// Container marker paths are variables so tests can point them at fixtures. +var ( + dockerEnvPath = "/.dockerenv" + podmanEnvPath = "/run/.containerenv" + cgroupPath = "/proc/1/cgroup" +) + // openBrowser tries to open url in the user's default browser. It returns an // error when no browser can plausibly be launched so the caller can fall back // to elicitation. On Linux it treats a headless session (no display server) as @@ -48,19 +55,22 @@ 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. 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. func isRunningInDocker() bool { if runtime.GOOS != "linux" { return false } - if _, err := os.Stat("/.dockerenv"); err == nil { + if isPodmanContainer() { + return true + } + if _, err := os.Stat(dockerEnvPath); err == nil { return true } - if data, err := os.ReadFile("/proc/1/cgroup"); err == nil { + if data, err := os.ReadFile(cgroupPath); err == nil { s := string(data) if strings.Contains(s, "docker") || strings.Contains(s, "containerd") { return true @@ -68,3 +78,16 @@ func isRunningInDocker() bool { } return false } + +// isPodmanContainer reports whether the process is running inside a Podman +// container. Podman does not create /.dockerenv; it sets container=podman and +// creates /run/.containerenv instead. +func isPodmanContainer() bool { + if os.Getenv("container") == "podman" { + return true + } + if _, err := os.Stat(podmanEnvPath); err == nil { + return true + } + return false +} diff --git a/internal/oauth/env_test.go b/internal/oauth/env_test.go new file mode 100644 index 0000000000..ab0c1d4179 --- /dev/null +++ b/internal/oauth/env_test.go @@ -0,0 +1,50 @@ +package oauth + +import ( + "os" + "path/filepath" + "runtime" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestIsPodmanContainer_PodmanEnvironmentVariable(t *testing.T) { + t.Setenv("container", "podman") + + assert.True(t, isPodmanContainer()) +} + +func TestIsPodmanContainer_ContainerEnvironmentFile(t *testing.T) { + t.Setenv("container", "") + + path := filepath.Join(t.TempDir(), "containerenv") + require.NoError(t, os.WriteFile(path, nil, 0o600)) + + original := podmanEnvPath + podmanEnvPath = path + t.Cleanup(func() { podmanEnvPath = original }) + + assert.True(t, isPodmanContainer()) +} + +func TestIsPodmanContainer_NoMarkers(t *testing.T) { + t.Setenv("container", "") + + original := podmanEnvPath + podmanEnvPath = filepath.Join(t.TempDir(), "missing") + t.Cleanup(func() { podmanEnvPath = original }) + + assert.False(t, isPodmanContainer()) +} + +func TestIsRunningInDocker_PodmanOnLinux(t *testing.T) { + if runtime.GOOS != "linux" { + t.Skip("container detection is Linux-only") + } + + t.Setenv("container", "podman") + + assert.True(t, isRunningInDocker()) +}