Skip to content

Commit 2fddfaf

Browse files
jchangxclaude
andcommitted
Use Docker CLI's RetrieveAuthTokenFromImage for non-Hub registries
Replace hand-rolled credential encoding with command.RetrieveAuthTokenFromImage which correctly handles IdentityToken auth, base64url encoding, and hostname normalization — the same auth path docker pull uses natively. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 47b54b9 commit 2fddfaf

File tree

1 file changed

+9
-23
lines changed

1 file changed

+9
-23
lines changed

pkg/docker/images.go

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package docker
22

33
import (
44
"context"
5-
"encoding/base64"
6-
"encoding/json"
75
"fmt"
86
"io"
97
"os"
@@ -13,6 +11,7 @@ import (
1311

1412
cerrdefs "github.com/containerd/errdefs"
1513
"github.com/distribution/reference"
14+
"github.com/docker/cli/cli/command"
1615
"github.com/docker/docker/api/types/image"
1716
"golang.org/x/sync/errgroup"
1817
)
@@ -53,25 +52,6 @@ func (c *dockerClient) InspectImage(ctx context.Context, name string) (image.Ins
5352
return c.apiClient().ImageInspect(ctx, name)
5453
}
5554

56-
// getCredStoreAuth resolves registry credentials from the Docker CLI credential store.
57-
func (c *dockerClient) getCredStoreAuth(hostname string) string {
58-
if c.cli == nil {
59-
return ""
60-
}
61-
authConfig, err := c.cli.ConfigFile().GetAuthConfig(hostname)
62-
if err != nil || authConfig.Username == "" {
63-
return ""
64-
}
65-
buf, err := json.Marshal(map[string]string{
66-
"username": authConfig.Username,
67-
"password": authConfig.Password,
68-
})
69-
if err != nil {
70-
return ""
71-
}
72-
return base64.StdEncoding.EncodeToString(buf)
73-
}
74-
7555
func (c *dockerClient) pullImage(ctx context.Context, imageName string, registryAuthFn func() string) error {
7656
inspect, err := c.apiClient().ImageInspect(ctx, imageName)
7757
if err != nil && !cerrdefs.IsNotFound(err) {
@@ -99,8 +79,14 @@ func (c *dockerClient) pullImage(ctx context.Context, imageName string, registry
9979
var pullOptions image.PullOptions
10080
if strings.HasPrefix(ref.Name(), "docker.io/") {
10181
pullOptions.RegistryAuth = registryAuthFn()
102-
} else {
103-
pullOptions.RegistryAuth = c.getCredStoreAuth(reference.Domain(ref))
82+
} else if c.cli != nil {
83+
// For non-Hub registries (e.g. dhi.io), resolve credentials from the
84+
// Docker CLI credential store. This uses the same auth path as
85+
// "docker pull" and works with any configured credential helper.
86+
encodedAuth, err := command.RetrieveAuthTokenFromImage(c.cli.ConfigFile(), imageName)
87+
if err == nil {
88+
pullOptions.RegistryAuth = encodedAuth
89+
}
10490
}
10591

10692
response, err := c.apiClient().ImagePull(ctx, imageName, pullOptions)

0 commit comments

Comments
 (0)