Skip to content

Commit e996e01

Browse files
committed
Add test
1 parent 0954e0f commit e996e01

File tree

5 files changed

+132
-52
lines changed

5 files changed

+132
-52
lines changed

go.mod

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@ module github.com/knqyf263/fanal
33
go 1.12
44

55
require (
6-
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect
7-
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect
86
github.com/aws/aws-sdk-go v1.19.11
97
github.com/docker/distribution v0.0.0-20180920194744-16128bbac47f
108
github.com/docker/docker v0.0.0-20180924202107-a9c061deec0f
119
github.com/genuinetools/reg v0.16.1-0.20190102165523-d959057b30da
1210
github.com/knqyf263/nested v0.0.1
1311
github.com/opencontainers/go-digest v0.0.0-20180430190053-c9281466c8b2
1412
github.com/pkg/errors v0.8.1
15-
github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e
1613
golang.org/x/crypto v0.0.0-20190404164418-38d8ce5564a5
1714
golang.org/x/xerrors v0.0.0-20190315151331-d61658bd2e18
18-
gopkg.in/alecthomas/kingpin.v2 v2.2.6 // indirect
1915
)

go.sum

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ github.com/Microsoft/go-winio v0.4.11 h1:zoIOcVf0xPN1tnMVbTtEdI+P8OofVk3NObnwOQ6
55
github.com/Microsoft/go-winio v0.4.11/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA=
66
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEVMRuU21PR1EtLVZJmdB18Gu3Rw=
77
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8D7ML55dXQrVaamCz2vxCfdQBasLZfHKk=
8-
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU=
9-
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
10-
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY=
11-
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
128
github.com/aws/aws-sdk-go v1.19.11 h1:tqaTGER6Byw3QvsjGW0p018U2UOqaJPeJuzoaF7jjoQ=
139
github.com/aws/aws-sdk-go v1.19.11/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
1410
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLMYoU8P317H5OQ+Via4RmuPwCS0=
@@ -133,8 +129,6 @@ google.golang.org/grpc v1.15.0 h1:Az/KuahOM4NAidTEuJCv/RonAA7rYsTPkqXVjr+8OOw=
133129
google.golang.org/grpc v1.15.0/go.mod h1:0JHn/cJsOMiMfNA9+DeHDlAU7KAAB5GDlYFpa9MZMio=
134130
gopkg.in/airbrake/gobrake.v2 v2.0.9 h1:7z2uVWwn7oVeeugY1DtlPAy5H+KYgB1KeKTnqjNatLo=
135131
gopkg.in/airbrake/gobrake.v2 v2.0.9/go.mod h1:/h5ZAUhDkGaJfjzjKLSjv6zCL6O0LLBxU4K+aSYdM/U=
136-
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
137-
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
138132
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
139133
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
140134
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=

token/ecr.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package token
2+
3+
import (
4+
"context"
5+
"encoding/base64"
6+
"strings"
7+
8+
"github.com/aws/aws-sdk-go/aws/session"
9+
"github.com/aws/aws-sdk-go/service/ecr"
10+
"github.com/aws/aws-sdk-go/service/ecr/ecriface"
11+
"golang.org/x/xerrors"
12+
)
13+
14+
type ECR struct {
15+
Client ecriface.ECRAPI
16+
}
17+
18+
func NewECR() *ECR {
19+
sess := session.Must(session.NewSessionWithOptions(session.Options{
20+
SharedConfigState: session.SharedConfigEnable,
21+
}))
22+
svc := ecr.New(sess)
23+
return &ECR{
24+
Client: svc,
25+
}
26+
}
27+
28+
func (e *ECR) GetCredential(ctx context.Context) (username, password string, err error) {
29+
input := &ecr.GetAuthorizationTokenInput{}
30+
31+
result, err := e.Client.GetAuthorizationTokenWithContext(ctx, input)
32+
if err != nil {
33+
return "", "", xerrors.Errorf("failed to get authorization token: %w", err)
34+
}
35+
36+
for _, data := range result.AuthorizationData {
37+
b, err := base64.StdEncoding.DecodeString(*data.AuthorizationToken)
38+
if err != nil {
39+
return "", "", xerrors.Errorf("base64 decode failed: %w", err)
40+
}
41+
// e.g. AWS:eyJwYXlsb2...
42+
split := strings.SplitN(string(b), ":", 2)
43+
if len(split) == 2 {
44+
return split[0], split[1], nil
45+
}
46+
}
47+
return "", "", nil
48+
}

