hecks 3.11.0 → 3.13.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.
Files changed (118) hide show
  1. checksums.yaml +4 -4
  2. data/lib/hecks/adapters/driven/script_runner.adapter +3 -0
  3. data/lib/hecks/adapters/driven/script_runner.rb +93 -0
  4. data/lib/hecks/adapters/driven.rb +1 -0
  5. data/lib/hecks/adapters/driving/cli_runner/resolution.rb +6 -3
  6. data/lib/hecks/adapters/driving/cli_runner.rb +1 -0
  7. data/lib/hecks/adapters/driving/newcomer.rb +50 -0
  8. data/lib/hecks/bluebook/assembly/contracts/read_models.rb +1 -1
  9. data/lib/hecks/bluebook/assembly/marks.rb +3 -15
  10. data/lib/hecks/bluebook/assembly/scalars.rb +39 -0
  11. data/lib/hecks/bluebook/meta_validator/readings.rb +6 -0
  12. data/lib/hecks/bluebook/meta_validator/reconstruction.rb +6 -3
  13. data/lib/hecks/bluebook/meta_validator/syntax_boot.rb +16 -12
  14. data/lib/hecks/bluebook/meta_validator.rb +28 -21
  15. data/lib/hecks/bluebook.rb +1 -0
  16. data/lib/hecks/deploy/bluebook/deploy.bluebook +17 -3
  17. data/lib/hecks/framework/bluebook/change.behaviors +30 -0
  18. data/lib/hecks/framework/bluebook/change.bluebook +162 -0
  19. data/lib/hecks/framework/bluebook/change.hecksagon +21 -0
  20. data/lib/hecks/framework/change.oidc.json +34 -0
  21. data/lib/hecks/framework/console_settings.oidc.json +103 -0
  22. data/lib/hecks/framework/governance.oidc.json +28 -0
  23. data/lib/hecks/framework/identity.oidc.json +18 -0
  24. data/lib/hecks/framework/privacy.oidc.json +30 -0
  25. data/lib/hecks/gate/stages.yml +35 -0
  26. data/lib/hecks/grammar/translation.oidc.json +48 -0
  27. data/lib/hecks/hecks/adapters/codebase/test_runner.rb +12 -1
  28. data/lib/hecks/hecks/adapters/deploy_toolchain.rb +2 -1
  29. data/lib/hecks/hecks/adapters/rehearsal/postgres_store.rb +5 -1
  30. data/lib/hecks/hecks/hecks.world +3 -0
  31. data/lib/hecks/language/bluebook/bluebook.behaviors +23 -0
  32. data/lib/hecks/language/bluebook/policy.behaviors +19 -0
  33. data/lib/hecks/language/bluebook/vocabulary.bluebook +52 -0
  34. data/lib/hecks/projection_files/expression_tables.rb +7 -2
  35. data/lib/hecks/projections/deploy/box/compose.rb +6 -4
  36. data/lib/hecks/projections/deploy/box/edge.rb +62 -0
  37. data/lib/hecks/projections/deploy/box/logging.rb +99 -0
  38. data/lib/hecks/projections/deploy/box/roll_scripts.rb +16 -8
  39. data/lib/hecks/projections/deploy/box/settings/cdn.rb +40 -0
  40. data/lib/hecks/projections/deploy/box/settings/logs.rb +40 -0
  41. data/lib/hecks/projections/deploy/box/settings/patterns.rb +11 -0
  42. data/lib/hecks/projections/deploy/box/settings/types.rb +16 -1
  43. data/lib/hecks/projections/deploy/box/settings.rb +6 -2
  44. data/lib/hecks/projections/deploy/box/stack_yaml.rb +5 -2
  45. data/lib/hecks/projections/deploy/box/templates/Makefile.tmpl +16 -0
  46. data/lib/hecks/projections/deploy/box/templates/box.yaml.tmpl +8 -4
  47. data/lib/hecks/projections/deploy/box/templates/deploy-box.sh.tmpl +3 -0
  48. data/lib/hecks/projections/deploy/box/templates/edge.mk.tmpl +17 -0
  49. data/lib/hecks/projections/deploy/box/templates/edge.yaml.tmpl +65 -0
  50. data/lib/hecks/projections/deploy/box/templates/log-group.yaml.tmpl +12 -0
  51. data/lib/hecks/projections/deploy/box/templates/log-policy.yaml.tmpl +7 -0
  52. data/lib/hecks/projections/deploy/box/templates/render-compose-taskdef.sh.tmpl +5 -4
  53. data/lib/hecks/projections/deploy/box/templates/render-compose.sh.tmpl +5 -4
  54. data/lib/hecks/projections/deploy/box.rb +15 -3
  55. data/lib/hecks/projections/deploy/fargate/cdn/lines.rb +12 -1
  56. data/lib/hecks/projections/site/admin_cms/templates/membership.rb +10 -7
  57. data/lib/hecks/projections/site/edge/behaviours.rb +10 -6
  58. data/lib/hecks/projections/site/edge/checks.rb +2 -0
  59. data/lib/hecks/projections/site/edge/function_checks.rb +104 -0
  60. data/lib/hecks/projections/site/edge/function_code.rb +86 -0
  61. data/lib/hecks/projections/site/edge/redirect_host.js.tmpl +65 -0
  62. data/lib/hecks/projections/site/edge.rb +35 -9
  63. data/lib/hecks/projections/site/site_cdn.rb +21 -7
  64. data/lib/hecks/projections/site/site_root/workflow.rb +5 -0
  65. data/lib/hecks/projections/site/table/row_builder.rb +12 -2
  66. data/lib/hecks/projections/site/table.rb +4 -2
  67. data/lib/hecks/projector/cli_audience.rb +20 -0
  68. data/lib/hecks/projector/cli_projector/usage.rb +15 -4
  69. data/lib/hecks/rust_build/host.rb +11 -4
  70. data/lib/hecks/rust_build/prebuilt_binary.rb +101 -0
  71. data/lib/hecks/site/bluebook/site.bluebook +16 -1
  72. data/lib/hecks/tools/ci_gate_decision.rb +35 -0
  73. data/lib/hecks/tools/ci_gates.rb +20 -1
  74. data/lib/hecks/tools/lanes/files.rb +8 -0
  75. data/lib/hecks/tools/lanes/prebuilt_workflow.rb +81 -0
  76. data/lib/hecks/tools/lanes.rb +1 -0
  77. data/lib/hecks/tools/oidc_manifests.rb +44 -6
  78. data/lib/hecks/tools/release_only.rb +111 -0
  79. data/lib/hecks/tools/site_routes/templates.rb +13 -9
  80. data/lib/hecks/version.rb +1 -1
  81. data/lib/hecks/vocabulary.rb +15 -1
  82. data/prebuilt/2b59737fbdbd5f44edda0fddce2611f45d76087b3a7c782d1f1a17ea58c77948.marshal +0 -0
  83. data/prebuilt/58208399bada10b0db99928c48e38789288fc58f7b71a72b21bfa386680e32ed.marshal +0 -0
  84. data/prebuilt/{verdicts-230ac18c25abfd3d92ede032bee2661a4c2cd2773ab0cf0e2ae52546ba2f7af9.json → verdicts-48318ca172edd21443b1ad02801c697fff100b746e828f09db987647641a5df2.json} +1 -1
  85. data/rust/host/HECKS_RELEASE +1 -1
  86. data/rust/host/src/auth.rs +58 -0
  87. data/rust/host/src/extension.rs +13 -1
  88. data/rust/host/src/lib.rs +1 -0
  89. data/rust/host/src/platform_signin/attack_tests.rs +1100 -0
  90. data/rust/host/src/platform_signin/config.rs +512 -0
  91. data/rust/host/src/platform_signin/cookie.rs +237 -0
  92. data/rust/host/src/platform_signin/extension.rs +190 -0
  93. data/rust/host/src/platform_signin/fetcher.rs +322 -0
  94. data/rust/host/src/platform_signin/handler.rs +251 -0
  95. data/rust/host/src/platform_signin/handler_tests.rs +554 -0
  96. data/rust/host/src/platform_signin/jti.rs +196 -0
  97. data/rust/host/src/platform_signin/jwks.rs +542 -0
  98. data/rust/host/src/platform_signin/jws.rs +249 -0
  99. data/rust/host/src/platform_signin/known_answer_tests.rs +237 -0
  100. data/rust/host/src/platform_signin/mapping_tests.rs +254 -0
  101. data/rust/host/src/platform_signin/mod.rs +75 -0
  102. data/rust/host/src/platform_signin/mode_tests.rs +382 -0
  103. data/rust/host/src/platform_signin/pages.rs +204 -0
  104. data/rust/host/src/platform_signin/query.rs +233 -0
  105. data/rust/host/src/platform_signin/session.rs +151 -0
  106. data/rust/host/src/platform_signin/settings.rs +385 -0
  107. data/rust/host/src/platform_signin/strict_json.rs +351 -0
  108. data/rust/host/src/platform_signin/test_support.rs +235 -0
  109. data/rust/host/src/platform_signin/ttl_tests.rs +94 -0
  110. data/rust/host/src/platform_signin/verify.rs +604 -0
  111. data/rust/host/src/platform_signin/wiring_support.rs +319 -0
  112. data/rust/host/src/web.rs +50 -6
  113. data/rust/parser/src/parse/value_object.rs +22 -10
  114. metadata +59 -7
  115. data/prebuilt/05c358d38d661a3774f813d49c66f5b68939af94432d66257a56f49ee2598994.marshal +0 -0
  116. data/prebuilt/26c40c61f09b09b11651c64668eda3ceb180f83cf2a3802d3033d2fd99ac841b.marshal +0 -0
  117. /data/lib/hecks/framework/{oidc.json → compliance.oidc.json} +0 -0
  118. /data/lib/hecks/grammar/{oidc.json → expression.oidc.json} +0 -0
