hecks 3.11.0 → 3.12.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 (85) hide show
  1. checksums.yaml +4 -4
  2. data/lib/hecks/adapters/driving/cli_runner/resolution.rb +6 -3
  3. data/lib/hecks/adapters/driving/cli_runner.rb +1 -0
  4. data/lib/hecks/adapters/driving/newcomer.rb +50 -0
  5. data/lib/hecks/bluebook/assembly/contracts/read_models.rb +1 -1
  6. data/lib/hecks/bluebook/assembly/marks.rb +3 -15
  7. data/lib/hecks/bluebook/assembly/scalars.rb +39 -0
  8. data/lib/hecks/bluebook/meta_validator/readings.rb +6 -0
  9. data/lib/hecks/bluebook/meta_validator/reconstruction.rb +6 -3
  10. data/lib/hecks/bluebook.rb +1 -0
  11. data/lib/hecks/deploy/bluebook/deploy.bluebook +16 -2
  12. data/lib/hecks/gate/stages.yml +32 -0
  13. data/lib/hecks/hecks/adapters/rehearsal/postgres_store.rb +5 -1
  14. data/lib/hecks/hecks/hecks.world +3 -0
  15. data/lib/hecks/language/bluebook/bluebook.behaviors +23 -0
  16. data/lib/hecks/language/bluebook/policy.behaviors +19 -0
  17. data/lib/hecks/language/bluebook/vocabulary.bluebook +52 -0
  18. data/lib/hecks/projection_files/expression_tables.rb +7 -2
  19. data/lib/hecks/projections/deploy/box/compose.rb +6 -4
  20. data/lib/hecks/projections/deploy/box/edge.rb +62 -0
  21. data/lib/hecks/projections/deploy/box/logging.rb +99 -0
  22. data/lib/hecks/projections/deploy/box/roll_scripts.rb +16 -8
  23. data/lib/hecks/projections/deploy/box/settings/cdn.rb +40 -0
  24. data/lib/hecks/projections/deploy/box/settings/logs.rb +40 -0
  25. data/lib/hecks/projections/deploy/box/settings/patterns.rb +11 -0
  26. data/lib/hecks/projections/deploy/box/settings/types.rb +16 -1
  27. data/lib/hecks/projections/deploy/box/settings.rb +6 -2
  28. data/lib/hecks/projections/deploy/box/stack_yaml.rb +5 -2
  29. data/lib/hecks/projections/deploy/box/templates/Makefile.tmpl +16 -0
  30. data/lib/hecks/projections/deploy/box/templates/box.yaml.tmpl +8 -4
  31. data/lib/hecks/projections/deploy/box/templates/deploy-box.sh.tmpl +3 -0
  32. data/lib/hecks/projections/deploy/box/templates/edge.mk.tmpl +17 -0
  33. data/lib/hecks/projections/deploy/box/templates/edge.yaml.tmpl +65 -0
  34. data/lib/hecks/projections/deploy/box/templates/log-group.yaml.tmpl +12 -0
  35. data/lib/hecks/projections/deploy/box/templates/log-policy.yaml.tmpl +7 -0
  36. data/lib/hecks/projections/deploy/box/templates/render-compose-taskdef.sh.tmpl +5 -4
  37. data/lib/hecks/projections/deploy/box/templates/render-compose.sh.tmpl +5 -4
  38. data/lib/hecks/projections/deploy/box.rb +15 -3
  39. data/lib/hecks/projections/site/site_root/workflow.rb +5 -0
  40. data/lib/hecks/projector/cli_audience.rb +20 -0
  41. data/lib/hecks/projector/cli_projector/usage.rb +15 -4
  42. data/lib/hecks/rust_build/host.rb +11 -4
  43. data/lib/hecks/rust_build/prebuilt_binary.rb +101 -0
  44. data/lib/hecks/tools/ci_gate_decision.rb +35 -0
  45. data/lib/hecks/tools/ci_gates.rb +20 -1
  46. data/lib/hecks/tools/lanes/files.rb +8 -0
  47. data/lib/hecks/tools/lanes/prebuilt_workflow.rb +81 -0
  48. data/lib/hecks/tools/lanes.rb +1 -0
  49. data/lib/hecks/tools/release_only.rb +111 -0
  50. data/lib/hecks/version.rb +1 -1
  51. data/lib/hecks/vocabulary.rb +15 -1
  52. data/prebuilt/9ee54e2f0d4d6d3fe046827779b221e02de665fba911a048ae5dd8b8fbfb7d90.marshal +0 -0
  53. data/prebuilt/d74a00e996222c1d8b676d5959092effc46b4dc59e0555f3bf9554ac58e3094a.marshal +0 -0
  54. data/prebuilt/{verdicts-230ac18c25abfd3d92ede032bee2661a4c2cd2773ab0cf0e2ae52546ba2f7af9.json → verdicts-df19f35868d3284a70f55e42dcef87ef4e65e6a678e5135f72e1f6cff4c1357b.json} +1 -1
  55. data/rust/host/HECKS_RELEASE +1 -1
  56. data/rust/host/src/extension.rs +13 -1
  57. data/rust/host/src/lib.rs +1 -0
  58. data/rust/host/src/platform_signin/attack_tests.rs +1100 -0
  59. data/rust/host/src/platform_signin/config.rs +512 -0
  60. data/rust/host/src/platform_signin/cookie.rs +237 -0
  61. data/rust/host/src/platform_signin/extension.rs +190 -0
  62. data/rust/host/src/platform_signin/fetcher.rs +322 -0
  63. data/rust/host/src/platform_signin/handler.rs +251 -0
  64. data/rust/host/src/platform_signin/handler_tests.rs +554 -0
  65. data/rust/host/src/platform_signin/jti.rs +196 -0
  66. data/rust/host/src/platform_signin/jwks.rs +542 -0
  67. data/rust/host/src/platform_signin/jws.rs +249 -0
  68. data/rust/host/src/platform_signin/known_answer_tests.rs +237 -0
  69. data/rust/host/src/platform_signin/mapping_tests.rs +254 -0
  70. data/rust/host/src/platform_signin/mod.rs +75 -0
  71. data/rust/host/src/platform_signin/mode_tests.rs +382 -0
  72. data/rust/host/src/platform_signin/pages.rs +204 -0
  73. data/rust/host/src/platform_signin/query.rs +233 -0
  74. data/rust/host/src/platform_signin/session.rs +151 -0
  75. data/rust/host/src/platform_signin/settings.rs +385 -0
  76. data/rust/host/src/platform_signin/strict_json.rs +351 -0
  77. data/rust/host/src/platform_signin/test_support.rs +235 -0
  78. data/rust/host/src/platform_signin/ttl_tests.rs +94 -0
  79. data/rust/host/src/platform_signin/verify.rs +604 -0
  80. data/rust/host/src/platform_signin/wiring_support.rs +319 -0
  81. data/rust/host/src/web.rs +50 -6
  82. data/rust/parser/src/parse/value_object.rs +22 -10
  83. metadata +42 -4
  84. data/prebuilt/05c358d38d661a3774f813d49c66f5b68939af94432d66257a56f49ee2598994.marshal +0 -0
  85. data/prebuilt/26c40c61f09b09b11651c64668eda3ceb180f83cf2a3802d3033d2fd99ac841b.marshal +0 -0
