Skip to content

Commit 9a4fada

Browse files
committed
fix: parse image auth config correctly
1 parent f609e64 commit 9a4fada

File tree

3 files changed

+41
-28
lines changed

3 files changed

+41
-28
lines changed

cli/docker.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,14 @@ func runDockerCVM(ctx context.Context, log slog.Logger, client dockerutil.Docker
367367
if err != nil {
368368
return xerrors.Errorf("set oom score: %w", err)
369369
}
370+
ref, err := name.NewTag(flags.innerImage)
371+
if err != nil {
372+
return xerrors.Errorf("parse ref: %w", err)
373+
}
370374

371375
var dockerAuth dockerutil.AuthConfig
372376
if flags.imagePullSecret != "" {
373-
dockerAuth, err = dockerutil.ParseAuthConfig(flags.imagePullSecret)
377+
dockerAuth, err = dockerutil.AuthConfigFromString(flags.imagePullSecret, ref.RegistryStr())
374378
if err != nil {
375379
return xerrors.Errorf("parse auth config: %w", err)
376380
}
@@ -379,10 +383,6 @@ func runDockerCVM(ctx context.Context, log slog.Logger, client dockerutil.Docker
379383
log.Info(ctx, "checking for docker config file", slog.F("path", flags.dockerConfig))
380384
if _, err := fs.Stat(flags.dockerConfig); err == nil {
381385
log.Info(ctx, "detected file", slog.F("image", flags.innerImage))
382-
ref, err := name.NewTag(flags.innerImage)
383-
if err != nil {
384-
return xerrors.Errorf("parse ref: %w", err)
385-
}
386386
dockerAuth, err = dockerutil.AuthConfigFromPath(flags.dockerConfig, ref.RegistryStr())
387387
if err != nil && !xerrors.Is(err, os.ErrNotExist) {
388388
return xerrors.Errorf("auth config from file: %w", err)

dockerutil/client.go

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,30 @@ func (a AuthConfig) Base64() (string, error) {
4848
return base64.URLEncoding.EncodeToString(authStr), nil
4949
}
5050

51-
func AuthConfigFromPath(path string, registry string) (AuthConfig, error) {
51+
func AuthConfigFromPath(path string, reg string) (AuthConfig, error) {
5252
var config dockercfg.Config
5353
err := dockercfg.FromFile(path, &config)
5454
if err != nil {
5555
return AuthConfig{}, xerrors.Errorf("load config: %w", err)
5656
}
5757

58-
hostname := dockercfg.ResolveRegistryHost(registry)
58+
return parseConfig(config, reg)
59+
}
5960

60-
if config, ok := config.AuthConfigs[registry]; ok {
61-
return AuthConfig(config), nil
61+
func AuthConfigFromString(raw string, reg string) (AuthConfig, error) {
62+
var cfg dockercfg.Config
63+
err := json.Unmarshal([]byte(raw), &cfg)
64+
if err != nil {
65+
return AuthConfig{}, xerrors.Errorf("parse config: %w", err)
6266
}
67+
return parseConfig(cfg, reg)
68+
}
69+
70+
func parseConfig(cfg dockercfg.Config, registry string) (AuthConfig, error) {
71+
72+
hostname := dockercfg.ResolveRegistryHost(registry)
6373

64-
username, secret, err := config.GetRegistryCredentials(hostname)
74+
username, secret, err := cfg.GetRegistryCredentials(hostname)
6575
if err != nil {
6676
return AuthConfig{}, xerrors.Errorf("get credentials from helper: %w", err)
6777
}
@@ -79,23 +89,5 @@ func AuthConfigFromPath(path string, registry string) (AuthConfig, error) {
7989
}
8090

8191
return AuthConfig{}, xerrors.Errorf("no auth config found for registry %s: %w", registry, os.ErrNotExist)
82-
}
83-
84-
func ParseAuthConfig(raw string) (AuthConfig, error) {
85-
type dockerConfig struct {
86-
AuthConfigs map[string]dockertypes.AuthConfig `json:"auths"`
87-
}
88-
89-
var conf dockerConfig
90-
if err := json.Unmarshal([]byte(raw), &conf); err != nil {
91-
return AuthConfig{}, xerrors.Errorf("parse docker auth config secret: %w", err)
92-
}
93-
if len(conf.AuthConfigs) != 1 {
94-
return AuthConfig{}, xerrors.Errorf("number of image pull auth configs not equal to 1 (%d)", len(conf.AuthConfigs))
95-
}
96-
for _, regConfig := range conf.AuthConfigs {
97-
return AuthConfig(regConfig), nil
98-
}
9992

100-
return AuthConfig{}, xerrors.New("no auth configs parsed.")
10193
}

dockerutil/client_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package dockerutil_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/coder/envbox/dockerutil"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestAuthConfigFromString(t *testing.T) {
11+
t.Parallel()
12+
13+
creds := `{ "auths": { "docker.registry.test": { "auth": "Zm9vQGJhci5jb206YWJjMTIzCg==" } } }`
14+
expectedUsername := "[email protected]"
15+
expectedPassword := "abc123"
16+
17+
cfg, err := dockerutil.AuthConfigFromString(creds, "docker.registry.test")
18+
require.NoError(t, err)
19+
require.Equal(t, expectedUsername, cfg.Username)
20+
require.Equal(t, expectedPassword, cfg.Password)
21+
}

0 commit comments

Comments
 (0)