@@ -611,6 +611,10 @@ Hecks.bluebook "Bluebook" do
611
611
  # `mode` is how `pattern` (an extended regex over changed paths) decides:
612
612
  # `touches` runs the job when any changed path matches, `skips_unless` runs it
613
613
  # when any changed path does NOT match (the pattern is the safe-to-skip list).
614
+ # `release_only` answers a different question and ignores `pattern`: `touched` is false only
615
+ # when the change edits nothing but the `ReleaseFile` rows' files and lines and the base it is
616
+ # measured against has every inheritable `RequiredCheck` green (ADR 0107), so the run may
617
+ # inherit that certification.
614
618
  # `push` is what a push event does: `skip` leaves the gate out of push runs,
615
619
  # `before_sha` diffs against the push's `before` commit.
616
620
  value_object "CiGate" do
@@ -624,19 +628,44 @@ Hecks.bluebook "Bluebook" do
624
628
  member name: "runtime_changed", workflow: "ci.yml", mode: "touches",
625
629
  pattern: '^lib/hecks/runtime/', push: "before_sha",
626
630
  label: "does this change touch lib/hecks/runtime/**?"
631
+ member name: "release_only", workflow: "ci.yml", mode: "release_only",
632
+ pattern: "", push: "before_sha",
633
+ label: "is this change only a release bump on a certified base?"
627
634
  member name: "postgres_io_relevant_changed", workflow: "ci-postgres-io-parallel.yml", mode: "skips_unless",
628
635
  pattern: '^(docs/|editors/|release/|deploy/|\.claude/|\.githooks/|rust/(parser|codegen|host|build|lsp|web|tests)/|\.rubocop\.yml$|\.rubocop_todo\.yml$|\.mcp\.json$|\.rspec-local\.example$|README\.md$|CHANGELOG\.md$|CONTRIBUTING\.md$|SECURITY\.md$|LICENSE$|\.gitignore$)',
629
636
  push: "skip",
