-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
executable file
·296 lines (236 loc) · 8.99 KB
/
main.tf
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
locals {
name = coalesce(module.this.name, var.name, "cf-middleware-${random_string.mw_service_random_suffix.result}")
enabled = module.this.enabled
auth_service_enabled = local.enabled && var.auth_service_config.enabled
urlrewrite_service_enabled = local.enabled && var.urlrewrite_service_config.enabled
aws_account_id = try(coalesce(var.aws_account_id, data.aws_caller_identity.current[0].account_id), "") # tflint-ignore: terraform_unused_declarations
aws_region_name = try(coalesce(var.aws_region_name, data.aws_region.current[0].name), "")
service_config = {
rewrite_url = {
enabled = local.urlrewrite_service_enabled
handler = "index.handle"
type = "urlrewrite"
}
check_auth = {
enabled = local.auth_service_enabled
handler = "index.checkAuthHandler"
type = "auth"
}
parse_auth = {
enabled = local.auth_service_enabled
handler = "index.parseAuthHandler"
type = "auth"
}
refresh_auth = {
enabled = local.auth_service_enabled
handler = "index.refreshAuthHandler"
type = "auth"
}
revoke_auth = {
enabled = local.auth_service_enabled
handler = "index.revokeAuthHandler"
type = "auth"
}
}
auth_service_names = toset([for svc_name, svc_opts in local.service_config : svc_name if svc_opts.enabled && svc_opts.type == "auth"])
auth_service_config = {
awsRegion = coalesce(var.auth_service_config.aws_region, local.aws_region_name)
cognitoIdpArn = var.auth_service_config.cognito_idp_arn
cognitoIdpDomain = var.auth_service_config.cognito_idp_domain
cognitoIdpJwks = var.auth_service_config.cognito_idp_jwks
cognitoIdpClientId = var.auth_service_config.cognito_idp_client_id
cognitoIdpClientSecret = var.auth_service_config.cognito_idp_client_secret
cognitoIdpClientScopes = var.auth_service_config.cognito_idp_client_scopes
redirectPathAuthRefresh = "/_edge/auth/refresh"
redirectPathAuthSignIn = "/_edge/auth/signin"
redirectPathAuthSignOut = "/"
urlSignOut = "/_edge/auth/signout"
logLevel = var.auth_service_config.log_level
oidcStateEncryptionKey = local.enabled ? random_password.auth_service_oidc_state_encrypt_key[0].result : ""
opaPolicyEnabled = var.auth_service_config.opa_policy_content != null && var.auth_service_config.opa_policy_content != ""
opaPolicyData = var.auth_service_config.opa_policy_data
}
auth_service_config_default_opa_policy_content = <<-EOF
package auth_at_edge_authz
results := []
EOF
urlrewrite_service_names = toset([for svc_name, svc_opts in local.service_config : svc_name if svc_opts.enabled && svc_opts.type == "urlrewrite"])
urlrewrite_service_config = {
logLevel = var.urlrewrite_service_config.log_level
policyPath = "/opt/app/dist/policy.wasm"
}
urlrewrite_policy_content = coalesce(
var.urlrewrite_service_config.policy_content,
<<-EOF
package urlrewriter
result := []
EOF
)
}
data "aws_caller_identity" "current" {
count = module.this.enabled && var.aws_account_id == "" ? 1 : 0
}
data "aws_region" "current" {
count = module.this.enabled && var.aws_region_name == "" ? 1 : 0
}
# ====================================================== middleware-services ===
module "mw_service_label" {
source = "cloudposse/label/null"
version = "0.25.0"
name = local.name
context = module.this.context
}
resource "random_string" "mw_service_random_suffix" {
length = 6
special = false
upper = false
}
# --------------------------------------------------------------- cf-polices ---
data "aws_cloudfront_response_headers_policy" "cors_preflight_hsts" {
count = local.enabled ? 1 : 0
name = "Managed-CORS-with-preflight-and-SecurityHeadersPolicy"
}
data "aws_cloudfront_origin_request_policy" "all" {
count = local.enabled ? 1 : 0
name = "Managed-AllViewer"
}
data "aws_cloudfront_cache_policy" "disabled" {
count = local.enabled ? 1 : 0
name = "Managed-CachingDisabled"
}
# ---------------------------------------------------------------------- iam ---
resource "aws_iam_role" "this" {
count = local.enabled ? 1 : 0
name = module.mw_service_label.id
description = ""
assume_role_policy = jsonencode({
Version = "2012-10-17",
Statement = [{
Effect = "Allow"
Principal = { "Service" : ["edgelambda.amazonaws.com", "lambda.amazonaws.com"] }
Action = ["sts:AssumeRole", "sts:TagSession"]
}]
})
managed_policy_arns = [
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
]
tags = module.mw_service_label.tags
lifecycle {
create_before_destroy = true
}
}
# ============================================================ auth-services ===
module "mw_auth_service_label" {
source = "cloudposse/label/null"
version = "0.25.0"
for_each = local.auth_service_names
attributes = [each.key]
context = module.mw_service_label.context
}
module "auth_service_code" {
source = "cruxstack/artifact-packager/docker"
version = "1.3.2"
enabled = local.auth_service_enabled
attributes = ["auth"]
artifact_src_path = "/tmp/package.zip"
artifact_dst_directory = "${path.module}/dist"
docker_build_context = abspath("${path.module}/assets/cf-mw-auth")
docker_build_args = { SERVICE_CONFIG_ENCODED = base64encode(jsonencode(local.auth_service_config)), SERVICE_POLICY_ENCODED = base64encode(coalesce(var.auth_service_config.opa_policy_content, local.auth_service_config_default_opa_policy_content)) }
docker_build_target = "package"
context = module.mw_service_label.context
}
resource "random_password" "auth_service_oidc_state_encrypt_key" {
count = local.enabled ? 1 : 0
length = 32 # 256 bits
special = true
}
resource "random_string" "auth_service" {
for_each = local.auth_service_names
length = 8
special = false
lower = false
upper = true
keepers = {
name = each.key
}
}
resource "aws_lambda_function" "auth_service" {
for_each = local.auth_service_names
function_name = "${module.mw_auth_service_label[each.key].id}-${random_string.auth_service[each.key].result}"
filename = module.auth_service_code.artifact_package_path
handler = local.service_config[each.key].handler
publish = true
runtime = "nodejs18.x"
timeout = 5
role = aws_iam_role.this[0].arn
layers = []
tags = module.mw_auth_service_label[each.key].tags
lifecycle {
create_before_destroy = true
}
depends_on = [
module.auth_service_code
]
}
resource "aws_lambda_permission" "auth_service_allow_cloudfront" {
for_each = aws_lambda_function.auth_service
function_name = each.value.function_name
statement_id = "AllowExecutionFromCloudFront"
action = "lambda:GetFunction"
principal = "edgelambda.amazonaws.com"
}
# ====================================================== urlrewrite-services ===
module "mw_urlrewrite_service_label" {
source = "cloudposse/label/null"
version = "0.25.0"
for_each = local.urlrewrite_service_names
attributes = [each.key]
context = module.mw_service_label.context
}
module "urlrewrite_service_code" {
source = "cruxstack/artifact-packager/docker"
version = "1.3.2"
enabled = local.urlrewrite_service_enabled
attributes = ["urlrewrite"]
artifact_dst_directory = "${path.module}/dist"
artifact_src_path = "/tmp/package.zip"
docker_build_context = abspath("${path.module}/assets/cf-mw-urlrewrite")
docker_build_args = { SERVICE_CONFIG_ENCODED = base64encode(jsonencode(local.urlrewrite_service_config)), SERVICE_POLICY_ENCODED = base64encode(local.urlrewrite_policy_content) }
docker_build_target = "package"
context = module.mw_service_label.context
}
resource "random_string" "urlrewrite_service" {
for_each = local.urlrewrite_service_names
length = 8
special = false
lower = false
upper = true
keepers = {
name = each.key
}
}
resource "aws_lambda_function" "urlrewrite_service" {
for_each = local.urlrewrite_service_names
function_name = "${module.mw_urlrewrite_service_label[each.key].id}- ${random_string.urlrewrite_service[each.key].result}"
filename = module.urlrewrite_service_code.artifact_package_path
handler = local.service_config[each.key].handler
publish = true
runtime = "nodejs18.x"
timeout = 5
role = aws_iam_role.this[0].arn
layers = []
tags = module.mw_urlrewrite_service_label[each.key].tags
lifecycle {
create_before_destroy = true
}
depends_on = [
module.urlrewrite_service_code
]
}
resource "aws_lambda_permission" "urlrewrite_service_allow_cloudfront" {
for_each = aws_lambda_function.urlrewrite_service
function_name = each.value.function_name
statement_id = "AllowExecutionFromCloudFront"
action = "lambda:GetFunction"
principal = "edgelambda.amazonaws.com"
}