@@ -38,18 +38,43 @@ const (
38
38
dockerHubRegistryHost = "registry-1.docker.io"
39
39
)
40
40
41
- func NewDockerAuthProvider (cfg * configfile.ConfigFile , tlsConfigs map [string ]* AuthTLSConfig ) session.Attachable {
41
+ type DockerAuthProviderConfig struct {
42
+ // ConfigFile is the docker config file
43
+ ConfigFile * configfile.ConfigFile
44
+ // TLSConfigs is a map of host to TLS config
45
+ TLSConfigs map [string ]* AuthTLSConfig
46
+ // ExpireCachedAuth is a function that returns true auth config should be refreshed
47
+ // instead of using a pre-cached result.
48
+ // If nil then the cached result will expire after 10 minutes.
49
+ // The function is called with the time the cached auth config was created
50
+ // and the server URL the auth config is for.
51
+ ExpireCachedAuth func (created time.Time , serverURL string ) bool
52
+ }
53
+
54
+ type authConfigCacheEntry struct {
55
+ Created time.Time
56
+ Auth * types.AuthConfig
57
+ }
58
+
59
+ func NewDockerAuthProvider (cfg DockerAuthProviderConfig ) session.Attachable {
60
+ if cfg .ExpireCachedAuth == nil {
61
+ cfg .ExpireCachedAuth = func (created time.Time , _ string ) bool {
62
+ return time .Since (created ) > 10 * time .Minute
63
+ }
64
+ }
42
65
return & authProvider {
43
- authConfigCache : map [string ]* types.AuthConfig {},
44
- config : cfg ,
66
+ authConfigCache : map [string ]authConfigCacheEntry {},
67
+ expireAc : cfg .ExpireCachedAuth ,
68
+ config : cfg .ConfigFile ,
45
69
seeds : & tokenSeeds {dir : config .Dir ()},
46
70
loggerCache : map [string ]struct {}{},
47
- tlsConfigs : tlsConfigs ,
71
+ tlsConfigs : cfg . TLSConfigs ,
48
72
}
49
73
}
50
74
51
75
type authProvider struct {
52
- authConfigCache map [string ]* types.AuthConfig
76
+ authConfigCache map [string ]authConfigCacheEntry
77
+ expireAc func (time.Time , string ) bool
53
78
config * configfile.ConfigFile
54
79
seeds * tokenSeeds
55
80
logger progresswriter.Logger
@@ -247,17 +272,25 @@ func (ap *authProvider) getAuthConfig(ctx context.Context, host string) (*types.
247
272
host = dockerHubConfigfileKey
248
273
}
249
274
250
- if _ , exists := ap .authConfigCache [host ]; ! exists {
251
- span , _ := tracing .StartSpan (ctx , fmt .Sprintf ("load credentials for %s" , host ))
252
- ac , err := ap .config .GetAuthConfig (host )
253
- tracing .FinishWithError (span , err )
254
- if err != nil {
255
- return nil , err
256
- }
257
- ap .authConfigCache [host ] = & ac
275
+ entry , exists := ap .authConfigCache [host ]
276
+ if exists && ! ap .expireAc (entry .Created , host ) {
277
+ return entry .Auth , nil
258
278
}
259
279
260
- return ap .authConfigCache [host ], nil
280
+ span , _ := tracing .StartSpan (ctx , fmt .Sprintf ("load credentials for %s" , host ))
281
+ ac , err := ap .config .GetAuthConfig (host )
282
+ tracing .FinishWithError (span , err )
283
+ if err != nil {
284
+ return nil , err
285
+ }
286
+ entry = authConfigCacheEntry {
287
+ Created : time .Now (),
288
+ Auth : & ac ,
289
+ }
290
+
291
+ ap .authConfigCache [host ] = entry
292
+
293
+ return entry .Auth , nil
261
294
}
262
295
263
296
func (ap * authProvider ) getAuthorityKey (ctx context.Context , host string , salt []byte ) (ed25519.PrivateKey , error ) {
0 commit comments