token/ecr_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package token
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/aws/aws-sdk-go/aws/request"
8+
9+
"github.com/aws/aws-sdk-go/aws"
10+
"github.com/aws/aws-sdk-go/service/ecr"
11+
"github.com/aws/aws-sdk-go/service/ecr/ecriface"
12+
)
13+
14+
type mockedECR struct {
15+
ecriface.ECRAPI
16+
Resp ecr.GetAuthorizationTokenOutput
17+
}
18+
19+
func (m mockedECR) GetAuthorizationTokenWithContext(ctx context.Context, input *ecr.GetAuthorizationTokenInput, options ...request.Option) (*ecr.GetAuthorizationTokenOutput, error) {
20+
return &m.Resp, nil
21+
}
22+
23+
func TestECRGetCredential(t *testing.T) {
24+
cases := []struct {
25+
Resp ecr.GetAuthorizationTokenOutput
26+
expectedUser string
27+
expectedPassword string
28+
}{
29+
{
30+
Resp: ecr.GetAuthorizationTokenOutput{
31+
AuthorizationData: []*ecr.AuthorizationData{
32+
{AuthorizationToken: aws.String("YXdzOnBhc3N3b3Jk")},
33+
},
34+
},
35+
expectedUser: "aws",
36+
expectedPassword: "password",
37+
},
38+
{
39+
Resp: ecr.GetAuthorizationTokenOutput{
40+
AuthorizationData: []*ecr.AuthorizationData{
41+
{AuthorizationToken: aws.String("YXdzOnBhc3N3b3JkOmJhZA==")},
42+
},
43+
},
44+
expectedUser: "aws",
45+
expectedPassword: "password:bad",
46+
},
47+
{
48+
Resp: ecr.GetAuthorizationTokenOutput{
49+
AuthorizationData: []*ecr.AuthorizationData{
50+
{AuthorizationToken: aws.String("YXdzcGFzc3dvcmQ=")},
51+
},
52+
},
53+
expectedUser: "",
54+
expectedPassword: "",
55+
},
56+
}
57+
58+
for i, c := range cases {
59+
e := ECR{
60+
Client: mockedECR{Resp: c.Resp},
61+
}
62+
username, password, err := e.GetCredential(context.Background())
63+
if err != nil {
64+
t.Fatalf("%d, unexpected error", err)
65+
}
66+
if username != c.expectedUser {
67+
t.Fatalf("%d, username: expected %s, got %s", i, c.expectedUser, username)
68+
}
69+
if password != c.expectedPassword {
70+
t.Fatalf("%d, password: expected %s, got %s", i, c.expectedPassword, password)
71+
}
72+
}
73+
}

token/token.go

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@ package token
22

33
import (
44
"context"
5-
"encoding/base64"
65
"strings"
76

8-
"golang.org/x/xerrors"
9-
107
"log"
118

12-
"github.com/aws/aws-sdk-go/aws/session"
13-
"github.com/aws/aws-sdk-go/service/ecr"
149
"github.com/docker/docker/api/types"
1510
)
1611

@@ -19,54 +14,28 @@ const (
1914
gcrURL = "grc.io"
2015
)
2116

17+
type Registry interface {
18+
GetCredential(ctx context.Context) (string, string, error)
19+
}
20+
2221
func GetToken(ctx context.Context, auth types.AuthConfig) types.AuthConfig {
2322
if auth.Username != "" || auth.Password != "" {
2423
return auth
2524
}
2625

27-
var username, password string
28-
var err error
29-
26+
var registry Registry
3027
switch {
3128
case strings.HasSuffix(auth.ServerAddress, ecrURL):
32-
username, password, err = GetECRAuthorizationToken(ctx)
29+
registry = NewECR()
3330
case strings.HasSuffix(auth.ServerAddress, gcrURL):
34-
username, password, err = GetGCRAuthorizationToken(ctx)
31+
registry = NewECR()
3532
}
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{}
5033

51-
result, err := svc.GetAuthorizationTokenWithContext(ctx, input)
34+
var err error
35+
auth.Username, auth.Password, err = registry.GetCredential(ctx)
5236
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)
6638
}
67-
return "", "", nil
68-
}
6939

70-
func GetGCRAuthorizationToken(ctx context.Context) (username, password string, err error) {
71-
return "", "", nil
40+
return auth
7241
}

0 commit comments

Comments
 (0)