This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
/
auth.go
199 lines (174 loc) · 6.02 KB
/
auth.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package docker
import (
"encoding/base64"
"encoding/json"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/docker/cli/cli/config"
"github.com/docker/cli/cli/config/types"
"github.com/docker/distribution/reference"
"github.com/hashicorp/go-hclog"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// CredentialsFromConfig returns the username and password present in the encoded
// auth string. This encoded auth string is one that users can pass as authentication
// information to registry.
func CredentialsFromConfig(encodedAuth string) (string, string, error) {
// Create a reader that base64 decodes our encoded auth and then
// JSON decodes that.
var authCfg types.AuthConfig
dec := json.NewDecoder(
base64.NewDecoder(base64.URLEncoding, strings.NewReader(encodedAuth)),
)
if err := dec.Decode(&authCfg); err != nil {
return "", "", status.Errorf(codes.FailedPrecondition,
"Failed to decode encoded_auth: %s", err)
}
return authCfg.Username, authCfg.Password, nil
}
// TempDockerConfig creates a new Docker configuration with the
// configured auth in it. It saves this Docker config to a temporary path
// and returns the path to that Docker file.
//
// We have to do this because `img` doesn't support setting auth for
// a single operation. Therefore, we must set auth in the Docker config,
// but we don't want to pollute any concurrent runs or the main file. So
// we create a copy.
//
// This can return ("", nil) if there is no custom Docker config necessary.
//
// Callers should defer file deletion for this temporary file.
func TempDockerConfig(
log hclog.Logger,
target *Image,
encodedAuth string,
) (string, error) {
if encodedAuth == "" {
return "", nil
}
// Create a reader that base64 decodes our encoded auth and then
// JSON decodes that.
var authCfg types.AuthConfig
var rdr io.Reader = strings.NewReader(encodedAuth)
rdr = base64.NewDecoder(base64.URLEncoding, rdr)
dec := json.NewDecoder(rdr)
if err := dec.Decode(&authCfg); err != nil {
return "", status.Errorf(codes.FailedPrecondition,
"Failed to decode encoded_auth: %s", err)
}
// Determine the host that we're setting auth for. We have to parse the
// image for this cause it may not contain a host. Luckily Docker has
// libs to normalize this all for us.
log.Trace("determining host for auth configuration", "image", target.Name())
ref, err := reference.ParseNormalizedNamed(target.Name())
if err != nil {
return "", status.Errorf(codes.Internal, "unable to parse image name: %s", err)
}
host := reference.Domain(ref)
log.Trace("auth host", "host", host)
// Parse our old Docker config and add the auth.
log.Trace("loading Docker configuration")
file, err := config.Load(config.Dir())
if err != nil {
return "", err
}
if file.AuthConfigs == nil {
file.AuthConfigs = map[string]types.AuthConfig{}
}
file.AuthConfigs[host] = authCfg
// Create a temporary directory for our config
td, err := ioutil.TempDir("", "wp-docker-config")
if err != nil {
return "", status.Errorf(codes.Internal,
"Failed to create temporary directory for Docker config: %s", err)
}
// Create a temporary file and write our Docker config to it
f, err := os.Create(filepath.Join(td, "config.json"))
if err != nil {
return "", status.Errorf(codes.Internal,
"Failed to create temporary file for Docker config: %s", err)
}
defer f.Close()
if err := file.SaveToWriter(f); err != nil {
return "", status.Errorf(codes.Internal,
"Failed to create temporary file for Docker config: %s", err)
}
log.Info("temporary Docker config created for auth",
"auth_host", host,
"path", td,
)
return td, nil
}
// TempDockerConfigWithPassword creates a new Docker configuration with the
// configured auth in it. It saves this Docker config to a temporary path
// and returns the path to that Docker file.
//
// We have to do this because `img` doesn't support setting auth for
// a single operation. Therefore, we must set auth in the Docker config,
// but we don't want to pollute any concurrent runs or the main file. So
// we create a copy.
//
// This can return ("", nil) if there is no custom Docker config necessary.
//
// Callers should defer file deletion for this temporary file.
func TempDockerConfigWithPassword(
log hclog.Logger,
target *Image,
username string,
password string,
) (string, error) {
if password == "" {
return "", nil
}
// Create a reader that base64 decodes our encoded auth and then
// JSON decodes that.
var authCfg types.AuthConfig
authCfg.Username = username
authCfg.Password = password
// Determine the host that we're setting auth for. We have to parse the
// image for this cause it may not contain a host. Luckily Docker has
// libs to normalize this all for us.
log.Trace("determining host for auth configuration", "image", target.Name())
ref, err := reference.ParseNormalizedNamed(target.Name())
if err != nil {
return "", status.Errorf(codes.Internal, "unable to parse image name: %s", err)
}
host := reference.Domain(ref)
log.Trace("auth host", "host", host)
// Parse our old Docker config and add the auth.
log.Trace("loading Docker configuration")
file, err := config.Load(config.Dir())
if err != nil {
return "", err
}
if file.AuthConfigs == nil {
file.AuthConfigs = map[string]types.AuthConfig{}
}
file.AuthConfigs[host] = authCfg
// Create a temporary directory for our config
td, err := ioutil.TempDir("", "wp-docker-config")
if err != nil {
return "", status.Errorf(codes.Internal,
"Failed to create temporary directory for Docker config: %s", err)
}
// Create a temporary file and write our Docker config to it
f, err := os.Create(filepath.Join(td, "config.json"))
if err != nil {
return "", status.Errorf(codes.Internal,
"Failed to create temporary file for Docker config: %s", err)
}
defer f.Close()
if err := file.SaveToWriter(f); err != nil {
return "", status.Errorf(codes.Internal,
"Failed to create temporary file for Docker config: %s", err)
}
log.Info("temporary Docker config created for auth",
"auth_host", host,
"path", td,
)
return td, nil
}