-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
271 lines (231 loc) · 7.27 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright 2021 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package docker
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"reflect"
"time"
"github.com/docker/distribution/reference"
"github.com/juju/errors"
"github.com/juju/loggo"
)
var logger = loggo.GetLogger("juju.docker")
// Token defines a token value with expiration time.
type Token struct {
// Value is the value of the token.
Value string
// ExpiresAt is the unix time in seconds and milliseconds when the authorization token expires.
ExpiresAt *time.Time
}
// UnmarshalJSON implements the json.Unmarshaller interface.
func (t *Token) UnmarshalJSON(value []byte) error {
err := json.Unmarshal(value, &t.Value)
return errors.Trace(err)
}
// String returns the string value.
func (t *Token) String() string {
if t.Empty() {
return ""
}
o := NewToken(t.Value)
o.mask()
return o.Value
}
// Content returns the raw content of the token.
func (t *Token) Content() string {
if t.Empty() {
return ""
}
return t.Value
}
// MarshalJSON implements the json.Marshaller interface.
func (t Token) MarshalJSON() ([]byte, error) {
if t.Empty() {
return nil, nil
}
return json.Marshal(t.Value)
}
// NewToken creates a Token.
func NewToken(value string) *Token {
if value == "" {
return nil
}
return &Token{Value: value}
}
// Empty checks if the auth information is empty.
func (t *Token) Empty() bool {
return t == nil || t.Value == ""
}
// Mask hides the token value.
func (t *Token) mask() {
if t.Empty() {
return
}
var b bytes.Buffer
for range t.Value {
b.WriteString("*")
}
t.Value = b.String()
}
// TokenAuthConfig contains authorization information for token auth.
// Juju does not support the docker credential helper because k8s does not support it either.
// https://kubernetes.io/docs/concepts/containers/images/#configuring-nodes-to-authenticate-to-a-private-registry
type TokenAuthConfig struct {
Email string `json:"email,omitempty" yaml:"email,omitempty"`
// IdentityToken is used to authenticate the user and get
// an access token for the registry.
IdentityToken *Token `json:"identitytoken,omitempty" yaml:"identitytoken,omitempty"`
// RegistryToken is a bearer token to be sent to a registry
RegistryToken *Token `json:"registrytoken,omitempty" yaml:"registrytoken,omitempty"`
}
// Empty checks if the auth information is empty.
func (ac TokenAuthConfig) Empty() bool {
return ac.RegistryToken.Empty() && ac.IdentityToken.Empty()
}
// Validate validates the spec.
func (ac *TokenAuthConfig) Validate() error {
return nil
}
func (ac *TokenAuthConfig) init() error {
return nil
}
// BasicAuthConfig contains authorization information for basic auth.
type BasicAuthConfig struct {
// Auth is the base64 encoded "username:password" string.
Auth *Token `json:"auth,omitempty" yaml:"auth,omitempty"`
// Username holds the username used to gain access to a non-public image.
Username string `json:"username" yaml:"username"`
// Password holds the password used to gain access to a non-public image.
Password string `json:"password" yaml:"password"`
}
// Empty checks if the auth information is empty.
func (ba BasicAuthConfig) Empty() bool {
return ba.Auth.Empty() && ba.Username == "" && ba.Password == ""
}
// Validate validates the spec.
func (ba *BasicAuthConfig) Validate() error {
return nil
}
func (ba *BasicAuthConfig) init() error {
if ba.Empty() {
return nil
}
if ba.Auth.Empty() {
ba.Auth = NewToken(base64.StdEncoding.EncodeToString([]byte(ba.Username + ":" + ba.Password)))
}
return nil
}
// ImageRepoDetails contains authorization information for connecting to a Registry.
type ImageRepoDetails struct {
BasicAuthConfig `json:",inline" yaml:",inline"`
TokenAuthConfig `json:",inline" yaml:",inline"`
// Repository is the namespace of the image repo.
Repository string `json:"repository,omitempty" yaml:"repository,omitempty"`
// ServerAddress is the auth server address.
ServerAddress string `json:"serveraddress,omitempty" yaml:"serveraddress,omitempty"`
// Region is the cloud region.
Region string `json:"region,omitempty" yaml:"region,omitempty"`
}
// AuthEqual compares if the provided one equals to current repository detail.
func (rid ImageRepoDetails) AuthEqual(r ImageRepoDetails) bool {
return reflect.DeepEqual(rid.BasicAuthConfig, r.BasicAuthConfig) &&
reflect.DeepEqual(rid.TokenAuthConfig, r.TokenAuthConfig)
}
// IsPrivate checks if the repository detail is private.
func (rid ImageRepoDetails) IsPrivate() bool {
return !rid.BasicAuthConfig.Empty() || !rid.TokenAuthConfig.Empty()
}
type dockerConfigData struct {
Auths map[string]ImageRepoDetails `json:"auths"`
}
// SecretData returns secret data format.
func (rid ImageRepoDetails) SecretData() ([]byte, error) {
if rid.BasicAuthConfig.Empty() && rid.TokenAuthConfig.Empty() {
return nil, nil
}
rid.Repository = ""
o := dockerConfigData{
Auths: map[string]ImageRepoDetails{
rid.ServerAddress: rid,
},
}
return json.Marshal(o)
}
// Content returns the json marshalled string with raw credentials.
func (rid ImageRepoDetails) Content() string {
d, _ := json.Marshal(rid)
return string(d)
}
// Validate validates the spec.
func (rid *ImageRepoDetails) Validate() error {
if rid.Repository == "" {
return errors.NotValidf("empty repository")
}
_, err := reference.ParseNormalizedNamed(rid.Repository)
if err != nil {
return errors.NewNotValid(err, fmt.Sprintf("docker image path %q", rid.Repository))
}
if err := rid.BasicAuthConfig.Validate(); err != nil {
return errors.Annotatef(err, "validating basic auth config for repository %q", rid.Repository)
}
if err := rid.TokenAuthConfig.Validate(); err != nil {
return errors.Annotatef(err, "validating token auth config for repository %q", rid.Repository)
}
return nil
}
func (rid *ImageRepoDetails) init() error {
if err := rid.BasicAuthConfig.init(); err != nil {
return errors.Annotatef(err, "initializing basic auth config for repository %q", rid.Repository)
}
if err := rid.TokenAuthConfig.init(); err != nil {
return errors.Annotatef(err, "initializing token auth config for repository %q", rid.Repository)
}
return nil
}
// Empty checks if the auth information is empty.
func (rid ImageRepoDetails) Empty() bool {
return rid == ImageRepoDetails{}
}
func fileExists(p string) (bool, error) {
info, err := os.Stat(p)
if os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, errors.Trace(err)
}
return !info.IsDir(), nil
}
// NewImageRepoDetails tries to parse a file path or file content and returns an instance of ImageRepoDetails.
func NewImageRepoDetails(contentOrPath string) (o *ImageRepoDetails, err error) {
if contentOrPath == "" {
return o, nil
}
data := []byte(contentOrPath)
isPath, err := fileExists(contentOrPath)
if err == nil && isPath {
logger.Debugf("reading image repository information from %q", contentOrPath)
data, err = ioutil.ReadFile(contentOrPath)
if err != nil {
return nil, errors.Trace(err)
}
}
o = &ImageRepoDetails{}
err = json.Unmarshal(data, o)
if err != nil {
logger.Tracef("unmarshalling %q, err %#v", contentOrPath, err)
return &ImageRepoDetails{Repository: contentOrPath}, nil
}
if err = o.Validate(); err != nil {
return nil, errors.Trace(err)
}
if err = o.init(); err != nil {
return nil, errors.Trace(err)
}
return o, nil
}