Skip to content

Commit

Permalink
Treat empty registry config as anonymous (#1512)
Browse files Browse the repository at this point in the history
Make DefaultKeychain return the Anonymous authenticator when the config
for a registry is empty.

Fixes #1510

Signed-off-by: Luiz Carvalho <lucarval@redhat.com>

Signed-off-by: Luiz Carvalho <lucarval@redhat.com>
  • Loading branch information
lcarva authored Dec 19, 2022
1 parent 23d895d commit ffc14a0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
4 changes: 4 additions & 0 deletions pkg/authn/keychain.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ func (dk *defaultKeychain) Resolve(target Resource) (Authenticator, error) {
if err != nil {
return nil, err
}
// cf.GetAuthConfig automatically sets the ServerAddress attribute. Since
// we don't make use of it, clear the value for a proper "is-empty" test.
// See: https://github.com/google/go-containerregistry/issues/1510
cfg.ServerAddress = ""
if cfg != empty {
break
}
Expand Down
26 changes: 21 additions & 5 deletions pkg/authn/keychain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,12 @@ func encode(user, pass string) string {

func TestVariousPaths(t *testing.T) {
tests := []struct {
desc string
content string
wantErr bool
target Resource
cfg *AuthConfig
desc string
content string
wantErr bool
target Resource
cfg *AuthConfig
anonymous bool
}{{
desc: "invalid config file",
target: testRegistry,
Expand Down Expand Up @@ -264,6 +265,17 @@ func TestVariousPaths(t *testing.T) {
Username: "foo",
Password: "bar",
},
}, {
desc: "ignore unrelated repo",
target: testRepo,
content: fmt.Sprintf(`{
"auths": {
"test.io/another-repo": {"auth": %q},
"test.io": {}
}
}`, encode("bar", "baz")),
cfg: &AuthConfig{},
anonymous: true,
}}

for _, test := range tests {
Expand Down Expand Up @@ -292,6 +304,10 @@ func TestVariousPaths(t *testing.T) {
if !reflect.DeepEqual(cfg, test.cfg) {
t.Errorf("got %+v, want %+v", cfg, test.cfg)
}

if test.anonymous != (auth == Anonymous) {
t.Fatalf("unexpected anonymous authenticator")
}
})
}
}
Expand Down

0 comments on commit ffc14a0

Please sign in to comment.