-
Notifications
You must be signed in to change notification settings - Fork 335
/
factory.go
107 lines (92 loc) · 3.6 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
// Copyright 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 (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ecr"
"github.com/aws/aws-sdk-go-v2/service/ecrpublic"
"github.com/aws/smithy-go/middleware"
"github.com/aws/smithy-go/transport/http"
"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 {
Config aws.Config
CacheDir string
}
// ClientFactory is a factory for creating clients to interact with ECR
type ClientFactory interface {
NewClient(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 userAgentLoadOption = config.WithAPIOptions([]func(*middleware.Stack) error{
http.AddHeaderValue("User-Agent", "amazon-ecr-credential-helper/"+version.Version),
})
// NewClientWithDefaults creates the client and defaults region
func (defaultClientFactory DefaultClientFactory) NewClientWithDefaults() Client {
awsConfig, err := config.LoadDefaultConfig(context.TODO(), userAgentLoadOption)
if err != nil {
panic(err)
}
return defaultClientFactory.NewClientWithOptions(Options{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) {
awsConfig, err := config.LoadDefaultConfig(
context.TODO(),
userAgentLoadOption,
config.WithRegion(region),
config.WithEndpointDiscovery(aws.EndpointDiscoveryEnabled),
)
if err != nil {
return nil, err
}
return defaultClientFactory.NewClientWithOptions(Options{Config: awsConfig}), nil
}
// NewClientFromRegion uses the region to create the client
func (defaultClientFactory DefaultClientFactory) NewClientFromRegion(region string) Client {
awsConfig, err := config.LoadDefaultConfig(
context.TODO(),
userAgentLoadOption,
config.WithRegion(region),
)
if err != nil {
panic(err)
}
return defaultClientFactory.NewClientWithOptions(Options{
Config: awsConfig,
})
}
// NewClient Create new client with AWS Config
func (defaultClientFactory DefaultClientFactory) NewClient(awsConfig aws.Config) Client {
return defaultClientFactory.NewClientWithOptions(Options{Config: awsConfig})
}
// NewClientWithOptions Create new client with Options
func (defaultClientFactory DefaultClientFactory) NewClientWithOptions(opts Options) Client {
// The ECR Public API is only available in us-east-1 today
publicConfig := opts.Config.Copy()
publicConfig.Region = "us-east-1"
return &defaultClient{
ecrClient: ecr.NewFromConfig(opts.Config),
ecrPublicClient: ecrpublic.NewFromConfig(publicConfig),
credentialCache: cache.BuildCredentialsCache(opts.Config, opts.CacheDir),
}
}