grada-run 0.41.4 → 0.42.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,60 @@
1
- # Fetch the AWS Managed Cache Policy (Optimized for standard web traffic)
2
- data "aws_cloudfront_cache_policy" "optimized" {
3
- name = "Managed-CachingOptimized"
1
+ # App CDN cache policies: a single path-only key (Managed-CachingOptimized)
2
+ # is wrong for an app server — one slot per path gets shared by HTML docs,
3
+ # API/RSC variants, redirects, and errors, so a cacheable variant poisons
4
+ # every other viewer of that path. Each traffic class below gets its own
5
+ # key instead (BUG-82).
6
+ data "aws_cloudfront_cache_policy" "disabled" {
7
+ name = "Managed-CachingDisabled"
8
+ }
9
+
10
+ # Default behavior key: query strings + cookies + router headers, so each
11
+ # page variant (search params, session, RSC/prefetch) caches independently
12
+ # and authenticated bytes never cross viewers. The header keys are harmless
13
+ # when absent — frameworks that never send them simply never vary on them.
14
+ resource "aws_cloudfront_cache_policy" "app_default" {
15
+ name = "${local.app_name}-app-default"
16
+ comment = "Per-variant app key: all query strings, cookies, and router headers"
17
+ default_ttl = 60
18
+ max_ttl = 86400
19
+ min_ttl = 0
20
+ parameters_in_cache_key_and_forwarded_to_origin {
21
+ cookies_config {
22
+ cookie_behavior = "all"
23
+ }
24
+ headers_config {
25
+ header_behavior = "whitelist"
26
+ headers {
27
+ items = ["RSC", "Next-Router-State-Tree", "Next-Router-Prefetch", "Next-Url"]
28
+ }
29
+ }
30
+ query_strings_config {
31
+ query_string_behavior = "all"
32
+ }
33
+ enable_accept_encoding_brotli = true
34
+ enable_accept_encoding_gzip = true
35
+ }
36
+ }
37
+
38
+ # Framework build assets are content-hashed: path-only key, immutable TTL.
39
+ resource "aws_cloudfront_cache_policy" "app_static" {
40
+ name = "${local.app_name}-app-static"
41
+ comment = "Content-hashed framework assets: immutable, long TTL"
42
+ default_ttl = 31536000
43
+ max_ttl = 31536000
44
+ min_ttl = 0
45
+ parameters_in_cache_key_and_forwarded_to_origin {
46
+ cookies_config {
47
+ cookie_behavior = "none"
48
+ }
49
+ headers_config {
50
+ header_behavior = "none"
51
+ }
52
+ query_strings_config {
53
+ query_string_behavior = "none"
54
+ }
55
+ enable_accept_encoding_brotli = true
56
+ enable_accept_encoding_gzip = true
57
+ }
4
58
  }
5
59
 
6
60
  # Fetch the AWS Managed Origin Request Policy (Passes query strings/cookies to your app)
@@ -8,17 +62,33 @@ data "aws_cloudfront_origin_request_policy" "all_viewer" {
8
62
  name = "Managed-AllViewerExceptHostHeader"
9
63
  }
10
64
 
65
+ # Managed security headers (HSTS, frame/clickjacking guards, content-type
66
+ # sniffing) on every behavior (BUG-92).
67
+ data "aws_cloudfront_response_headers_policy" "security" {
68
+ name = "Managed-SecurityHeadersPolicy"
69
+ }
70
+
11
71
  # trivy:ignore:AVD-AWS-0011 - WAF is omitted by default to prevent unexpected monthly costs for users
12
72
  resource "aws_cloudfront_distribution" "cdn" {
13
73
  enabled = true
14
74
  is_ipv6_enabled = true
75
+ comment = "${local.app_name}-cdn"
76
+ price_class = "PriceClass_100"
15
77
  wait_for_deployment = false # Prevents Terraform from hanging for 10+ minutes
16
-
78
+
17
79
  origin {
18
80
  # Point the CDN at the Load Balancer we provision in main.tf
19
81
  domain_name = aws_lb.main.dns_name
20
82
  origin_id = "ALBOrigin"
21
83
 
84
+ # Origin secret (BUG-91): proves to the ALB that the request came
85
+ # through THIS distribution — any other tenant's distro, or a direct
86
+ # ALB hit, lacks the header and gets a 403 from the listener rule.
87
+ custom_header {
88
+ name = "X-Grada-Origin-Secret"
89
+ value = random_password.origin_secret.result
90
+ }
91
+
22
92
  custom_origin_config {
23
93
  http_port = 80
24
94
  https_port = 443
@@ -28,6 +98,31 @@ resource "aws_cloudfront_distribution" "cdn" {
28
98
  }
29
99
  }
30
100
 
101
+ ordered_cache_behavior {
102
+ path_pattern = "/api/*"
103
+ allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
104
+ cached_methods = ["GET", "HEAD"]
105
+ target_origin_id = "ALBOrigin"
106
+
107
+ viewer_protocol_policy = "redirect-to-https"
108
+
109
+ cache_policy_id = data.aws_cloudfront_cache_policy.disabled.id
110
+ origin_request_policy_id = data.aws_cloudfront_origin_request_policy.all_viewer.id
111
+ response_headers_policy_id = data.aws_cloudfront_response_headers_policy.security.id
112
+ }
113
+
114
+ ordered_cache_behavior {
115
+ path_pattern = "/_next/static/*"
116
+ allowed_methods = ["GET", "HEAD", "OPTIONS"]
117
+ cached_methods = ["GET", "HEAD"]
118
+ target_origin_id = "ALBOrigin"
119
+
120
+ viewer_protocol_policy = "redirect-to-https"
121
+
122
+ cache_policy_id = aws_cloudfront_cache_policy.app_static.id
123
+ response_headers_policy_id = data.aws_cloudfront_response_headers_policy.security.id
124
+ }
125
+
31
126
  default_cache_behavior {
32
127
  # Allow all HTTP methods so API POST/PUT requests still work
33
128
  allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
@@ -36,9 +131,41 @@ resource "aws_cloudfront_distribution" "cdn" {
36
131
 
37
132
  viewer_protocol_policy = "redirect-to-https"
38
133
 
39
- # Attach the Managed Policies
40
- cache_policy_id = data.aws_cloudfront_cache_policy.optimized.id
41
- origin_request_policy_id = data.aws_cloudfront_origin_request_policy.all_viewer.id
134
+ # Per-variant app key + full-variance origin forwarding
135
+ cache_policy_id = aws_cloudfront_cache_policy.app_default.id
136
+ origin_request_policy_id = data.aws_cloudfront_origin_request_policy.all_viewer.id
137
+ response_headers_policy_id = data.aws_cloudfront_response_headers_policy.security.id
138
+ }
139
+
140
+ # Error TTL caps: origin error bodies pass through untouched, but cache
141
+ # briefly so a transient 404/5xx never sticks for hours.
142
+ custom_error_response {
143
+ error_code = 400
144
+ error_caching_min_ttl = 60
145
+ }
146
+ custom_error_response {
147
+ error_code = 403
148
+ error_caching_min_ttl = 60
149
+ }
150
+ custom_error_response {
151
+ error_code = 404
152
+ error_caching_min_ttl = 60
153
+ }
154
+ custom_error_response {
155
+ error_code = 500
156
+ error_caching_min_ttl = 10
157
+ }
158
+ custom_error_response {
159
+ error_code = 502
160
+ error_caching_min_ttl = 10
161
+ }
162
+ custom_error_response {
163
+ error_code = 503
164
+ error_caching_min_ttl = 10
165
+ }
166
+ custom_error_response {
167
+ error_code = 504
168
+ error_caching_min_ttl = 10
42
169
  }
43
170
 
44
171
  restrictions {
@@ -65,7 +192,7 @@ output "z_NEXT_STEP_REQUIRED" {
65
192
  🚀 INFRASTRUCTURE PROVISIONED SUCCESSFULLY!
66
193
  ====================================================================
67
194
 
68
- Your AWS environment is ready. However, your URLs listed above will
195
+ Your AWS environment is ready. However, your URLs listed above will
69
196
  return an error until your application code is actually deployed.
70
197
 
71
198
  TO DEPLOY YOUR APP:
@@ -73,8 +200,8 @@ TO DEPLOY YOUR APP:
73
200
  2. git commit -m "ci: configure grada"
74
201
  3. git push origin main
75
202
 
76
- Once the GitHub Action completes, your site will be live at the
203
+ Once the GitHub Action completes, your site will be live at the
77
204
  CloudFront URL!
78
205
  ====================================================================
79
206
  EOT
80
- }
207
+ }
@@ -28,9 +28,17 @@ provider "aws" {
28
28
 
29
29
  locals {
30
30
  # Safely read the keys generated by the CLI, default to empty list if file doesn't exist.
31
- # toset() collapses hand-duplicated entries: duplicate secrets render the
32
- # same RegisterTaskDefinition 400 as a name collision (BUG-79).
33
- secret_keys = fileexists("${path.module}/secret_keys.json") ? toset(jsondecode(file("${path.module}/secret_keys.json"))) : []
31
+ # distinct() collapses hand-duplicated entries (duplicates render the
32
+ # same RegisterTaskDefinition 400 as a name collision) while preserving
33
+ # file order, so upgrading trees get no task-definition churn (BUG-81).
34
+ secret_keys = fileexists("${path.module}/secret_keys.json") ? distinct(jsondecode(file("${path.module}/secret_keys.json"))) : []
35
+
36
+ # Exact container env + static-secret names (union over app and worker
37
+ # task definitions), maintained by the CLI at scaffold/add time. The
38
+ # app task-definition precondition below intersects it with
39
+ # secret_keys.json so CI fails plan with a named error instead of
40
+ # dying late on the raw ECS 400.
41
+ managed_env_names = {{MANAGED_ENV_NAMES}}
34
42
 
35
43
  # Workspace suffix for PR preview environments (e.g., "-pr-123"). Empty for production.
36
44
  env_suffix = terraform.workspace == "default" ? "" : "-${terraform.workspace}"
@@ -139,6 +147,13 @@ resource "aws_ecs_task_definition" "app" {
139
147
  execution_role_arn = aws_iam_role.execution_role.arn
140
148
  task_role_arn = aws_iam_role.task_role.arn
141
149
 
150
+ lifecycle {
151
+ precondition {
152
+ condition = length(setintersection(toset(local.secret_keys), toset(local.managed_env_names))) == 0
153
+ error_message = "secret_keys.json collides with container environment: ${join(", ", setintersection(toset(local.secret_keys), toset(local.managed_env_names)))}. Remove the key(s) from .env and re-push (push skips them automatically). If push does not skip the key, this guard list is stale — remove the entry from managed_env_names in terraform/main.tf."
154
+ }
155
+ }
156
+
142
157
  container_definitions = jsonencode([
143
158
  {
144
159
  name = "${local.app_name}-container"
@@ -213,6 +228,16 @@ resource "aws_lb_target_group" "app" {
213
228
  }
214
229
  }
215
230
 
231
+ # Origin secret (BUG-91): only requests bearing the header CloudFront
232
+ # injects may reach the app. The CloudFront edge-IP security group alone
233
+ # cannot prove WHICH distribution sent a request — any tenant's distro
234
+ # could point at this ALB — so the listener forwards solely on a header
235
+ # match and 403s everything else (including direct ALB hits).
236
+ resource "random_password" "origin_secret" {
237
+ length = 32
238
+ special = false
239
+ }
240
+
216
241
  # trivy:ignore:AVD-AWS-0054 - CloudFront handles HTTPS edge termination; ALB uses HTTP to avoid complex ACM DNS validation for users
217
242
  resource "aws_lb_listener" "http" {
218
243
  load_balancer_arn = aws_lb.main.arn
@@ -220,9 +245,30 @@ resource "aws_lb_listener" "http" {
220
245
  protocol = "HTTP"
221
246
 
222
247
  default_action {
248
+ type = "fixed-response"
249
+ fixed_response {
250
+ content_type = "text/plain"
251
+ message_body = "Forbidden"
252
+ status_code = "403"
253
+ }
254
+ }
255
+ }
256
+
257
+ resource "aws_lb_listener_rule" "origin_secret" {
258
+ listener_arn = aws_lb_listener.http.arn
259
+ priority = 1
260
+
261
+ action {
223
262
  type = "forward"
224
263
  target_group_arn = aws_lb_target_group.app.arn
225
264
  }
265
+
266
+ condition {
267
+ http_header {
268
+ http_header_name = "X-Grada-Origin-Secret"
269
+ values = [random_password.origin_secret.result]
270
+ }
271
+ }
226
272
  }
227
273
 
228
274
  # --- ECS Service ---
@@ -292,6 +338,10 @@ output "ecr_repository_url" {
292
338
  }
293
339
 
294
340
  # --- CloudWatch Alarms ---
341
+ # State-only alarm: visible in the console, notifies nobody. There is no
342
+ # notification destination at scaffold time; `grada alerts` provisions
343
+ # separate alarms wired to SNS, and this one stays as a console signal so
344
+ # quiet trees still show red (BUG-101).
295
345
  resource "aws_cloudwatch_metric_alarm" "alb_5xx_errors" {
296
346
  alarm_name = "${local.app_name}-high-5xx-errors"
297
347
  comparison_operator = "GreaterThanThreshold"
@@ -80,8 +80,11 @@ resource "aws_security_group" "alb" {
80
80
  to_port = 80
81
81
  protocol = "tcp"
82
82
  # CloudFront-only: the origin speaks plaintext HTTP, so direct browser
83
- # access would bypass the CDN (and TLS on domained trees). Debugging
84
- # via the direct URL needs a temporary personal-IP rule.
83
+ # access would bypass the CDN (and TLS on domained trees). The origin
84
+ # secret (random_password.origin_secret) proves WHICH distribution sent
85
+ # a request — the listener 403s anything without it. Debugging via the
86
+ # direct URL needs both a temporary personal-IP rule and the
87
+ # X-Grada-Origin-Secret header value from the Terraform state.
85
88
  prefix_list_ids = [data.aws_ec2_managed_prefix_list.cloudfront.id]
86
89
  }
87
90
 
@@ -47,7 +47,10 @@ resource "aws_iam_role" "github_actions" {
47
47
  # CI trust, scoped at scaffold time from --github-repo, the git
48
48
  # remote, or the setup prompt. "repo:*" means any GitHub repo's
49
49
  # Actions can assume this role — re-scope before going live.
50
- "token.actions.githubusercontent.com:sub" : "{{GITHUB_REPO_SUB}}"
50
+ # Scoped trusts render both subject-claim formats: the classic
51
+ # `repo:<owner>/<name>:*` plus the immutable-claim companion
52
+ # `repo:<owner>@*/<name>@*:*` (repos created after 2026-07-15).
53
+ "token.actions.githubusercontent.com:sub" : {{GITHUB_REPO_SUB}}
51
54
  }
52
55
  StringEquals = {
53
56
  "token.actions.githubusercontent.com:aud" : "sts.amazonaws.com"