mirror of
https://github.com/moby/buildkit.git
synced 2026-08-07 00:00:52 +00:00
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:
@@ -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
|
||||
|
||||
31
session/auth/authprovider/authprovider_test.go
Normal file
31
session/auth/authprovider/authprovider_test.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user