...
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package cache
15
16 import (
17 "context"
18 "crypto/md5"
19 "encoding/base64"
20 "fmt"
21 "os"
22
23 "github.com/aws/aws-sdk-go-v2/aws"
24 "github.com/mitchellh/go-homedir"
25 "github.com/sirupsen/logrus"
26
27 ecrconfig "github.com/awslabs/amazon-ecr-credential-helper/ecr-login/config"
28 )
29
30 func BuildCredentialsCache(config aws.Config, cacheDir string) CredentialsCache {
31 if os.Getenv("AWS_ECR_DISABLE_CACHE") != "" {
32 logrus.Debug("Cache disabled due to AWS_ECR_DISABLE_CACHE")
33 return NewNullCredentialsCache()
34 }
35
36 if cacheDir == "" {
37
38 cacheDir = ecrconfig.GetCacheDir()
39 }
40
41 cacheDir, err := homedir.Expand(cacheDir)
42 if err != nil {
43 logrus.WithError(err).Debug("Could not expand cache path, disabling cache")
44 return NewNullCredentialsCache()
45 }
46
47 cacheFilename := "cache.json"
48
49 credentials, err := config.Credentials.Retrieve(context.TODO())
50 if err != nil {
51 logrus.WithError(err).Debug("Could not fetch credentials for cache prefix, disabling cache")
52 return NewNullCredentialsCache()
53 }
54
55 return NewFileCredentialsCache(cacheDir, cacheFilename, credentialsCachePrefix(config.Region, credentials), credentialsPublicCacheKey(credentials))
56 }
57
58
59
60 func credentialsCachePrefix(region string, credentials aws.Credentials) string {
61 return fmt.Sprintf("%s-%s-", region, checksum(credentials.AccessKeyID))
62 }
63
64 func credentialsPublicCacheKey(credentials aws.Credentials) string {
65 return fmt.Sprintf("%s-%s", ServiceECRPublic, checksum(credentials.AccessKeyID))
66 }
67
68
69 func checksum(text string) string {
70 hasher := md5.New()
71 data := hasher.Sum([]byte(text))
72 return base64.StdEncoding.EncodeToString(data)
73 }
74
View as plain text