-
Notifications
You must be signed in to change notification settings - Fork 21
/
auth.go
103 lines (81 loc) · 2.67 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
// Copyright (c) 2018 Target Brands, Inc.
package docker
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/pkg/errors"
"github.com/target/portauthority/pkg/docker/registry"
"golang.org/x/oauth2/google"
)
// AuthConfig struct init
type AuthConfig struct {
// RegistryURL, Username, Password are required for obtaining a token.
// Repo and Tag are required for docker.io as as tokens must have a directed
// scope.
RegistryURL string
Repo string
Tag string
Username string
Password string
}
// Token struct init
type Token struct {
Token string `json:"token"`
}
// AuthRegistry will attempt to authenticate to a register with the provided
// credentials, returning the resulting token.
func AuthRegistry(authConfig *AuthConfig) (*Token, error) {
token := &Token{}
// Need special handling for obtaining the GCR token at this point in time.
// TODO: At some point, the token should be obtained from the Docker client
// once a valid GCR token can be extracted.
if strings.Contains(authConfig.RegistryURL, "gcr.io") && authConfig.Password != "" { // gcr\.io$
jwtConfig, err := google.JWTConfigFromJSON([]byte(authConfig.Password), "https://www.googleapis.com/auth/devstorage.read_only")
if err != nil {
return token, errors.Wrap(err, "error getting gcr token")
}
gcrtoken, err := jwtConfig.TokenSource(context.Background()).Token()
if err != nil {
return token, errors.Wrap(err, "getting gcr token error")
}
token = &Token{Token: gcrtoken.AccessToken}
return token, nil
}
// Format appropriate URL
url := fmt.Sprintf("%s/v2/", authConfig.RegistryURL)
if authConfig.Repo != "" && authConfig.Tag != "" {
url = fmt.Sprintf("%s/v2/%s/manifests/%s", authConfig.RegistryURL, authConfig.Repo, authConfig.Tag)
}
resp, err := http.Get(url)
if resp == nil {
return token, errors.Wrap(err, "no response")
}
if err != nil {
return token, errors.Wrap(err, "error making initial request to registry for auth")
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusUnauthorized {
authService := registry.ParseOauthHeader(resp)
authReq, err := authService.Request(authConfig.Username, authConfig.Password)
if err != nil {
return token, errors.Wrap(err, "error building auth request")
}
tokenResp, err := http.DefaultClient.Do(authReq)
if err != nil {
return token, errors.Wrap(err, "error performing token request")
}
defer tokenResp.Body.Close()
var t Token
decoder := json.NewDecoder(tokenResp.Body)
err = decoder.Decode(&t)
if err != nil {
return token, errors.Wrap(err, "error decoding token response")
}
token = &Token{Token: t.Token}
return token, nil
}
return token, nil
}