Skip to content

Commit 1f871f6

Browse files
committed
simplify unmarshaling, and other nits
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 63c990c commit 1f871f6

File tree

2 files changed

+40
-34
lines changed

2 files changed

+40
-34
lines changed

cli/config/configfile/file.go

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -49,34 +49,28 @@ type ConfigFile struct {
4949
}
5050

5151
type configEnvAuth struct {
52-
Auth string `json:"auth"`
52+
Auth string `json:"auth,omitempty"`
5353
}
5454

5555
type 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
}

cli/config/configfile/file_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,10 +495,25 @@ const envTestAuthConfig = `{
495495
"auths": {
496496
"env.example.test": {
497497
"auth": "ZW52X3VzZXI6ZW52X3Bhc3M="
498-
}
498+
},
499+
"https://index.docker.io/v1/": {},
500+
"https://index.docker.io/v1/access-token": {},
501+
"https://index.docker.io/v1/refresh-token": {}
499502
}
500503
}`
501504

505+
const mixedAuthConfig = `{
506+
"auths": {
507+
"env.example.test": {
508+
"auth": "ZW52X3VzZXI6ZW52X3Bhc3M="
509+
},
510+
"https://index.docker.io/v1/": {},
511+
"https://index.docker.io/v1/access-token": {},
512+
"https://index.docker.io/v1/refresh-token": {}
513+
}
514+
}
515+
`
516+
502517
func TestGetAllCredentialsFromEnvironment(t *testing.T) {
503518
t.Run("case=can parse DOCKER_AUTH_CONFIG auth field", func(t *testing.T) {
504519
config := &ConfigFile{}

0 commit comments

Comments
 (0)