Skip to content

Commit 32e4735

Browse files
authored
Merge pull request #3627 from tonistiigi/imagetools-auth
imagetools: use dockerconfig for auth
2 parents 0a62a9e + 27dde04 commit 32e4735

File tree

7 files changed

+384
-76
lines changed

7 files changed

+384
-76
lines changed

driver/driver.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/docker/buildx/store"
1010
"github.com/docker/buildx/util/progress"
11-
clitypes "github.com/docker/cli/cli/config/types"
1211
controlapi "github.com/moby/buildkit/api/services/control"
1312
"github.com/moby/buildkit/client"
1413
"github.com/pkg/errors"
@@ -58,10 +57,6 @@ type Info struct {
5857
DynamicNodes []store.Node
5958
}
6059

61-
type Auth interface {
62-
GetAuthConfig(registryHostname string) (clitypes.AuthConfig, error)
63-
}
64-
6560
type Driver interface {
6661
Factory() Factory
6762
Bootstrap(context.Context, progress.Logger) error

driver/manager.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/docker/cli/cli/context/store"
99
"github.com/moby/buildkit/client"
10+
"github.com/moby/buildkit/session/auth/authprovider"
1011
"github.com/moby/buildkit/util/tracing/delegated"
1112
dockerclient "github.com/moby/moby/client"
1213
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
@@ -35,7 +36,7 @@ type InitConfig struct {
3536
BuildkitdFlags []string
3637
Files map[string][]byte
3738
DriverOpts map[string]string
38-
Auth Auth
39+
Auth authprovider.AuthConfigProvider
3940
Platforms []ocispecs.Platform
4041
ContextPathHash string
4142
DialMeta map[string][]string

store/storeutil/storeutil.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/docker/buildx/store"
99
"github.com/docker/buildx/util/confutil"
1010
"github.com/docker/buildx/util/dockerutil"
11+
"github.com/docker/buildx/util/dockerutil/dockerconfig"
1112
"github.com/docker/buildx/util/imagetools"
1213
"github.com/docker/buildx/util/resolver"
1314
"github.com/docker/cli/cli/command"
@@ -109,7 +110,7 @@ func GetNodeGroup(txn *store.Txn, dockerCli command.Cli, name string) (*store.No
109110
}
110111

111112
func GetImageConfig(dockerCli command.Cli, ng *store.NodeGroup) (opt imagetools.Opt, err error) {
112-
opt.Auth = dockerCli.ConfigFile()
113+
opt.Auth = dockerconfig.LoadAuthConfig(dockerCli)
113114

114115
if ng == nil || len(ng.Nodes) == 0 {
115116
return opt, nil

util/dockerutil/dockerconfig/configprovider.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ type authConfigProvider struct {
4343
}
4444

4545
func (ap *authConfigProvider) load(ctx context.Context, host string, scopes []string, cacheExpireCheck authprovider.ExpireCachedAuthCheck) (types.AuthConfig, error) {
46+
if cacheExpireCheck == nil {
47+
cacheExpireCheck = func(created time.Time, _ string) bool {
48+
// Tokens for Google Artifact Registry via Workload Identity expire after 5 minutes.
49+
return time.Since(created) > 4*time.Minute+50*time.Second
50+
}
51+
}
52+
4653
ac, err := ap.loadHost(ctx, host, scopes, cacheExpireCheck)
4754
if err != nil {
4855
return types.AuthConfig{}, err

util/imagetools/auth.go

Lines changed: 6 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5,68 +5,14 @@ import (
55
"encoding/base64"
66
"encoding/json"
77
"net/http"
8-
"sync"
9-
"time"
108

119
"github.com/containerd/containerd/v2/core/remotes/docker"
1210
"github.com/distribution/reference"
13-
"github.com/docker/cli/cli/config/types"
11+
"github.com/moby/buildkit/session/auth/authprovider"
1412
)
1513

16-
type authConfig struct {
17-
mu sync.Mutex
18-
authConfigCache map[string]authConfigCacheEntry
19-
cfg Auth
20-
}
21-
22-
type authConfigCacheEntry struct {
23-
Created time.Time
24-
Auth types.AuthConfig
25-
}
26-
27-
func newAuthConfig(a Auth) *authConfig {
28-
return &authConfig{
29-
authConfigCache: map[string]authConfigCacheEntry{},
30-
cfg: a,
31-
}
32-
}
33-
34-
func (a *authConfig) credentials(host string) (string, string, error) {
35-
ac, err := a.authConfig(host)
36-
if err != nil {
37-
return "", "", err
38-
}
39-
if ac.IdentityToken != "" {
40-
return "", ac.IdentityToken, nil
41-
}
42-
return ac.Username, ac.Password, nil
43-
}
44-
45-
func (a *authConfig) authConfig(host string) (types.AuthConfig, error) {
46-
const defaultExpiration = 2 * time.Minute
47-
48-
if host == "registry-1.docker.io" {
49-
host = "https://index.docker.io/v1/"
50-
}
51-
a.mu.Lock()
52-
defer a.mu.Unlock()
53-
54-
if c, ok := a.authConfigCache[host]; ok && time.Since(c.Created) <= defaultExpiration {
55-
return c.Auth, nil
56-
}
57-
ac, err := a.cfg.GetAuthConfig(host)
58-
if err != nil {
59-
return types.AuthConfig{}, err
60-
}
61-
a.authConfigCache[host] = authConfigCacheEntry{
62-
Created: time.Now(),
63-
Auth: ac,
64-
}
65-
return ac, nil
66-
}
67-
68-
func RegistryAuthForRef(ref string, a Auth) (string, error) {
69-
if a == nil {
14+
func RegistryAuthForRef(ref string, auth authprovider.AuthConfigProvider) (string, error) {
15+
if auth == nil {
7016
return "", nil
7117
}
7218
r, err := parseRef(ref)
@@ -77,7 +23,7 @@ func RegistryAuthForRef(ref string, a Auth) (string, error) {
7723
if host == "docker.io" {
7824
host = "https://index.docker.io/v1/"
7925
}
80-
ac, err := a.GetAuthConfig(host)
26+
ac, err := auth(context.TODO(), host, nil, nil)
8127
if err != nil {
8228
return "", err
8329
}
@@ -90,11 +36,11 @@ func RegistryAuthForRef(ref string, a Auth) (string, error) {
9036

9137
type withBearerAuthorizer struct {
9238
docker.Authorizer
93-
AuthConfig *authConfig
39+
AuthConfig authprovider.AuthConfigProvider
9440
}
9541

9642
func (a *withBearerAuthorizer) Authorize(ctx context.Context, req *http.Request) error {
97-
ac, err := a.AuthConfig.authConfig(req.Host)
43+
ac, err := a.AuthConfig(ctx, req.Host, nil, nil)
9844
if err == nil && ac.RegistryToken != "" {
9945
req.Header.Set("Authorization", "Bearer "+ac.RegistryToken)
10046
return nil

util/imagetools/inspect.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,16 @@ import (
1111
"github.com/containerd/log"
1212
"github.com/distribution/reference"
1313
"github.com/docker/buildx/util/resolver"
14-
clitypes "github.com/docker/cli/cli/config/types"
14+
"github.com/docker/buildx/util/resolver/auth"
15+
"github.com/moby/buildkit/session/auth/authprovider"
1516
"github.com/moby/buildkit/util/contentutil"
1617
"github.com/moby/buildkit/util/tracing"
1718
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
1819
"github.com/sirupsen/logrus"
1920
)
2021

21-
type Auth interface {
22-
GetAuthConfig(registryHostname string) (clitypes.AuthConfig, error)
23-
}
24-
2522
type Opt struct {
26-
Auth Auth
23+
Auth authprovider.AuthConfigProvider
2724
RegistryConfig map[string]resolver.RegistryConfig
2825
}
2926

@@ -34,11 +31,10 @@ type Resolver struct {
3431
}
3532

3633
func New(opt Opt) *Resolver {
37-
ac := newAuthConfig(opt.Auth)
38-
dockerAuth := docker.NewDockerAuthorizer(docker.WithAuthCreds(ac.credentials), docker.WithAuthClient(http.DefaultClient))
34+
dockerAuth := auth.NewDockerAuthorizer(auth.WithAuthProvider(opt.Auth), auth.WithAuthClient(http.DefaultClient))
3935
auth := &withBearerAuthorizer{
4036
Authorizer: dockerAuth,
41-
AuthConfig: ac,
37+
AuthConfig: opt.Auth,
4238
}
4339
return &Resolver{
4440
auth: auth,

0 commit comments

Comments
 (0)