Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: read podman config on Windows #1837

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion pkg/authn/keychain.go
Expand Up @@ -17,6 +17,7 @@ package authn
import (
"os"
"path/filepath"
"runtime"
"sync"
"time"

Expand Down Expand Up @@ -97,7 +98,13 @@ func (dk *defaultKeychain) Resolve(target Resource) (Authenticator, error) {
return nil, err
}
} else {
f, err := os.Open(filepath.Join(os.Getenv("XDG_RUNTIME_DIR"), "containers/auth.json"))
var configBaseDir string
if runtime.GOOS == "linux" {
configBaseDir = os.Getenv("XDG_RUNTIME_DIR")
} else {
configBaseDir = filepath.Join(home, ".config")
}
f, err := os.Open(filepath.Join(configBaseDir, "containers", "auth.json"))
if err != nil {
return Anonymous, nil
}
Expand Down
28 changes: 24 additions & 4 deletions pkg/authn/keychain_test.go
Expand Up @@ -23,9 +23,12 @@ import (
"path"
"path/filepath"
"reflect"
"runtime"
"testing"
"time"

"github.com/mitchellh/go-homedir"

"github.com/google/go-containerregistry/pkg/name"
)

Expand Down Expand Up @@ -99,12 +102,19 @@ func TestPodmanConfig(t *testing.T) {
}
fresh++
p := filepath.Join(tmpdir, fmt.Sprintf("%d", fresh))
t.Setenv("XDG_RUNTIME_DIR", p)
var ctrDir string
if runtime.GOOS == "linux" {
t.Setenv("XDG_RUNTIME_DIR", p)
ctrDir = filepath.Join(p, "containers")
} else {
setHomedir(t, p)
ctrDir = filepath.Join(p, ".config", "containers")
}
os.Unsetenv("DOCKER_CONFIG")
if err := os.MkdirAll(filepath.Join(p, "containers"), 0777); err != nil {
t.Fatalf("mkdir %s/containers: %v", p, err)
if err := os.MkdirAll(ctrDir, 0777); err != nil {
t.Fatalf("mkdir %s: %v", ctrDir, err)
}
cfg := filepath.Join(p, "containers/auth.json")
cfg := filepath.Join(ctrDir, "auth.json")
content := fmt.Sprintf(`{"auths": {"test.io": {"auth": %q}}}`, encode("foo", "bar"))
if err := os.WriteFile(cfg, []byte(content), 0600); err != nil {
t.Fatalf("write %q: %v", cfg, err)
Expand Down Expand Up @@ -181,6 +191,16 @@ func TestPodmanConfig(t *testing.T) {
}
}

func setHomedir(t *testing.T, p string) {
oldDisableCache := homedir.DisableCache
t.Cleanup(func() {
homedir.DisableCache = oldDisableCache
})
homedir.DisableCache = true
t.Setenv("HOME", p)
t.Setenv("USERPROFILE", p)
}

func encode(user, pass string) string {
delimited := fmt.Sprintf("%s:%s", user, pass)
return base64.StdEncoding.EncodeToString([]byte(delimited))
Expand Down