@@ -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)"
@@ -0,0 +1,17 @@
1
+ # GENERATED by hecks deploy project -- re-run it to refresh this file rather than hand-editing.
2
+ # Included by the Makefile beside it; the aliases come from the domain's deployed_to("AwsBox")
3
+ # block (cdn).
4
+
5
+ EDGE_STACK = @@EDGE_STACK@@
6
+
7
+ .PHONY: edge
8
+
9
+ # Creates or updates the CloudFront edge stack in front of the box (run `make stacks` first: the
10
+ # box's AppOriginDomain output is the distribution's origin). The certificate is never created
11
+ # here: issue it in ACM in us-east-1, validate its DNS records, then pass its ARN.
12
+ # make edge CERTIFICATE_ARN=arn:aws:acm:us-east-1:<account>:certificate/<id>
13
+ edge:
14
+ @test -n "$(CERTIFICATE_ARN)" || { echo "usage: make edge CERTIFICATE_ARN=arn:aws:acm:us-east-1:..." >&2; exit 1; }
15
+ aws cloudformation deploy --region @@REGION@@ --template-file edge.yaml --stack-name $(EDGE_STACK) \
16
+ --parameter-overrides CertificateArn=$(CERTIFICATE_ARN) \
17
+ AppOriginDomain=$$(aws cloudformation describe-stacks --region @@REGION@@ --stack-name $(BOX_STACK) --query "Stacks[0].Outputs[?OutputKey=='AppOriginDomain'].OutputValue" --output text)
@@ -0,0 +1,65 @@
1
+ AWSTemplateFormatVersion: "2010-09-09"
2
+ Description: >
3
+ The CloudFront edge of @@STACK@@: one distribution in front of the app box. Its only origin is the
4
+ box's public DNS name over plain http on port 80, and every request to it carries the origin secret
5
+ header the box's proxy checks. Nothing is cached: the origin decides what a response means.
6
+
7
+ Parameters:
8
+ AppOriginDomain:
9
+ Type: String
10
+ AllowedPattern: ".+"
11
+ ConstraintDescription: the box stack's AppOriginDomain output
12
+ Description: DNS name of the app box, the distribution's only origin.
13
+ CertificateArn:
14
+ Type: String
15
+ AllowedPattern: "arn:aws:acm:us-east-1:.+"
16
+ ConstraintDescription: an ACM certificate in us-east-1 that covers every alias
17
+ Description: >
18
+ An issued ACM certificate in us-east-1 that covers every name in DomainNames. It is never created
19
+ here: DNS validation is a step an operator performs.
20
+ DomainNames:
21
+ Type: CommaDelimitedList
22
+ Default: "@@ALIASES@@"
23
+ Description: The host names the distribution answers on.
24
+
25
+ Resources:
26
+ Distribution:
27
+ Type: AWS::CloudFront::Distribution
28
+ DeletionPolicy: Retain
29
+ UpdateReplacePolicy: Retain
30
+ Properties:
31
+ DistributionConfig:
32
+ Enabled: true
33
+ HttpVersion: http2
34
+ IPV6Enabled: true
35
+ Aliases: !Ref DomainNames
36
+ ViewerCertificate:
37
+ AcmCertificateArn: !Ref CertificateArn
38
+ SslSupportMethod: sni-only
39
+ MinimumProtocolVersion: TLSv1.2_2021
40
+ Origins:
41
+ - Id: BoxOrigin
42
+ DomainName: !Ref AppOriginDomain
43
+ OriginCustomHeaders:
44
+ # Resolved by CloudFormation from Secrets Manager when the stack deploys, so the value
45
+ # never appears on a command line or in this file.
46
+ - HeaderName: @@ORIGIN_HEADER@@
47
+ HeaderValue: "{{resolve:secretsmanager:@@ORIGIN_SECRET@@:SecretString}}"
48
+ CustomOriginConfig:
49
+ OriginProtocolPolicy: http-only
50
+ OriginSSLProtocols: [TLSv1.2]
51
+ DefaultCacheBehavior:
52
+ TargetOriginId: BoxOrigin
53
+ ViewerProtocolPolicy: redirect-to-https
54
+ AllowedMethods: [GET, HEAD, OPTIONS, PUT, POST, PATCH, DELETE]
55
+ CachedMethods: [GET, HEAD]
56
+ Compress: true
57
+ CachePolicyId: 4135ea2d-6df8-44a3-9df3-4b5a84be39ad # Managed-CachingDisabled
58
+ OriginRequestPolicyId: 216adef6-5c7f-47e4-b989-5492eafa07d3 # Managed-AllViewer
59
+
60
+ Outputs:
61
+ DistributionId:
62
+ Value: !Ref Distribution
63
+ DistributionDomainName:
64
+ Description: Point each alias's DNS record (CNAME or alias) at this name.
65
+ Value: !GetAtt Distribution.DomainName
@@ -0,0 +1,12 @@
1
+ # Every container's log, one stream per service. The group is made here and not by Docker
2
+ # (awslogs-create-group is "false"), so the role below needs no right to create groups, and the
3
+ # retention is a declaration. A production group outlives the stack: the evidence a container
4
+ # wrote must not go with a deleted stack.
5
+ BoxLogGroup:
6
+ Type: AWS::Logs::LogGroup
7
+ DeletionPolicy: !If [IsProduction, Retain, Delete]
8
+ UpdateReplacePolicy: !If [IsProduction, Retain, Delete]
9
+ Properties:
10
+ LogGroupName: @@LOG_GROUP@@
11
+ RetentionInDays: @@RETENTION_DAYS@@
12
+
@@ -0,0 +1,7 @@
1
+ - PolicyName: write-logs
2
+ PolicyDocument:
3
+ Version: "2012-10-17"
4
+ Statement:
5
+ - Effect: Allow
6
+ Action: [logs:CreateLogStream, logs:PutLogEvents, logs:DescribeLogStreams]
7
+ Resource: !GetAtt BoxLogGroup.Arn
@@ -13,6 +13,7 @@
13
13
  # When the world names origin_env, the named origin secret is compared with those variables in the
