-
Notifications
You must be signed in to change notification settings - Fork 155
/
client.go
131 lines (105 loc) · 3.78 KB
/
client.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
Copyright 2021 The Kubermatic Kubernetes Platform contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License 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 aws
import (
"context"
"errors"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
awscredentials "github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/eks"
"github.com/aws/aws-sdk-go-v2/service/iam"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/aws/smithy-go"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/utils/ptr"
)
const (
maxRetries = 3
)
type ClientSet struct {
EC2 *ec2.Client
EKS *eks.Client
IAM *iam.Client
}
type endpointResolver struct {
Url string
}
func (e *endpointResolver) ResolveEndpoint(service, region string, options ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{
URL: e.Url,
SigningName: service,
SigningRegion: region,
}, nil
}
func ValidateCredentials(ctx context.Context, accessKeyID, secretAccessKey string) error {
cfg, err := awsconfig.LoadDefaultConfig(ctx,
awsconfig.WithRegion("us-east-2"),
awsconfig.WithCredentialsProvider(awscredentials.NewStaticCredentialsProvider(accessKeyID, secretAccessKey, "")),
awsconfig.WithRetryMaxAttempts(maxRetries),
)
if err != nil {
return err
}
client := ec2.NewFromConfig(cfg)
_, err = client.DescribeRegions(ctx, &ec2.DescribeRegionsInput{})
return err
}
func GetClientSet(ctx context.Context, accessKeyID, secretAccessKey, assumeRoleARN, assumeRoleExternalID, region string) (*ClientSet, error) {
return getClientSet(ctx, accessKeyID, secretAccessKey, assumeRoleARN, assumeRoleExternalID, region, "")
}
func GetAWSConfig(ctx context.Context, accessKeyID, secretAccessKey, assumeRoleARN, assumeRoleExternalID, region, endpoint string) (aws.Config, error) {
opts := []func(*awsconfig.LoadOptions) error{
awsconfig.WithRegion(region),
awsconfig.WithCredentialsProvider(awscredentials.NewStaticCredentialsProvider(accessKeyID, secretAccessKey, "")),
awsconfig.WithRetryMaxAttempts(maxRetries),
}
if endpoint != "" {
opts = append(opts,
awsconfig.WithEndpointResolverWithOptions(&endpointResolver{Url: endpoint}),
)
}
cfg, err := awsconfig.LoadDefaultConfig(ctx, opts...)
if err != nil {
return aws.Config{}, err
}
if assumeRoleARN != "" {
stsSvc := sts.NewFromConfig(cfg)
creds := stscreds.NewAssumeRoleProvider(stsSvc, assumeRoleARN,
func(o *stscreds.AssumeRoleOptions) {
o.ExternalID = ptr.To(assumeRoleExternalID)
},
)
cfg.Credentials = creds
}
return cfg, nil
}
func getClientSet(ctx context.Context, accessKeyID, secretAccessKey, assumeRoleARN, assumeRoleExternalID, region, endpoint string) (*ClientSet, error) {
cfg, err := GetAWSConfig(ctx, accessKeyID, secretAccessKey, assumeRoleARN, assumeRoleExternalID, region, endpoint)
if err != nil {
return nil, fmt.Errorf("failed to create API session: %w", err)
}
return &ClientSet{
EC2: ec2.NewFromConfig(cfg),
EKS: eks.NewFromConfig(cfg),
IAM: iam.NewFromConfig(cfg),
}, nil
}
var notFoundErrors = sets.New("NoSuchEntity", "InvalidVpcID.NotFound", "InvalidRouteTableID.NotFound", "InvalidGroup.NotFound")
func isNotFound(err error) bool {
var aerr smithy.APIError
return errors.As(err, &aerr) && notFoundErrors.Has(aerr.ErrorCode())
}