@@ -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
9137type withBearerAuthorizer struct {
9238 docker.Authorizer
93- AuthConfig * authConfig
39+ AuthConfig authprovider. AuthConfigProvider
9440}
9541
9642func (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
0 commit comments