630
637
  label: "does this change touch anything rspec_postgres_io_parallel covers?"
631
638
  end
632
639
 
640
+ # The files a release bump edits, and the lines of each it may change. A commit whose whole
641
+ # diff against a certified base falls inside these rows inherits the base's certification
642
+ # (`CiGate` `release_only`, ADR 0107): its checks report success without their jobs running,
643
+ # and the `release_consistency` check still runs on it. `path` is the file, exact; `lines` is
644
+ # an extended regex every added or removed line of that file must match, empty for any line.
645
+ value_object "ReleaseFile" do
646
+ attribute :path, String
647
+ attribute :lines, String
648
+
649
+ member path: "lib/hecks/version.rb", lines: "^\\s*VERSION = \"[0-9]+\\.[0-9]+\\.[0-9]+\"\\.freeze$"
650
+ member path: "CHANGELOG.md", lines: ""
651
+ member path: "README.md", lines: "^(\\*\\*Status:\\*\\* )?Current release: `[0-9]+\\.[0-9]+\\.[0-9]+`"
652
+ member path: "Gemfile.lock", lines: "^ hecks \\([0-9]+\\.[0-9]+\\.[0-9]+\\)$"
653
+ member path: "rust/host/HECKS_RELEASE", lines: "^[0-9]+\\.[0-9]+\\.[0-9]+$"
654
+ member path: "packages/hecks-client/package.json", lines: "^\\s*\"version\": \"[0-9]+\\.[0-9]+\\.[0-9]+\",?$"
655
+ member path: "packages/hecks-client/package-lock.json", lines: "^\\s*\"version\": \"[0-9]+\\.[0-9]+\\.[0-9]+\",?$"
656
+ end
657
+
633
658
  # The checks a commit must have passed before a `Promotion` moves it onto a guarded lane. Each
634
659
  # is the name of a job in .github/workflows that reports to a commit as a check run; a check
635
660
  # that a path gate skipped reports `skipped`, which counts as passed. `hecks promote` reads
636
661
  # these rows, and hecks project_lanes writes them into the ruleset of any lane that requires
637
662
  # them, so the list lives here and nowhere else.
663
+ #
664
+ # `runs` is `always` for a check a release bump still runs (it is not inherited from the base);
665
+ # empty for a check a bump may inherit.
638
666
  value_object "RequiredCheck" do
639
667
  attribute :name, String
668
+ attribute :runs, String
640
669
 
641
670
  member name: "rspec"
642
671
  member name: "checks"
@@ -647,6 +676,7 @@ Hecks.bluebook "Bluebook" do
647
676
  member name: "rspec_rust_codegen"
648
677
  member name: "rspec_rust_host"
649
678
  member name: "rspec_fuzzing"
679
+ member name: "release_consistency", runs: "always"
650
680
  end
651
681
 
652
682
  # The branches a change travels along. `main` takes pushes with no gate; `stable` takes only
@@ -679,6 +709,28 @@ Hecks.bluebook "Bluebook" do
679
709
  release_trailer: "Release-Approved-By"
680
710
  end
681
711
 
712
+ # A binary hecks builds once per change to its crate and publishes, so a client that needs the
713
+ # same binary downloads it instead of compiling it (ADR 0110). `hecks regeneration_run.project_lanes`
714
+ # writes .github/workflows/publish-<name>.yml from each row; `Hecks::RustBuild::PrebuiltBinary`
715
+ # reads the same rows to name what to download.
716
+ #
717
+ # `crate` is the directory under the checkout that is built and keyed: the key is a hash of every
718
+ # file in it that goes into the build, so the same bytes in the crate are the same release.
719
+ # `executable` is the Cargo binary and `target` the Rust triple it is built for. `runner` is the
720
+ # GitHub-hosted image it is built on, pinned (never `-latest`) because a binary links the
721
+ # runner's glibc and must run on every host that runs that image or a newer one.
722
+ # The release is tagged `<name>-<key>` and carries `<executable>-<target>` and its `.sha256`.
723
+ value_object "PrebuiltBinary" do
724
+ attribute :name, String
725
+ attribute :crate, String
726
+ attribute :executable, String
727
+ attribute :target, String
728
+ attribute :runner, String
729
+
730
+ member name: "host", crate: "rust/host", executable: "bootstrap", target: "x86_64-unknown-linux-gnu",
731
+ runner: "ubuntu-24.04"
732
+ end
733
+
682
734
  # The dials of the hecks_qa loop: what each is called, the type of value it takes and what it
683
735
  # does. qa/settings.yml holds the values, which are edited and reviewed as a diff; this table
684
736
  # holds the rest, so the loader (`Hecks::Fuzzing::QaSettings`) checks the file against it and
@@ -47,10 +47,15 @@ module Hecks
47
47
  declared = algebra.fetch(operator[:symbol]) do
48
48
  raise Refused, "#{operator[:symbol]} is admitted but Vocabulary::Comparison declares no algebra for it"
49
49
  end
50
- row.merge(compares_less_than: declared[:compares_less_than],
51
- compares_equal: declared[:compares_equal], negated: declared[:negated])
50
+ row.merge(compares_less_than: spelled_true?(declared[:compares_less_than]),
51
+ compares_equal: spelled_true?(declared[:compares_equal]),
52
+ negated: spelled_true?(declared[:negated]))
52
53
  end
53
54
 
55
+ # @param cell [Object] a `Comparison` cell, declared as the text `"true"` or `"false"`
56
+ # @return [Boolean] whether the cell spells true
57
+ def spelled_true?(cell) = cell.to_s == "true"
58
+
54
59
  # @param dropped [Hash{String => Array<String>}] operator to the guards evaluating through it
55
60
  # @return [String] one line for each operator a projection would strand
56
61
  def dropped_message(dropped)
@@ -13,11 +13,13 @@ module Hecks
13
13
  # an ECS task definition when the world names one
14
14
  def render_compose_sh(plan, region)
15
15
  if plan.task_definition
