-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathecr.go
34 lines (27 loc) · 1.02 KB
/
ecr.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
package main
import (
"context"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/pkg/errors"
)
// Subset so we can test, we can fake the subset of ECR that the controller needs
type ecrClient int
func newECRClient() ecrClient {
return ecrClient(0)
}
// Need to support multiple ECR repos so we cannot relay on normal env vars or config file, hence the region id and secret args
func (e ecrClient) GetAuthToken(ctx context.Context, region, id, secret string) (*ecr.AuthorizationData, error) {
creds := credentials.NewStaticCredentials(id, secret, "")
config := aws.NewConfig().WithCredentials(creds).WithRegion(region)
sess, _ := session.NewSession(config)
svc := ecr.New(sess)
inp := &ecr.GetAuthorizationTokenInput{}
out, err := svc.GetAuthorizationTokenWithContext(ctx, inp)
if err != nil {
return nil, errors.Wrap(err, "get ECR authorization token failed")
}
return out.AuthorizationData[0], nil
}