/
registry.go
427 lines (358 loc) · 13.8 KB
/
registry.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// Copyright © 2019 Banzai Cloud
//
// 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 registry
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"regexp"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecr"
dockerTypes "github.com/docker/docker/api/types"
"github.com/heroku/docker-registry-client/registry"
imagev1 "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/patrickmn/go-cache"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
const ecrCredentialsKey = "AWS_ECR_CREDENTIALS"
var logger *log.Logger
var ecrHostPattern *regexp.Regexp
func init() {
logger = log.New()
if viper.GetBool("enable_json_log") {
logger.SetFormatter(&log.JSONFormatter{})
}
// Adapted from https://github.com/awslabs/amazon-ecr-credential-helper/blob/master/ecr-login/api/client.go#L34
ecrHostPattern = regexp.MustCompile(`([a-zA-Z0-9][a-zA-Z0-9-_]*)\.dkr\.ecr(\-fips)?\.([a-zA-Z0-9][a-zA-Z0-9-_]*)\.amazonaws\.com(\.cn)?`)
}
// ImageRegistry is a docker registry
type ImageRegistry interface {
GetImageConfig(
clientset kubernetes.Interface,
namespace string,
container *corev1.Container,
podSpec *corev1.PodSpec) (*imagev1.ImageConfig, error)
}
// Registry impl
type Registry struct {
imageCache *cache.Cache
credentialsCache *cache.Cache
}
// NewRegistry creates and initializes registry
func NewRegistry() ImageRegistry {
return &Registry{
imageCache: cache.New(cache.NoExpiration, cache.NoExpiration),
credentialsCache: cache.New(12*time.Hour, 12*time.Hour),
}
}
type DockerCreds struct {
Auths map[string]dockerTypes.AuthConfig `json:"auths"`
}
// IsAllowedToCache checks that information about Docker image can be cached
// base on image name and container PullPolicy
func IsAllowedToCache(container *corev1.Container) bool {
if container.ImagePullPolicy == corev1.PullAlways {
return false
}
_, reference := parseContainerImage(container.Image)
return reference != "latest"
}
// GetImageConfig returns entrypoint and command of container
func (r *Registry) GetImageConfig(
clientset kubernetes.Interface,
namespace string,
container *corev1.Container,
podSpec *corev1.PodSpec) (*imagev1.ImageConfig, error) {
allowToCache := IsAllowedToCache(container)
if allowToCache {
if imageConfig, cacheHit := r.imageCache.Get(container.Image); cacheHit {
logger.Infof("found image %s in cache", container.Image)
return imageConfig.(*imagev1.ImageConfig), nil
}
}
containerInfo := ContainerInfo{Namespace: namespace, clientset: clientset}
err := containerInfo.Collect(container, podSpec, r.credentialsCache)
if err != nil {
return nil, err
}
logger.Infoln("I'm using registry", containerInfo.RegistryAddress)
imageConfig, err := getImageBlob(containerInfo)
if imageConfig != nil && allowToCache {
r.imageCache.Set(container.Image, imageConfig, cache.DefaultExpiration)
}
return imageConfig, err
}
// GetImageBlob download image blob from registry
func getImageBlob(container ContainerInfo) (*imagev1.ImageConfig, error) {
imageName, reference := parseContainerImage(container.Image)
registrySkipVerify := viper.GetBool("registry_skip_verify")
var hub *registry.Registry
var err error
if registrySkipVerify {
hub, err = registry.NewInsecure(container.RegistryAddress, container.RegistryUsername, container.RegistryPassword)
} else {
hub, err = registry.New(container.RegistryAddress, container.RegistryUsername, container.RegistryPassword)
}
if err != nil {
return nil, fmt.Errorf("cannot create client for registry: %s", err.Error())
}
manifest, err := hub.ManifestV2(imageName, reference)
if err != nil {
return nil, fmt.Errorf("cannot download manifest for image: %s", err.Error())
}
reader, err := hub.DownloadBlob(imageName, manifest.Config.Digest)
if err != nil {
return nil, fmt.Errorf("cannot download blob: %s", err.Error())
}
defer reader.Close()
b, err := ioutil.ReadAll(reader)
if err != nil {
return nil, fmt.Errorf("cannot read blob: %s", err.Error())
}
var imageMetadata imagev1.Image
err = json.Unmarshal(b, &imageMetadata)
if err != nil {
return nil, fmt.Errorf("cannot unmarshal BlobResponse JSON: %s", err.Error())
}
return &imageMetadata.Config, nil
}
// parseContainerImage returns image and reference
func parseContainerImage(image string) (string, string) {
var split []string
if strings.Contains(image, "@") {
split = strings.SplitN(image, "@", 2)
} else {
split = strings.SplitN(image, ":", 2)
}
imageName := split[0]
reference := "latest"
if len(split) > 1 {
reference = split[1]
}
return imageName, reference
}
// K8s structure keeps information retrieved from POD definition
type ContainerInfo struct {
clientset kubernetes.Interface
Namespace string
ImagePullSecrets string
RegistryAddress string
RegistryName string
RegistryUsername string
RegistryPassword string
Image string
}
func (k *ContainerInfo) readDockerSecret(namespace, secretName string) (map[string][]byte, error) {
ctx := context.Background()
secret, err := k.clientset.CoreV1().Secrets(namespace).Get(ctx, secretName, metav1.GetOptions{})
if err != nil {
return nil, err
}
return secret.Data, nil
}
func (k *ContainerInfo) parseDockerConfig(dockerCreds DockerCreds) (bool, error) {
for registryName, registryAuth := range dockerCreds.Auths {
if strings.HasPrefix(registryName, "https://") {
registryName = strings.TrimPrefix(registryName, "https://")
}
// kubectl create secret docker-registry for DockerHub creates
// registry credentials with API version suffixes, trim it!
if strings.HasSuffix(registryName, "/v1/") {
registryName = strings.TrimSuffix(registryName, "/v1/")
} else if strings.HasSuffix(registryName, "/v2/") {
registryName = strings.TrimSuffix(registryName, "/v2/")
}
registryName = strings.TrimSuffix(registryName, "/")
if strings.HasPrefix(k.Image, registryName) {
k.RegistryName = registryName
if registryAuth.ServerAddress != "" {
k.RegistryAddress = registryAuth.ServerAddress
} else {
k.RegistryAddress = fmt.Sprintf("https://%s", registryName)
}
if len(registryAuth.Username) > 0 && len(registryAuth.Password) > 0 {
// auths.<registry>.username and auths.<registry>.username are present
// in the config.json, use them
k.RegistryUsername = registryAuth.Username
k.RegistryPassword = registryAuth.Password
} else if len(registryAuth.Auth) > 0 {
// auths.<registry>.username and auths.<registry>.username are not present
// in the config.json, fall back to the base64 encoded auths.<registry>.auth field
// The registry.Auth field contains a base64 encoded string of the format <username>:<password>
decodedAuth, err := base64.StdEncoding.DecodeString(registryAuth.Auth)
if err != nil {
return false, fmt.Errorf("failed to decode auth field for registry %s: %s", registryName, err.Error())
}
auth := strings.Split(string(decodedAuth), ":")
if len(auth) != 2 {
return false, fmt.Errorf("unexpected number of elements in auth field for registry %s: %d (expected 2)", registryName, len(auth))
}
// decodedAuth is something like ":xxx"
if len(auth[0]) <= 0 {
return false, fmt.Errorf("username element of auth field for registry %s missing", registryName)
}
// decodedAuth is something like "xxx:"
if len(auth[1]) <= 0 {
return false, fmt.Errorf("password element of auth field for registry %s missing", registryName)
}
k.RegistryUsername = auth[0]
k.RegistryPassword = auth[1]
} else {
// the auths section has an entry for the registry, but it neither contains
// username/password fields nor an auth field, fail
return false, fmt.Errorf("found %s in imagePullSecrets but it contains no usable credentials; either username/password fields or an auth field are required", registryName)
}
return true, nil
}
}
return false, nil
}
func (k *ContainerInfo) fixDockerHubImage(image string) string {
slash := strings.Index(image, "/")
if slash == -1 { // Is it a DockerHub library repository?
image = "index.docker.io/library/" + image
} else if !strings.Contains(image[:slash], ".") { // DockerHub organization names can't contain '.'
image = "index.docker.io/" + image
} else if strings.HasPrefix(image, "docker.io/") {
image = "index." + image
} else {
return image
}
// if in the end there is no RegistryAddress defined it should be a public DockerHub repository
k.RegistryAddress = "https://index.docker.io"
k.RegistryName = "index.docker.io"
return image
}
func (k *ContainerInfo) checkImagePullSecret(namespace string, secret string) (bool, error) {
data, err := k.readDockerSecret(namespace, secret)
if err != nil {
return false, fmt.Errorf("cannot read imagePullSecret '%s' in namespace '%s': %s", secret, namespace, err.Error())
}
var dockerCreds DockerCreds
dockerConfigJSONKey := viper.GetString("default_image_pull_docker_config_json_key")
err = json.Unmarshal(data[dockerConfigJSONKey], &dockerCreds)
if err != nil {
return false, fmt.Errorf("cannot unmarshal docker configuration from imagePullSecret: %s", err.Error())
}
found, err := k.parseDockerConfig(dockerCreds)
return found, err
}
// Collect reads information from k8s and load them into the structure
func (k *ContainerInfo) Collect(container *corev1.Container, podSpec *corev1.PodSpec, credentialsCache *cache.Cache) error {
k.Image = k.fixDockerHubImage(container.Image)
var err error
found := false
// Check for registry credentials in imagePullSecrets attached to the pod
// ImagePullSecrets attached to ServiceAccounts do not have to be considered
// explicitly as ServiceAccount ImagePullSecrets are automatically attached
// to a pod.
for _, imagePullSecret := range podSpec.ImagePullSecrets {
found, err = k.checkImagePullSecret(k.Namespace, imagePullSecret.Name)
if err != nil {
return err
}
if found {
logger.Infof("found credentials for registry %s in pod imagePullSecret: %s/%s", k.RegistryName, k.Namespace, imagePullSecret.Name)
break
}
}
// The pod imagePullSecrets did not contained matching credentials.
// Try to find matching registry credentials in the default imagePullSecret if one was provided.
if !found {
defaultImagePullSecret := viper.GetString("default_image_pull_secret")
defaultImagePullSecretNamespace := viper.GetString("default_image_pull_secret_namespace")
if len(defaultImagePullSecret) > 0 && len(defaultImagePullSecretNamespace) > 0 {
found, err = k.checkImagePullSecret(defaultImagePullSecretNamespace, defaultImagePullSecret)
if err != nil {
return err
}
if found {
logger.Infof("found credentials for registry %s in default imagePullSecret: %s/%s", k.RegistryName, defaultImagePullSecretNamespace, defaultImagePullSecret)
}
}
}
// In case of other docker registry
if k.RegistryName == "" && k.RegistryAddress == "" {
registryName := container.Image
if strings.HasPrefix(registryName, "https://") {
registryName = strings.TrimPrefix(registryName, "https://")
}
registryName = strings.Split(registryName, "/")[0]
k.RegistryName = registryName
k.RegistryAddress = fmt.Sprintf("https://%s", registryName)
}
// Clean registry from image
k.Image = strings.TrimPrefix(k.Image, fmt.Sprintf("%s/", k.RegistryName))
if !found {
// if still no credentials and it is an ECR image, try to get credentials through an EC2 instance role
if ecrRegistryID, region := getECRRegistryIDAndRegion(k.RegistryAddress); ecrRegistryID != "" {
logger.Infof("trying to request AWS credentials for ECR registry %s", k.RegistryAddress)
var data string
cacheKey := ecrCredentialsKey + k.RegistryAddress
cachedToken, usingCache := credentialsCache.Get(cacheKey)
if usingCache {
data = cachedToken.(string)
logger.Infof("Using cached AWS ECR Token for registry %s", k.RegistryAddress)
} else {
sess, err := session.NewSession()
if err != nil {
logger.Info("Failed to create AWS session, trying with no credentials")
return nil
}
svc := ecr.New(sess, aws.NewConfig().WithRegion(region))
req := ecr.GetAuthorizationTokenInput{
RegistryIds: []*string{aws.String(ecrRegistryID)},
}
resp, err := svc.GetAuthorizationToken(&req)
if err != nil {
logger.Infof("Failed to get AWS ECR Token, trying with no credentials")
return nil
}
// We requested only one entry
authData := resp.AuthorizationData[0]
decodedData, err := base64.StdEncoding.DecodeString(aws.StringValue(authData.AuthorizationToken))
data = string(decodedData)
if err != nil {
return err
}
expiration := authData.ExpiresAt.Sub(time.Now().Add(5 * time.Minute))
credentialsCache.Set(cacheKey, data, expiration)
logger.Infof("Caching ECR token with expiration in %+v", expiration)
}
token := strings.SplitN(data, ":", 2)
k.RegistryUsername = token[0]
k.RegistryPassword = token[1]
logger.Infof("got AWS credentials for ecr registry %s", k.RegistryAddress)
} else {
logger.Infof("found no credentials for registry %s, assuming it is public", k.RegistryAddress)
}
}
return nil
}
func getECRRegistryIDAndRegion(registryAddr string) (string, string) {
matches := ecrHostPattern.FindStringSubmatch(registryAddr)
if len(matches) < 3 {
return "", ""
}
return matches[1], matches[3]
}