Skip to content

Commit 60d134b

Browse files
authored
Merge pull request moby#3910 from nicks/nicks/auth
authprovider: fix a bug where registry-1.docker.io auth was always a cache miss
2 parents 36917a9 + c501213 commit 60d134b

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

session/auth/authprovider/authprovider.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import (
2929
)
3030

3131
const defaultExpiration = 60
32+
const dockerHubConfigfileKey = "https://index.docker.io/v1/"
33+
const dockerHubRegistryHost = "registry-1.docker.io"
3234

3335
func NewDockerAuthProvider(cfg *configfile.ConfigFile) session.Attachable {
3436
return &authProvider{
@@ -183,10 +185,12 @@ func (ap *authProvider) VerifyTokenAuthority(ctx context.Context, req *auth.Veri
183185
func (ap *authProvider) getAuthConfig(host string) (*types.AuthConfig, error) {
184186
ap.mu.Lock()
185187
defer ap.mu.Unlock()
188+
189+
if host == dockerHubRegistryHost {
190+
host = dockerHubConfigfileKey
191+
}
192+
186193
if _, exists := ap.authConfigCache[host]; !exists {
187-
if host == "registry-1.docker.io" {
188-
host = "https://index.docker.io/v1/"
189-
}
190194
ac, err := ap.config.GetAuthConfig(host)
191195
if err != nil {
192196
return nil, err
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package authprovider
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/docker/cli/cli/config/configfile"
8+
"github.com/docker/cli/cli/config/types"
9+
"github.com/moby/buildkit/session/auth"
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func TestFetchTokenCaching(t *testing.T) {
15+
cfg := &configfile.ConfigFile{
16+
AuthConfigs: map[string]types.AuthConfig{
17+
dockerHubConfigfileKey: {Username: "user", RegistryToken: "hunter2"},
18+
},
19+
}
20+
p := NewDockerAuthProvider(cfg).(*authProvider)
21+
res, err := p.FetchToken(context.Background(), &auth.FetchTokenRequest{Host: dockerHubRegistryHost})
22+
require.NoError(t, err)
23+
assert.Equal(t, "hunter2", res.Token)
24+
25+
cfg.AuthConfigs[dockerHubConfigfileKey] = types.AuthConfig{Username: "user", RegistryToken: "hunter3"}
26+
res, err = p.FetchToken(context.Background(), &auth.FetchTokenRequest{Host: dockerHubRegistryHost})
27+
require.NoError(t, err)
28+
29+
// Verify that we cached the result instead of returning hunter3.
30+
assert.Equal(t, "hunter2", res.Token)
31+
}

0 commit comments

Comments
 (0)