-
-
Notifications
You must be signed in to change notification settings - Fork 343
/
Copy pathfactory.go
457 lines (411 loc) · 16.2 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package v2
import (
"context"
"fmt"
"os"
"sync"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/retry"
aws_config "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/aws-sdk-go-v2/service/amp"
"github.com/aws/aws-sdk-go-v2/service/apigateway"
"github.com/aws/aws-sdk-go-v2/service/apigatewayv2"
"github.com/aws/aws-sdk-go-v2/service/autoscaling"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch"
"github.com/aws/aws-sdk-go-v2/service/databasemigrationservice"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/resourcegroupstaggingapi"
"github.com/aws/aws-sdk-go-v2/service/shield"
"github.com/aws/aws-sdk-go-v2/service/storagegateway"
"github.com/aws/aws-sdk-go-v2/service/sts"
aws_logging "github.com/aws/smithy-go/logging"
"github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/clients"
"github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/clients/account"
account_v2 "github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/clients/account/v2"
cloudwatch_client "github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/clients/cloudwatch"
cloudwatch_v2 "github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/clients/cloudwatch/v2"
"github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/clients/tagging"
tagging_v2 "github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/clients/tagging/v2"
"github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/logging"
"github.com/nerdswords/yet-another-cloudwatch-exporter/pkg/model"
)
type awsRegion = string
type CachingFactory struct {
logger logging.Logger
stsOptions func(*sts.Options)
clients map[model.Role]map[awsRegion]*cachedClients
mu sync.Mutex
refreshed bool
cleared bool
fipsEnabled bool
endpointURLOverride string
}
type cachedClients struct {
awsConfig *aws.Config
// if we know that this job is only used for static
// then we don't have to construct as many cached connections
// later on
onlyStatic bool
cloudwatch cloudwatch_client.Client
tagging tagging.Client
account account.Client
}
// Ensure the struct properly implements the interface
var _ clients.Factory = &CachingFactory{}
// NewFactory creates a new client factory to use when fetching data from AWS with sdk v2
func NewFactory(logger logging.Logger, jobsCfg model.JobsConfig, fips bool) (*CachingFactory, error) {
var options []func(*aws_config.LoadOptions) error
options = append(options, aws_config.WithLogger(aws_logging.LoggerFunc(func(classification aws_logging.Classification, format string, v ...interface{}) {
if classification == aws_logging.Debug {
if logger.IsDebugEnabled() {
logger.Debug(fmt.Sprintf(format, v...))
}
} else if classification == aws_logging.Warn {
logger.Warn(fmt.Sprintf(format, v...))
} else { // AWS logging only supports debug or warn, log everything else as error
logger.Error(fmt.Errorf("unexected aws error classification: %s", classification), fmt.Sprintf(format, v...))
}
})))
options = append(options, aws_config.WithLogConfigurationWarnings(true))
endpointURLOverride := os.Getenv("AWS_ENDPOINT_URL")
options = append(options, aws_config.WithRetryMaxAttempts(5))
c, err := aws_config.LoadDefaultConfig(context.TODO(), options...)
if err != nil {
return nil, fmt.Errorf("failed to load default aws config: %w", err)
}
stsOptions := createStsOptions(jobsCfg.StsRegion, logger.IsDebugEnabled(), endpointURLOverride, fips)
cache := map[model.Role]map[awsRegion]*cachedClients{}
for _, discoveryJob := range jobsCfg.DiscoveryJobs {
for _, role := range discoveryJob.Roles {
if _, ok := cache[role]; !ok {
cache[role] = map[awsRegion]*cachedClients{}
}
for _, region := range discoveryJob.Regions {
regionConfig := awsConfigForRegion(role, &c, region, stsOptions)
cache[role][region] = &cachedClients{
awsConfig: regionConfig,
onlyStatic: false,
}
}
}
}
for _, staticJob := range jobsCfg.StaticJobs {
for _, role := range staticJob.Roles {
if _, ok := cache[role]; !ok {
cache[role] = map[awsRegion]*cachedClients{}
}
for _, region := range staticJob.Regions {
// Discovery job client definitions have precedence
if _, exists := cache[role][region]; !exists {
regionConfig := awsConfigForRegion(role, &c, region, stsOptions)
cache[role][region] = &cachedClients{
awsConfig: regionConfig,
onlyStatic: true,
}
}
}
}
}
for _, customNamespaceJob := range jobsCfg.CustomNamespaceJobs {
for _, role := range customNamespaceJob.Roles {
if _, ok := cache[role]; !ok {
cache[role] = map[awsRegion]*cachedClients{}
}
for _, region := range customNamespaceJob.Regions {
// Discovery job client definitions have precedence
if _, exists := cache[role][region]; !exists {
regionConfig := awsConfigForRegion(role, &c, region, stsOptions)
cache[role][region] = &cachedClients{
awsConfig: regionConfig,
onlyStatic: true,
}
}
}
}
}
return &CachingFactory{
logger: logger,
clients: cache,
fipsEnabled: fips,
stsOptions: stsOptions,
endpointURLOverride: endpointURLOverride,
}, nil
}
func (c *CachingFactory) GetCloudwatchClient(region string, role model.Role, concurrency cloudwatch_client.ConcurrencyConfig) cloudwatch_client.Client {
if !c.refreshed {
// if we have not refreshed then we need to lock in case we are accessing concurrently
c.mu.Lock()
defer c.mu.Unlock()
}
if client := c.clients[role][region].cloudwatch; client != nil {
return cloudwatch_client.NewLimitedConcurrencyClient(client, concurrency.NewLimiter())
}
c.clients[role][region].cloudwatch = cloudwatch_v2.NewClient(c.logger, c.createCloudwatchClient(c.clients[role][region].awsConfig))
return cloudwatch_client.NewLimitedConcurrencyClient(c.clients[role][region].cloudwatch, concurrency.NewLimiter())
}
func (c *CachingFactory) GetTaggingClient(region string, role model.Role, concurrencyLimit int) tagging.Client {
if !c.refreshed {
// if we have not refreshed then we need to lock in case we are accessing concurrently
c.mu.Lock()
defer c.mu.Unlock()
}
if client := c.clients[role][region].tagging; client != nil {
return tagging.NewLimitedConcurrencyClient(client, concurrencyLimit)
}
c.clients[role][region].tagging = tagging_v2.NewClient(
c.logger,
c.createTaggingClient(c.clients[role][region].awsConfig),
c.createAutoScalingClient(c.clients[role][region].awsConfig),
c.createAPIGatewayClient(c.clients[role][region].awsConfig),
c.createAPIGatewayV2Client(c.clients[role][region].awsConfig),
c.createEC2Client(c.clients[role][region].awsConfig),
c.createDMSClient(c.clients[role][region].awsConfig),
c.createPrometheusClient(c.clients[role][region].awsConfig),
c.createStorageGatewayClient(c.clients[role][region].awsConfig),
c.createShieldClient(c.clients[role][region].awsConfig),
)
return tagging.NewLimitedConcurrencyClient(c.clients[role][region].tagging, concurrencyLimit)
}
func (c *CachingFactory) GetAccountClient(region string, role model.Role) account.Client {
if !c.refreshed {
// if we have not refreshed then we need to lock in case we are accessing concurrently
c.mu.Lock()
defer c.mu.Unlock()
}
if client := c.clients[role][region].account; client != nil {
return client
}
c.clients[role][region].account = account_v2.NewClient(c.logger, c.createStsClient(c.clients[role][region].awsConfig))
return c.clients[role][region].account
}
func (c *CachingFactory) Refresh() {
if c.refreshed {
return
}
c.mu.Lock()
defer c.mu.Unlock()
// Avoid double refresh in the event Refresh() is called concurrently
if c.refreshed {
return
}
for _, regionClients := range c.clients {
for _, cache := range regionClients {
cache.cloudwatch = cloudwatch_v2.NewClient(c.logger, c.createCloudwatchClient(cache.awsConfig))
if cache.onlyStatic {
continue
}
cache.tagging = tagging_v2.NewClient(
c.logger,
c.createTaggingClient(cache.awsConfig),
c.createAutoScalingClient(cache.awsConfig),
c.createAPIGatewayClient(cache.awsConfig),
c.createAPIGatewayV2Client(cache.awsConfig),
c.createEC2Client(cache.awsConfig),
c.createDMSClient(cache.awsConfig),
c.createPrometheusClient(cache.awsConfig),
c.createStorageGatewayClient(cache.awsConfig),
c.createShieldClient(cache.awsConfig),
)
cache.account = account_v2.NewClient(c.logger, c.createStsClient(cache.awsConfig))
}
}
c.refreshed = true
c.cleared = false
}
func (c *CachingFactory) Clear() {
if c.cleared {
return
}
// Prevent concurrent reads/write if clear is called during execution
c.mu.Lock()
defer c.mu.Unlock()
// Avoid double clear in the event Refresh() is called concurrently
if c.cleared {
return
}
for _, regions := range c.clients {
for _, cache := range regions {
cache.cloudwatch = nil
cache.account = nil
cache.tagging = nil
}
}
c.refreshed = false
c.cleared = true
}
func (c *CachingFactory) createCloudwatchClient(regionConfig *aws.Config) *cloudwatch.Client {
return cloudwatch.NewFromConfig(*regionConfig, func(options *cloudwatch.Options) {
if c.logger.IsDebugEnabled() {
options.ClientLogMode = aws.LogRequestWithBody | aws.LogResponseWithBody
}
if c.endpointURLOverride != "" {
options.BaseEndpoint = aws.String(c.endpointURLOverride)
}
// Setting an explicit retryer will override the default settings on the config
options.Retryer = retry.NewStandard(func(options *retry.StandardOptions) {
options.MaxAttempts = 5
options.MaxBackoff = 3 * time.Second
})
if c.fipsEnabled {
options.EndpointOptions.UseFIPSEndpoint = aws.FIPSEndpointStateEnabled
}
})
}
func (c *CachingFactory) createTaggingClient(regionConfig *aws.Config) *resourcegroupstaggingapi.Client {
return resourcegroupstaggingapi.NewFromConfig(*regionConfig, func(options *resourcegroupstaggingapi.Options) {
if c.logger.IsDebugEnabled() {
options.ClientLogMode = aws.LogRequestWithBody | aws.LogResponseWithBody
}
if c.endpointURLOverride != "" {
options.BaseEndpoint = aws.String(c.endpointURLOverride)
}
// The FIPS setting is ignored because FIPS is not available for resource groups tagging apis
// If enabled the SDK will try to use non-existent FIPS URLs, https://github.com/aws/aws-sdk-go-v2/issues/2138#issuecomment-1570791988
// AWS FIPS Reference: https://aws.amazon.com/compliance/fips/
})
}
func (c *CachingFactory) createAutoScalingClient(assumedConfig *aws.Config) *autoscaling.Client {
return autoscaling.NewFromConfig(*assumedConfig, func(options *autoscaling.Options) {
if c.logger.IsDebugEnabled() {
options.ClientLogMode = aws.LogRequestWithBody | aws.LogResponseWithBody
}
if c.endpointURLOverride != "" {
options.BaseEndpoint = aws.String(c.endpointURLOverride)
}
// The FIPS setting is ignored because FIPS is not available for EC2 autoscaling apis
// If enabled the SDK will try to use non-existent FIPS URLs, https://github.com/aws/aws-sdk-go-v2/issues/2138#issuecomment-1570791988
// AWS FIPS Reference: https://aws.amazon.com/compliance/fips/
// EC2 autoscaling has FIPS compliant URLs for govcloud, but they do not use any FIPS prefixing, and should work
// with sdk v2s EndpointResolverV2
})
}
func (c *CachingFactory) createAPIGatewayClient(assumedConfig *aws.Config) *apigateway.Client {
return apigateway.NewFromConfig(*assumedConfig, func(options *apigateway.Options) {
if c.logger.IsDebugEnabled() {
options.ClientLogMode = aws.LogRequestWithBody | aws.LogResponseWithBody
}
if c.endpointURLOverride != "" {
options.BaseEndpoint = aws.String(c.endpointURLOverride)
}
if c.fipsEnabled {
options.EndpointOptions.UseFIPSEndpoint = aws.FIPSEndpointStateEnabled
}
})
}
func (c *CachingFactory) createAPIGatewayV2Client(assumedConfig *aws.Config) *apigatewayv2.Client {
return apigatewayv2.NewFromConfig(*assumedConfig, func(options *apigatewayv2.Options) {
if c.logger.IsDebugEnabled() {
options.ClientLogMode = aws.LogRequestWithBody | aws.LogResponseWithBody
}
if c.endpointURLOverride != "" {
options.BaseEndpoint = aws.String(c.endpointURLOverride)
}
if c.fipsEnabled {
options.EndpointOptions.UseFIPSEndpoint = aws.FIPSEndpointStateEnabled
}
})
}
func (c *CachingFactory) createEC2Client(assumedConfig *aws.Config) *ec2.Client {
return ec2.NewFromConfig(*assumedConfig, func(options *ec2.Options) {
if c.logger.IsDebugEnabled() {
options.ClientLogMode = aws.LogRequestWithBody | aws.LogResponseWithBody
}
if c.endpointURLOverride != "" {
options.BaseEndpoint = aws.String(c.endpointURLOverride)
}
if c.fipsEnabled {
options.EndpointOptions.UseFIPSEndpoint = aws.FIPSEndpointStateEnabled
}
})
}
func (c *CachingFactory) createDMSClient(assumedConfig *aws.Config) *databasemigrationservice.Client {
return databasemigrationservice.NewFromConfig(*assumedConfig, func(options *databasemigrationservice.Options) {
if c.logger.IsDebugEnabled() {
options.ClientLogMode = aws.LogRequestWithBody | aws.LogResponseWithBody
}
if c.endpointURLOverride != "" {
options.BaseEndpoint = aws.String(c.endpointURLOverride)
}
if c.fipsEnabled {
options.EndpointOptions.UseFIPSEndpoint = aws.FIPSEndpointStateEnabled
}
})
}
func (c *CachingFactory) createStorageGatewayClient(assumedConfig *aws.Config) *storagegateway.Client {
return storagegateway.NewFromConfig(*assumedConfig, func(options *storagegateway.Options) {
if c.logger.IsDebugEnabled() {
options.ClientLogMode = aws.LogRequestWithBody | aws.LogResponseWithBody
}
if c.endpointURLOverride != "" {
options.BaseEndpoint = aws.String(c.endpointURLOverride)
}
if c.fipsEnabled {
options.EndpointOptions.UseFIPSEndpoint = aws.FIPSEndpointStateEnabled
}
})
}
func (c *CachingFactory) createPrometheusClient(assumedConfig *aws.Config) *amp.Client {
return amp.NewFromConfig(*assumedConfig, func(options *amp.Options) {
if c.logger.IsDebugEnabled() {
options.ClientLogMode = aws.LogRequestWithBody | aws.LogResponseWithBody
}
if c.endpointURLOverride != "" {
options.BaseEndpoint = aws.String(c.endpointURLOverride)
}
// The FIPS setting is ignored because FIPS is not available for amp apis
// If enabled the SDK will try to use non-existent FIPS URLs, https://github.com/aws/aws-sdk-go-v2/issues/2138#issuecomment-1570791988
// AWS FIPS Reference: https://aws.amazon.com/compliance/fips/
})
}
func (c *CachingFactory) createStsClient(awsConfig *aws.Config) *sts.Client {
return sts.NewFromConfig(*awsConfig, c.stsOptions)
}
func (c *CachingFactory) createShieldClient(awsConfig *aws.Config) *shield.Client {
return shield.NewFromConfig(*awsConfig, func(options *shield.Options) {
if c.logger.IsDebugEnabled() {
options.ClientLogMode = aws.LogRequestWithBody | aws.LogResponseWithBody
}
if c.endpointURLOverride != "" {
options.BaseEndpoint = aws.String(c.endpointURLOverride)
}
if c.fipsEnabled {
options.EndpointOptions.UseFIPSEndpoint = aws.FIPSEndpointStateEnabled
}
})
}
func createStsOptions(stsRegion string, isDebugLoggingEnabled bool, endpointURLOverride string, fipsEnabled bool) func(*sts.Options) {
return func(options *sts.Options) {
if stsRegion != "" {
options.Region = stsRegion
}
if isDebugLoggingEnabled {
options.ClientLogMode = aws.LogRequestWithBody | aws.LogResponseWithBody
}
if endpointURLOverride != "" {
options.BaseEndpoint = aws.String(endpointURLOverride)
}
if fipsEnabled {
options.EndpointOptions.UseFIPSEndpoint = aws.FIPSEndpointStateEnabled
}
}
}
var defaultRole = model.Role{}
func awsConfigForRegion(r model.Role, c *aws.Config, region awsRegion, stsOptions func(*sts.Options)) *aws.Config {
regionalConfig := c.Copy()
regionalConfig.Region = region
if r == defaultRole {
return ®ionalConfig
}
// based on https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/credentials/stscreds#hdr-Assume_Role
// found via https://github.com/aws/aws-sdk-go-v2/issues/1382
regionalSts := sts.NewFromConfig(*c, stsOptions)
credentials := stscreds.NewAssumeRoleProvider(regionalSts, r.RoleArn, func(options *stscreds.AssumeRoleOptions) {
if r.ExternalID != "" {
options.ExternalID = aws.String(r.ExternalID)
}
})
regionalConfig.Credentials = aws.NewCredentialsCache(credentials)
return ®ionalConfig
}