infra: close the direct-S3 fleet write path (#1007)
region-remote-host no longer grants its off-cloud principal anything on the platform deployment bucket. A non-empty deployment_bucket_arn now fails the plan and points the operator at the fleet door. New access_key_status lets a root pin a switched-off key Inactive. The BYO fleet page drops the grandfathered direct-S3 paragraph; the remote-host runbook, module READMEs and comments match. A Go guard pins the module's files by digest and a tofu test checks the plan. Census overlay approved by Astra (#280). Copy approved by Paul. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Squashed from close-direct-s3-fleet-writes into main.
14 files changed,
+436
−91
docs/superpowers/notes/census-proposal-close-direct-s3-fleet-writes.md+25 −0infra/modules/README.md+11 −12infra/modules/region-remote-host/README.md+18 −18infra/modules/region-remote-host/main.tf+8 −35infra/modules/region-remote-host/tests/no_deployment_bucket.tftest.hcl+72 −0infra/modules/region-remote-host/variables.tf+17 −1infra/remote-host.md+6 −3internal/console/accountfleet_test.go+6 −6internal/console/templates/fleet_byo.html+0 −7internal/host/agent.go+6 −6internal/host/bootstrap.go+4 −3internal/provision/remote_host_module_test.go+250 −0internal/provision/testdata/plan14-root-reads-chain.tsv+1 −0internal/provision/testdata/plan14-root-reads-close-direct-s3-fleet-writes.tsv+12 −0
CI passed — run details
diff --git a/docs/superpowers/notes/census-proposal-close-direct-s3-fleet-writes.md b/docs/superpowers/notes/census-proposal-close-direct-s3-fleet-writes.md| new file mode 100644 |
| index 000000000..bfea0e9c7 |
| --- /dev/null |
| +++ b/docs/superpowers/notes/census-proposal-close-direct-s3-fleet-writes.md |
| @@ -0,0 +1,25 @@ |
| +# Census approval: close-direct-s3-fleet-writes |
| + |
| +Ruling `census-ruling-close-direct-s3-fleet-writes`. The independent reviewer approves or rejects every change below, 1 with a new disposition; nothing goes to the census owner (Paul, #280, 2026-10-26). |
| + |
| +| unit | change | read sites | what changed | |
| +|---|---|---|---| |
| +| `infra/modules/region-remote-host/main.tf` `external` | body | unchanged (1) | removes aws_iam_user_policy.host_deployment_bucket (the whole-deployment-bucket grant); access key status from var.access_key_status; no read, IAM narrowed | |
| +| `infra/modules/region-remote-host/variables.tf` `external` | body | unchanged (1) | deployment_bucket_arn now validated to be empty; adds access_key_status (Active|Inactive, default Active); no read | |
| + |
| +Envelopes re-pinned: `infra/modules/region-remote-host`, `infra/modules/region-remote-host/tests`, `internal/host`. |
| + |
| +Review: a model from the other family than the author reads the branch's diff against main on the amadan.net remote (`origin` or `amadan`, whichever points at amadan.net) and gives a verdict for every unit and every re-pinned envelope above. For a unit, compare the change with its recorded disposition and basis below; for an envelope, read every changed function in the package and what it reaches. Reject if the change lets root open, run, query or follow anything it did not before from a path or input a tenant, an app or a box agent can create, write or name; if it weakens a check, refusal, validation, argument or environment the basis relies on; or if the diff cannot settle either. Otherwise approve. Record each verdict on #280 with the tip. A rejected unit goes back to the author to correct; nothing escalates to the census owner. |
| + |
| +Recorded dispositions, which each change must still satisfy: |
| + |
| +- `infra/modules/region-remote-host/main.tf` `external`: legacy, task accepted-legacy. Basis: accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-data-lifecycle-336;basis=adds the same aws_s3_bucket_lifecycle_configuration "data" on the regional data bucket; no read, no IAM change |
| +- `infra/modules/region-remote-host/variables.tf` `external`: legacy, task accepted-legacy. Basis: accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-data-lifecycle-336;basis=adds variable data_noncurrent_days (number, default 7, validated whole and >= 1); no read |
| + |
| +New dispositions, which census-propose wrote and the reviewer checks against the unit's reads and the diff: |
| + |
| +- new `infra/modules/region-remote-host/tests/no_deployment_bucket.tftest.hcl:external`: reads +file:external; written legacy, accepted-legacy, frozen |
| + |
| +Seal: `da718fcd010b3c1db861196e0bbb9121723a9857ad16c2a15a329983688b8981` → `c6655e82e81ccd2a7547655eb393ec3fd10bba396255a1edb30d59b7ad1e682e`. |
| + |
| +Overlay: `internal/provision/testdata/plan14-root-reads-close-direct-s3-fleet-writes.tsv`, appended to `internal/provision/testdata/plan14-root-reads-chain.tsv`. |
diff --git a/infra/modules/README.md b/infra/modules/README.md| index e91c128f4..bf2ca851c 100644 |
| --- a/infra/modules/README.md |
| +++ b/infra/modules/README.md |
| @@ -139,23 +139,22 @@ Outputs: `clamd_addr` (set as the console's `CARLOS_CLAMD_ADDR`), `stream_max_by |
| ## region-remote-host |
| -What an off-cloud CARLOS host (customer fleets and tito's hq) |
| -needs from AWS when there is no EC2 instance profile to lean on: a region |
| -data bucket plus a scoped IAM user + access key, limited to this region's |
| -data bucket always, plus the deployment bucket only when |
| -`deployment_bucket_arn` is set — the same wrong-region-fails-at-IAM |
| -residency property `region-host` gives cloud hosts. Fleet-API consumers |
| -(issue #73) omit `deployment_bucket_arn` entirely: the box reaches the |
| -platform's deployment bucket over the console's edge with its tunnel |
| -token, not a direct IAM grant. Consumed by customer fleets (paulca/pi-fleet |
| -is the live example) and the platform's tito hq; the module is reusable |
| -as-is for both. |
| +What an off-cloud CARLOS host needs from AWS when there is no EC2 |
| +instance profile to lean on: a region data bucket, plus an IAM user and |
| +access key limited to that bucket (the same wrong-region-fails-at-IAM |
| +property `region-host` gives cloud hosts). The user gets nothing on the |
| +platform's deployment bucket: the box reaches it through the console's edge |
| +with its tunnel token. Setting `deployment_bucket_arn` fails the plan |
| +(#1007); see the module's README. |
| | Variable | Description | |
| |---|---| |
| | `region_code` | Short region code this host serves (e.g. `pi`, `hq`). | |
| | `data_bucket_name` | Name of this region's data bucket (created here). | |
| -| `deployment_bucket_arn` | ARN of the existing deployment bucket the host reads releases/status from and writes heartbeats to. | |
| +| `sibling_staging_arns` | Sibling regions' data buckets this host may stage moves into (`moves/*` only). Default none. | |
| +| `data_noncurrent_days` | Days a noncurrent version in the data bucket is kept. Default 7. | |
| +| `access_key_status` | `Active` (default) or `Inactive`. Pin `Inactive` after switching a key off, so an apply cannot turn it back on. | |
| +| `deployment_bucket_arn` | Removed. Must stay unset. | |
| Outputs: `bucket_name`, `access_key_id`, `secret_access_key` (sensitive). |
diff --git a/infra/modules/region-remote-host/README.md b/infra/modules/region-remote-host/README.md| index aceb8ff16..aa7a0d31e 100644 |
| --- a/infra/modules/region-remote-host/README.md |
| +++ b/infra/modules/region-remote-host/README.md |
| @@ -6,26 +6,26 @@ inline policy and access key, limited to this region's data bucket and |
| nothing else (wrong-region-fails-at-IAM, the same residency property |
| `region-host` gives cloud hosts). |
| -Consumed by customer fleets (paulca/pi-fleet is the live example) and |
| -the platform's tito hq box. The module is reusable as-is for both — they |
| -differ only in account and variable values, not shape. |
| +Consumed by customer fleets and self-hosted operators' fleets from their |
| +own tofu roots, by git URL. |
| -**`deployment_bucket_arn` is optional (default `""`).** The fleet API |
| -surface (issue #73) means a fleet box no longer needs any IAM grant on the |
| -platform's deployment bucket at all: it reads releases and status, and |
| -writes heartbeats, through the console's edge (`/store/o/...`) using the |
| -same tunnel token it already dials with — the door authenticates and |
| -scopes the request, not a bucket-level AWS credential. That is the shape |
| -every new fleet-API consumer's tofu root should apply with: leave |
| -`deployment_bucket_arn` unset, and the module emits no deployment-bucket |
| -IAM statement at all. |
| +**The principal gets nothing on the platform's deployment bucket.** A fleet |
| +box reads releases and status, and writes heartbeats, through the console's |
| +edge (`/store/o/...`) with the tunnel token it already dials in with. The |
| +door checks the token and limits the box to its own fleet and apps. |
| -The variable still exists for **grandfathered direct-S3 boxes** — ones |
| -still running the pre-#73 agent, or deliberately kept off the fleet API — |
| -which need RW straight on the deployment bucket exactly as before; set |
| -`deployment_bucket_arn` explicitly for those and the module emits the |
| -extra scoped statement (`aws_iam_user_policy.host_deployment_bucket`) on |
| -top of the always-present own-data-bucket grant. |
| +The module used to grant read and write on the whole deployment bucket when |
| +`deployment_bucket_arn` was set, for boxes on the pre-fleet-API agent. That |
| +let a box rewrite any instance record in the bucket (#1007), so the grant is |
| +gone. The variable stays only to fail a plan that still sets it. If your |
| +root sets it: remove it, and move the box to the fleet door. Register the |
| +box with `carlos fleets add-box`, set `CARLOS_FLEET` and the |
| +`CARLOS_TUNNEL_` variables, and unset `CARLOS_DEPLOYMENT_BUCKET`. The |
| +console's BYO fleet page has the full list. |
| + |
| +**`access_key_status`** (`Active` by default) switches the host's access |
| +key off without destroying it. Once you deactivate a key by hand, set this |
| +to `Inactive` in your root, or the next apply turns the key back on. |
| The S3 action list in the inline policy is not independently chosen: it is |
| copied verbatim from `infra/modules/region-host/main.tf`'s |
diff --git a/infra/modules/region-remote-host/main.tf b/infra/modules/region-remote-host/main.tf| index 322fbee8d..95cca5593 100644 |
| --- a/infra/modules/region-remote-host/main.tf |
| +++ b/infra/modules/region-remote-host/main.tf |
| @@ -4,10 +4,12 @@ |
| # |
| # Residency is enforced the same way as region-host: the principal reaches |
| # ITS OWN region's data bucket and nothing else. A wrong-region host fails |
| -# loudly at the IAM layer, same property as every cloud host. The deployment |
| -# bucket is grandfathered-optional (var.deployment_bucket_arn, default "") |
| -# — a fleet-API box needs no platform-bucket grant at all; see the |
| -# aws_iam_user_policy.host_deployment_bucket comment below. |
| +# loudly at the IAM layer, same property as every cloud host. The principal |
| +# gets nothing on the platform's deployment bucket: a fleet box reaches it |
| +# through the fleet door with its tunnel token (#73). The direct-S3 grant |
| +# this module used to offer let a box write any deployment record (#1007); |
| +# var.deployment_bucket_arn now only exists to refuse a root that still |
| +# sets it. |
| # |
| # The data bucket follows the deployment-buckets convention for this class |
| # of bucket (infra/modules/README.md, infra/modules/deployment-buckets/ |
| @@ -86,7 +88,8 @@ resource "aws_iam_user" "host" { |
| } |
| resource "aws_iam_access_key" "host" { |
| - user = aws_iam_user.host.name |
| + user = aws_iam_user.host.name |
| + status = var.access_key_status |
| } |
| # Scope: THIS region's data bucket, always — wrong-region-fails-at-IAM, same |
| @@ -156,33 +159,3 @@ resource "aws_iam_user_policy" "host" { |
| ) |
| }) |
| } |
| - |
| -# Deployment-bucket grant: GRANDFATHERED, direct-S3 boxes only. A fleet-API |
| -# box (issue #73) never needs this — it reaches the platform's deployment |
| -# bucket through the console's edge, over the same tunnel token it dials |
| -# with, so var.deployment_bucket_arn is left "" and this whole statement |
| -# (and the IAM policy resource carrying it) is omitted. count, not a dynamic |
| -# block, matches the same optional-statement pattern used by |
| -# aws_iam_role_policy.host_route53 in the region-host module. |
| -resource "aws_iam_user_policy" "host_deployment_bucket" { |
| - count = var.deployment_bucket_arn != "" ? 1 : 0 |
| - name = "carlos-remote-host-${var.region_code}-deployment-bucket" |
| - user = aws_iam_user.host.name |
| - policy = jsonencode({ |
| - Version = "2012-10-17" |
| - Statement = [ |
| - { |
| - Sid = "DeploymentBucketObjects" |
| - Effect = "Allow" |
| - Action = ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"] |
| - Resource = ["${var.deployment_bucket_arn}/*"] |
| - }, |
| - { |
| - Sid = "DeploymentBucketList" |
| - Effect = "Allow" |
| - Action = ["s3:ListBucket", "s3:GetBucketLocation"] |
| - Resource = [var.deployment_bucket_arn] |
| - }, |
| - ] |
| - }) |
| -} |
diff --git a/infra/modules/region-remote-host/tests/no_deployment_bucket.tftest.hcl b/infra/modules/region-remote-host/tests/no_deployment_bucket.tftest.hcl| new file mode 100644 |
| index 000000000..404221b0c |
| --- /dev/null |
| +++ b/infra/modules/region-remote-host/tests/no_deployment_bucket.tftest.hcl |
| @@ -0,0 +1,72 @@ |
| +# Platform #1007: the module never grants the host anything on the platform's |
| +# deployment bucket, and a root that still sets deployment_bucket_arn is |
| +# refused rather than silently ignored. The key status is pinnable so an |
| +# apply cannot turn a switched-off key back on. |
| + |
| +mock_provider "aws" { |
| + mock_resource "aws_s3_bucket" { |
| + defaults = { |
| + arn = "arn:aws:s3:::data" |
| + } |
| + } |
| +} |
| + |
| +variables { |
| + region_code = "uk" |
| + data_bucket_name = "data" |
| +} |
| + |
| +run "policy_reaches_only_its_own_bucket" { |
| + command = plan |
| + |
| + assert { |
| + condition = alltrue(flatten([ |
| + for s in jsondecode(aws_iam_user_policy.host.policy).Statement : [ |
| + for r in flatten([s.Resource]) : r == "arn:aws:s3:::data" || r == "arn:aws:s3:::data/*" |
| + ] |
| + ])) |
| + error_message = "with no siblings, every statement must name only the host's own data bucket" |
| + } |
| +} |
| + |
| +run "deployment_bucket_arn_is_refused" { |
| + command = plan |
| + |
| + variables { |
| + deployment_bucket_arn = "arn:aws:s3:::carlos-platform" |
| + } |
| + |
| + expect_failures = [var.deployment_bucket_arn] |
| +} |
| + |
| +run "access_key_defaults_active" { |
| + command = plan |
| + |
| + assert { |
| + condition = aws_iam_access_key.host.status == "Active" |
| + error_message = "the access key must default to Active" |
| + } |
| +} |
| + |
| +run "access_key_can_be_pinned_inactive" { |
| + command = plan |
| + |
| + variables { |
| + access_key_status = "Inactive" |
| + } |
| + |
| + assert { |
| + condition = aws_iam_access_key.host.status == "Inactive" |
| + error_message = "access_key_status must set the key's status" |
| + } |
| +} |
| + |
| +run "access_key_status_refuses_other_values" { |
| + command = plan |
| + |
| + variables { |
| + access_key_status = "active" |
| + } |
| + |
| + expect_failures = [var.access_key_status] |
| +} |
diff --git a/infra/modules/region-remote-host/variables.tf b/infra/modules/region-remote-host/variables.tf| index 0dfa71070..3acc02829 100644 |
| --- a/infra/modules/region-remote-host/variables.tf |
| +++ b/infra/modules/region-remote-host/variables.tf |
| @@ -9,9 +9,25 @@ variable "data_bucket_name" { |
| } |
| variable "deployment_bucket_arn" { |
| - description = "ARN of the existing deployment bucket the host reads releases/status from and writes heartbeats to. Optional (default \"\"): a fleet-API box (fleet API surface, issue #73) reaches the platform's deployment bucket over the console's edge with its tunnel token, not a direct IAM grant — leave this unset and no deployment-bucket statement is emitted. Only a grandfathered direct-S3 box needs it set." |
| + description = "Removed (#1007). Must stay unset. The module no longer grants any access to the platform's deployment bucket: a fleet box reads releases and writes heartbeats through the fleet door with its tunnel token." |
| type = string |
| default = "" |
| + |
| + validation { |
| + condition = var.deployment_bucket_arn == "" |
| + error_message = "deployment_bucket_arn is no longer supported (#1007): it gave the box write access to every record in the deployment bucket. Remove it from your root and move the box to the fleet door: register it with carlos fleets add-box, set CARLOS_FLEET and the CARLOS_TUNNEL_ variables, and unset CARLOS_DEPLOYMENT_BUCKET." |
| + } |
| +} |
| + |
| +variable "access_key_status" { |
| + description = "Status of the host's access key: Active (the default) or Inactive. Set Inactive to switch a key off without destroying it, so a later apply cannot quietly turn it back on." |
| + type = string |
| + default = "Active" |
| + |
| + validation { |
| + condition = contains(["Active", "Inactive"], var.access_key_status) |
| + error_message = "access_key_status must be Active or Inactive." |
| + } |
| } |
| variable "sibling_staging_arns" { |
diff --git a/infra/remote-host.md b/infra/remote-host.md| index dfe9fed54..fb4723fa4 100644 |
| --- a/infra/remote-host.md |
| +++ b/infra/remote-host.md |
| @@ -10,9 +10,12 @@ GOOS-GOARCH — linux-arm64 or linux-amd64; a 32-bit OS resolves nothing). |
| ## Steps |
| 1. Provision the IAM principal + region data bucket |
| - (`infra/modules/region-remote-host`). Scope: deployment bucket + this |
| - host's own region bucket, and NOTHING else — wrong-region-fails-at-IAM |
| - holds off-cloud too. A scoped access key is acceptable to start; IAM |
| + (`infra/modules/region-remote-host`). Scope: this host's own region |
| + bucket and nothing else, so wrong-region-fails-at-IAM holds off-cloud |
| + too. The key gets nothing on the platform's deployment bucket: the box |
| + reads releases and writes heartbeats through the fleet door with its |
| + tunnel token (#1007). Register the fleet and box first, as the console's |
| + BYO fleet page (`/a/{sqid}/fleet/byo`) describes. A scoped access key is acceptable to start; IAM |
| Roles Anywhere is the long-term, hq-grade alternative (no long-lived |
| key on the box at all). |
diff --git a/internal/console/accountfleet_test.go b/internal/console/accountfleet_test.go| index 975295770..825d26a52 100644 |
| --- a/internal/console/accountfleet_test.go |
| +++ b/internal/console/accountfleet_test.go |
| @@ -406,12 +406,12 @@ func TestFleetBYOPageMemberReadable(t *testing.T) { |
| t.Fatalf("BYO page missing %q in:\n%s", want, body) |
| } |
| } |
| - // The primary env contract is bucketless: no platform-bucket var or |
| - // credential belongs there (the box reaches releases/heartbeats through |
| - // the console's edge with its tunnel token). CARLOS_DEPLOYMENT_BUCKET |
| - // still appears, but only inside the demoted legacy-direct-S3 note. |
| - if !strings.Contains(body, "Legacy direct-S3") { |
| - t.Fatalf("BYO page missing the legacy direct-S3 note in:\n%s", body) |
| + // The env contract is bucketless: no platform-bucket var or credential |
| + // belongs there (the box reaches releases/heartbeats through the |
| + // console's edge with its tunnel token). The direct-S3 path is closed |
| + // (#1007), so the page must not offer it any more. |
| + if strings.Contains(body, "Legacy direct-S3") || strings.Contains(body, "grandfathered") { |
| + t.Fatalf("BYO page still offers the closed direct-S3 path in:\n%s", body) |
| } |
| } |
diff --git a/internal/console/templates/fleet_byo.html b/internal/console/templates/fleet_byo.html| index 52a05cb23..4746ebfa3 100644 |
| --- a/internal/console/templates/fleet_byo.html |
| +++ b/internal/console/templates/fleet_byo.html |
| @@ -51,13 +51,6 @@ in with — not a bucket-level AWS grant.</p> |
| defaults to this on its own; set it explicitly only if you're pointing status/heartbeats |
| somewhere non-standard.</p> |
| -<h3>Legacy direct-S3 (grandfathered)</h3> |
| -<p class="small">A box still running a pre-fleet-API agent build needs |
| -<code>CARLOS_DEPLOYMENT_BUCKET</code> set to the platform's own deployment bucket, plus an AWS |
| -credential granting it RW there, in addition to the hibernate-bucket vars above. New boxes should |
| -not be configured this way — it is kept working for boxes that haven't upgraded yet, not as a |
| -choice.</p> |
| - |
| <h2>Hardening</h2> |
| <ul> |
| <li>Run the agent as a dedicated non-root user, not root.</li> |
diff --git a/internal/host/agent.go b/internal/host/agent.go| index 4f0b25aaa..183c52aa9 100644 |
| --- a/internal/host/agent.go |
| +++ b/internal/host/agent.go |
| @@ -5692,12 +5692,12 @@ func OpenDeployStore(ctx context.Context) (release.Store, error) { return openDe |
| // selection rule can be tested without mutating process-wide env. |
| // |
| // Order is the whole contract (spec F5): a bucket or dir still set WINS, |
| -// before fleet identity is even consulted, so every platform box and every |
| -// grandfathered fleet box that still has bucket credentials keeps exactly |
| -// today's store, byte for byte. Only a box that resolves a fleet identity |
| -// AND has no bucket/dir at all reaches the door — that is the cutover |
| -// switch, per box, no flag day: drop the bucket var and the box is on the |
| -// API; put it back and it is off. |
| +// before fleet identity is even consulted, so every platform box keeps |
| +// exactly today's store, byte for byte. Only a box that resolves a fleet |
| +// identity AND has no bucket/dir at all reaches the door. Fleet boxes must |
| +// not set the bucket var: region-remote-host no longer grants a fleet box |
| +// any access to the deployment bucket (#1007), so the bucket branch is for |
| +// platform boxes only. |
| func openDeployStoreEnv(ctx context.Context, getenv func(string) string) (release.Store, error) { |
| if b := getenv("CARLOS_DEPLOYMENT_BUCKET"); b != "" { |
| return release.NewDeploymentS3Store(ctx, b) |
diff --git a/internal/host/bootstrap.go b/internal/host/bootstrap.go| index 7920d6a25..e5228f4ce 100644 |
| --- a/internal/host/bootstrap.go |
| +++ b/internal/host/bootstrap.go |
| @@ -395,9 +395,10 @@ func (f *repeatableFlag) Set(v string) error { |
| // bootstrapOffcloud is bootstrap for a box with no EC2 instance profile |
| // (issue #53 point 4): identical structural work, plus the credential |
| -// preconditions — a staged 0600 credentials file scoped (at IAM) to the |
| -// deployment bucket + this host's own region bucket and nothing else, so |
| -// wrong-region-fails-at-IAM holds off-cloud too. The flag is explicit; no |
| +// preconditions — a staged 0600 credentials file scoped (at IAM) to this |
| +// host's own region bucket and nothing else, so wrong-region-fails-at-IAM |
| +// holds off-cloud too. Releases and heartbeats go through the fleet door, |
| +// not a deployment-bucket grant (#1007). The flag is explicit; no |
| // IMDS sniffing (the agent's one IMDS probe is already gated for non-EC2). |
| // |
| // Credential shape and mode checks precede bootstrap mutations. A rejected |
diff --git a/internal/provision/remote_host_module_test.go b/internal/provision/remote_host_module_test.go| new file mode 100644 |
| index 000000000..d7f89415e |
| --- /dev/null |
| +++ b/internal/provision/remote_host_module_test.go |
| @@ -0,0 +1,250 @@ |
| +package provision |
| + |
| +import ( |
| + "crypto/sha256" |
| + "encoding/hex" |
| + "os" |
| + "path/filepath" |
| + "regexp" |
| + "slices" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +// #1007: region-remote-host once granted its off-cloud principal read and |
| +// write on the whole platform deployment bucket, which let a fleet box |
| +// rewrite any instance record. These guards keep the module unable to grant |
| +// that again. CI has no OpenTofu and a text scan of HCL can always be |
| +// dodged (Astra #1007 R2), so the module's .tf files are pinned by digest: |
| +// any change fails until someone has reviewed it and updated the pin. The |
| +// readable checks below say what that review must confirm. The module's own |
| +// tofu tests (tests/no_deployment_bucket.tftest.hcl) check the plan. |
| + |
| +const remoteHostModule = "infra/modules/region-remote-host" |
| + |
| +// SHA-256 of main.tf, outputs.tf and variables.tf (see remoteHostDigest). |
| +// Before changing it, confirm the module still grants nothing outside its |
| +// own data bucket and a sibling's moves/ prefix, and still refuses a |
| +// non-empty deployment_bucket_arn (#1007). |
| +const remoteHostModuleDigest = "d0bcbc927254cd8a29e7d5f4bbbc0b9748243a53f6bcb444e0cde26ed98c2af2" |
| + |
| +// Every block the module may declare. A new resource, data source or child |
| +// module is a possible new grant, so it fails here until reviewed. |
| +var remoteHostBlocks = []string{ |
| + `resource "aws_iam_access_key" "host"`, |
| + `resource "aws_iam_user" "host"`, |
| + `resource "aws_iam_user_policy" "host"`, |
| + `resource "aws_s3_bucket" "data"`, |
| + `resource "aws_s3_bucket_lifecycle_configuration" "data"`, |
| + `resource "aws_s3_bucket_public_access_block" "data"`, |
| + `resource "aws_s3_bucket_server_side_encryption_configuration" "data"`, |
| + `resource "aws_s3_bucket_versioning" "data"`, |
| +} |
| + |
| +// The only Resource values the host policy may use: its own data bucket, |
| +// or a sibling's bucket (the arn loop variable) for moves/ staging. |
| +var remoteHostResources = []string{ |
| + `["${aws_s3_bucket.data.arn}/*"]`, |
| + `[aws_s3_bucket.data.arn]`, |
| + `"${arn}/moves/*"`, |
| + `arn`, |
| +} |
| + |
| +func TestRegionRemoteHostGrantsNoDeploymentBucket(t *testing.T) { |
| + dir := filepath.Join("../..", remoteHostModule) |
| + entries, err := os.ReadDir(dir) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + // Every top-level entry counts, whatever its extension: OpenTofu also |
| + // loads .tofu, .tofu.json and *_override files. Dot-files are skipped |
| + // because OpenTofu ignores them too. Only tests/ may be a directory, |
| + // and nothing may be a symlink. |
| + files := map[string]string{} |
| + for _, e := range entries { |
| + name := e.Name() |
| + switch { |
| + case strings.HasPrefix(name, "."): |
| + continue |
| + case e.Type()&os.ModeSymlink != 0: |
| + t.Errorf("region-remote-host/%s is a symlink; the module must hold only regular files (#1007)", name) |
| + continue |
| + case e.IsDir(): |
| + if name != "tests" { |
| + t.Errorf("region-remote-host/%s is an unexpected directory; review it, then update this guard (#1007)", name) |
| + } |
| + continue |
| + } |
| + b, err := os.ReadFile(filepath.Join(dir, name)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + files[name] = string(b) |
| + } |
| + for _, problem := range remoteHostModuleProblems(files) { |
| + t.Error(problem) |
| + } |
| +} |
| + |
| +// Each mutation is a way the grant could come back; the guard must catch |
| +// every one. |
| +func TestRegionRemoteHostGuardCatchesRegrants(t *testing.T) { |
| + base := map[string]string{} |
| + for _, name := range []string{"main.tf", "variables.tf", "outputs.tf", "README.md"} { |
| + base[name] = string(groupGuardFile(t, filepath.Join(remoteHostModule, name))) |
| + } |
| + if p := remoteHostModuleProblems(base); len(p) != 0 { |
| + t.Fatalf("baseline must pass: %v", p) |
| + } |
| + ownObjects := `Resource = ["${aws_s3_bucket.data.arn}/*"]` |
| + condition := `condition = var.deployment_bucket_arn == ""` |
| + for _, m := range []struct { |
| + name string |
| + file string |
| + mutate func(string) string |
| + }{ |
| + {"old grant resource", "main.tf", func(s string) string { |
| + return s + "\nresource \"aws_iam_user_policy\" \"host_deployment_bucket\" {\n user = aws_iam_user.host.name\n}\n" |
| + }}, |
| + {"indented attachment", "main.tf", func(s string) string { |
| + return s + "\n resource \"aws_iam_user_policy_attachment\" \"s3\" {\n user = aws_iam_user.host.name\n}\n" |
| + }}, |
| + {"child module", "main.tf", func(s string) string { |
| + return s + "\nmodule \"extra\" {\n source = \"./extra\"\n}\n" |
| + }}, |
| + {"NotResource", "main.tf", func(s string) string { |
| + return strings.Replace(s, ownObjects, `NotResource = ["${aws_s3_bucket.data.arn}/*"]`, 1) |
| + }}, |
| + {"NotAction", "main.tf", func(s string) string { |
| + return strings.Replace(s, `Action = ["s3:GetObjectVersion"]`, `NotAction = ["s3:GetObjectVersion"]`, 1) |
| + }}, |
| + {"inline wildcard statement", "main.tf", func(s string) string { |
| + return strings.Replace(s, "Statement = concat([\n", "Statement = concat([\n { Effect = \"Allow\", Action = [\"s3:*\"], Resource = \"*\" },\n", 1) |
| + }}, |
| + {"deployment bucket resource", "main.tf", func(s string) string { |
| + return strings.Replace(s, ownObjects, `Resource = ["${var.deployment_bucket_arn}/*"]`, 1) |
| + }}, |
| + {"variable used", "main.tf", func(s string) string { |
| + return s + "\nlocals {\n x = var.deployment_bucket_arn\n}\n" |
| + }}, |
| + {"condition weakened", "variables.tf", func(s string) string { |
| + return strings.Replace(s, condition, condition+` || true`, 1) |
| + }}, |
| + {"condition commented out", "variables.tf", func(s string) string { |
| + return strings.Replace(s, condition, "# "+condition+"\n condition = true", 1) |
| + }}, |
| + {"quoted Resource key", "main.tf", func(s string) string { |
| + return strings.Replace(s, ownObjects, `"Resource" = ["*"]`, 1) |
| + }}, |
| + {"multiline Resource list", "main.tf", func(s string) string { |
| + return strings.Replace(s, ownObjects, "Resource = [\n \"*\",\n ]", 1) |
| + }}, |
| + {"block-commented condition", "variables.tf", func(s string) string { |
| + return strings.Replace(s, condition, "/*\n "+condition+"\n */ condition = var.deployment_bucket_arn == \"\" || true", 1) |
| + }}, |
| + {"new tf file", "extra.tf", func(string) string { return "# nothing yet\n" }}, |
| + {"tf.json file", "extra.tf.json", func(string) string { return "{}" }}, |
| + {"tofu file", "extra.tofu", func(string) string { return "# nothing yet\n" }}, |
| + {"tofu.json file", "extra.tofu.json", func(string) string { return "{}" }}, |
| + {"override file", "main_override.tofu", func(string) string { return "# nothing yet\n" }}, |
| + {"resource in variables.tf", "variables.tf", func(s string) string { |
| + return s + "\nresource \"aws_iam_user_policy_attachment\" \"s3\" {\n user = \"x\"\n}\n" |
| + }}, |
| + } { |
| + files := map[string]string{} |
| + for k, v := range base { |
| + files[k] = v |
| + } |
| + mutated := m.mutate(files[m.file]) |
| + if mutated == files[m.file] { |
| + t.Fatalf("%s: mutation did not apply; update the test", m.name) |
| + } |
| + files[m.file] = mutated |
| + if len(remoteHostModuleProblems(files)) == 0 { |
| + t.Errorf("%s: guard did not notice", m.name) |
| + } |
| + } |
| +} |
| + |
| +func remoteHostModuleProblems(files map[string]string) []string { |
| + var problems []string |
| + var names []string |
| + for name := range files { |
| + if name != "README.md" { |
| + names = append(names, name) |
| + } |
| + } |
| + slices.Sort(names) |
| + if !slices.Equal(names, []string{"main.tf", "outputs.tf", "variables.tf"}) { |
| + problems = append(problems, "region-remote-host files changed to "+strings.Join(names, ", ")+"; review any new grant, then update this guard") |
| + } |
| + if got := remoteHostDigest(files, names); got != remoteHostModuleDigest { |
| + problems = append(problems, "region-remote-host changed (digest "+got+"). Review it against #1007 (no grant outside its own data bucket and a sibling's moves/ prefix; deployment_bucket_arn still refused), then update remoteHostModuleDigest") |
| + } |
| + |
| + var code strings.Builder |
| + for _, name := range names { |
| + code.WriteString(stripHCLComments(files[name])) |
| + } |
| + all := code.String() |
| + |
| + blocks := regexp.MustCompile(`(?m)^\s*(resource|data|module)\s+"[^"]*"(\s+"[^"]*")?`).FindAllString(all, -1) |
| + for i := range blocks { |
| + blocks[i] = strings.Join(strings.Fields(blocks[i]), " ") |
| + } |
| + slices.Sort(blocks) |
| + if !slices.Equal(blocks, remoteHostBlocks) { |
| + problems = append(problems, "region-remote-host declares "+strings.Join(blocks, "; ")+"; want exactly "+strings.Join(remoteHostBlocks, "; ")+" (#1007)") |
| + } |
| + |
| + if regexp.MustCompile(`\bNot(Resource|Action|Principal)\b`).MatchString(all) { |
| + problems = append(problems, "region-remote-host uses NotResource/NotAction/NotPrincipal, which grants by exclusion (#1007)") |
| + } |
| + resources := regexp.MustCompile(`\bResource\s*=\s*("[^"\n]*"|\[[^\]\n]*\]|[A-Za-z_.]+)`).FindAllStringSubmatch(all, -1) |
| + if len(resources) == 0 { |
| + problems = append(problems, "no Resource values found; the policy shape changed, update this guard") |
| + } |
| + for _, r := range resources { |
| + if !slices.Contains(remoteHostResources, r[1]) { |
| + problems = append(problems, "region-remote-host grants on "+r[1]+"; it may name only its own data bucket or a sibling's moves/ prefix (#1007)") |
| + } |
| + } |
| + // The variable must refuse every non-empty value: exactly one |
| + // condition, and it is the plain equality. Outside its own block, |
| + // nothing may read it. |
| + start := strings.Index(all, `variable "deployment_bucket_arn" {`) |
| + if start < 0 { |
| + return append(problems, "variables.tf must keep deployment_bucket_arn so a root that sets it fails the plan (#1007)") |
| + } |
| + block := all[start:] |
| + if end := regexp.MustCompile(`(?m)^}`).FindStringIndex(block); end != nil { |
| + block = block[:end[1]] |
| + } |
| + if strings.Contains(strings.Replace(all, block, "", 1), "deployment_bucket_arn") { |
| + problems = append(problems, "region-remote-host code must not use deployment_bucket_arn (#1007)") |
| + } |
| + conditions := regexp.MustCompile(`(?m)^\s*condition\s*=\s*(.*?)\s*$`).FindAllStringSubmatch(block, -1) |
| + if len(conditions) != 1 || conditions[0][1] != `var.deployment_bucket_arn == ""` { |
| + problems = append(problems, `deployment_bucket_arn must have exactly one validation, condition = var.deployment_bucket_arn == "" (#1007)`) |
| + } |
| + return problems |
| +} |
| + |
| +func remoteHostDigest(files map[string]string, names []string) string { |
| + h := sha256.New() |
| + for _, name := range names { |
| + h.Write([]byte(name + "\x00" + files[name] + "\x00")) |
| + } |
| + return hex.EncodeToString(h.Sum(nil)) |
| +} |
| + |
| +func stripHCLComments(s string) string { |
| + var out strings.Builder |
| + for _, line := range strings.Split(s, "\n") { |
| + if t := strings.TrimSpace(line); strings.HasPrefix(t, "#") || strings.HasPrefix(t, "//") { |
| + continue |
| + } |
| + out.WriteString(line + "\n") |
| + } |
| + return out.String() |
| +} |
diff --git a/internal/provision/testdata/plan14-root-reads-chain.tsv b/internal/provision/testdata/plan14-root-reads-chain.tsv| index bbe115138..9d511301e 100644 |
| --- a/internal/provision/testdata/plan14-root-reads-chain.tsv |
| +++ b/internal/provision/testdata/plan14-root-reads-chain.tsv |
| @@ -223,3 +223,4 @@ plan14-root-reads-platform-mail-submission.tsv edd68225d4f61e5953ecaa710fe07e8c0 |
| plan14-root-reads-drain-quiesce-1062.tsv 66404a2fb26ea541e99d9c3d3bd87cb3f2a346eda95cdb7d7838cab2fcd844f5 |
| plan14-root-reads-flat-plans.tsv 66404a2fb26ea541e99d9c3d3bd87cb3f2a346eda95cdb7d7838cab2fcd844f5 |
| plan14-root-reads-sr-followups.tsv da718fcd010b3c1db861196e0bbb9121723a9857ad16c2a15a329983688b8981 |
| +plan14-root-reads-close-direct-s3-fleet-writes.tsv c6655e82e81ccd2a7547655eb393ec3fd10bba396255a1edb30d59b7ad1e682e |
diff --git a/internal/provision/testdata/plan14-root-reads-close-direct-s3-fleet-writes.tsv b/internal/provision/testdata/plan14-root-reads-close-direct-s3-fleet-writes.tsv| new file mode 100644 |
| index 000000000..cea0c1213 |
| --- /dev/null |
| +++ b/internal/provision/testdata/plan14-root-reads-close-direct-s3-fleet-writes.tsv |
| @@ -0,0 +1,12 @@ |
| +# Census overlay close-direct-s3-fleet-writes, written by census-propose (#369). census-ruling-close-direct-s3-fleet-writes, approved on #280 before the merge; the approval card is docs/superpowers/notes/census-proposal-close-direct-s3-fleet-writes.md. |
| +# 2 body, 0 sites, 3 envelope: each changed unit re-frozen at its existing disposition with its claim appended. |
| +# 1 new dispositions: 0 reads re-frozen at their disposition with the claim, 1 new units frozen accepted-legacy, 0 quiet units retired while they read nothing, 0 other. |
| +@legacy ba3765246aea0b411e10ef6c9d1657cc1b097af016dc74e529ae7a79889ce8d2 infra/modules/region-remote-host/main.tf external legacy accepted-legacy accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-data-lifecycle-336;basis=adds the same aws_s3_bucket_lifecycle_configuration "data" on the regional data bucket; no read, no IAM change;rev3=census-ruling-close-direct-s3-fleet-writes;basis=removes aws_iam_user_policy.host_deployment_bucket (the whole-deployment-bucket grant); access key status from var.access_key_status; no read, IAM narrowed file:external@0 3e70d7771753e6ca68e5d057fdbcf4b41ff463e671dc817b923468119aac8b3c |
| +@legacy c2cd38372429a6ca94cdf9b29e56f02cbdb4de7533e3df7a326d5d1010da539e infra/modules/region-remote-host/variables.tf external legacy accepted-legacy accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-data-lifecycle-336;basis=adds variable data_noncurrent_days (number, default 7, validated whole and >= 1); no read;rev3=census-ruling-close-direct-s3-fleet-writes;basis=deployment_bucket_arn now validated to be empty; adds access_key_status (Active|Inactive, default Active); no read file:external@0 60615245522d28230b590c5b5839d1bbc9c5c4c96b878b2b5d4429e7f94ba984 |
| +@legacy d44126b0f3457ff58d6f3762264acc424fdccea69c0bf8dbad9be40ade0f3e15 infra/modules/region-remote-host/tests/no_deployment_bucket.tftest.hcl external legacy accepted-legacy accepted-legacy;rev3=census-ruling-close-direct-s3-fleet-writes;basis=new tofu test with a mock provider: own-bucket-only policy, deployment_bucket_arn refused, key status; no read file:external@0 59dca4eb8bed18219bb22d4a5a3016fe92523d740b7397b22570c1e4d25b0838 |
| +infra/modules/region-remote-host/main.tf external legacy accepted-legacy accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-data-lifecycle-336;basis=adds the same aws_s3_bucket_lifecycle_configuration "data" on the regional data bucket; no read, no IAM change;rev3=census-ruling-close-direct-s3-fleet-writes;basis=removes aws_iam_user_policy.host_deployment_bucket (the whole-deployment-bucket grant); access key status from var.access_key_status; no read, IAM narrowed file:external@0 3e70d7771753e6ca68e5d057fdbcf4b41ff463e671dc817b923468119aac8b3c |
| +infra/modules/region-remote-host/variables.tf external legacy accepted-legacy accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-data-lifecycle-336;basis=adds variable data_noncurrent_days (number, default 7, validated whole and >= 1); no read;rev3=census-ruling-close-direct-s3-fleet-writes;basis=deployment_bucket_arn now validated to be empty; adds access_key_status (Active|Inactive, default Active); no read file:external@0 60615245522d28230b590c5b5839d1bbc9c5c4c96b878b2b5d4429e7f94ba984 |
| +infra/modules/region-remote-host/tests/no_deployment_bucket.tftest.hcl external legacy accepted-legacy accepted-legacy;rev3=census-ruling-close-direct-s3-fleet-writes;basis=new tofu test with a mock provider: own-bucket-only policy, deployment_bucket_arn refused, key status; no read file:external@0 59dca4eb8bed18219bb22d4a5a3016fe92523d740b7397b22570c1e4d25b0838 |
| +@coverage infra/modules/region-remote-host 4d697952b38b79d2862652499419d5e5efec83d3406c4e21254f95ed4185544d |
| +@coverage infra/modules/region-remote-host/tests 6bb867c17012b3c98e6bc98c068235a25bdc9a09750273c3fe9b88d741ff92b2 |
| +@coverage internal/host 5144ac29579fea13c16f97f9cc4240e6f5ca3c5b8755652d346f9840fae0c453 |