@@ -2,15 +2,10 @@ package token
2
2
3
3
import (
4
4
"context"
5
- "encoding/base64"
6
5
"strings"
7
6
8
- "golang.org/x/xerrors"
9
-
10
7
"log"
11
8
12
- "github.com/aws/aws-sdk-go/aws/session"
13
- "github.com/aws/aws-sdk-go/service/ecr"
14
9
"github.com/docker/docker/api/types"
15
10
)
16
11
@@ -19,54 +14,28 @@ const (
19
14
gcrURL = "grc.io"
20
15
)
21
16
17
+ type Registry interface {
18
+ GetCredential (ctx context.Context ) (string , string , error )
19
+ }
20
+
22
21
func GetToken (ctx context.Context , auth types.AuthConfig ) types.AuthConfig {
23
22
if auth .Username != "" || auth .Password != "" {
24
23
return auth
25
24
}
26
25
27
- var username , password string
28
- var err error
29
-
26
+ var registry Registry
30
27
switch {
31
28
case strings .HasSuffix (auth .ServerAddress , ecrURL ):
32
- username , password , err = GetECRAuthorizationToken ( ctx )
29
+ registry = NewECR ( )
33
30
case strings .HasSuffix (auth .ServerAddress , gcrURL ):
34
- username , password , err = GetGCRAuthorizationToken ( ctx )
31
+ registry = NewECR ( )
35
32
}
36
- if err != nil {
37
- log .Printf ("failed to get token: %s" , err )
38
- }
39
- auth .Username = username
40
- auth .Password = password
41
- return auth
42
- }
43
-
44
- func GetECRAuthorizationToken (ctx context.Context ) (username , password string , err error ) {
45
- sess := session .Must (session .NewSessionWithOptions (session.Options {
46
- SharedConfigState : session .SharedConfigEnable ,
47
- }))
48
- svc := ecr .New (sess )
49
- input := & ecr.GetAuthorizationTokenInput {}
50
33
51
- result , err := svc .GetAuthorizationTokenWithContext (ctx , input )
34
+ var err error
35
+ auth .Username , auth .Password , err = registry .GetCredential (ctx )
52
36
if err != nil {
53
- return "" , "" , xerrors .Errorf ("failed to get authorization token: %w" , err )
54
- }
55
-
56
- for _ , data := range result .AuthorizationData {
57
- b , err := base64 .StdEncoding .DecodeString (* data .AuthorizationToken )
58
- if err != nil {
59
- return "" , "" , xerrors .Errorf ("base64 decode failed: %w" , err )
60
- }
61
- // e.g. AWS:eyJwYXlsb2...
62
- split := strings .SplitN (string (b ), ":" , 2 )
63
- if len (split ) == 2 {
64
- return split [0 ], split [1 ], nil
65
- }
37
+ log .Printf ("failed to get token: %s" , err )
66
38
}
67
- return "" , "" , nil
68
- }
69
39
70
- func GetGCRAuthorizationToken (ctx context.Context ) (username , password string , err error ) {
71
- return "" , "" , nil
40
+ return auth
72
41
}
0 commit comments