14
14
  # task definition and the render refuses on any difference (Caddy checks the CDN's header against
15
15
  # the secret; the containers check it against their own copy). Values are never printed.
16
+ @@COMPOSE_LOG_NOTE@@
16
17
  set -euo pipefail
17
18
  HERE=$(cd "$(dirname "$0")" && pwd)
18
19
  DB_HOST=${1:?rds endpoint}
@@ -36,7 +37,7 @@ if [ -n "$ORIGIN_ENV" ]; then
36
37
  fi
37
38
 
38
39
  jq --argjson def "$DEF" --arg host "$DB_HOST" --arg secret "$DB_SECRET" '
39
- def log: {driver: "json-file", options: {"max-size": "10m", "max-file": "3"}};
40
+ @@LOG_DEF@@
40
41
  def env(c): ((c.environment // []) | map({(.name): .value}) | add // {})
41
42
  | (if has("DB_HOST") then .DB_HOST = $host | .DB_SECRET_ARN = $secret else . end);
42
43
  . as $in
@@ -44,16 +45,16 @@ jq --argjson def "$DEF" --arg host "$DB_HOST" --arg secret "$DB_SECRET" '
44
45
  ($in.services | with_entries(.value |= (. as $s
45
46
  | ($def | map(select(.name == $s.name)) | first) as $c
46
47
  | if $c == null then error("task definition has no container named " + $s.name) else
47
- {image: $c.image, network_mode: "host", restart: "unless-stopped", logging: log,
48
+ {image: $c.image, network_mode: "host", restart: "unless-stopped", logging: @@LOG_SERVICE@@,
48
49
  environment: ({PORT: ($s.port | tostring)} + env($c))}
49
50
  + (if (($c.secrets // []) | length) > 0 then {env_file: [($s.name + ".secrets.env")]} else {} end)
50
51
  end)))
51
52
  + {caddy: ({image: "@@PROXY_IMAGE@@", network_mode: "host", restart: "unless-stopped",
52
- volumes: ["./Caddyfile:/etc/caddy/Caddyfile:ro", "./caddy-extra:/etc/caddy/extra:ro"], logging: log}
53
+ volumes: ["./Caddyfile:/etc/caddy/Caddyfile:ro", "./caddy-extra:/etc/caddy/extra:ro"], logging: @@LOG_CADDY@@}
53
54
  + (if $in.origin then {env_file: ["caddy.secrets.env"]} else {} end))}
54
55
  + (if $in.tunnel then {cloudflared: {image: $in.tunnel.image, network_mode: "host", restart: "unless-stopped",
55
56
  command: ["tunnel", "--no-autoupdate", "--url", $in.tunnel.url, "run"],
56
- env_file: ["cloudflared.secrets.env"], logging: log}} else {} end))}' \
57
+ env_file: ["cloudflared.secrets.env"], logging: @@LOG_TUNNEL@@}} else {} end))}' \
57
58
  "$HERE/services.json" > compose.json
