Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions internal/oauth/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -48,23 +55,39 @@ 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
Comment on lines +67 to +68
}
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
}
}
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
}
50 changes: 50 additions & 0 deletions internal/oauth/env_test.go
Original file line number Diff line number Diff line change
@@ -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())
}