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
22 changes: 15 additions & 7 deletions internal/oauth/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
Expand Down Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions internal/oauth/env_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
}
}