16
- template("render-compose-taskdef.sh.tmpl", "STACK" => plan.infra_name, "FAMILY" => plan.task_definition,
17
- "PROXY_IMAGE" => plan.proxy_image)
16
+ template("render-compose-taskdef.sh.tmpl",
17
+ { "STACK" => plan.infra_name, "FAMILY" => plan.task_definition, "PROXY_IMAGE" => plan.proxy_image }
18
+ .merge(compose_log_values(plan, region, "$s.name")))
18
19
  else
19
- template("render-compose.sh.tmpl", "STACK" => plan.infra_name, "REGION" => region,
20
- "DB_NAME" => plan.database_name, "PROXY_IMAGE" => plan.proxy_image)
20
+ template("render-compose.sh.tmpl",
21
+ { "STACK" => plan.infra_name, "REGION" => region, "DB_NAME" => plan.database_name,
22
+ "PROXY_IMAGE" => plan.proxy_image }.merge(compose_log_values(plan, region, ".name")))
21
23
  end
22
24
  end
23
25
 
@@ -0,0 +1,62 @@
1
+ module Hecks
2
+ module Projections
3
+ module Deploy
4
+ module Box
5
+ # The CloudFront edge of a box deploy, for a world that declares `cdn`: the `edge.yaml`
6
+ # stack, the `edge.mk` that deploys it, and the two pieces `make stacks` gains so the box
7
+ # admits the CDN's addresses. Mixed into `Box`, which supplies `template`.
8
+ module Edge
9
+ # The AWS-managed prefix list of CloudFront's origin-facing addresses. Its id differs in
10
+ # every region, so it is looked up by this name rather than written down.
11
+ PREFIX_LIST_NAME = "com.amazonaws.global.cloudfront.origin-facing".freeze
12
+
13
+ # @param plan [Settings::Plan] the resolved settings
14
+ # @param region [String] the validated region
15
+ # @return [Hash{String => String}] `edge.yaml` and `edge.mk`, or nothing without a `cdn`
16
+ def edge_files(plan, region)
17
+ return {} unless plan.cdn
18
+
19
+ { "edge.yaml" => edge_yaml(plan),
20
+ "edge.mk" => template("edge.mk.tmpl", "EDGE_STACK" => plan.edge_stack, "REGION" => region) }
21
+ end
22
+
23
+ # @param plan [Settings::Plan] the resolved settings, with a `cdn`
24
+ # @return [String] the CloudFront stack
25
+ def edge_yaml(plan)
26
+ values = { "STACK" => plan.infra_name, "ALIASES" => plan.cdn.aliases.join(","),
27
+ "ORIGIN_HEADER" => plan.origin_header, "ORIGIN_SECRET" => plan.origin_secret }
28
+ template("edge.yaml.tmpl", values)
29
+ end
30
+
31
+ # @param plan [Settings::Plan] the resolved settings
32
+ # @param region [String] the validated region
33
+ # @return [Hash{String => String}] the Makefile markers `CDN_CHECK` and `CDN_PARAM`:
34
+ # empty without a `cdn`, so a world that does not opt in renders the same Makefile
35
+ def edge_makefile_values(plan, region)
36
+ return { "CDN_CHECK" => "", "CDN_PARAM" => "" } unless plan.cdn
37
+
38
+ { "CDN_CHECK" => prefix_list_check(region), "CDN_PARAM" => "\t\tCdnPrefixListId=#{prefix_list_lookup(region)} \\" }
39
+ end
40
+
41
+ private
42
+
43
+ def prefix_list_query(region)
44
+ "aws ec2 describe-managed-prefix-lists --region #{region} --filters " \
45
+ "Name=prefix-list-name,Values=#{PREFIX_LIST_NAME} --query \"PrefixLists[0].PrefixListId\" --output text"
46
+ end
47
+
48
+ def prefix_list_lookup(region)
49
+ "$$(#{prefix_list_query(region)})"
50
+ end
51
+
52
+ # Stops `make stacks` before it changes anything when the prefix list cannot be found,
53
+ # because a box deployed without it has no inbound rule and the CDN gets a 504.
54
+ def prefix_list_check(region)
55
+ "\t@#{prefix_list_query(region)} | grep -q '^pl-' || { echo \"no #{PREFIX_LIST_NAME} " \
56
+ "prefix list in #{region}: the box would admit no CDN and answer 504\" >&2; exit 1; }"
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,99 @@
1
+ module Hecks
2
+ module Projections
3
+ module Deploy
4
+ module Box
5
+ # Where the box's containers write their logs. With `logs "cloudwatch"` (the default) every
6
+ # service uses Docker's `awslogs` driver into one log group the box stack creates, one
7
+ # stream per service, and the box role may write to that group only. With `logs "local"`
8
+ # the services keep Docker's rotating `json-file` logs, which a roll deletes with the
9
+ # container.
10
+ #
11
+ # `docker logs` keeps working with `awslogs`: Docker's dual-logging cache (Docker 20.10 and
12
+ # later, which the Amazon Linux 2023 package installs) keeps a local copy for reads.
13
+ # Mixed into `Box`, which supplies `template`.
14
+ module Logging
15
+ LOG_EXIT_CODE = "# 44 the box stack has no log group yet: update the stack (make stacks) before rolling\n".freeze
16
+ LOG_NOTE = <<~NOTE.freeze
17
+ # Every container logs to the CloudWatch group the box stack creates. A container whose log
18
+ # stream cannot be written does not start, so the roll first checks that the stack has the
19
+ # group (and the role's right to write it) and exits 44 if not. SKIP_LOG_GROUP_CHECK=1 skips it.
20
+ # `docker logs` and the log capture below still read the box's local copy (Docker's dual-logging cache).
21
+ #
22
+ NOTE
23
+ LOG_GROUP_CHECK = <<~CHECK.freeze
24
+ if [ "${SKIP_LOG_GROUP_CHECK:-}" = 1 ]; then
25
+ echo "==> skipping the log group check (SKIP_LOG_GROUP_CHECK=1)"
26
+ elif ! aws cloudformation describe-stack-resource --stack-name "$BOX_STACK" --logical-resource-id BoxLogGroup >/dev/null 2>&1; then
27
+ echo "==> $BOX_STACK has no BoxLogGroup: its containers would fail to start on the awslogs driver." >&2
28
+ echo " Run make stacks to update box.yaml (log group and the role's logs policy) first, then roll again." >&2
29
+ exit 44
30
+ fi
31
+ CHECK
32
+ COMPOSE_LOG_NOTE = <<~NOTE.freeze
33
+ #
34
+ # Every service logs to the CloudWatch group the box stack creates (one stream per service). A
35
+ # container whose stream cannot be written does not start, so update the box stack (make stacks:
36
+ # the group and the role's logs policy) before the first roll that uses this file. `docker logs`
37
+ # still reads the box's local copy.
38
+ NOTE
39
+ JSON_FILE = '{driver: "json-file", options: {"max-size": "10m", "max-file": "3"}}'.freeze
40
+
41
+ # @param plan [Settings::Plan] the resolved settings
42
+ # @return [String] the log group resource spliced into `box.yaml`, or nothing for `local`
43
+ def log_group_yaml(plan)
44
+ return "" unless plan.cloudwatch_logs?
45
+
46
+ template("log-group.yaml.tmpl", "LOG_GROUP" => plan.log_group,
47
+ "RETENTION_DAYS" => plan.log_retention_days.to_s)
48
+ end
49
+
50
+ # @param plan [Settings::Plan] the resolved settings
51
+ # @return [String] the role policy that lets the box write to its log group only, or
52
+ # nothing for `local`
53
+ def log_policy_yaml(plan)
54
+ plan.cloudwatch_logs? ? template("log-policy.yaml.tmpl", {}) : ""
55
+ end
56
+
57
+ # The values `render-compose.sh` splices in: the jq definition of `log` and the call that
58
+ # names each service's stream. `local` renders the `json-file` definition and the bare
59
+ # `log`, so its output does not change.
60
+ #
61
+ # @param plan [Settings::Plan] the resolved settings
62
+ # @param region [String] the validated region
63
+ # @param service [String] the jq expression for a service's name in the template
64
+ # @return [Hash{String => String}] the template values
65
+ def compose_log_values(plan, region, service)
66
+ return { "COMPOSE_LOG_NOTE" => "", **local_log_values } unless plan.cloudwatch_logs?
67
+
68
+ { "COMPOSE_LOG_NOTE" => COMPOSE_LOG_NOTE, "LOG_DEF" => awslogs_definition(plan, region),
69
+ "LOG_SERVICE" => "log(#{service})", "LOG_CADDY" => 'log("caddy")', "LOG_TUNNEL" => 'log("cloudflared")' }
70
+ end
71
+
72
+ # The `deploy-box.sh` pieces that stop a roll onto a stack without the log group.
73
+ #
74
+ # @param plan [Settings::Plan] the resolved settings
75
+ # @return [Hash{String => String}] the exit-code line, the comment and the check, or empty
76
+ # strings for `local`
77
+ def log_roll_values(plan)
78
+ return { "LOG_EXIT_CODE" => "", "LOG_NOTE" => "", "LOG_GROUP_CHECK" => "" } unless plan.cloudwatch_logs?
79
+
80
+ { "LOG_EXIT_CODE" => LOG_EXIT_CODE, "LOG_NOTE" => LOG_NOTE, "LOG_GROUP_CHECK" => LOG_GROUP_CHECK }
81
+ end
82
+
83
+ private
84
+
85
+ def awslogs_definition(plan, region)
86
+ options = %({"awslogs-region": "#{region}", "awslogs-group": "#{plan.log_group}", \
87
+ "awslogs-create-group": "false", tag: $service})
88
+ %(def log($service): {driver: "awslogs", options: #{options}};)
89
+ end
90
+
91
+ def local_log_values
92
+ { "LOG_DEF" => "def log: #{JSON_FILE};", "LOG_SERVICE" => "log", "LOG_CADDY" => "log",
93
+ "LOG_TUNNEL" => "log" }
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
@@ -45,7 +45,8 @@ module Hecks
45
45
  "RDS_STACK" => plan.rds_stack, "REGION" => region,
46
46
  "DIR" => "/opt/#{plan.infra_name}", "HEALTH_CHECKS" => health_checks(plan),
47
47
  "SMOKE_HEADER" => smoke_header(plan), "USAGE" => deploy_usage(plan),
48
- "DB_SECRET_LINE" => database_secret_line(plan))
48
+ "DB_SECRET_LINE" => database_secret_line(plan),
49
+ **log_roll_values(plan))
49
50
  end
