@@ -49,34 +49,28 @@ type ConfigFile struct {
4949}
5050
5151type configEnvAuth struct {
52- Auth string `json:"auth"`
52+ Auth string `json:"auth,omitempty "`
5353}
5454
5555type configEnv struct {
5656 AuthConfigs map [string ]configEnvAuth `json:"auths"`
5757}
5858
59- var errDockerAuthConfigNotSet = errors .New ("DOCKER_AUTH_CONFIG environment variable is not set" )
60-
61- func (c * configEnv ) LoadFromEnv () error {
62- v := os .Getenv ("DOCKER_AUTH_CONFIG" )
59+ func unmarshalJSONAuth (v string ) (map [string ]types.AuthConfig , error ) {
6360 if v == "" {
64- return errDockerAuthConfigNotSet
65- }
66- if c .AuthConfigs == nil {
67- c .AuthConfigs = make (map [string ]configEnvAuth )
68- }
69- if err := json .NewDecoder (strings .NewReader (v )).Decode (c ); err != nil && ! errors .Is (err , io .EOF ) {
70- return err
61+ return nil , errors .New ("value is empty" )
7162 }
72- return nil
73- }
7463
75- func (c * configEnv ) GetAuthConfigs () (map [string ]types.AuthConfig , error ) {
64+ var c configEnv
65+ if err := json .Unmarshal ([]byte (v ), & c ); err != nil {
66+ return nil , err
67+ }
7668 authConfigs := make (map [string ]types.AuthConfig )
7769 for addr , envAuth := range c .AuthConfigs {
7870 if envAuth .Auth == "" {
79- return authConfigs , fmt .Errorf ("DOCKER_AUTH_CONFIG environment variable is missing auth for %s" , addr )
71+ // auths may contain empty configs, which could be present
72+ // if a credentials-helper is/was used for storing.
73+ continue
8074 }
8175 username , password , err := decodeAuth (envAuth .Auth )
8276 if err != nil {
@@ -314,37 +308,34 @@ func (configFile *ConfigFile) GetCredentialsStore(registryHostname string) crede
314308 store = newNativeStore (configFile , helper )
315309 }
316310
317- // use DOCKER_AUTH_CONFIG if set
318- // if a parse error occurs it falls back to the native or file store
311+ // use DOCKER_AUTH_CONFIG if set.
312+ v := os .Getenv ("DOCKER_AUTH_CONFIG" )
313+ if v == "" {
314+ return store
315+ }
319316 envStore , err := memorystore .New (
320- withEnvConfig (),
317+ withEnvConfig (v ),
321318 memorystore .WithFallbackStore (store ),
322319 )
323320 if err != nil {
324321 _ , _ = fmt .Fprintln (os .Stderr , "Failed to create credential store from DOCKER_AUTH_CONFIG: " , err )
322+
323+ // fall back to the native or file store.
325324 return store
326325 }
327326
328327 return envStore
329328}
330329
331- func withEnvConfig () memorystore.Options {
330+ // withEnvConfig configures the memory store with a static auth-config
331+ // from a JSON-encoded auth-config, usually taken from a "DOCKER_AUTH_CONFIG"
332+ // environment variable.
333+ func withEnvConfig (v string ) memorystore.Options {
332334 return func (c * memorystore.Config ) error {
333- envConfig := & configEnv {}
334- err := envConfig .LoadFromEnv ()
335+ authConfigs , err := unmarshalJSONAuth (v )
335336 if err != nil {
336- // ignore if DOCKER_AUTH_CONFIG is not set
337- if errors .Is (err , errDockerAuthConfigNotSet ) {
338- return nil
339- }
340- return err
337+ return fmt .Errorf ("unmarshaling auth-config: %w" , err )
341338 }
342-
343- authConfigs , err := envConfig .GetAuthConfigs ()
344- if err != nil {
345- return err
346- }
347-
348339 return memorystore .WithAuthConfig (authConfigs )(c )
349340 }
350341}
0 commit comments