authprovider: fix a bug where registry-1.docker.io auth was always a cache miss

Signed-off-by: Nick Santos <nick.santos@docker.com>
This commit is contained in:
Nick Santos
2023-05-26 18:43:12 -04:00
parent 9fd591a7d5
commit 0fa754cde0
2 changed files with 38 additions and 3 deletions

View File

@@ -29,6 +29,8 @@ import (
)
const defaultExpiration = 60
const dockerIndexConfigfileKey = "https://index.docker.io/v1/"
const dockerRegistryHost = "registry-1.docker.io"
func NewDockerAuthProvider(cfg *configfile.ConfigFile) session.Attachable {
return &authProvider{
@@ -183,10 +185,12 @@ func (ap *authProvider) VerifyTokenAuthority(ctx context.Context, req *auth.Veri
func (ap *authProvider) getAuthConfig(host string) (*types.AuthConfig, error) {
ap.mu.Lock()
defer ap.mu.Unlock()
if host == dockerRegistryHost {
host = dockerIndexConfigfileKey
}
if _, exists := ap.authConfigCache[host]; !exists {
if host == "registry-1.docker.io" {
host = "https://index.docker.io/v1/"
}
ac, err := ap.config.GetAuthConfig(host)
if err != nil {
return nil, err

View File

@@ -0,0 +1,31 @@
package authprovider
import (
"context"
"testing"
"github.com/docker/cli/cli/config/configfile"
"github.com/docker/cli/cli/config/types"
"github.com/moby/buildkit/session/auth"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFetchTokenCaching(t *testing.T) {
cfg := &configfile.ConfigFile{
AuthConfigs: map[string]types.AuthConfig{
dockerIndexConfigfileKey: {Username: "user", RegistryToken: "hunter2"},
},
}
p := NewDockerAuthProvider(cfg).(*authProvider)
res, err := p.FetchToken(context.Background(), &auth.FetchTokenRequest{Host: dockerRegistryHost})
require.NoError(t, err)
assert.Equal(t, "hunter2", res.Token)
cfg.AuthConfigs[dockerIndexConfigfileKey] = types.AuthConfig{Username: "user", RegistryToken: "hunter3"}
res, err = p.FetchToken(context.Background(), &auth.FetchTokenRequest{Host: dockerRegistryHost})
require.NoError(t, err)
// Verify that we cached the result instead of returning hunter3.
assert.Equal(t, "hunter2", res.Token)
}