58
59
 
59
60
  jq --argjson def "$DEF" '. as $in
@@ -6,6 +6,7 @@
6
6
  # Tags default to "latest". Every container gets DB_HOST, DB_NAME, DB_SECRET_ARN and PORT, and runs on the
7
7
  # box's own network, so the proxy reaches each one on 127.0.0.1:<port>. Secrets named in services.json
8
8
  # are written to secrets.json for fetch-secrets.sh to resolve on the box.
9
+ @@COMPOSE_LOG_NOTE@@
9
10
  set -euo pipefail
10
11
  HERE=$(cd "$(dirname "$0")" && pwd)
11
12
  DB_HOST=${1:?rds endpoint}
@@ -18,20 +19,20 @@ ECR="$ACCOUNT.dkr.ecr.$REGION.amazonaws.com"
18
19
  TAGS=$(printf '%s\n' "$@" | jq -R 'select(length > 0) | split("=") | {(.[0]): .[1]}' | jq -s 'add // {}')
19
20
 
20
21
  jq --arg ecr "$ECR" --argjson tags "$TAGS" --arg host "$DB_HOST" --arg secret "$DB_SECRET" --arg db "@@DB_NAME@@" '
21
- def log: {driver: "json-file", options: {"max-size": "10m", "max-file": "3"}};
22
+ @@LOG_DEF@@
22
23
  . as $in
