/
load_balancers.go
503 lines (485 loc) · 17.4 KB
/
load_balancers.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package elbv1
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
elbv1 "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing"
"github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing/types"
"github.com/cloudquery/cloudquery/plugins/source/aws/client"
"github.com/cloudquery/cq-provider-sdk/provider/diag"
"github.com/cloudquery/cq-provider-sdk/provider/schema"
)
type ELBv1LoadBalancerWrapper struct {
types.LoadBalancerDescription
Tags map[string]string
Attributes *types.LoadBalancerAttributes
}
func Elbv1LoadBalancers() *schema.Table {
return &schema.Table{
Name: "aws_elbv1_load_balancers",
Description: "Information about a load balancer.",
Resolver: fetchElbv1LoadBalancers,
Multiplex: client.ServiceAccountRegionMultiplexer("elasticloadbalancing"),
IgnoreError: client.IgnoreAccessDeniedServiceDisabled,
DeleteFilter: client.DeleteAccountRegionFilter,
Options: schema.TableCreationOptions{PrimaryKeys: []string{"account_id", "region", "name"}},
IgnoreInTests: true,
Columns: []schema.Column{
{
Name: "account_id",
Description: "The AWS Account ID of the resource.",
Type: schema.TypeString,
Resolver: client.ResolveAWSAccount,
},
{
Name: "region",
Description: "The AWS Region of the resource.",
Type: schema.TypeString,
Resolver: client.ResolveAWSRegion,
},
{
Name: "arn",
Description: "The Amazon Resource Name (ARN) for the resource.",
Type: schema.TypeString,
Resolver: client.ResolveARN(client.ElasticLoadBalancingService, func(resource *schema.Resource) ([]string, error) {
return []string{"loadbalancer", *resource.Item.(ELBv1LoadBalancerWrapper).LoadBalancerName}, nil
}),
},
{
Name: "attributes_access_log_enabled",
Type: schema.TypeBool,
Resolver: schema.PathResolver("Attributes.AccessLog.Enabled"),
},
{
Name: "attributes_access_log_s3_bucket_name",
Type: schema.TypeString,
Resolver: schema.PathResolver("Attributes.AccessLog.S3BucketName"),
},
{
Name: "attributes_access_log_s3_bucket_prefix",
Type: schema.TypeString,
Resolver: schema.PathResolver("Attributes.AccessLog.S3BucketPrefix"),
},
{
Name: "attributes_access_log_emit_interval",
Type: schema.TypeInt,
Resolver: schema.PathResolver("Attributes.AccessLog.EmitInterval"),
},
{
Name: "attributes_connection_settings_idle_timeout",
Type: schema.TypeInt,
Resolver: schema.PathResolver("Attributes.ConnectionSettings.IdleTimeout"),
},
{
Name: "attributes_cross_zone_load_balancing_enabled",
Type: schema.TypeBool,
Resolver: schema.PathResolver("Attributes.CrossZoneLoadBalancing.Enabled"),
},
{
Name: "attributes_connection_draining_enabled",
Type: schema.TypeBool,
Resolver: schema.PathResolver("Attributes.ConnectionDraining.Enabled"),
},
{
Name: "attributes_connection_draining_timeout",
Type: schema.TypeInt,
Resolver: schema.PathResolver("Attributes.ConnectionDraining.Timeout"),
},
{
Name: "attributes_additional_attributes",
Type: schema.TypeJSON,
Resolver: resolveElbv1loadBalancerAttributesAdditionalAttributes,
},
{
Name: "tags",
Type: schema.TypeJSON,
},
{
Name: "availability_zones",
Description: "The Availability Zones for the load balancer.",
Type: schema.TypeStringArray,
},
{
Name: "canonical_hosted_zone_name",
Description: "The DNS name of the load balancer.",
Type: schema.TypeString,
},
{
Name: "canonical_hosted_zone_name_id",
Description: "The ID of the Amazon Route 53 hosted zone for the load balancer.",
Type: schema.TypeString,
Resolver: schema.PathResolver("CanonicalHostedZoneNameID"),
},
{
Name: "created_time",
Description: "The date and time the load balancer was created.",
Type: schema.TypeTimestamp,
},
{
Name: "dns_name",
Description: "The DNS name of the load balancer.",
Type: schema.TypeString,
Resolver: schema.PathResolver("DNSName"),
},
{
Name: "health_check_healthy_threshold",
Description: "The number of consecutive health checks successes required before moving the instance to the Healthy state.",
Type: schema.TypeInt,
Resolver: schema.PathResolver("HealthCheck.HealthyThreshold"),
},
{
Name: "health_check_interval",
Description: "The approximate interval, in seconds, between health checks of an individual instance.",
Type: schema.TypeInt,
Resolver: schema.PathResolver("HealthCheck.Interval"),
},
{
Name: "health_check_target",
Description: "The instance being checked.",
Type: schema.TypeString,
Resolver: schema.PathResolver("HealthCheck.Target"),
},
{
Name: "health_check_timeout",
Description: "The amount of time, in seconds, during which no response means a failed health check.",
Type: schema.TypeInt,
Resolver: schema.PathResolver("HealthCheck.Timeout"),
},
{
Name: "health_check_unhealthy_threshold",
Description: "The number of consecutive health check failures required before moving the instance to the Unhealthy state.",
Type: schema.TypeInt,
Resolver: schema.PathResolver("HealthCheck.UnhealthyThreshold"),
},
{
Name: "instances",
Description: "The IDs of the instances for the load balancer.",
Type: schema.TypeStringArray,
Resolver: schema.PathResolver("Instances.InstanceId"),
},
{
Name: "name",
Description: "The name of the load balancer.",
Type: schema.TypeString,
Resolver: schema.PathResolver("LoadBalancerName"),
},
{
Name: "other_policies",
Description: "The policies other than the stickiness policies.",
Type: schema.TypeStringArray,
Resolver: schema.PathResolver("Policies.OtherPolicies"),
},
{
Name: "scheme",
Description: "The type of load balancer.",
Type: schema.TypeString,
},
{
Name: "security_groups",
Description: "The security groups for the load balancer.",
Type: schema.TypeStringArray,
},
{
Name: "source_security_group_name",
Description: "The name of the security group.",
Type: schema.TypeString,
Resolver: schema.PathResolver("SourceSecurityGroup.GroupName"),
},
{
Name: "source_security_group_owner_alias",
Description: "The owner of the security group.",
Type: schema.TypeString,
Resolver: schema.PathResolver("SourceSecurityGroup.OwnerAlias"),
},
{
Name: "subnets",
Description: "The IDs of the subnets for the load balancer.",
Type: schema.TypeStringArray,
},
{
Name: "vpc_id",
Description: "The ID of the VPC for the load balancer.",
Type: schema.TypeString,
Resolver: schema.PathResolver("VPCId"),
},
},
Relations: []*schema.Table{
{
Name: "aws_elbv1_load_balancer_backend_server_descriptions",
Description: "Information about the configuration of an EC2 instance.",
Resolver: schema.PathTableResolver("BackendServerDescriptions"),
IgnoreInTests: true,
Columns: []schema.Column{
{
Name: "load_balancer_cq_id",
Description: "Unique CloudQuery ID of aws_elbv1_load_balancers table (FK)",
Type: schema.TypeUUID,
Resolver: schema.ParentIdResolver,
},
{
Name: "name",
Description: "The name of the load balancer.",
Type: schema.TypeString,
Resolver: schema.ParentResourceFieldResolver("name"),
},
{
Name: "instance_port",
Description: "The port on which the EC2 instance is listening.",
Type: schema.TypeInt,
},
{
Name: "policy_names",
Description: "The names of the policies enabled for the EC2 instance.",
Type: schema.TypeStringArray,
},
},
},
{
Name: "aws_elbv1_load_balancer_listeners",
Description: "The policies enabled for a listener.",
Resolver: schema.PathTableResolver("ListenerDescriptions"),
IgnoreInTests: true,
Columns: []schema.Column{
{
Name: "load_balancer_cq_id",
Description: "Unique CloudQuery ID of aws_elbv1_load_balancers table (FK)",
Type: schema.TypeUUID,
Resolver: schema.ParentIdResolver,
},
{
Name: "load_balance_name",
Description: "The name of the load balancer.",
Type: schema.TypeString,
Resolver: schema.ParentResourceFieldResolver("name"),
},
{
Name: "listener_instance_port",
Description: "The port on which the instance is listening.",
Type: schema.TypeInt,
Resolver: schema.PathResolver("Listener.InstancePort"),
},
{
Name: "listener_load_balancer_port",
Description: "The port on which the load balancer is listening.",
Type: schema.TypeInt,
Resolver: schema.PathResolver("Listener.LoadBalancerPort"),
},
{
Name: "listener_protocol",
Description: "The load balancer transport protocol to use for routing: HTTP, HTTPS, TCP, or SSL.",
Type: schema.TypeString,
Resolver: schema.PathResolver("Listener.Protocol"),
},
{
Name: "listener_instance_protocol",
Description: "The protocol to use for routing traffic to instances: HTTP, HTTPS, TCP, or SSL.",
Type: schema.TypeString,
Resolver: schema.PathResolver("Listener.InstanceProtocol"),
},
{
Name: "listener_ssl_certificate_id",
Description: "The Amazon Resource Name (ARN) of the server certificate.",
Type: schema.TypeString,
Resolver: schema.PathResolver("Listener.SSLCertificateId"),
},
{
Name: "policy_names",
Description: "The policies.",
Type: schema.TypeStringArray,
},
},
},
{
Name: "aws_elbv1_load_balancer_policies_app_cookie_stickiness",
Description: "Information about a policy for application-controlled session stickiness.",
Resolver: schema.PathTableResolver("Policies.AppCookieStickinessPolicies"),
IgnoreInTests: true,
Columns: []schema.Column{
{
Name: "load_balancer_cq_id",
Description: "Unique CloudQuery ID of aws_elbv1_load_balancers table (FK)",
Type: schema.TypeUUID,
Resolver: schema.ParentIdResolver,
},
{
Name: "load_balance_name",
Description: "The name of the load balancer.",
Type: schema.TypeString,
Resolver: schema.ParentResourceFieldResolver("name"),
},
{
Name: "cookie_name",
Description: "The name of the application cookie used for stickiness.",
Type: schema.TypeString,
},
{
Name: "policy_name",
Description: "The mnemonic name for the policy being created.",
Type: schema.TypeString,
},
},
},
{
Name: "aws_elbv1_load_balancer_policies_lb_cookie_stickiness",
Description: "Information about a policy for duration-based session stickiness.",
Resolver: schema.PathTableResolver("Policies.LBCookieStickinessPolicies"),
IgnoreInTests: true,
Columns: []schema.Column{
{
Name: "load_balancer_cq_id",
Description: "Unique CloudQuery ID of aws_elbv1_load_balancers table (FK)",
Type: schema.TypeUUID,
Resolver: schema.ParentIdResolver,
},
{
Name: "load_balance_name",
Description: "The name of the load balancer.",
Type: schema.TypeString,
Resolver: schema.ParentResourceFieldResolver("name"),
},
{
Name: "cookie_expiration_period",
Description: "The time period, in seconds, after which the cookie should be considered stale.",
Type: schema.TypeBigInt,
},
{
Name: "policy_name",
Description: "The name of the policy.",
Type: schema.TypeString,
},
},
},
{
Name: "aws_elbv1_load_balancer_policies",
Description: "Information about a policy.",
Resolver: fetchElbv1LoadBalancerPolicies,
IgnoreInTests: true,
Columns: []schema.Column{
{
Name: "load_balancer_cq_id",
Description: "Unique CloudQuery ID of aws_elbv1_load_balancers table (FK)",
Type: schema.TypeUUID,
Resolver: schema.ParentIdResolver,
},
{
Name: "load_balance_name",
Description: "The name of the load balancer.",
Type: schema.TypeString,
Resolver: schema.ParentResourceFieldResolver("name"),
},
{
Name: "policy_attribute_descriptions",
Description: "The policy attributes.",
Type: schema.TypeJSON,
Resolver: resolveElbv1loadBalancerPolicyPolicyAttributeDescriptions,
},
{
Name: "policy_name",
Description: "The name of the policy.",
Type: schema.TypeString,
},
{
Name: "policy_type_name",
Description: "The name of the policy type.",
Type: schema.TypeString,
},
},
},
},
}
}
// ====================================================================================================================
// Table Resolver Functions
// ====================================================================================================================
func fetchElbv1LoadBalancers(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
c := meta.(*client.Client)
svc := c.Services().ELBv1
processLoadBalancers := func(loadBalancers []types.LoadBalancerDescription) error {
tagsCfg := &elbv1.DescribeTagsInput{LoadBalancerNames: make([]string, 0, len(loadBalancers))}
for _, lb := range loadBalancers {
tagsCfg.LoadBalancerNames = append(tagsCfg.LoadBalancerNames, *lb.LoadBalancerName)
}
tagsResponse, err := svc.DescribeTags(ctx, tagsCfg)
if err != nil {
return diag.WrapError(err)
}
for _, lb := range loadBalancers {
loadBalancerAttributes, err := svc.DescribeLoadBalancerAttributes(ctx, &elbv1.DescribeLoadBalancerAttributesInput{LoadBalancerName: lb.LoadBalancerName})
if err != nil {
if c.IsNotFoundError(err) {
continue
}
return diag.WrapError(err)
}
wrapper := ELBv1LoadBalancerWrapper{
LoadBalancerDescription: lb,
Tags: client.TagsToMap(getTagsByLoadBalancerName(*lb.LoadBalancerName, tagsResponse.TagDescriptions)),
Attributes: loadBalancerAttributes.LoadBalancerAttributes,
}
res <- wrapper
}
return nil
}
var config elbv1.DescribeLoadBalancersInput
for {
response, err := svc.DescribeLoadBalancers(ctx, &config)
if err != nil {
return diag.WrapError(err)
}
for i := 0; i < len(response.LoadBalancerDescriptions); i += 20 {
end := i + 20
if end > len(response.LoadBalancerDescriptions) {
end = len(response.LoadBalancerDescriptions)
}
loadBalancers := response.LoadBalancerDescriptions[i:end]
if err := processLoadBalancers(loadBalancers); err != nil {
return diag.WrapError(err)
}
}
if aws.ToString(response.NextMarker) == "" {
break
}
config.Marker = response.NextMarker
}
return nil
}
func resolveElbv1loadBalancerAttributesAdditionalAttributes(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
r := resource.Item.(ELBv1LoadBalancerWrapper)
if r.Attributes == nil {
return nil
}
response := make(map[string]interface{}, len(r.Attributes.AdditionalAttributes))
for _, a := range r.Attributes.AdditionalAttributes {
response[*a.Key] = a.Value
}
return diag.WrapError(resource.Set(c.Name, response))
}
func fetchElbv1LoadBalancerPolicies(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
r := parent.Item.(ELBv1LoadBalancerWrapper)
c := meta.(*client.Client)
svc := c.Services().ELBv1
response, err := svc.DescribeLoadBalancerPolicies(ctx, &elbv1.DescribeLoadBalancerPoliciesInput{LoadBalancerName: r.LoadBalancerName})
if err != nil {
return diag.WrapError(err)
}
res <- response.PolicyDescriptions
return nil
}
func resolveElbv1loadBalancerPolicyPolicyAttributeDescriptions(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
r := resource.Item.(types.PolicyDescription)
response := make(map[string]interface{}, len(r.PolicyAttributeDescriptions))
for _, a := range r.PolicyAttributeDescriptions {
response[*a.AttributeName] = a.AttributeValue
}
return diag.WrapError(resource.Set(c.Name, response))
}
// ====================================================================================================================
// User Defined Helpers
// ====================================================================================================================
func getTagsByLoadBalancerName(id string, tagsResponse []types.TagDescription) []types.Tag {
for _, t := range tagsResponse {
if id == *t.LoadBalancerName {
return t.Tags
}
}
return nil
}