50
51
 
51
52
  # @param plan [Settings::Plan] the resolved settings
@@ -94,18 +95,25 @@ module Hecks
94
95
  end
95
96
 
96
97
  # @param plan [Settings::Plan] the resolved settings
98
+ # @param region [String] the validated region
97
99
  # @return [String] a Makefile whose targets create the two stacks and roll the box
98
- def makefile(plan)
99
- hint = plan.task_definition ? "[TASKDEF=family:revision]" : '[TAGS="web=20260101 worker=20260101"]'
100
- base = template("Makefile.tmpl", { "STACK" => plan.infra_name, "DEPLOY_HINT" => hint,
101
- "RDS_STACK" => plan.rds_stack, "BOX_STACK" => plan.box_stack,
102
- "DEPLOY_RECIPE" => RollRecipe.deploy(plan) }
103
- .merge(database_makefile_values(plan)))
104
- plan.hosting ? "#{base}\ninclude hosting.mk\n" : base
100
+ def makefile(plan, region)
101
+ values = makefile_values(plan, region)
102
+ base = template("Makefile.tmpl", values)
103
+ includes = [("hosting.mk" if plan.hosting), ("edge.mk" if plan.cdn)].compact
104
+ includes.empty? ? base : "#{base}\n#{includes.map { |file| "include #{file}\n" }.join}"
105
105
  end
