-
Notifications
You must be signed in to change notification settings - Fork 3
/
aws.go
197 lines (162 loc) · 5.25 KB
/
aws.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package credential
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/hashicorp/hcl/v2"
"github.com/turbot/go-kit/helpers"
"github.com/turbot/pipe-fittings/v2/modconfig"
"github.com/turbot/pipe-fittings/v2/utils"
"github.com/zclconf/go-cty/cty"
)
type AwsCredential struct {
CredentialImpl
AccessKey *string `json:"access_key,omitempty" cty:"access_key" hcl:"access_key,optional"`
SecretKey *string `json:"secret_key,omitempty" cty:"secret_key" hcl:"secret_key,optional"`
SessionToken *string `json:"session_token,omitempty" cty:"session_token" hcl:"session_token,optional"`
Ttl *int `json:"ttl,omitempty" cty:"ttl" hcl:"ttl,optional"`
Profile *string `json:"profile,omitempty" cty:"profile" hcl:"profile,optional"`
}
func (c *AwsCredential) Validate() hcl.Diagnostics {
if c.AccessKey != nil && c.SecretKey == nil {
return hcl.Diagnostics{
{
Severity: hcl.DiagError,
Summary: "access_key defined without secret_key",
Subject: &c.DeclRange,
},
}
}
if c.SecretKey != nil && c.AccessKey == nil {
return hcl.Diagnostics{
{
Severity: hcl.DiagError,
Summary: "secret_key defined without access_key",
Subject: &c.DeclRange,
},
}
}
return hcl.Diagnostics{}
}
func (c *AwsCredential) Resolve(ctx context.Context) (Credential, error) {
// if access key and secret key are provided, just return it
if c.AccessKey != nil && c.SecretKey != nil {
return c, nil
}
var cfg aws.Config
var err error
// Load the AWS configuration from the shared credentials file
if c.Profile != nil {
cfg, err = config.LoadDefaultConfig(ctx, config.WithSharedConfigProfile(*c.Profile), config.WithRegion("us-east-1"))
if err != nil {
return nil, err
}
} else {
cfg, err = config.LoadDefaultConfig(context.TODO(), config.WithRegion("us-east-1"))
if err != nil {
return nil, err
}
}
// Access the credentials from the configuration
creds, err := cfg.Credentials.Retrieve(context.TODO())
if err != nil {
return nil, err
}
// Don't modify existing credential, resolve to a new one
newCreds := &AwsCredential{
CredentialImpl: c.CredentialImpl,
Ttl: c.Ttl,
AccessKey: &creds.AccessKeyID,
SecretKey: &creds.SecretAccessKey,
}
if creds.SessionToken != "" {
newCreds.SessionToken = &creds.SessionToken
}
return newCreds, nil
}
// in seconds
func (c *AwsCredential) GetTtl() int {
if c.Ttl == nil {
return 5 * 60
}
return *c.Ttl
}
func (c *AwsCredential) getEnv() map[string]cty.Value {
env := map[string]cty.Value{}
if c.AccessKey != nil {
env["AWS_ACCESS_KEY_ID"] = cty.StringVal(*c.AccessKey)
}
if c.SecretKey != nil {
env["AWS_SECRET_ACCESS_KEY"] = cty.StringVal(*c.SecretKey)
}
if c.SessionToken != nil {
env["AWS_SESSION_TOKEN"] = cty.StringVal(*c.SessionToken)
}
return env
}
func (c *AwsCredential) CtyValue() (cty.Value, error) {
ctyValue, err := modconfig.GetCtyValue(c)
if err != nil {
return cty.NilVal, err
}
valueMap := ctyValue.AsValueMap()
valueMap["env"] = cty.ObjectVal(c.getEnv())
return cty.ObjectVal(valueMap), nil
}
func (c *AwsCredential) Equals(otherCredential Credential) bool {
// If both pointers are nil, they are considered equal
if c == nil && helpers.IsNil(otherCredential) {
return true
}
if (c == nil && !helpers.IsNil(otherCredential)) || (c != nil && helpers.IsNil(otherCredential)) {
return false
}
other, ok := otherCredential.(*AwsCredential)
if !ok {
return false
}
if !utils.PtrEqual(c.AccessKey, other.AccessKey) {
return false
}
if !utils.PtrEqual(c.SecretKey, other.SecretKey) {
return false
}
if !utils.PtrEqual(c.Profile, other.Profile) {
return false
}
if !utils.SafeIntEqual(c.Ttl, other.Ttl) {
return false
}
return true
}
// TODO: should we merge AwsConnectionConfig with AwsCredential? They have distinct usage but the data model is very similar
type AwsConnectionConfig struct {
Regions []string `cty:"regions" hcl:"regions,optional"`
DefaultRegion *string `cty:"default_region" hcl:"default_region,optional"`
Profile *string `cty:"profile" hcl:"profile,optional"`
AccessKey *string `cty:"access_key" hcl:"access_key,optional"`
SecretKey *string `cty:"secret_key" hcl:"secret_key,optional"`
SessionToken *string `cty:"session_token" hcl:"session_token,optional"`
MaxErrorRetryAttempts *int `cty:"max_error_retry_attempts" hcl:"max_error_retry_attempts,optional"`
MinErrorRetryDelay *int `cty:"min_error_retry_delay" hcl:"min_error_retry_delay,optional"`
IgnoreErrorCodes []string `cty:"ignore_error_codes" hcl:"ignore_error_codes,optional"`
EndpointUrl *string `cty:"endpoint_url" hcl:"endpoint_url,optional"`
S3ForcePathStyle *bool `cty:"s3_force_path_style" hcl:"s3_force_path_style,optional"`
}
func (c *AwsConnectionConfig) GetCredential(name, shortName string) Credential {
awsCred := &AwsCredential{
CredentialImpl: CredentialImpl{
HclResourceImpl: modconfig.HclResourceImpl{
FullName: name,
ShortName: shortName,
UnqualifiedName: name,
},
Type: "aws",
},
Profile: c.Profile,
AccessKey: c.AccessKey,
SecretKey: c.SecretKey,
SessionToken: c.SessionToken,
}
return awsCred
}