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

Default keychain looks also into $HOME/.config/containers/auth.json #1977

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkg/authn/keychain.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ func (dk *defaultKeychain) ResolveContext(_ context.Context, target Resource) (A
if err != nil {
return nil, err
}
} else if fileExists(filepath.Join(home, ".config/containers/auth.json")) {
f, err := os.Open(filepath.Join(home, ".config/containers/auth.json"))
if err != nil {
return nil, err
}
defer f.Close()
cf, err = config.LoadFromReader(f)
if err != nil {
return nil, err
}
} else {
return Anonymous, nil
}
Expand Down
32 changes: 27 additions & 5 deletions pkg/authn/keychain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,44 @@ func TestPodmanConfig(t *testing.T) {

os.Unsetenv("DOCKER_CONFIG")
// At first, $DOCKER_CONFIG is unset and $HOME/.docker/config.json isn't
// found, but Podman auth $XDG_RUNTIME_DIR/containers/auth.json is configured.
// This should return Podman's auth $XDG_RUNTIME_DIR/containers/auth.json.
// found. $XDG_RUNTIME_DIR is unset too to simulate macOS/Windows environment
// This should return Podman's auth $HOME/.config/containers/auth.json.
writeConfig(t, filepath.Join(os.Getenv("HOME"), ".config", "containers"), "auth.json",
fmt.Sprintf(`{"auths": {"test.io": {"auth": %q}}}`,
encode("DEFAULT-MAC-WIN-foo", "DEFAULT-MAC-WIN-bar")))
defer func() { os.Remove(filepath.Join(os.Getenv("HOME"), ".config/containers/auth.json")) }()
auth, err := DefaultKeychain.Resolve(testRegistry)
if err != nil {
t.Fatalf("Resolve() = %v", err)
}
got, err := auth.Authorization()
if err != nil {
t.Fatal(err)
}
want := &AuthConfig{
Username: "DEFAULT-MAC-WIN-foo",
Password: "DEFAULT-MAC-WIN-bar",
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %+v, want %+v", got, want)
}

// Then, XDG_RUNTIME_DIR is populated, to simulate a Linux environment,
// and Podman auth $XDG_RUNTIME_DIR/containers/auth.json is configured.
p := filepath.Join(tmpdir, fmt.Sprintf("%d", fresh))
t.Setenv("XDG_RUNTIME_DIR", p)
writeConfig(t, filepath.Join(p, "containers"), "auth.json",
fmt.Sprintf(`{"auths": {"test.io": {"auth": %q}}}`,
encode("XDG_RUNTIME_DIR-foo", "XDG_RUNTIME_DIR-bar")))
auth, err := DefaultKeychain.Resolve(testRegistry)
auth, err = DefaultKeychain.Resolve(testRegistry)
if err != nil {
t.Fatalf("Resolve() = %v", err)
}
got, err := auth.Authorization()
got, err = auth.Authorization()
if err != nil {
t.Fatal(err)
}
want := &AuthConfig{
want = &AuthConfig{
Username: "XDG_RUNTIME_DIR-foo",
Password: "XDG_RUNTIME_DIR-bar",
}
Expand Down
Loading