106
106
 
107
107
  private
108
108
 
109
+ def makefile_values(plan, region)
110
+ hint = plan.task_definition ? "[TASKDEF=family:revision]" : '[TAGS="web=20260101 worker=20260101"]'
111
+ { "STACK" => plan.infra_name, "DEPLOY_HINT" => hint, "RDS_STACK" => plan.rds_stack,
112
+ "BOX_STACK" => plan.box_stack, "DEPLOY_RECIPE" => RollRecipe.deploy(plan),
113
+ "AMI_PARAMETER" => ami_parameter(plan.instance_type) }
114
+ .merge(database_makefile_values(plan), edge_makefile_values(plan, region))
115
+ end
116
+
109
117
  def schema_list(migration)
110
118
  migration.schemas.map { |name| "`#{name}`" }.join(", ")
111
119
  end
@@ -0,0 +1,40 @@
1
+ require_relative "patterns"
2
+
3
+ module Hecks
4
+ module Projections
5
+ module Deploy
6
+ module Box
7
+ module Settings
8
+ # Reads the `cdn` setting: the CloudFront edge in front of the box. Extended onto
9
+ # `Settings`. The alias names themselves are checked by the Deploy bluebook's
10
+ # `BoxTarget.Declare` before this runs; this checks the shape and the guard.
11
+ module CdnReaders
12
+ include Patterns
13
+
14
+ # @param value [Hash{Symbol => Object}, nil] the world's `cdn` setting
15
+ # @param origin_header [String, nil] the declared origin header
16
+ # @return [Cdn, nil] the edge to generate, or nil when the world declares none
17
+ # @raise [ArgumentError] when the shape is wrong or there is no origin guard
18
+ def read_cdn(value, origin_header)
19
+ return nil if value.nil?
20
+
21
+ aliases = cdn_aliases(value)
22
+ raise ArgumentError, CDN_NEEDS_ORIGIN if origin_header.nil?
23
+
24
+ Settings::Cdn.new(aliases: aliases.uniq)
25
+ end
26
+
27
+ private
28
+
29
+ def cdn_aliases(value)
30
+ listed = value.is_a?(Hash) && value.keys == [:aliases] ? value[:aliases] : nil
31
+ raise ArgumentError, CDN_SHAPE unless listed.is_a?(Array) && !listed.empty?
32
+
33
+ listed.map { |name| check(:cdn_aliases, name, CDN_ALIAS) }
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,40 @@
1
+ require_relative "patterns"
2
+
3
+ module Hecks
4
+ module Projections
5
+ module Deploy
6
+ module Box
7
+ module Settings
8
+ # Reads the `logs` and `log_retention_days` settings: where the containers' logs go and
9
+ # how long CloudWatch keeps them. Extended onto `Settings`.
10
+ #
11
+ # Retention is one of CloudWatch's own values and never "never expire", so a box's log
12
+ # bill is bounded by a declaration.
13
+ module LogReaders
14
+ include Patterns
15
+
16
+ # @param settings [Hash{Symbol => Object}] the world's `AwsBox` settings
17
+ # @return [Hash{Symbol => Object}] the plan's `logs` and `log_retention_days`
18
+ # @raise [ArgumentError] when `logs` is not a known destination, or the retention is not
19
+ # a value CloudWatch accepts
20
+ def read_logs(settings)
21
+ destination = settings.fetch(:logs, "cloudwatch").to_s
22
+ raise ArgumentError, LOGS_SHAPE unless LOG_DESTINATIONS.include?(destination)
23
+
24
+ days = settings.fetch(:log_retention_days, DEFAULT_LOG_RETENTION_DAYS)
25
+ raise ArgumentError, retention_refusal(days) unless LOG_RETENTION_DAYS.include?(days)
26
+
27
+ { logs: destination, log_retention_days: days }
28
+ end
29
+
30
+ private
31
+
32
+ def retention_refusal(days)
33
+ "log_retention_days: #{days.inspect} is not a CloudWatch retention; use one of #{LOG_RETENTION_DAYS.join(", ")}"
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -23,6 +23,13 @@ module Hecks
23
23
  S3_BUCKET = /\A[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]\z/
24
24
  S3_SHAPE = "s3_access: a list of `{ bucket: \"name\", write: true }` hashes (write is optional)".freeze
25
25
  FROM_TASKDEF = %i[env secrets repository].freeze
26
+ LOG_DESTINATIONS = %w[cloudwatch local].freeze
27
+ DEFAULT_LOG_RETENTION_DAYS = 30
28
+ # The retention periods CloudWatch Logs accepts, in days, without "never expire".
29
+ LOG_RETENTION_DAYS = [1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1096, 1827, 2192, 2557,
30
+ 2922, 3288, 3653].freeze
31
+ LOGS_SHAPE = "logs: \"cloudwatch\" ships every container's log to CloudWatch Logs; " \
32
+ "\"local\" keeps Docker's rotating json-file logs on the box".freeze
26
33
 
27
34
  # The settings that size an RDS instance: with `shared_database` they are the shared
28
35
  # instance's.
@@ -37,6 +44,10 @@ module Hecks
37
44
  "(the secret holding the tunnel token)".freeze
38
45
  ORIGIN_ENV_SHAPE = "origin_env: the container environment variable names that hold the origin secret " \
39
46
  "need an origin_secret and a task_definition to compare against".freeze
47
+ CDN_ALIAS = /\A[a-z0-9.-]{4,253}\z/
48
+ CDN_SHAPE = "cdn: a hash with `aliases`, the non-empty list of host names the CloudFront edge answers on".freeze
49
+ CDN_NEEDS_ORIGIN = "cdn: the box admits CloudFront's addresses on port 80, so it needs the origin guard " \
50
+ "that refuses every other caller; declare origin_header and origin_secret".freeze
40
51
  ORIGIN_PAIR = "origin_header and origin_secret go together: the header a CDN sends, " \
