-
Notifications
You must be signed in to change notification settings - Fork 8
/
client.go
101 lines (82 loc) · 2.62 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package dockerutil
import (
"context"
"encoding/base64"
"encoding/json"
"os"
"github.com/cpuguy83/dockercfg"
dockertypes "github.com/docker/docker/api/types"
dockerclient "github.com/docker/docker/client"
"golang.org/x/xerrors"
)
type clientKey struct{}
// WithClient sets the provided DockerClient on the context.
// It should only be used for tests.
func WithClient(ctx context.Context, client DockerClient) context.Context {
return context.WithValue(ctx, clientKey{}, client)
}
// Client returns the DockerClient set on the context. If one can't be
// found a default client is returned.
func Client(ctx context.Context) (DockerClient, error) {
client := ctx.Value(clientKey{})
if client == nil {
client, err := dockerclient.NewClientWithOpts(dockerclient.FromEnv)
if err != nil {
return nil, xerrors.Errorf("new env client: %w", err)
}
return client, nil
}
//nolint we should panic if this isn't the case.
return client.(DockerClient), nil
}
type AuthConfig dockertypes.AuthConfig
func (a AuthConfig) Base64() (string, error) {
authStr, err := json.Marshal(a)
if err != nil {
return "", xerrors.Errorf("json marshal: %w", err)
}
return base64.URLEncoding.EncodeToString(authStr), nil
}
func AuthConfigFromPath(path string, registry string) (AuthConfig, error) {
var config dockercfg.Config
err := dockercfg.FromFile(path, &config)
if err != nil {
return AuthConfig{}, xerrors.Errorf("load config: %w", err)
}
hostname := dockercfg.ResolveRegistryHost(registry)
if config, ok := config.AuthConfigs[registry]; ok {
return AuthConfig(config), nil
}
username, secret, err := config.GetRegistryCredentials(hostname)
if err != nil {
return AuthConfig{}, xerrors.Errorf("get credentials from helper: %w", err)
}
if secret != "" {
if username == "" {
return AuthConfig{
IdentityToken: secret,
}, nil
}
return AuthConfig{
Username: username,
Password: secret,
}, nil
}
return AuthConfig{}, xerrors.Errorf("no auth config found for registry %s: %w", registry, os.ErrNotExist)
}
func ParseAuthConfig(raw string) (AuthConfig, error) {
type dockerConfig struct {
AuthConfigs map[string]dockertypes.AuthConfig `json:"auths"`
}
var conf dockerConfig
if err := json.Unmarshal([]byte(raw), &conf); err != nil {
return AuthConfig{}, xerrors.Errorf("parse docker auth config secret: %w", err)
}
if len(conf.AuthConfigs) != 1 {
return AuthConfig{}, xerrors.Errorf("number of image pull auth configs not equal to 1 (%d)", len(conf.AuthConfigs))
}
for _, regConfig := range conf.AuthConfigs {
return AuthConfig(regConfig), nil
}
return AuthConfig{}, xerrors.New("no auth configs parsed.")
}