This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
/
auth.go
283 lines (246 loc) · 6.94 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
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright 2015 The rkt Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/client/metadata"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/private/signer/v4"
"io"
"net/http"
"strings"
"time"
)
const (
authHeader string = "Authorization"
defaultAWSRegion string = "us-east-1"
awsS3Service string = "s3"
)
type authV1JsonParser struct{}
type authV1 struct {
Domains []string `json:"domains"`
Type string `json:"type"`
Credentials json.RawMessage `json:"credentials"`
}
type basicV1 struct {
User string `json:"user"`
Password string `json:"password"`
}
type oauthV1 struct {
Token string `json:"token"`
}
type awsV1 struct {
AccessKeyID string `json:"accessKeyID"`
SecretAccessKey string `json:"secretAccessKey"`
Region string `json:"awsRegion"`
}
type dockerAuthV1JsonParser struct{}
type dockerAuthV1 struct {
Registries []string `json:"registries"`
Credentials basicV1 `json:"credentials"`
}
func init() {
addParser("auth", "v1", &authV1JsonParser{})
addParser("dockerAuth", "v1", &dockerAuthV1JsonParser{})
registerSubDir("auth.d", []string{"auth", "dockerAuth"})
}
type basicAuthHeaderer struct {
auth basicV1
}
func (h *basicAuthHeaderer) GetHeader() http.Header {
headers := make(http.Header)
creds := []byte(fmt.Sprintf("%s:%s", h.auth.User, h.auth.Password))
encodedCreds := base64.StdEncoding.EncodeToString(creds)
headers.Add(authHeader, "Basic "+encodedCreds)
return headers
}
func (h *basicAuthHeaderer) SignRequest(r *http.Request) *http.Request {
r.Header.Set(authHeader, h.GetHeader().Get(authHeader))
return r
}
type oAuthBearerTokenHeaderer struct {
auth oauthV1
}
func (h *oAuthBearerTokenHeaderer) GetHeader() http.Header {
headers := make(http.Header)
headers.Add(authHeader, "Bearer "+h.auth.Token)
return headers
}
func (h *oAuthBearerTokenHeaderer) SignRequest(r *http.Request) *http.Request {
r.Header.Set(authHeader, h.GetHeader().Get(authHeader))
return r
}
type awsAuthHeaderer struct {
auth awsV1
}
func (h *awsAuthHeaderer) GetHeader() http.Header {
return make(http.Header)
}
func (h *awsAuthHeaderer) SignRequest(r *http.Request) *http.Request {
region := h.auth.Region
var body io.ReadSeeker
if r.Body != nil {
body = r.Body.(io.ReadSeeker)
}
if len(region) == 0 {
region = guessAWSRegion(r.URL.Host)
}
v4.Sign(&request.Request{
ClientInfo: metadata.ClientInfo{
SigningRegion: region,
SigningName: awsS3Service,
},
Config: aws.Config{
Credentials: credentials.NewStaticCredentials(h.auth.AccessKeyID, h.auth.SecretAccessKey, ""),
},
HTTPRequest: r,
Body: body,
Time: time.Now(),
})
return r
}
func guessAWSRegion(host string) string {
// Separate out potential :<port>
hostParts := strings.Split(host, ":")
host = strings.ToLower(hostParts[0])
parts := strings.Split(host, ".")
if len(parts) < 3 || parts[len(parts)-2] != "amazonaws" {
return defaultAWSRegion
}
// Try to guess region based on url, but if nothing
// matches, just fall back to defaultAWSRegion.
if strings.HasSuffix(host, "s3.amazonaws.com") || strings.HasSuffix(host, "s3-external-1.amazonaws.com") {
return "us-east-1"
} else if len(parts) > 3 && strings.HasPrefix(parts[len(parts)-3], "s3-") {
return parts[len(parts)-3][3:]
} else if len(parts) > 4 && parts[len(parts)-4] == "s3" {
return parts[len(parts)-3]
} else {
return defaultAWSRegion
}
}
func (p *authV1JsonParser) parse(config *Config, raw []byte) error {
var auth authV1
if err := json.Unmarshal(raw, &auth); err != nil {
return err
}
if len(auth.Domains) == 0 {
return fmt.Errorf("no domains specified")
}
if len(auth.Type) == 0 {
return fmt.Errorf("no auth type specified")
}
var (
err error
headerer Headerer
)
switch auth.Type {
case "basic":
headerer, err = p.getBasicV1Headerer(auth.Credentials)
case "oauth":
headerer, err = p.getOAuthV1Headerer(auth.Credentials)
case "aws":
headerer, err = p.getAWSV1Headerer(auth.Credentials)
default:
err = fmt.Errorf("unknown auth type: %q", auth.Type)
}
if err != nil {
return err
}
for _, domain := range auth.Domains {
if _, ok := config.AuthPerHost[domain]; ok {
return fmt.Errorf("auth for domain %q is already specified", domain)
}
config.AuthPerHost[domain] = headerer
}
return nil
}
func (p *authV1JsonParser) getBasicV1Headerer(raw json.RawMessage) (Headerer, error) {
var basic basicV1
if err := json.Unmarshal(raw, &basic); err != nil {
return nil, err
}
if err := validateBasicV1(&basic); err != nil {
return nil, err
}
return &basicAuthHeaderer{
auth: basic,
}, nil
}
func (p *authV1JsonParser) getOAuthV1Headerer(raw json.RawMessage) (Headerer, error) {
var oauth oauthV1
if err := json.Unmarshal(raw, &oauth); err != nil {
return nil, err
}
if len(oauth.Token) == 0 {
return nil, fmt.Errorf("no oauth bearer token specified")
}
return &oAuthBearerTokenHeaderer{
auth: oauth,
}, nil
}
func (p *authV1JsonParser) getAWSV1Headerer(raw json.RawMessage) (Headerer, error) {
var aws awsV1
if err := json.Unmarshal(raw, &aws); err != nil {
return nil, err
}
if len(aws.AccessKeyID) == 0 {
return nil, fmt.Errorf("no AWS Access Key ID specified")
}
if len(aws.SecretAccessKey) == 0 {
return nil, fmt.Errorf("no AWS Secret Access Key specified")
}
return &awsAuthHeaderer{
auth: aws,
}, nil
}
func (p *dockerAuthV1JsonParser) parse(config *Config, raw []byte) error {
var auth dockerAuthV1
if err := json.Unmarshal(raw, &auth); err != nil {
return err
}
if len(auth.Registries) == 0 {
return fmt.Errorf("no registries specified")
}
if err := validateBasicV1(&auth.Credentials); err != nil {
return err
}
basic := BasicCredentials{
User: auth.Credentials.User,
Password: auth.Credentials.Password,
}
for _, registry := range auth.Registries {
if _, ok := config.DockerCredentialsPerRegistry[registry]; ok {
return fmt.Errorf("credentials for docker registry %q are already specified", registry)
}
config.DockerCredentialsPerRegistry[registry] = basic
}
return nil
}
func validateBasicV1(basic *basicV1) error {
if basic == nil {
return fmt.Errorf("no credentials")
}
if len(basic.User) == 0 {
return fmt.Errorf("user not specified")
}
if len(basic.Password) == 0 {
return fmt.Errorf("password not specified")
}
return nil
}