41
52
  "and the secret that holds its value".freeze
42
53
  end
@@ -45,18 +45,33 @@ module Hecks
45
45
  # @!attribute [r] write [Boolean] whether a production box may also write and delete
46
46
  Bucket = Struct.new(:name, :write, keyword_init: true)
47
47
 
48
+ # The CloudFront edge a world asks for with `cdn`.
49
+ #
50
+ # @!attribute [r] aliases [Array<String>] the host names the distribution answers on
51
+ Cdn = Struct.new(:aliases, keyword_init: true)
52
+
48
53
  # Everything the generator reads, checked.
49
54
  Plan = Struct.new(
50
55
  :infra_name, :stack_prefix, :instance_type, :volume_gb, :swap_gb, :database_class,
51
56
  :storage_gb, :backup_days, :snapshots_keep, :database_name, :engine_version,
52
57
  :containers, :routes, :default_container, :origin_header, :origin_secret,
53
58
  :secret_prefixes, :writable_secrets, :origin_env, :tunnel, :tunnel_service, :proxy_image,
54
- :task_definition, :migration, :s3_buckets, :hosting, :shared_database, keyword_init: true
59
+ :task_definition, :migration, :s3_buckets, :hosting, :shared_database, :cdn,
60
+ :logs, :log_retention_days, keyword_init: true
55
61
  ) do
56
62
  # @return [String] the CloudFormation stack that holds the database: the shared
57
63
  # instance's when the world names one, else this site's own
58
64
  def rds_stack = shared_database || "#{stack_prefix}-#{infra_name}-rds"
59
65
 
66
+ # @return [String] the CloudFormation stack that holds the CloudFront edge
67
+ def edge_stack = "#{stack_prefix}-#{infra_name}-edge"
68
+
69
+ # @return [String] the CloudWatch log group the box's containers write to
70
+ def log_group = "/#{box_stack}"
71
+
72
+ # @return [Boolean] whether the containers' logs ship to CloudWatch Logs
73
+ def cloudwatch_logs? = logs == "cloudwatch"
74
+
60
75
  # @return [Boolean] whether the database lives on an instance shared with other sites
61
76
  def shared? = !shared_database.nil?
62
77
 
@@ -4,6 +4,8 @@ require_relative "settings/types"
4
4
  require_relative "settings/checks"
5
5
  require_relative "settings/containers"
6
6
  require_relative "settings/origin"
7
+ require_relative "settings/cdn"
8
+ require_relative "settings/logs"
7
9
 
8
10
  module Hecks
9
11
  module Projections
@@ -21,6 +23,8 @@ module Hecks
21
23
  extend Checks
22
24
  extend ContainerReaders
23
25
  extend OriginReaders
26
+ extend CdnReaders
27
+ extend LogReaders
24
28
 
25
29
  module_function
26
30
 
@@ -50,7 +54,7 @@ module Hecks
50
54
  shared_database: read_shared_database(settings),
51
55
  s3_buckets: read_s3_access(settings.fetch(:s3_access, [])),
52
56
  hosting: HostingSettings.read(settings, task_definition)
53
- }
57
+ }.merge(read_logs(settings))
54
58
  end
55
59
 
56
60
  # @param settings [Hash{Symbol => Object}] the world's `AwsBox` settings
@@ -62,7 +66,7 @@ module Hecks
62
66
  origin_env = read_origin_env(settings, secret, task_definition)
63
67
  tunnel, tunnel_service = read_tunnel(settings.fetch(:tunnel, false), containers)
64
68
  { origin_header: header, origin_secret: secret, origin_env: origin_env,
65
- tunnel: tunnel, tunnel_service: tunnel_service }
69
+ tunnel: tunnel, tunnel_service: tunnel_service, cdn: read_cdn(settings[:cdn], header) }
66
70
  end
67
71
 
68
72
  # @return [Hash{Symbol => Object}] the plan's names, sizes, routes and secret scopes
@@ -20,16 +20,19 @@ module Hecks
20
20
  template("box.yaml.tmpl",
21
21
  "STACK" => plan.infra_name, "INSTANCE_TYPE" => plan.instance_type,
22
22
  "VOLUME_GB" => plan.volume_gb.to_s, "SNAPSHOTS_KEEP" => plan.snapshots_keep.to_s,
23
- "AMI_PARAMETER" => ami_parameter(plan.instance_type),
24
23
  "COMPUTE_DOMAIN" => compute_domain(region),
25
24
  "SECRET_RESOURCES" => secret_resources(plan), "WRITABLE_SECRETS" => writable_secrets(plan),
26
25
  "TUNNEL_EGRESS" => tunnel_egress(plan),
27
26
  "SWAP_COMMANDS" => swap_commands(plan), "ECR_REPOSITORIES" => ecr_repositories(plan),
28
- "ECR_OUTPUTS" => ecr_outputs(plan), "S3_POLICY" => s3_policy(plan))
27
+ "ECR_OUTPUTS" => ecr_outputs(plan), "S3_POLICY" => s3_policy(plan),
28
+ "LOG_GROUP" => log_group_yaml(plan), "LOG_POLICY" => log_policy_yaml(plan))
29
29
  end
30
30
 
31
31
  # Graviton families end their generation digit with `g` (`t4g`, `m7gd`, `c6gn`).
32
32
  #
33
+ # Only a first create reads it, from `make stacks`: a live box keeps the image it runs,
34
+ # because a newer one would replace the instance.
35
+ #
33
36
  # @param instance_type [String] an EC2 instance type
34
37
  # @return [String] the SSM parameter of the matching Amazon Linux image
35
38
  def ami_parameter(instance_type)
@@ -1,21 +1,37 @@
1
1
  # @@STACK@@: @@WHAT@@.
2
2
  # make stacks VPC=vpc-... PRIVATE_SUBNETS=subnet-a,subnet-b PUBLIC_SUBNET=subnet-c [REHEARSAL=true]
3
3
  # make deploy @@DEPLOY_HINT@@
