forked from awslabs/amazon-ecr-credential-helper
/
factory.go
111 lines (97 loc) · 3.89 KB
/
factory.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
// Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 api
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/awslabs/amazon-ecr-credential-helper/ecr-login/cache"
"github.com/awslabs/amazon-ecr-credential-helper/ecr-login/version"
)
// Options makes the constructors more configurable
type Options struct {
Session *session.Session
Config *aws.Config
CacheDir string
}
// ClientFactory is a factory for creating clients to interact with ECR
type ClientFactory interface {
NewClient(awsSession *session.Session, awsConfig *aws.Config) Client
NewClientWithOptions(opts Options) Client
NewClientFromRegion(region string) Client
NewClientWithFipsEndpoint(region string) (Client, error)
NewClientWithDefaults() Client
}
// DefaultClientFactory is a default implementation of the ClientFactory
type DefaultClientFactory struct{}
var userAgentHandler = request.NamedHandler{
Name: "ecr-login.UserAgentHandler",
Fn: request.MakeAddToUserAgentHandler("amazon-ecr-credential-helper", version.Version),
}
// NewClientWithDefaults creates the client and defaults region
func (defaultClientFactory DefaultClientFactory) NewClientWithDefaults() Client {
awsSession := session.New()
awsSession.Handlers.Build.PushBackNamed(userAgentHandler)
awsConfig := awsSession.Config
return defaultClientFactory.NewClientWithOptions(Options{
Session: awsSession,
Config: awsConfig,
})
}
// NewClientWithFipsEndpoint overrides the default ECR service endpoint in a given region to use the FIPS endpoint
func (defaultClientFactory DefaultClientFactory) NewClientWithFipsEndpoint(region string) (Client, error) {
awsSession := session.New()
awsSession.Handlers.Build.PushBackNamed(userAgentHandler)
endpoint, err := getServiceEndpoint("ecr-fips", region)
if err != nil {
return nil, err
}
awsConfig := awsSession.Config.WithEndpoint(endpoint).WithRegion(region)
return defaultClientFactory.NewClientWithOptions(Options{
Session: awsSession,
Config: awsConfig,
}), nil
}
// NewClientFromRegion uses the region to create the client
func (defaultClientFactory DefaultClientFactory) NewClientFromRegion(region string) Client {
awsSession := session.New()
awsSession.Handlers.Build.PushBackNamed(userAgentHandler)
awsConfig := &aws.Config{Region: aws.String(region)}
return defaultClientFactory.NewClientWithOptions(Options{
Session: awsSession,
Config: awsConfig,
})
}
// NewClient Create new client with AWS Config
func (defaultClientFactory DefaultClientFactory) NewClient(awsSession *session.Session, awsConfig *aws.Config) Client {
return defaultClientFactory.NewClientWithOptions(Options{
Session: awsSession,
Config: awsConfig,
})
}
// NewClientWithOptions Create new client with Options
func (defaultClientFactory DefaultClientFactory) NewClientWithOptions(opts Options) Client {
return &defaultClient{
ecrClient: ecr.New(opts.Session, opts.Config),
credentialCache: cache.BuildCredentialsCache(opts.Session, aws.StringValue(opts.Config.Region), opts.CacheDir),
}
}
func getServiceEndpoint(service, region string) (string, error) {
resolver := endpoints.DefaultResolver()
endpoint, err := resolver.EndpointFor(service, region, func(opts *endpoints.Options) {
opts.ResolveUnknownService = true
})
return endpoint.URL, err
}