@@ -11,9 +11,6 @@ import (
11
11
12
12
// V2 SDK
13
13
awsv2 "github.com/aws/aws-sdk-go-v2/aws"
14
- "github.com/aws/aws-sdk-go-v2/aws/retry"
15
- configv2 "github.com/aws/aws-sdk-go-v2/config"
16
- credentialsv2 "github.com/aws/aws-sdk-go-v2/credentials"
17
14
cloudtrailv2 "github.com/aws/aws-sdk-go-v2/service/cloudtrail"
18
15
cloudtrailv2types "github.com/aws/aws-sdk-go-v2/service/cloudtrail/types"
19
16
ec2v2 "github.com/aws/aws-sdk-go-v2/service/ec2"
@@ -52,68 +49,33 @@ type Client interface {
52
49
ListRunningInstances (infraID string ) ([]ec2v2types.Instance , error )
53
50
ListNonRunningInstances (infraID string ) ([]ec2v2types.Instance , error )
54
51
PollInstanceStopEventsFor (instances []ec2v2types.Instance , retryTimes int ) ([]cloudtrailv2types.Event , error )
55
- GetAWSCredentials () awsv2.Credentials
52
+ GetBaseConfig () * awsv2.Config
56
53
GetSecurityGroupID (infraID string ) (string , error )
57
54
GetSubnetID (infraID string ) ([]string , error )
58
55
IsSubnetPrivate (subnet string ) (bool , error )
59
56
GetRouteTableForSubnet (subnetID string ) (ec2v2types.RouteTable , error )
60
57
}
61
58
62
59
type SdkClient struct {
63
- Credentials awsv2.Credentials
60
+ BaseConfig * awsv2.Config
64
61
Region string
65
62
CloudtrailClient CloudTrailAPI
66
63
Ec2Client EC2API
67
64
StsClient StsAPI
68
65
}
69
66
70
- func NewClient (accessID , accessSecret , token , region string ) (* SdkClient , error ) {
71
- staticCredentials := awsv2 .NewCredentialsCache (credentialsv2 .NewStaticCredentialsProvider (accessID , accessSecret , token ))
72
- config , err := configv2 .LoadDefaultConfig (context .TODO (),
73
- configv2 .WithRegion (region ),
74
- configv2 .WithCredentialsProvider (staticCredentials ),
75
- configv2 .WithRetryer (func () awsv2.Retryer {
76
- return retry .AddWithMaxBackoffDelay (retry .AddWithMaxAttempts (retry .NewStandard (), maxRetries ), time .Second * 5 )
77
- }),
78
- )
79
- if err != nil {
80
- return nil , err
81
- }
82
- creds , err := config .Credentials .Retrieve (context .TODO ())
83
- if err != nil {
84
- return nil , err
85
- }
67
+ func NewClient (config awsv2.Config ) (* SdkClient , error ) {
86
68
return & SdkClient {
87
- Credentials : creds ,
88
- Region : region ,
69
+ BaseConfig : & config ,
89
70
CloudtrailClient : cloudtrailv2 .NewFromConfig (config ),
90
71
Ec2Client : ec2v2 .NewFromConfig (config ),
91
72
StsClient : stsv2 .NewFromConfig (config ),
92
73
}, nil
93
74
}
94
75
95
76
// GetAWSCredentials gets the AWS credentials
96
- func (c * SdkClient ) GetAWSCredentials () awsv2.Credentials {
97
- return c .Credentials
98
- }
99
-
100
- // AssumeRole returns you a new client in the account specified in the roleARN
101
- func (c * SdkClient ) AssumeRole (roleARN , region string ) (* SdkClient , error ) {
102
- input := & stsv2.AssumeRoleInput {
103
- RoleArn : & roleARN ,
104
- RoleSessionName : awsv2 .String ("CAD" ),
105
- }
106
- out , err := c .StsClient .AssumeRole (context .TODO (), input )
107
- if err != nil {
108
- return nil , err
109
- }
110
- if region == "" {
111
- region = c .Region
112
- }
113
- return NewClient (* out .Credentials .AccessKeyId ,
114
- * out .Credentials .SecretAccessKey ,
115
- * out .Credentials .SessionToken ,
116
- region )
77
+ func (c * SdkClient ) GetBaseConfig () * awsv2.Config {
78
+ return c .BaseConfig
117
79
}
118
80
119
81
// ListRunningInstances lists all running or starting instances that belong to a cluster
0 commit comments