4
+ # `make stacks` keeps a live box on the image it runs, so an update never replaces the instance.
5
+ # To move to a newer image on purpose: make stacks ... AMI_ID=ami-...
4
6
  RDS_STACK = @@RDS_STACK@@
5
7
  BOX_STACK = @@BOX_STACK@@
6
8
  @@REHEARSAL_NOTE@@
7
9
  REHEARSAL ?= false
10
+ AMI_ID ?=
8
11
 
9
12
  HECKS ?= hecks
10
13
 
11
14
  @@PHONY@@
12
15
  stacks:
16
+ @@CDN_CHECK@@
13
17
  @@RDS_DEPLOY@@
18
+ @AMI="$(AMI_ID)"; SRC="given as AMI_ID"; \
19
+ if [ -z "$$AMI" ]; then \
20
+ BOX=$$(aws cloudformation describe-stack-resource --stack-name $(BOX_STACK) --logical-resource-id Box --query StackResourceDetail.PhysicalResourceId --output text 2>/dev/null); \
21
+ case "$$BOX" in \
22
+ i-*) AMI=$$(aws ec2 describe-instances --instance-ids "$$BOX" --query "Reservations[0].Instances[0].ImageId" --output text); SRC="pinned: the image $$BOX runs" ;; \
23
+ *) AMI=$$(aws ssm get-parameter --name @@AMI_PARAMETER@@ --query Parameter.Value --output text); SRC="new: no $(BOX_STACK) stack yet, so the latest image" ;; \
24
+ esac; \
25
+ fi; \
26
+ case "$$AMI" in ami-*) ;; *) echo "no ami- image found for $(BOX_STACK): pass one with AMI_ID=ami-..." >&2; exit 1 ;; esac; \
27
+ echo "==> AmiId $$AMI ($$SRC)"; \
14
28
  aws cloudformation deploy --template-file box.yaml --stack-name $(BOX_STACK) --capabilities CAPABILITY_IAM \
15
29
  --parameter-overrides VpcId=$(VPC) SubnetId=$(PUBLIC_SUBNET) \
16
30
  DbSecurityGroupId=$$(aws cloudformation describe-stacks --stack-name $(RDS_STACK) --query "Stacks[0].Outputs[?OutputKey=='DbSecurityGroupId'].OutputValue" --output text) \
17
31
  @@DB_SECRET_PARAM@@
32
+ AmiId=$$AMI \
18
33
  AlertTopicArn=$$(aws cloudformation describe-stacks --stack-name $(RDS_STACK) --query "Stacks[0].Outputs[?OutputKey=='AlertTopicArn'].OutputValue" --output text) \
34
+ @@CDN_PARAM@@
19
35
  Rehearsal=$(REHEARSAL)
20
36
 
21
37
  @@PROVISION@@
@@ -20,9 +20,12 @@ Parameters:
20
20
  Type: String
21
21
  Default: @@INSTANCE_TYPE@@
22
22
  AmiId:
23
- Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
24
- Default: @@AMI_PARAMETER@@
25
- Description: Never re-resolve this on a live box, because a newer image replaces the instance.
23
+ Type: AWS::EC2::Image::Id
24
+ Description: >-
25
+ The image the box runs, always given and never resolved here. Resolving the public image
26
+ key again on a live box finds the newest image, and a changed ImageId replaces the instance
27
+ and its Elastic IP association. make stacks passes the running box's own image, and takes a
28
+ newer one only when AMI_ID is given.
26
29
  RootVolumeGb:
27
30
  Type: Number
28
31
  Default: @@VOLUME_GB@@
@@ -82,7 +85,7 @@ Resources:
82
85
  SourceSecurityGroupId: !Ref BoxSecurityGroup
83
86
  Description: App box
84
87
 
85
- BoxRole:
88
+ @@LOG_GROUP@@ BoxRole:
86
89
  Type: AWS::IAM::Role
87
90
  Properties:
88
91
  AssumeRolePolicyDocument:
@@ -106,6 +109,7 @@ Resources:
106
109
  @@SECRET_RESOURCES@@
107
110
  @@WRITABLE_SECRETS@@
108
111
  @@S3_POLICY@@
112
+ @@LOG_POLICY@@
109
113
 
110
114
  BoxProfile:
111
115
  Type: AWS::IAM::InstanceProfile
@@ -10,6 +10,7 @@
10
10
  # 42 the roll succeeded but the box is not healthy after it
11
11
  # 43 no restore anchor: the database's backups cannot be read, or are kept fewer days than
12
12
  # MIN_BACKUP_RETENTION_DAYS (default 3)
13
+ @@LOG_EXIT_CODE@@
13
14
  #
14
15
  # Before anything changes, the roll names the point the database can be restored to: the instance, the
15
16
  # moment this roll starts, and how long its continuous backups are kept. A roll that mints a new era is
@@ -23,6 +24,7 @@
23
24
  # cannot run, the roll goes on. SKIP_LOG_CAPTURE=1 skips it; LOG_CAPTURE_SERVICES="a b" limits it to
24
25
  # those services (deploy-service.sh sets the one it rolls).
25
26
  #
27
+ @@LOG_NOTE@@
26
28
  @@USAGE@@
27
29
  set -euo pipefail
28
30
  HERE=$(cd "$(dirname "$0")" && pwd)
@@ -37,6 +39,7 @@ DB_HOST=$(out "$RDS_STACK" DbEndpoint)
37
39
  @@DB_SECRET_LINE@@
38
40
  [ -n "$BOX" ] && [ "$BOX" != None ] || { echo "no instance in stack $BOX_STACK" >&2; exit 40; }
39
41
  ACCOUNT=$(aws sts get-caller-identity --query Account --output text)
42
+ @@LOG_GROUP_CHECK@@
40
43
 
41
44
  if [ "${SKIP_RESTORE_ANCHOR:-}" = 1 ]; then
42
45
  echo "==> skipping the restore anchor (SKIP_RESTORE_ANCHOR=1)"