forked from awslabs/amazon-ecr-credential-helper
/
client.go
199 lines (177 loc) · 6.43 KB
/
client.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
// Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 api
import (
"encoding/base64"
"fmt"
"regexp"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/aws/aws-sdk-go/service/ecr/ecriface"
"github.com/awslabs/amazon-ecr-credential-helper/ecr-login/cache"
log "github.com/cihub/seelog"
)
const proxyEndpointScheme = "https://"
const programName = "docker-credential-ecr-login"
var ecrPattern = regexp.MustCompile(`(^[a-zA-Z0-9][a-zA-Z0-9-_]*)\.dkr\.ecr\.([a-zA-Z0-9][a-zA-Z0-9-_]*)\.amazonaws\.com(\.cn)?`)
type Registry struct {
ID string
Region string
}
func ExtractRegistry(serverURL string) (*Registry, error) {
if (strings.HasPrefix(serverURL, proxyEndpointScheme)) {
serverURL = strings.TrimPrefix(serverURL, proxyEndpointScheme)
}
matches := ecrPattern.FindStringSubmatch(serverURL)
if len(matches) == 0 {
return nil, fmt.Errorf(programName + " can only be used with Amazon EC2 Container Registry.")
} else if len(matches) < 3 {
return nil, fmt.Errorf(serverURL + "is not a valid repository URI for Amazon EC2 Container Registry.")
}
registry := &Registry{
ID: matches[1],
Region: matches[2],
}
return registry, nil
}
type Client interface {
GetCredentials(serverURL string) (*Auth, error)
GetCredentialsByRegistryID(registryID string) (*Auth, error)
ListCredentials() ([]*Auth, error)
}
type defaultClient struct {
ecrClient ecriface.ECRAPI
credentialCache cache.CredentialsCache
}
type Auth struct {
ProxyEndpoint string
Username string
Password string
}
// GetCredentials returns username, password, and proxyEndpoint
func (self *defaultClient) GetCredentials(serverURL string) (*Auth, error) {
registry, err := ExtractRegistry(serverURL)
if err != nil {
return nil, err
}
log.Debugf("Retrieving credentials for %s in %s (%s)", registry.ID, registry.Region, serverURL)
return self.GetCredentialsByRegistryID(registry.ID)
}
// GetCredentials returns username, password, and proxyEndpoint
func (self *defaultClient) GetCredentialsByRegistryID(registryID string) (*Auth, error) {
cachedEntry := self.credentialCache.Get(registryID)
if cachedEntry != nil {
if cachedEntry.IsValid(time.Now()) {
log.Debugf("Using cached token for %s", registryID)
return extractToken(cachedEntry.AuthorizationToken, cachedEntry.ProxyEndpoint)
}
log.Debugf("Cached token is no longer valid. RequestAt: %s, ExpiresAt: %s", cachedEntry.RequestedAt, cachedEntry.ExpiresAt)
}
auth, err := self.getAuthorizationToken(registryID)
// if we have a cached token, fall back to avoid failing the request. This may result an expired token
// being returned, but if there is a 500 or timeout from the service side, we'd like to attempt to re-use an
// old token. We invalidate tokens prior to their expiration date to help mitigate this scenario.
if err != nil && cachedEntry != nil {
log.Infof("Got error fetching authorization token. Falling back to cached token. Error was: %s", err)
return extractToken(cachedEntry.AuthorizationToken, cachedEntry.ProxyEndpoint)
}
return auth, err
}
func (self *defaultClient) ListCredentials() ([]*Auth, error) {
auths := []*Auth{}
for _, authEntry := range self.credentialCache.List() {
auth, err := extractToken(authEntry.AuthorizationToken, authEntry.ProxyEndpoint)
if err != nil {
log.Debugf("Could not extract token: %v", err)
} else {
auths = append(auths, auth)
}
}
// If cache is empty, get authorization token of default registry
if len(auths) == 0 {
log.Debug("No credential cache")
auth, err := self.getAuthorizationToken("")
if err != nil {
log.Debugf("Couldn't get authorization token: %v", err)
} else {
auths = append(auths, auth)
}
return auths, err
}
return auths, nil
}
func (self *defaultClient) getAuthorizationToken(registryID string) (*Auth, error) {
var input *ecr.GetAuthorizationTokenInput
if registryID == "" {
log.Debugf("Calling ECR.GetAuthorizationToken for default registry")
input = &ecr.GetAuthorizationTokenInput{}
} else {
log.Debugf("Calling ECR.GetAuthorizationToken for %s", registryID)
input = &ecr.GetAuthorizationTokenInput{
RegistryIds: []*string{aws.String(registryID)},
}
}
output, err := self.ecrClient.GetAuthorizationToken(input)
if err != nil || output == nil {
if err == nil {
if registryID == "" {
err = fmt.Errorf("Mising AuthorizationData in ECR response for default registry")
} else {
err = fmt.Errorf("Missing AuthorizationData in ECR response for %s", registryID)
}
}
return nil, err
}
for _, authData := range output.AuthorizationData {
if authData.ProxyEndpoint != nil && authData.AuthorizationToken != nil {
authEntry := cache.AuthEntry{
AuthorizationToken: aws.StringValue(authData.AuthorizationToken),
RequestedAt: time.Now(),
ExpiresAt: aws.TimeValue(authData.ExpiresAt),
ProxyEndpoint: aws.StringValue(authData.ProxyEndpoint),
}
registry, err := ExtractRegistry(authEntry.ProxyEndpoint)
if err != nil {
return nil, fmt.Errorf("Invalid ProxyEndpoint returned by ECR: %s", authEntry.ProxyEndpoint)
}
auth, err := extractToken(authEntry.AuthorizationToken, authEntry.ProxyEndpoint)
if err != nil {
return nil, err
}
self.credentialCache.Set(registry.ID, &authEntry)
return auth, nil
}
}
if registryID == "" {
return nil, fmt.Errorf("No AuthorizationToken found for default registry")
} else {
return nil, fmt.Errorf("No AuthorizationToken found for %s", registryID)
}
}
func extractToken(token string, proxyEndpoint string) (*Auth, error) {
decodedToken, err := base64.StdEncoding.DecodeString(token)
if err != nil {
return nil, fmt.Errorf("Invalid token: %v:", err)
}
parts := strings.SplitN(string(decodedToken), ":", 2)
if len(parts) < 2 {
return nil, fmt.Errorf("Invalid token: expected two parts, got %n", len(parts))
}
return &Auth{
Username: parts[0],
Password: parts[1],
ProxyEndpoint: proxyEndpoint,
}, nil
}