-
Notifications
You must be signed in to change notification settings - Fork 3
/
emit_cfn.go
268 lines (225 loc) · 6.11 KB
/
emit_cfn.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"hash/crc32"
"sort"
)
type stringOrRef struct {
Value string
Ref string
}
func (s stringOrRef) MarshalJSON() ([]byte, error) {
if len(s.Value) > 0 {
return json.Marshal(s.Value)
} else {
return json.Marshal(map[string]string{"Ref": s.Ref})
}
}
type rule struct {
Priority int
ListenerArn json.RawMessage
Actions []ActionProperty
Conditions []cfnCondition
}
type keyvaluePair struct {
Key string
Value string
}
type cfnCondition struct {
Field string
HostHeaderConfig *jsonValues `json:",omitempty"`
PathPatternConfig *jsonValues `json:",omitempty"`
HttpRequestMethodConfig *jsonValues `json:",omitempty"`
SourceIpConfig *jsonValues `json:",omitempty"`
HttpHeaderConfig *httpHeaderValues `json:",omitempty"`
}
type jsonValues struct {
Values json.RawMessage
}
type httpHeaderValues struct {
jsonValues
HttpHeaderName string
}
type LoadBalancerProperties struct {
Name string
Type string
Scheme string
IpAddressType string
SecurityGroups []string
Subnets []string
Tags []keyvaluePair
LoadBalancerAttributes []keyvaluePair
}
type ListenerProperties struct {
LoadBalancerArn string
Certificates []CertificateProperty
DefaultActions []ActionProperty
Port int
Protocol string
}
type ActionProperty struct {
//AuthenticateCognitoConfig *AuthenticateCognitoActionConfig
//FixedResponseConfig *FixedResponseActionConfig
//ForwardConfig *ForwardActionConfig
//RedirectConfig *RedirectActionConfig
Type string
Order int `json:",omitempty"`
Oidc *albEventOidc `json:"AuthenticateOidcConfig,omitempty"`
TargetGroupArn *stringOrRef `json:",omitempty"`
}
type CertificateProperty struct {
}
func calculatePriority(conds []cfnCondition) int {
/*
Rule limits for condition values, wildcards, and total rules.
100 total rules per application load balancer
5 condition values per rule
5 wildcards per rule
5 weighted target groups per rule
host
ip
path
method
header
*/
crc32q := crc32.MakeTable(0xD5828281)
cksum := func(val json.RawMessage, ceil int) int {
sum32 := crc32.Checksum(val, crc32q)
return int(sum32) % ceil
}
priority := 49_999
for _, cond := range conds {
if cond.HostHeaderConfig != nil && len(cond.HostHeaderConfig.Values) > 0 {
priority -= cksum(cond.HostHeaderConfig.Values, 49_000)
}
if cond.SourceIpConfig != nil && len(cond.SourceIpConfig.Values) > 0 {
priority -= cksum(cond.SourceIpConfig.Values, 49_000)
}
if cond.PathPatternConfig != nil && len(cond.PathPatternConfig.Values) > 0 {
priority -= cksum(cond.PathPatternConfig.Values, 1_000)
}
if cond.HttpRequestMethodConfig != nil && len(cond.HttpRequestMethodConfig.Values) > 0 {
priority -= cksum(cond.HttpRequestMethodConfig.Values, 100)
}
panic("IMPLEMENT ME")
//if cond.HttpHeaderConfig != nil && len(cond.HttpHeaderConfig.Values) > 0 {
// priority -= cksum(cond.HttpHeaderConfig.Values, 10)
//}
}
return priority
}
func convertConditions(input albEventConditions) []cfnCondition {
output := []cfnCondition{}
if len(input.Host) > 0 {
output = append(output, cfnCondition{
Field: "host-header",
HostHeaderConfig: &jsonValues{Values: input.Host},
})
}
if len(input.Path) > 0 {
output = append(output, cfnCondition{
Field: "path-pattern",
PathPatternConfig: &jsonValues{Values: input.Path},
})
}
if len(input.Method) > 0 {
output = append(output, cfnCondition{
Field: "http-request-method",
HttpRequestMethodConfig: &jsonValues{Values: input.Method},
})
}
for name, values := range input.Header {
output = append(output, cfnCondition{
Field: "http-header",
HttpHeaderConfig: &httpHeaderValues{
HttpHeaderName: name,
jsonValues: jsonValues{Values: values},
},
})
}
if len(input.Ip) > 0 {
output = append(output, cfnCondition{
Field: "source-ip",
SourceIpConfig: &jsonValues{Values: input.Ip},
})
}
return output
}
func loadBalancerJson(props LoadBalancerProperties) []byte {
bytes, _ := json.Marshal(props)
return []byte(fmt.Sprintf(`
{
"Type": "AWS::ElasticLoadBalancingV2::LoadBalancer",
"Properties": %s
}
`, string(bytes)))
}
func trailingTagsJson(tags map[string]json.RawMessage) string {
if len(tags) == 0 {
return ""
}
keys := []string{}
for k, _ := range tags {
keys = append(keys, k)
}
sort.Strings(keys)
buf := &bytes.Buffer{}
buf.WriteString(`,
"Tags": [`)
for _, k := range keys {
buf.WriteString(fmt.Sprintf(`{"Key": "%s", "Value": %s},`, k, string(tags[k])))
}
buf.Truncate(buf.Len() - 1) // chomp comma
buf.WriteString("]")
return buf.String()
}
func targetGroupJson(functionName, target string, tags map[string]json.RawMessage) []byte {
return []byte(fmt.Sprintf(`
{
"DependsOn": ["%sAlbPermission"],
"Type": "AWS::ElasticLoadBalancingV2::TargetGroup",
"Properties": {
"TargetType": "lambda",
"Targets": [
{"Id": %s}
]%s
}
}
`, functionName, target, trailingTagsJson(tags)))
}
func targetFunctionJson(functionName string) string {
return fmt.Sprintf(`{"Fn::GetAtt": ["%s", "Arn"]}`, functionName)
}
func targetAliasJson(aliasName string) string {
return fmt.Sprintf(`{"Ref": "%s"}`, aliasName)
}
func permissionJson(target string) []byte {
return []byte(fmt.Sprintf(`
{
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:InvokeFunction",
"Principal": "elasticloadbalancing.amazonaws.com",
"SourceArn": {"Fn::Sub": "arn:aws:elasticloadbalancing:${AWS::Region}:${AWS::AccountId}:targetgroup/*"},
"FunctionName": %s
}
}
`, target))
}
func httpsListenerJson() []byte {
return []byte(``)
}
func httpListenerJson() []byte {
return []byte(``)
}
func listenerRuleJson(rule rule) []byte {
bytes, _ := json.Marshal(rule)
return []byte(fmt.Sprintf(`
{
"Type": "AWS::ElasticLoadBalancingV2::ListenerRule",
"Properties": %s
}
`, string(bytes)))
}