23
24
  | {services: (
24
25
  ($in.services | with_entries(.value |= (
25
26
  {image: ($ecr + "/" + .repository + ":" + ($tags[.name] // "latest")),
26
- network_mode: "host", restart: "unless-stopped", logging: log,
27
+ network_mode: "host", restart: "unless-stopped", logging: @@LOG_SERVICE@@,
27
28
  environment: ({PORT: (.port | tostring), DB_HOST: $host, DB_NAME: $db, DB_SECRET_ARN: $secret} + .env)}
28
29
  + (if (.secrets | length) > 0 then {env_file: [(.name + ".secrets.env")]} else {} end))))
29
30
  + {caddy: ({image: "@@PROXY_IMAGE@@", network_mode: "host", restart: "unless-stopped",
30
- volumes: ["./Caddyfile:/etc/caddy/Caddyfile:ro", "./caddy-extra:/etc/caddy/extra:ro"], logging: log}
31
+ volumes: ["./Caddyfile:/etc/caddy/Caddyfile:ro", "./caddy-extra:/etc/caddy/extra:ro"], logging: @@LOG_CADDY@@}
31
32
  + (if $in.origin then {env_file: ["caddy.secrets.env"]} else {} end))}
32
33
  + (if $in.tunnel then {cloudflared: {image: $in.tunnel.image, network_mode: "host", restart: "unless-stopped",
33
34
  command: ["tunnel", "--no-autoupdate", "--url", $in.tunnel.url, "run"],
34
- env_file: ["cloudflared.secrets.env"], logging: log}} else {} end))}' \
35
+ env_file: ["cloudflared.secrets.env"], logging: @@LOG_TUNNEL@@}} else {} end))}' \
35
36
  "$HERE/services.json" > compose.json
36
37
 
37
38
  jq '. as $in