-
Notifications
You must be signed in to change notification settings - Fork 83
/
cf_template.go
501 lines (459 loc) · 19.5 KB
/
cf_template.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
package aws
import (
"encoding/json"
"fmt"
"strings"
"crypto/sha256"
"sort"
cloudformation "github.com/mweagle/go-cloudformation"
)
const (
// https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticloadbalancingv2-listenerrule-rulecondition.html#cfn-elasticloadbalancingv2-listenerrule-rulecondition-field
listenerRuleConditionHostField = "host-header"
// https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticloadbalancingv2-listenerrule-action.html#cfn-elasticloadbalancingv2-listenerrule-action-type
listenerRuleActionTypeFixedRes = "fixed-response"
// internalTrafficDenyRulePriority defines the [priority][0] for the
// rule that denies requests directed to internal domains on the HTTP
// and HTTPS listener.
//
// [0]: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-elasticloadbalancingv2-listenerrule.html#cfn-elasticloadbalancingv2-listenerrule-priority
internalTrafficDenyRulePriority int64 = 1
)
func hashARNs(certARNs []string) []byte {
hash := sha256.New()
for _, arn := range certARNs {
hash.Write([]byte(arn))
hash.Write([]byte{'\000'})
}
return hash.Sum(nil)
}
func generateTemplate(spec *stackSpec) (string, error) {
template := cloudformation.NewTemplate()
template.Description = "Load Balancer for Kubernetes Ingress"
template.Parameters = map[string]*cloudformation.Parameter{
parameterLoadBalancerSchemeParameter: {
Type: "String",
Description: "The Load Balancer scheme - 'internal' or 'internet-facing'",
Default: "internet-facing",
},
parameterLoadBalancerSecurityGroupParameter: {
Type: "List<AWS::EC2::SecurityGroup::Id>",
Description: "The security group ID for the Load Balancer",
},
parameterLoadBalancerSubnetsParameter: {
Type: "List<AWS::EC2::Subnet::Id>",
Description: "The list of subnets IDs for the Load Balancer",
},
parameterTargetGroupHealthCheckPathParameter: {
Type: "String",
Description: "The healthcheck path",
Default: "/kube-system/healthz",
},
parameterTargetGroupHealthCheckPortParameter: {
Type: "Number",
Description: "The healthcheck port",
Default: "9999",
},
parameterTargetGroupTargetPortParameter: {
Type: "Number",
Description: "The target port",
Default: "9999",
},
parameterTargetGroupHealthCheckIntervalParameter: {
Type: "Number",
Description: "The healthcheck interval",
Default: "10",
},
parameterTargetGroupHealthCheckTimeoutParameter: {
Type: "Number",
Description: "The healthcheck timeout",
Default: "5",
},
parameterTargetGroupVPCIDParameter: {
Type: "AWS::EC2::VPC::Id",
Description: "The VPCID for the TargetGroup",
},
parameterListenerSslPolicyParameter: {
Type: "String",
Description: "The HTTPS SSL Security Policy Name",
Default: "ELBSecurityPolicy-2016-08",
},
parameterIpAddressTypeParameter: {
Type: "String",
Description: "IP Address Type, 'ipv4' or 'dualstack'",
Default: IPAddressTypeIPV4,
},
parameterLoadBalancerTypeParameter: {
Type: "String",
Description: "Loadbalancer Type, 'application' or 'network'",
Default: LoadBalancerTypeApplication,
},
parameterHTTP2Parameter: {
Type: "String",
Description: "H2 Enabled",
Default: "true",
},
}
if spec.wafWebAclId != "" {
template.Parameters[parameterLoadBalancerWAFWebACLIDParameter] = &cloudformation.Parameter{
Type: "String",
Description: "Associated WAF ID or ARN.",
}
}
const httpsTargetGroupName = "TG"
template.Outputs = map[string]*cloudformation.Output{
outputLoadBalancerDNSName: {
Description: "DNS name for the LoadBalancer",
Value: cloudformation.GetAtt("LB", "DNSName").String(),
},
outputTargetGroupARN: {
Description: "The ARN of the TargetGroup",
Value: cloudformation.Ref(httpsTargetGroupName).String(),
},
}
template.AddResource(httpsTargetGroupName, newTargetGroup(spec, parameterTargetGroupTargetPortParameter))
if !spec.httpDisabled {
// Use the same target group for HTTP Listener or create another one if needed
httpTargetGroupName := httpsTargetGroupName
if spec.httpTargetPort != spec.targetPort {
httpTargetGroupName = "TGHTTP"
template.Parameters[parameterTargetGroupHTTPTargetPortParameter] = &cloudformation.Parameter{
Type: "Number",
Description: "The HTTP target port",
}
template.Outputs[outputHTTPTargetGroupARN] = &cloudformation.Output{
Description: "The ARN of the HTTP TargetGroup",
Value: cloudformation.Ref(httpTargetGroupName).String(),
}
template.AddResource(httpTargetGroupName, newTargetGroup(spec, parameterTargetGroupHTTPTargetPortParameter))
}
// Add an HTTP Listener resource
if spec.loadbalancerType == LoadBalancerTypeApplication {
if spec.httpRedirectToHTTPS {
template.AddResource("HTTPListener", &cloudformation.ElasticLoadBalancingV2Listener{
DefaultActions: &cloudformation.ElasticLoadBalancingV2ListenerActionList{
{
Type: cloudformation.String("redirect"),
RedirectConfig: &cloudformation.ElasticLoadBalancingV2ListenerRedirectConfig{
Protocol: cloudformation.String("HTTPS"),
Port: cloudformation.String("443"),
Host: cloudformation.String("#{host}"),
Path: cloudformation.String("/#{path}"),
Query: cloudformation.String("#{query}"),
StatusCode: cloudformation.String("HTTP_301"),
},
},
},
LoadBalancerArn: cloudformation.Ref("LB").String(),
Port: cloudformation.Integer(80),
Protocol: cloudformation.String("HTTP"),
})
} else {
template.AddResource("HTTPListener", &cloudformation.ElasticLoadBalancingV2Listener{
DefaultActions: &cloudformation.ElasticLoadBalancingV2ListenerActionList{
{
Type: cloudformation.String("forward"),
TargetGroupArn: cloudformation.Ref(httpTargetGroupName).String(),
},
},
LoadBalancerArn: cloudformation.Ref("LB").String(),
Port: cloudformation.Integer(80),
Protocol: cloudformation.String("HTTP"),
})
if spec.denyInternalDomains {
template.AddResource(
"HTTPRuleBlockInternalTraffic",
generateDenyInternalTrafficRule(
"HTTPListener",
internalTrafficDenyRulePriority,
spec.internalDomains,
spec.denyInternalDomainsResponse,
),
)
}
}
} else if spec.loadbalancerType == LoadBalancerTypeNetwork {
template.AddResource("HTTPListener", &cloudformation.ElasticLoadBalancingV2Listener{
DefaultActions: &cloudformation.ElasticLoadBalancingV2ListenerActionList{
{
Type: cloudformation.String("forward"),
TargetGroupArn: cloudformation.Ref(httpTargetGroupName).String(),
},
},
LoadBalancerArn: cloudformation.Ref("LB").String(),
Port: cloudformation.Integer(80),
Protocol: cloudformation.String("TCP"),
})
}
}
if len(spec.certificateARNs) > 0 {
// Sort the certificate names so we have a stable order.
certificateARNs := make([]string, 0, len(spec.certificateARNs))
for certARN := range spec.certificateARNs {
certificateARNs = append(certificateARNs, certARN)
}
sort.Slice(certificateARNs, func(i, j int) bool {
return certificateARNs[i] < certificateARNs[j]
})
// Add an HTTPS Listener resource with the first certificate as the default one
if spec.loadbalancerType == LoadBalancerTypeApplication {
template.AddResource("HTTPSListener", &cloudformation.ElasticLoadBalancingV2Listener{
DefaultActions: &cloudformation.ElasticLoadBalancingV2ListenerActionList{
{
Type: cloudformation.String("forward"),
TargetGroupArn: cloudformation.Ref(httpsTargetGroupName).String(),
},
},
Certificates: &cloudformation.ElasticLoadBalancingV2ListenerCertificatePropertyList{
{
CertificateArn: cloudformation.String(certificateARNs[0]),
},
},
LoadBalancerArn: cloudformation.Ref("LB").String(),
Port: cloudformation.Integer(443),
Protocol: cloudformation.String("HTTPS"),
SslPolicy: cloudformation.Ref(parameterListenerSslPolicyParameter).String(),
})
if spec.denyInternalDomains {
template.AddResource(
"HTTPSRuleBlockInternalTraffic",
generateDenyInternalTrafficRule(
"HTTPSListener",
internalTrafficDenyRulePriority,
spec.internalDomains,
spec.denyInternalDomainsResponse,
),
)
}
} else if spec.loadbalancerType == LoadBalancerTypeNetwork {
template.AddResource("HTTPSListener", &cloudformation.ElasticLoadBalancingV2Listener{
DefaultActions: &cloudformation.ElasticLoadBalancingV2ListenerActionList{
{
Type: cloudformation.String("forward"),
TargetGroupArn: cloudformation.Ref(httpsTargetGroupName).String(),
},
},
Certificates: &cloudformation.ElasticLoadBalancingV2ListenerCertificatePropertyList{
{
CertificateArn: cloudformation.String(certificateARNs[0]),
},
},
LoadBalancerArn: cloudformation.Ref("LB").String(),
Port: cloudformation.Integer(443),
Protocol: cloudformation.String("TLS"),
SslPolicy: cloudformation.Ref(parameterListenerSslPolicyParameter).String(),
})
}
// Add a ListenerCertificate resource with all of the certificates, including the default one
certificateList := make(cloudformation.ElasticLoadBalancingV2ListenerCertificateCertificateList, 0, len(certificateARNs))
for _, certARN := range certificateARNs {
c := cloudformation.ElasticLoadBalancingV2ListenerCertificateCertificate{
CertificateArn: cloudformation.String(certARN),
}
certificateList = append(certificateList, c)
}
// Use a new resource name every time to avoid a bug where CloudFormation fails to perform an update properly
resourceName := fmt.Sprintf("HTTPSListenerCertificate%x", hashARNs(certificateARNs))
template.AddResource(resourceName, &cloudformation.ElasticLoadBalancingV2ListenerCertificate{
Certificates: &certificateList,
ListenerArn: cloudformation.Ref("HTTPSListener").String(),
})
}
// Build up the LoadBalancerAttributes list, as there is no way to make attributes conditional in the template
lbAttrList := make(cloudformation.ElasticLoadBalancingV2LoadBalancerLoadBalancerAttributeList, 0, 5)
if spec.loadbalancerType == LoadBalancerTypeApplication {
lbAttrList = append(lbAttrList,
cloudformation.ElasticLoadBalancingV2LoadBalancerLoadBalancerAttribute{
Key: cloudformation.String("idle_timeout.timeout_seconds"),
Value: cloudformation.String(fmt.Sprintf("%d", spec.idleConnectionTimeoutSeconds)),
},
)
lbAttrList = append(lbAttrList,
cloudformation.ElasticLoadBalancingV2LoadBalancerLoadBalancerAttribute{
Key: cloudformation.String("routing.http2.enabled"),
Value: cloudformation.String(fmt.Sprintf("%t", spec.http2)),
},
)
}
if spec.loadbalancerType == LoadBalancerTypeNetwork {
lbAttrList = append(lbAttrList,
cloudformation.ElasticLoadBalancingV2LoadBalancerLoadBalancerAttribute{
Key: cloudformation.String("load_balancing.cross_zone.enabled"),
Value: cloudformation.String(fmt.Sprintf("%t", spec.nlbCrossZone)),
},
)
lbAttrList = append(lbAttrList,
cloudformation.ElasticLoadBalancingV2LoadBalancerLoadBalancerAttribute{
// https://docs.aws.amazon.com/elasticloadbalancing/latest/network/network-load-balancers.html#load-balancer-attributes
// https://docs.aws.amazon.com/elasticloadbalancing/latest/network/network-load-balancers.html#zonal-dns-affinity
Key: cloudformation.String("dns_record.client_routing_policy"),
// availability_zone_affinity 100%
// partial_availability_zone_affinity 85%
// any_availability_zone 0%
Value: cloudformation.String(spec.nlbZoneAffinity),
},
)
}
if spec.albLogsS3Bucket != "" {
lbAttrList = append(lbAttrList,
cloudformation.ElasticLoadBalancingV2LoadBalancerLoadBalancerAttribute{
Key: cloudformation.String("access_logs.s3.enabled"),
Value: cloudformation.String("true"),
},
)
lbAttrList = append(lbAttrList,
cloudformation.ElasticLoadBalancingV2LoadBalancerLoadBalancerAttribute{
Key: cloudformation.String("access_logs.s3.bucket"),
Value: cloudformation.String(spec.albLogsS3Bucket),
},
)
if spec.albLogsS3Prefix != "" {
lbAttrList = append(lbAttrList,
cloudformation.ElasticLoadBalancingV2LoadBalancerLoadBalancerAttribute{
Key: cloudformation.String("access_logs.s3.prefix"),
Value: cloudformation.String(spec.albLogsS3Prefix),
},
)
}
} else {
lbAttrList = append(lbAttrList,
cloudformation.ElasticLoadBalancingV2LoadBalancerLoadBalancerAttribute{
Key: cloudformation.String("access_logs.s3.enabled"),
Value: cloudformation.String("false"),
},
)
}
lb := &cloudformation.ElasticLoadBalancingV2LoadBalancer{
LoadBalancerAttributes: &lbAttrList,
IPAddressType: cloudformation.Ref(parameterIpAddressTypeParameter).String(),
Scheme: cloudformation.Ref(parameterLoadBalancerSchemeParameter).String(),
Subnets: cloudformation.Ref(parameterLoadBalancerSubnetsParameter).StringList(),
Tags: &cloudformation.TagList{
{
Key: cloudformation.String("StackName"),
Value: cloudformation.Ref("AWS::StackName").String(),
},
},
}
// Security groups can't be set for 'network' load balancers
if spec.loadbalancerType != LoadBalancerTypeNetwork {
lb.SecurityGroups = cloudformation.Ref(parameterLoadBalancerSecurityGroupParameter).StringList()
}
// TODO(mlarsen): hack to only set type on "new" stacks where this
// features was enabled. Adding the Type value for existing Load
// Balancers will cause them to be recreated which is disruptive (and
// breaks because AWS tries to attach the same TG to multiple LBs).
// Can be removed in a later version.
if spec.loadbalancerType == LoadBalancerTypeApplication || spec.loadbalancerType == LoadBalancerTypeNetwork {
lb.Type = cloudformation.Ref(parameterLoadBalancerTypeParameter).String()
}
template.AddResource("LB", lb)
if spec.loadbalancerType == LoadBalancerTypeApplication && spec.wafWebAclId != "" {
if strings.HasPrefix(spec.wafWebAclId, "arn:aws:wafv2:") {
template.AddResource("WAFAssociation", &cloudformation.WAFv2WebACLAssociation{
ResourceArn: cloudformation.Ref("LB").String(),
WebACLArn: cloudformation.Ref(parameterLoadBalancerWAFWebACLIDParameter).String(),
})
} else {
template.AddResource("WAFAssociation", &cloudformation.WAFRegionalWebACLAssociation{
ResourceArn: cloudformation.Ref("LB").String(),
WebACLID: cloudformation.Ref(parameterLoadBalancerWAFWebACLIDParameter).String(),
})
}
}
for idx, alarm := range spec.cwAlarms {
resourceName := fmt.Sprintf("CloudWatchAlarm%d", idx)
template.AddResource(resourceName, &cloudformation.CloudWatchAlarm{
ActionsEnabled: alarm.ActionsEnabled,
AlarmActions: alarm.AlarmActions,
AlarmDescription: alarm.AlarmDescription,
AlarmName: normalizeCloudWatchAlarmName(alarm.AlarmName),
ComparisonOperator: alarm.ComparisonOperator,
Dimensions: normalizeCloudWatchAlarmDimensions(alarm.Dimensions),
EvaluateLowSampleCountPercentile: alarm.EvaluateLowSampleCountPercentile,
EvaluationPeriods: alarm.EvaluationPeriods,
ExtendedStatistic: alarm.ExtendedStatistic,
InsufficientDataActions: alarm.InsufficientDataActions,
MetricName: alarm.MetricName,
Namespace: normalizeCloudWatchAlarmNamespace(alarm.Namespace),
OKActions: alarm.OKActions,
Period: alarm.Period,
Statistic: alarm.Statistic,
Threshold: alarm.Threshold,
TreatMissingData: alarm.TreatMissingData,
Unit: alarm.Unit,
})
}
stackTemplate, err := json.MarshalIndent(template, "", " ")
if err != nil {
return "", err
}
return string(stackTemplate), nil
}
func generateDenyInternalTrafficRule(listenerName string, rulePriority int64, internalDomains []string, resp denyResp) cloudformation.ElasticLoadBalancingV2ListenerRule {
values := cloudformation.StringList()
for _, domain := range internalDomains {
values.Literal = append(values.Literal, cloudformation.String(domain))
}
return cloudformation.ElasticLoadBalancingV2ListenerRule{
Conditions: &cloudformation.ElasticLoadBalancingV2ListenerRuleRuleConditionList{
cloudformation.ElasticLoadBalancingV2ListenerRuleRuleCondition{
Field: cloudformation.String(listenerRuleConditionHostField),
Values: values,
},
},
Actions: &cloudformation.ElasticLoadBalancingV2ListenerRuleActionList{
cloudformation.ElasticLoadBalancingV2ListenerRuleAction{
Type: cloudformation.String(listenerRuleActionTypeFixedRes),
FixedResponseConfig: &cloudformation.ElasticLoadBalancingV2ListenerRuleFixedResponseConfig{
ContentType: cloudformation.String(resp.contentType),
MessageBody: cloudformation.String(resp.body),
StatusCode: cloudformation.String(fmt.Sprintf("%d", resp.statusCode)),
},
},
},
Priority: cloudformation.Integer(rulePriority),
ListenerArn: cloudformation.Ref(listenerName).String(),
}
}
func newTargetGroup(spec *stackSpec, targetPortParameter string) *cloudformation.ElasticLoadBalancingV2TargetGroup {
var targetType *cloudformation.StringExpr
if spec.targetType != "" {
targetType = cloudformation.String(spec.targetType)
}
protocol := "HTTP"
healthCheckProtocol := "HTTP"
healthyThresholdCount, unhealthyThresholdCount := spec.albHealthyThresholdCount, spec.albUnhealthyThresholdCount
if spec.loadbalancerType == LoadBalancerTypeNetwork {
protocol = "TCP"
healthCheckProtocol = "HTTP"
// For NLBs the healthy and unhealthy threshold count value must be equal
healthyThresholdCount, unhealthyThresholdCount = spec.nlbHealthyThresholdCount, spec.nlbHealthyThresholdCount
} else if spec.targetHTTPS {
protocol = "HTTPS"
healthCheckProtocol = "HTTPS"
}
targetGroup := &cloudformation.ElasticLoadBalancingV2TargetGroup{
TargetGroupAttributes: &cloudformation.ElasticLoadBalancingV2TargetGroupTargetGroupAttributeList{
{
Key: cloudformation.String("deregistration_delay.timeout_seconds"),
Value: cloudformation.String(fmt.Sprintf("%d", spec.deregistrationDelayTimeoutSeconds)),
},
},
HealthCheckIntervalSeconds: cloudformation.Ref(parameterTargetGroupHealthCheckIntervalParameter).Integer(),
HealthCheckPath: cloudformation.Ref(parameterTargetGroupHealthCheckPathParameter).String(),
HealthCheckPort: cloudformation.Ref(parameterTargetGroupHealthCheckPortParameter).String(),
HealthCheckProtocol: cloudformation.String(healthCheckProtocol),
HealthyThresholdCount: cloudformation.Integer(int64(healthyThresholdCount)),
UnhealthyThresholdCount: cloudformation.Integer(int64(unhealthyThresholdCount)),
Port: cloudformation.Ref(targetPortParameter).Integer(),
Protocol: cloudformation.String(protocol),
TargetType: targetType,
VPCID: cloudformation.Ref(parameterTargetGroupVPCIDParameter).String(),
}
// custom target group healthcheck only supported when the target group protocol is != TCP
if protocol != "TCP" {
targetGroup.HealthCheckTimeoutSeconds = cloudformation.Ref(parameterTargetGroupHealthCheckTimeoutParameter).Integer()
}
return targetGroup
}