/
docker.go
147 lines (119 loc) · 3.25 KB
/
docker.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package credhelper
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/remotes/docker"
helperclient "github.com/docker/docker-credential-helpers/client"
"github.com/mitchellh/go-homedir"
log "github.com/sirupsen/logrus"
)
const (
DockerConfigEnv = "DOCKER_CONFIG"
DefaultDockerConfigName = "config.json"
)
func GetConfigDir() (string, error) {
var base string
var err error
if val := os.Getenv(DockerConfigEnv); val != "" {
base = val
} else {
base, err = homedir.Dir()
if err != nil {
return "", err
}
base = filepath.Join(base, ".docker")
}
return filepath.Join(base, DefaultDockerConfigName), nil
}
type DockerConfig struct {
CredentialHelpers map[string]string `json:"credHelpers"`
}
func ReadDockerConfig(re io.Reader) (DockerConfig, error) {
var cfg DockerConfig
err := json.NewDecoder(re).Decode(&cfg)
if err != nil {
return DockerConfig{}, err
}
if cfg.CredentialHelpers == nil {
cfg.CredentialHelpers = make(map[string]string)
}
return cfg, nil
}
func ReadHostDockerConfig() (DockerConfig, error) {
dir, err := GetConfigDir()
if err != nil {
return DockerConfig{}, err
}
f, err := os.Open(dir)
if err != nil {
return DockerConfig{}, err
}
defer f.Close()
return ReadDockerConfig(f)
}
func seedAuthHeaders(host docker.RegistryHost) error {
if host.Authorizer == nil {
return nil
}
cli := http.DefaultClient
if host.Client != nil {
cli = host.Client
}
v2URL := fmt.Sprintf("%s://%s%s/", host.Scheme, host.Host, host.Path)
resp, err := cli.Get(v2URL)
if err != nil {
return err
}
if resp.StatusCode == http.StatusUnauthorized {
log.WithField("url", v2URL).Debug("seeding authorization headers")
err = host.Authorizer.AddResponses(context.Background(), []*http.Response{resp})
if err != nil && !errdefs.IsNotImplemented(err) {
return err
}
}
return nil
}
func RegistryHostsFromDockerConfig() docker.RegistryHosts {
return func(host string) ([]docker.RegistryHost, error) {
// FIXME This should be cached somewhere
cfg, err := ReadHostDockerConfig()
if err != nil {
return nil, err
}
// Don't error if the file doesn't exist
if os.IsNotExist(err) {
return nil, nil
}
registryHost := docker.RegistryHost{
Host: host,
Scheme: "https",
Path: "/v2",
Capabilities: docker.HostCapabilityPull | docker.HostCapabilityResolve | docker.HostCapabilityPush,
}
helperName, ok := cfg.CredentialHelpers[host]
if !ok {
// If no credential helper is specified, fall back on the default behavior.
registryHost.Authorizer = docker.NewDockerAuthorizer()
return []docker.RegistryHost{registryHost}, nil
}
registryHost.Authorizer = docker.NewDockerAuthorizer(docker.WithAuthCreds(func(host string) (string, string, error) {
p := helperclient.NewShellProgramFunc(fmt.Sprintf("docker-credential-%s", helperName))
creds, err := helperclient.Get(p, fmt.Sprintf("%s://%s", registryHost.Scheme, registryHost.Host))
if err != nil {
return "", "", err
}
return creds.Username, creds.Secret, nil
}))
err = seedAuthHeaders(registryHost)
if err != nil {
return nil, err
}
return []docker.RegistryHost{registryHost}, nil
}
}