-
Notifications
You must be signed in to change notification settings - Fork 90
/
auth.go
173 lines (145 loc) · 4.73 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
package registry
import (
"crypto/tls"
"encoding/base64"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/containers/image/v5/pkg/docker/config"
"github.com/containers/image/v5/types"
"github.com/docker/distribution/registry/client/auth/challenge"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/version"
)
var (
insecureClient = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
Proxy: http.ProxyFromEnvironment,
},
}
)
func LoadAuthForRegistry(endpoint string) (string, string, error) {
sys := &types.SystemContext{DockerDisableV1Ping: true}
username, password, err := config.GetAuthentication(sys, endpoint)
if err != nil {
return "", "", errors.Wrapf(err, "error loading username and password")
}
return username, password, nil
}
func TestPushAccess(endpoint, username, password, org string) error {
endpoint = sanitizeEndpoint(endpoint)
// We need to check if we can push images to a repo.
// We cannot get push permission to an org alone.
scope := org + "/testrepo"
basicAuthToken := makeBasicAuthToken(username, password)
if IsECREndpoint(endpoint) {
token, err := GetECRBasicAuthToken(endpoint, username, password)
if err != nil {
return errors.Wrap(err, "failed to ping registry")
}
basicAuthToken = token
scope = org // ECR has no concept of organization and it should be an empty string
}
pingURL := fmt.Sprintf("https://%s/v2/", endpoint)
resp, err := insecureClient.Get(pingURL)
if err != nil {
// attempt with http
pingURL = fmt.Sprintf("http://%s/v2/", endpoint)
resp, err = insecureClient.Get(pingURL)
if err != nil {
return errors.Wrap(err, "failed to ping registry")
}
}
if resp.StatusCode == http.StatusOK {
// Anonymous registry that does not require authentication
return nil
}
if resp.StatusCode != http.StatusUnauthorized {
return errors.Errorf("unexpected status code: %v", resp.StatusCode)
}
challenges := challenge.ResponseChallenges(resp)
if len(challenges) == 0 {
return errors.Wrap(err, "no auth challenges found for endpoint")
}
if challenges[0].Scheme == "basic" {
// ecr uses basic auth. not much more we can do here without actually pushing an image
return nil
}
host := challenges[0].Parameters["realm"]
v := url.Values{}
v.Set("service", challenges[0].Parameters["service"])
v.Set("scope", fmt.Sprintf("repository:%s:push", scope))
authURL := host + "?" + v.Encode()
req, err := http.NewRequest("GET", authURL, nil)
if err != nil {
return errors.Wrap(err, "failed to create auth request")
}
req.Header.Add("User-Agent", fmt.Sprintf("KOTS/%s", version.Version()))
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", basicAuthToken))
resp, err = insecureClient.Do(req)
if err != nil {
return errors.Wrap(err, "failed to execute auth request")
}
defer resp.Body.Close()
authBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errors.Wrap(err, "failed to load auth response")
}
if resp.StatusCode != http.StatusOK {
return errors.New(errorResponseToString(resp.StatusCode, authBody))
}
bearerToken, err := newBearerTokenFromJSONBlob(authBody)
if err != nil {
// not a fatal error - some registries don't return JWTs
logger.NewLogger().Info("failed to parse registry auth bearer token, continuing anyways: %s", err.Error())
return nil
}
jwtToken, err := bearerToken.getJwtToken()
if err != nil {
// not a fatal error - some registries don't return JWTs
logger.NewLogger().Info("failed to parse registry auth jwt, continuing anyways: %s", err.Error())
return nil
}
claims, err := getJwtTokenClaims(jwtToken)
if err != nil {
// not a fatal error - some registries don't return JWTs
logger.NewLogger().Info("failed to find registry auth claims in jwt, continuing anyways: %s", err.Error())
return nil
}
for _, access := range claims.Access {
if access.Type != "repository" {
continue
}
if access.Name != scope {
continue
}
for _, action := range access.Actions {
if action == "push" {
return nil
}
}
}
return errors.Errorf("%q has no push permission in %q", username, org)
}
func makeBasicAuthToken(username, password string) string {
token := fmt.Sprintf("%s:%s", username, password)
return base64.StdEncoding.EncodeToString([]byte(token))
}
func sanitizeEndpoint(endpoint string) string {
endpoint = strings.TrimPrefix(endpoint, "http://")
endpoint = strings.TrimPrefix(endpoint, "https://")
endpoint = strings.TrimSuffix(endpoint, "/v2/")
endpoint = strings.TrimSuffix(endpoint, "/v2")
endpoint = strings.TrimSuffix(endpoint, "/v1/")
endpoint = strings.TrimSuffix(endpoint, "/v1")
if endpoint == "docker.io" {
endpoint = "index.docker.io"
}
return endpoint
}