Skip to content

Commit b30b0de

Browse files
authored
Supporting changes for new docker client (#2344)
* integration test for secrets * move credentials logic off command.Command the credential stuff isn't tied to the docker client. This moves logic to helper functions that both clients can use * move CreateTarFile & CreateAptTarFile to helpers The monobase helpers from command.Command were calling docker run, instead have them call the run functions on the client since the logic doesn't depend on client implementation * error helpers helpers to map errors from different backends to known types. needed to support both clients * more docker integration tests * Update go.mod * fix for merge flub and correct new client func name * remove some code that's only used in the sdk client
1 parent 63b6246 commit b30b0de

21 files changed

Lines changed: 1012 additions & 359 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ require (
2929
github.com/vincent-petithory/dataurl v1.0.0
3030
github.com/xeipuuv/gojsonschema v1.2.0
3131
github.com/xeonx/timeago v1.0.0-rc5
32+
golang.org/x/crypto v0.37.0
3233
golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0
3334
golang.org/x/sync v0.14.0
3435
golang.org/x/sys v0.33.0
@@ -272,7 +273,6 @@ require (
272273
go.uber.org/automaxprocs v1.6.0 // indirect
273274
go.uber.org/multierr v1.11.0 // indirect
274275
go.uber.org/zap v1.27.0 // indirect
275-
golang.org/x/crypto v0.37.0 // indirect
276276
golang.org/x/exp/typeparams v0.0.0-20250210185358-939b2ce775ac // indirect
277277
golang.org/x/mod v0.24.0 // indirect
278278
golang.org/x/net v0.39.0 // indirect

pkg/docker/apt.go

Lines changed: 0 additions & 81 deletions
This file was deleted.

pkg/docker/command/command.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ type Command interface {
1515
Pull(ctx context.Context, ref string, force bool) (*image.InspectResponse, error)
1616
Push(ctx context.Context, ref string) error
1717
LoadUserInformation(ctx context.Context, registryHost string) (*UserInfo, error)
18-
CreateTarFile(ctx context.Context, ref string, tmpDir string, tarFile string, folder string) (string, error)
19-
CreateAptTarFile(ctx context.Context, tmpDir string, aptTarFile string, packages ...string) (string, error)
2018
Inspect(ctx context.Context, ref string) (*image.InspectResponse, error)
2119
ImageExists(ctx context.Context, ref string) (bool, error)
2220
ContainerLogs(ctx context.Context, containerID string, w io.Writer) error

pkg/docker/command/errors.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ func (e *NotFoundError) Is(target error) bool {
2929
func IsNotFoundError(err error) bool {
3030
return errors.Is(err, &NotFoundError{})
3131
}
32+
33+
var ErrAuthorizationFailed = errors.New("authorization failed")

pkg/docker/credentials.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package docker
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"io"
8+
"os"
9+
"os/exec"
10+
"strings"
11+
12+
"github.com/docker/cli/cli/config"
13+
"github.com/docker/cli/cli/config/configfile"
14+
"github.com/docker/cli/cli/config/types"
15+
16+
"github.com/replicate/cog/pkg/docker/command"
17+
"github.com/replicate/cog/pkg/util/console"
18+
)
19+
20+
func loadUserInformation(ctx context.Context, registryHost string) (*command.UserInfo, error) {
21+
conf := config.LoadDefaultConfigFile(os.Stderr)
22+
credsStore := conf.CredentialsStore
23+
if credsStore == "" {
24+
authConf, err := loadAuthFromConfig(conf, registryHost)
25+
if err != nil {
26+
return nil, err
27+
}
28+
return &command.UserInfo{
29+
Token: authConf.Password,
30+
Username: authConf.Username,
31+
}, nil
32+
}
33+
credsHelper, err := loadAuthFromCredentialsStore(ctx, credsStore, registryHost)
34+
if err != nil {
35+
return nil, err
36+
}
37+
return &command.UserInfo{
38+
Token: credsHelper.Secret,
39+
Username: credsHelper.Username,
40+
}, nil
41+
}
42+
43+
func loadAuthFromConfig(conf *configfile.ConfigFile, registryHost string) (types.AuthConfig, error) {
44+
return conf.AuthConfigs[registryHost], nil
45+
}
46+
47+
func loadAuthFromCredentialsStore(ctx context.Context, credsStore string, registryHost string) (*CredentialHelperInput, error) {
48+
var out strings.Builder
49+
binary := dockerCredentialBinary(credsStore)
50+
cmd := exec.CommandContext(ctx, binary, "get")
51+
cmd.Env = os.Environ()
52+
cmd.Stdout = &out
53+
cmd.Stderr = &out
54+
stdin, err := cmd.StdinPipe()
55+
if err != nil {
56+
return nil, err
57+
}
58+
defer stdin.Close()
59+
console.Debug("$ " + strings.Join(cmd.Args, " "))
60+
err = cmd.Start()
61+
if err != nil {
62+
return nil, err
63+
}
64+
_, err = io.WriteString(stdin, registryHost)
65+
if err != nil {
66+
return nil, err
67+
}
68+
err = stdin.Close()
69+
if err != nil {
70+
return nil, err
71+
}
72+
err = cmd.Wait()
73+
if err != nil {
74+
return nil, fmt.Errorf("exec wait error: %w", err)
75+
}
76+
77+
var config CredentialHelperInput
78+
err = json.Unmarshal([]byte(out.String()), &config)
79+
if err != nil {
80+
return nil, err
81+
}
82+
83+
return &config, nil
84+
}
85+
86+
func dockerCredentialBinary(credsStore string) string {
87+
return "docker-credential-" + credsStore
88+
}

0 commit comments

Comments
 (0)