carlos / platform Public

infra: refuse broad store prefixes, fit the console role in IAM quotas

With store_conversion on, store_bucket_prefix must be a dedicated prefix:
literal, lower-case and ending in -store-. No deployment, data or sibling
staging bucket name may start with it. No root can list every bucket a
broad prefix would match, so a broad prefix is refused at plan. With the
switch off every prefix plans as before.

The complete console role went over IAM's 10,240-character inline total.
host_store_conversion, the media-engine console policy and
host_analytics_prune are now customer-managed attached policies, with
the same grants and switches. The aggregate check now counts every
inline policy on the role and stays a plan-time refusal. The complete
role totals 9,912 characters.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 86093e1a9edba378fcb63b87f40e0df5abc855b5 parent d7956b1
13 files changed, +478 −90
  • docs/superpowers/notes/media-conversion-copy-deck.md +4 −1
  • docs/superpowers/plans/2026-11-16-media-conversion-plan-3-engine.md +6 −4
  • infra/modules/media-engine/README.md +4 −0
  • infra/modules/media-engine/iam.tf +8 −2
  • infra/modules/media-engine/tests/media_engine.tftest.hcl +18 −4
  • infra/modules/media-engine/variables.tf +1 −1
  • infra/modules/region-host/README.md +5 −1
  • infra/modules/region-host/main.tf +45 −10
  • infra/modules/region-host/managed_fleets.tf +8 −6
  • infra/modules/region-host/tests/analytics_prune.tftest.hcl +23 −6
  • infra/modules/region-host/tests/managed_fleets/managed_fleets.tftest.hcl +178 −17
  • infra/modules/region-host/tests/store_conversion.tftest.hcl +177 −37
  • infra/modules/region-host/variables.tf +1 −1
diff --git a/docs/superpowers/notes/media-conversion-copy-deck.md b/docs/superpowers/notes/media-conversion-copy-deck.md
index 044627e52..c9c5b55ab 100644
--- a/docs/superpowers/notes/media-conversion-copy-deck.md
+++ b/docs/superpowers/notes/media-conversion-copy-deck.md
@@ -718,6 +718,9 @@ refusal, for whoever deploys it.
| Key | Text |
|---|---|
-| `infra.region_host.readme.store_conversion` | The `Media conversion and browser uploads` section of `infra/modules/region-host/README.md` (pending Luna pass; plan 3 Task 12) |
+| `infra.region_host.readme.store_conversion` | The `Media conversion and browser uploads` section of `infra/modules/region-host/README.md`, including the prefix rule and the customer-managed policy paragraphs (pending Luna pass; plan 3 Task 12, fix 1) |
| `infra.region_host.store_conversion.description` | The `description` of `variable "store_conversion"` in `infra/modules/region-host/variables.tf` (pending Luna pass; plan 3 Task 12) |
| `infra.region_host.store_conversion.refused` | `store_conversion needs store_bucket_prefix: with no prefix there are no object stores on this deployment to convert, and the grant would name every bucket in the account.` (pending Luna pass; plan 3 Task 12) |
+| `infra.region_host.store_conversion.refused_prefix` | `store_conversion needs a dedicated store_bucket_prefix: lower-case letters, digits and hyphens, ending in -store-, such as carlos-store-. A broader prefix such as carlos- can match the platform's own buckets, and this grant can replace the CORS, lifecycle and tag settings of every bucket it matches.` (pending Luna pass; plan 3 Task 12 fix 1) |
+| `infra.region_host.store_conversion.refused_overlap` | `store_conversion refuses a store_bucket_prefix that matches the deployment, data or sibling staging buckets, because this grant can replace the CORS, lifecycle and tag settings of every bucket it matches. Choose a prefix none of those bucket names start with.` (pending Luna pass; plan 3 Task 12 fix 1) |
+| `infra.media_engine.readme.console_policy` | The paragraph on the customer-managed console policy in `infra/modules/media-engine/README.md`, before `Region` (pending Luna pass; plan 3 Task 12 fix 1) |
diff --git a/docs/superpowers/plans/2026-11-16-media-conversion-plan-3-engine.md b/docs/superpowers/plans/2026-11-16-media-conversion-plan-3-engine.md
index ef45edb6d..451d685e9 100644
--- a/docs/superpowers/plans/2026-11-16-media-conversion-plan-3-engine.md
+++ b/docs/superpowers/plans/2026-11-16-media-conversion-plan-3-engine.md
@@ -244,7 +244,7 @@ Depends on plan 1 Task 3 Step 4: the muxer rules and the parser's accepted box s
**Interfaces:**
- Variables (`pdf-host`'s style): `name`, `region`, `store_region`, `store_bucket_prefix`, `deployment_bucket_name`, `console_role_name`, `console_role_arn`, `image_digest` (default `""`), `image_built_at`, `image_ffmpeg`, `vpc_cidr` (default `10.98.0.0/24`), `logs` (default `true`), `log_retention_days` (default 14), `reaper_max_age_minutes` (default 70), `classes` (default: the four rows, each `{preset, class, cpu, memory}`: `video-720p/short` 4096/8192, `video-720p/long` 16384/32768, `audio-aac/short` 1024/2048, `audio-aac/long` 2048/4096; spike).
- Outputs: `carlos_media_engine` (`""` while `image_digest` is `""`; else `jsonencode` of plan 3 Task 9's `Config` with `v = 1`), `repository_url`, `cluster_arn`, `conversion_role_arn`.
-- Resources: an ECR repository (`IMMUTABLE` tags, scan on push); the VPC with DNS support and hostnames, two private `/25` subnets in two zones, one route table with no default route, no internet gateway and no NAT; a tasks security group with no ingress and egress only TCP 443 to the S3 prefix list and to the endpoints' security group; the endpoints' group admitting TCP 443 from the tasks' group only; an S3 gateway endpoint and interface endpoints `ecr.api`, `ecr.dkr` and (when `logs`) `logs`, private DNS on; their policies (below); a Route 53 Resolver DNS Firewall with an allow list (`s3.<region>.amazonaws.com`, `*.s3.<region>.amazonaws.com`, `api.ecr.<region>.amazonaws.com`, `*.dkr.ecr.<region>.amazonaws.com`, `logs.<region>.amazonaws.com`) at priority 100 `ALLOW` with `firewall_domain_redirection_action` `TRUST_REDIRECTION_DOMAIN` (AWS answers these names through its own CNAMEs; Astra round 1 of Task 11) and `*` at 200 `BLOCK` with `NXDOMAIN` and the default redirection handling, associated with the VPC with mutation protection on, and `firewall_fail_open` `DISABLED`; a log group; the task execution role (trust `ecs-tasks.amazonaws.com` with `aws:SourceAccount`; `ecr:GetAuthorizationToken` on `*`, the three pull actions on the repository, `logs:CreateLogStream` and `logs:PutLogEvents` on the log group); the conversion role (trust: `console_role_arn` only; Allow `s3:GetObject` on `arn:aws:s3:::<prefix>*/*`, `s3:PutObject` on `arn:aws:s3:::<prefix>*/derived/*` and on `arn:aws:s3:::<deployment bucket>/media-results/*`; Deny `s3:GetObject` on `arn:aws:s3:::<prefix>*/*` when `s3:ExistingObjectTag/carlos-scan` is `infected`; Deny `s3:PutObject` on `*` when `ForAnyValue:StringEquals s3:RequestObjectTagKeys` is any of `carlos-scan`, `carlos-scan-version`, `carlos-scan-at`, `carlos-scan-sig`; max session 3600 s); the cluster (Fargate only, Container Insights off); four task definitions (one per `classes` entry, family `<name>-<preset>-<class>`, the Global Constraints' container shape, `deadline` with 64 CPU units and 64 MiB, `codec` with the rest, `codec` env `TMPDIR=/scratch/tmp`, `awslogs` on `codec` only when `logs`, `runtime_platform` `LINUX`/`ARM64`, no `task_role_arn`, no `pid_mode`, volume `scratch` with no host path; if plan 1 reports that RunTask refuses `ephemeralStorage` per run, each definition instead sets `ephemeral_storage` to its class's largest need, 48 GiB for video long and the 20 GiB default elsewhere, and `EphemeralGiB` is never sent), created only when `image_digest` is set, each with `skip_destroy = true` so a new digest leaves active the revisions a running console still launches; a role policy on `console_role_name` (`sts:AssumeRole` on the conversion role; `ecs:RunTask` on `arn:aws:ecs:<region>:<acct>:task-definition/<name>-*:*` with `ArnEquals ecs:cluster`; `ecs:DescribeTasks` and `ecs:StopTask` on `arn:aws:ecs:<region>:<acct>:task/<cluster>/*`; `ecs:ListTasks` on `*` with `ArnEquals ecs:cluster`; `ecs:DescribeTaskDefinition` on `*`; `iam:PassRole` on the execution role with `iam:PassedToService` `ecs-tasks.amazonaws.com`; `s3:ListBucket` on `arn:aws:s3:::<deployment bucket>` with no `s3:prefix` condition, because `GetObject` carries no prefix and an absent result must read as not found); the reaper (a Python 3.13 arm64 Lambda from `reaper/reaper.py`, env `CLUSTER` and `MAX_AGE_MINUTES`, timeout 50 s, its own log group, a role with `ecs:ListTasks` under the cluster condition, `ecs:DescribeTasks` and `ecs:StopTask` on the cluster's tasks and logging to its own group only, an EventBridge rule `rate(1 minute)` targeting it); a precondition that `region == store_region`.
+- Resources: an ECR repository (`IMMUTABLE` tags, scan on push); the VPC with DNS support and hostnames, two private `/25` subnets in two zones, one route table with no default route, no internet gateway and no NAT; a tasks security group with no ingress and egress only TCP 443 to the S3 prefix list and to the endpoints' security group; the endpoints' group admitting TCP 443 from the tasks' group only; an S3 gateway endpoint and interface endpoints `ecr.api`, `ecr.dkr` and (when `logs`) `logs`, private DNS on; their policies (below); a Route 53 Resolver DNS Firewall with an allow list (`s3.<region>.amazonaws.com`, `*.s3.<region>.amazonaws.com`, `api.ecr.<region>.amazonaws.com`, `*.dkr.ecr.<region>.amazonaws.com`, `logs.<region>.amazonaws.com`) at priority 100 `ALLOW` with `firewall_domain_redirection_action` `TRUST_REDIRECTION_DOMAIN` (AWS answers these names through its own CNAMEs; Astra round 1 of Task 11) and `*` at 200 `BLOCK` with `NXDOMAIN` and the default redirection handling, associated with the VPC with mutation protection on, and `firewall_fail_open` `DISABLED`; a log group; the task execution role (trust `ecs-tasks.amazonaws.com` with `aws:SourceAccount`; `ecr:GetAuthorizationToken` on `*`, the three pull actions on the repository, `logs:CreateLogStream` and `logs:PutLogEvents` on the log group); the conversion role (trust: `console_role_arn` only; Allow `s3:GetObject` on `arn:aws:s3:::<prefix>*/*`, `s3:PutObject` on `arn:aws:s3:::<prefix>*/derived/*` and on `arn:aws:s3:::<deployment bucket>/media-results/*`; Deny `s3:GetObject` on `arn:aws:s3:::<prefix>*/*` when `s3:ExistingObjectTag/carlos-scan` is `infected`; Deny `s3:PutObject` on `*` when `ForAnyValue:StringEquals s3:RequestObjectTagKeys` is any of `carlos-scan`, `carlos-scan-version`, `carlos-scan-at`, `carlos-scan-sig`; max session 3600 s); the cluster (Fargate only, Container Insights off); four task definitions (one per `classes` entry, family `<name>-<preset>-<class>`, the Global Constraints' container shape, `deadline` with 64 CPU units and 64 MiB, `codec` with the rest, `codec` env `TMPDIR=/scratch/tmp`, `awslogs` on `codec` only when `logs`, `runtime_platform` `LINUX`/`ARM64`, no `task_role_arn`, no `pid_mode`, volume `scratch` with no host path; if plan 1 reports that RunTask refuses `ephemeralStorage` per run, each definition instead sets `ephemeral_storage` to its class's largest need, 48 GiB for video long and the 20 GiB default elsewhere, and `EphemeralGiB` is never sent), created only when `image_digest` is set, each with `skip_destroy = true` so a new digest leaves active the revisions a running console still launches; a customer-managed policy attached to `console_role_name`, so it stays outside the role's 10,240-character inline total (`sts:AssumeRole` on the conversion role; `ecs:RunTask` on `arn:aws:ecs:<region>:<acct>:task-definition/<name>-*:*` with `ArnEquals ecs:cluster`; `ecs:DescribeTasks` and `ecs:StopTask` on `arn:aws:ecs:<region>:<acct>:task/<cluster>/*`; `ecs:ListTasks` on `*` with `ArnEquals ecs:cluster`; `ecs:DescribeTaskDefinition` on `*`; `iam:PassRole` on the execution role with `iam:PassedToService` `ecs-tasks.amazonaws.com`; `s3:ListBucket` on `arn:aws:s3:::<deployment bucket>` with no `s3:prefix` condition, because `GetObject` carries no prefix and an absent result must read as not found); the reaper (a Python 3.13 arm64 Lambda from `reaper/reaper.py`, env `CLUSTER` and `MAX_AGE_MINUTES`, timeout 50 s, its own log group, a role with `ecs:ListTasks` under the cluster condition, `ecs:DescribeTasks` and `ecs:StopTask` on the cluster's tasks and logging to its own group only, an EventBridge rule `rate(1 minute)` targeting it); a precondition that `region == store_region`.
- Endpoint policies: S3 gateway: Allow `s3:GetObject`, `s3:PutObject` on the store prefix and `media-results/*` for principal `*` with `StringEquals aws:PrincipalArn` = the conversion role ARN; Allow `s3:GetObject` on `arn:aws:s3:::prod-<region>-starport-layer-bucket/*` for `*`. ECR API and Docker: Allow `ecr:BatchGetImage`, `ecr:GetDownloadUrlForLayer`, `ecr:BatchCheckLayerAvailability` on the repository and `ecr:GetAuthorizationToken` on `*`, each with `aws:PrincipalArn` = the execution role. Logs: Allow `logs:CreateLogStream`, `logs:PutLogEvents` on the log group with `aws:PrincipalArn` = the execution role.
- `reaper.py`: `handler(event, context)` lists `RUNNING` tasks (paginated), describes them in batches of 100, and stops each whose `createdAt` is older than `MAX_AGE_MINUTES` with reason `carlos media reaper: older than <n> minutes`; it reads no other input.
@@ -265,13 +265,15 @@ Depends on plan 1 Task 3 Step 4: the muxer rules and the parser's accepted box s
### Task 12: `region-host` `store_conversion` **[Astra: production infrastructure]**
**Files:**
-- Modify: `infra/modules/region-host/variables.tf` (beside `store_scanning`, :297-301), `infra/modules/region-host/main.tf` (boundary statements :809-821; a new policy beside `host_store_scan`, :962-980), `infra/modules/region-host/README.md`
+- Modify: `infra/modules/region-host/variables.tf` (beside `store_scanning`, :297-301), `infra/modules/region-host/main.tf` (boundary statements :809-821; a new policy beside `host_store_scan`, :962-980; `host_analytics_prune`), `infra/modules/region-host/managed_fleets.tf` (`host_inline_policies`), `infra/modules/region-host/README.md`, `infra/modules/region-host/tests/analytics_prune.tftest.hcl`, `infra/modules/region-host/tests/managed_fleets/managed_fleets.tftest.hcl`, `internal/objstore/termination_credentials_test.go` (the `store boundary statements` digest)
- Create: `infra/modules/region-host/tests/store_conversion.tftest.hcl` (the shape of `store_scanning.tftest.hcl`)
**Interfaces:**
-- Produces: `variable "store_conversion"` (bool, default false; set it with `CARLOS_STORE_CONVERSION=1` on the console); `aws_iam_role_policy.host_store_conversion` (count on the switch; precondition `store_bucket_prefix != ""`) allowing `s3:GetBucketPolicy`, `s3:GetEncryptionConfiguration`, `s3:GetBucketVersioning`, `s3:GetBucketCORS`, `s3:PutBucketCORS`, `s3:GetLifecycleConfiguration`, `s3:PutLifecycleConfiguration` on `arn:aws:s3:::<prefix>*`; the store boundary's object statement gains `s3:AbortMultipartUpload` and `s3:ListMultipartUploadParts` when the switch is on.
+- Produces: `variable "store_conversion"` (bool, default false; set it with `CARLOS_STORE_CONVERSION=1` on the console); `aws_iam_policy.host_store_conversion` and `aws_iam_role_policy_attachment.host_store_conversion` (count on the switch), a customer-managed policy allowing `s3:GetBucketPolicy`, `s3:GetEncryptionConfiguration`, `s3:GetBucketVersioning`, `s3:GetBucketCORS`, `s3:PutBucketCORS`, `s3:GetLifecycleConfiguration`, `s3:PutLifecycleConfiguration`, `s3:GetBucketTagging`, `s3:PutBucketTagging` on `arn:aws:s3:::<prefix>*`, and denying `s3:PutBucketCORS`, `s3:PutLifecycleConfiguration` and `s3:PutBucketTagging` on the deployment and data buckets; the store boundary's object statement gains `s3:AbortMultipartUpload` and `s3:ListMultipartUploadParts` when the switch is on.
+- Preconditions with the switch on: `store_bucket_prefix` is set; it is a dedicated store prefix (literal, lower-case, ending in `-store-`, such as `carlos-store-`), because no root can list every bucket a broader prefix would match; and no deployment, data or sibling staging bucket name starts with it. With the switch off every prefix plans as before.
+- The console role's inline total: `host_analytics_prune` becomes a customer-managed attached policy with the same grant and the same switch, and `host_inline_policies` counts every inline policy on the role. The complete console role, every feature on, fits IAM's 10,240-character inline total and 6,144 characters per managed policy; a role over the total is refused at plan.
-- [ ] **Step 1: Write the failing tftest:** off by default creates nothing and leaves the boundary byte for byte; on grants exactly those seven actions on that resource; on adds the two multipart actions to the boundary and nothing else; on without a prefix fails its precondition.
+- [ ] **Step 1: Write the failing tftest:** off by default creates nothing and leaves the boundary byte for byte; on grants exactly those nine actions on that resource and the Deny; on adds the two multipart actions to the boundary and nothing else; on without a prefix, with a broad or wildcard prefix, or with a platform bucket under the prefix fails its precondition; off accepts every prefix as before, with fleets off and on; the complete console role fits IAM's quotas, and longer names are refused at plan.
- [ ] **Step 2:** Run `cd infra/modules/region-host && tofu init -backend=false && tofu test -filter=tests/store_conversion.tftest.hcl`. Expected: FAIL.
- [ ] **Step 3:** Implement; README paragraph beside `store_scanning`'s; copy-review it.
- [ ] **Step 4:** Rerun, and `tofu test` for the module's other files. Expected: PASS.
diff --git a/infra/modules/media-engine/README.md b/infra/modules/media-engine/README.md
index a92ed3eb5..244f05599 100644
--- a/infra/modules/media-engine/README.md
+++ b/infra/modules/media-engine/README.md
@@ -39,6 +39,10 @@ variable "media_engine_drill" {
Keep the module’s name `media_engine` and leave the variable’s default at
`null`. The endpoint drill relies on both.
+The module attaches one customer-managed policy to the console’s role, so it
+does not count toward IAM’s 10,240-character limit on that role’s inline
+policies.
+
## Region
The engine must run in the same region as the deployment’s stores. If `region`
diff --git a/infra/modules/media-engine/iam.tf b/infra/modules/media-engine/iam.tf
index 6c05ea94b..eb63af0b3 100644
--- a/infra/modules/media-engine/iam.tf
+++ b/infra/modules/media-engine/iam.tf
@@ -100,9 +100,10 @@ resource "aws_iam_role_policy" "conversion" {
})
}
-resource "aws_iam_role_policy" "console" {
+# A customer-managed policy, because the console role's inline policies are
+# close to IAM's 10,240-character total.
+resource "aws_iam_policy" "console" {
name = "${var.name}-console"
- role = var.console_role_name
policy = jsonencode({
Version = "2012-10-17"
Statement = [
@@ -157,3 +158,8 @@ resource "aws_iam_role_policy" "console" {
]
})
}
+
+resource "aws_iam_role_policy_attachment" "console" {
+ role = var.console_role_name
+ policy_arn = aws_iam_policy.console.arn
+}
diff --git a/infra/modules/media-engine/tests/media_engine.tftest.hcl b/infra/modules/media-engine/tests/media_engine.tftest.hcl
index adf62098e..bce9e8c0d 100644
--- a/infra/modules/media-engine/tests/media_engine.tftest.hcl
+++ b/infra/modules/media-engine/tests/media_engine.tftest.hcl
@@ -18,6 +18,11 @@ override_resource {
values = { arn = "arn:aws:iam::123456789012:role/carlos-test-media-conversion" }
}
+override_resource {
+ target = aws_iam_policy.console
+ values = { arn = "arn:aws:iam::123456789012:policy/carlos-test-media-console" }
+}
+
override_resource {
target = aws_iam_role.reaper
values = { arn = "arn:aws:iam::123456789012:role/carlos-test-media-reaper" }
@@ -528,11 +533,20 @@ run "conversion_role_policy_exact" {
run "console_policy_exact" {
command = apply
assert {
- condition = aws_iam_role_policy.console.role == "carlos-test-console"
- error_message = "the policy goes on the console's role"
+ condition = aws_iam_role_policy_attachment.console.role == "carlos-test-console"
+ error_message = "the policy is attached to the console's role"
+ }
+ assert {
+ condition = aws_iam_role_policy_attachment.console.policy_arn == "arn:aws:iam::123456789012:policy/carlos-test-media-console"
+ error_message = "the attachment names the module's console policy"
+ }
+ # IAM caps a customer-managed policy at 6,144 characters.
+ assert {
+ condition = length(aws_iam_policy.console.policy) < 6144
+ error_message = "the console policy must fit IAM's 6,144-character limit for a managed policy"
}
assert {
- condition = jsonencode(jsondecode(aws_iam_role_policy.console.policy).Statement) == jsonencode([
+ condition = jsonencode(jsondecode(aws_iam_policy.console.policy).Statement) == jsonencode([
{ Action = "sts:AssumeRole", Effect = "Allow", Resource = "arn:aws:iam::123456789012:role/carlos-test-media-conversion", Sid = "AssumeConversion" },
{
Action = "ecs:RunTask"
@@ -692,7 +706,7 @@ run "drill_open_opens_only_the_endpoints" {
condition = (
!strcontains(aws_iam_role_policy.conversion.policy, "999999999999")
&& !strcontains(aws_iam_role_policy.execution.policy, "999999999999")
- && !strcontains(aws_iam_role_policy.console.policy, "999999999999")
+ && !strcontains(aws_iam_policy.console.policy, "999999999999")
&& !strcontains(aws_iam_role.conversion.assume_role_policy, "999999999999")
)
error_message = "the drill's user reaches no role"
diff --git a/infra/modules/media-engine/variables.tf b/infra/modules/media-engine/variables.tf
index b1dab13fc..b2ce7c492 100644
--- a/infra/modules/media-engine/variables.tf
+++ b/infra/modules/media-engine/variables.tf
@@ -46,7 +46,7 @@ variable "deployment_bucket_name" {
}
variable "console_role_name" {
- description = "The console's IAM role name. The module adds one inline policy to it."
+ description = "The console's IAM role name. The module attaches one customer-managed policy to it."
type = string
validation {
condition = can(regex("^[A-Za-z0-9+=,.@_-]{1,64}$", var.console_role_name))
diff --git a/infra/modules/region-host/README.md b/infra/modules/region-host/README.md
index 628b85426..ca2a4165a 100644
--- a/infra/modules/region-host/README.md
+++ b/infra/modules/region-host/README.md
@@ -108,7 +108,11 @@ looks exactly like a deploy that did nothing.
## Media conversion and browser uploads
-`store_conversion = true` lets this host’s role check the buckets under `store_bucket_prefix` before media conversion uses them, and set them up for browser uploads. The role can read each bucket’s policy, encryption and versioning, and read and change its CORS rules, lifecycle rules and tags. Store users can also list the parts of their multipart uploads and abort those uploads. The role can never change CORS rules, lifecycle rules or tags on the deployment and data buckets, even if the prefix matches their names. It is off by default, so a deployment that has not switched it on plans no change. Set it on the console’s box only, along with `CARLOS_STORE_CONVERSION=1` on the console. It requires `store_bucket_prefix`.
+`store_conversion = true` lets this host’s role check the buckets under `store_bucket_prefix` before media conversion uses them, and set them up for browser uploads. The role can read each bucket’s policy, encryption and versioning, and read and change its CORS rules, lifecycle rules and tags. Store users can also list the parts of their multipart uploads and abort those uploads. It is off by default, so a deployment that has not switched it on plans no change. Set it on the console’s box only, along with `CARLOS_STORE_CONVERSION=1` on the console.
+
+The switch needs a `store_bucket_prefix` used only for stores: lower-case letters, digits and hyphens, ending in `-store-`, such as `carlos-store-`. The plan fails on a broader prefix such as `carlos-`, because this grant can replace the settings of every bucket the prefix matches. It also fails if the name of the deployment bucket, the data bucket or a sibling staging bucket starts with the prefix.
+
+The grant is a customer-managed policy attached to the role, so it does not count toward IAM’s 10,240-character limit on the role’s inline policies.
## Checking it worked
diff --git a/infra/modules/region-host/main.tf b/infra/modules/region-host/main.tf
index 1d2205690..9edd23396 100644
--- a/infra/modules/region-host/main.tf
+++ b/infra/modules/region-host/main.tf
@@ -459,10 +459,16 @@ resource "aws_iam_role_policy" "host_costexplorer" {
# bucket grants above are untouched. The deployment bucket's own box-evidence Deny
# (deployment-buckets/main.tf, BoxRecordsAreNeverDeleted) covers control/
# only and still wins over any Allow.
-resource "aws_iam_role_policy" "host_analytics_prune" {
+#
+# A customer-managed policy, because the console role's inline policies are
+# close to IAM's 10,240-character total (managed_fleets.tf counts them). An
+# apply that moves an existing role from the inline policy to this one can
+# leave a moment without the grant. A prune pass caught in it reports a missing
+# permission, falls back to current-key deletes of the same expired files, and
+# the next day's pass removes the versions left behind.
+resource "aws_iam_policy" "host_analytics_prune" {
count = var.enable_analytics_version_prune ? 1 : 0
name = "${var.iam_name}-analytics-prune"
- role = aws_iam_role.host.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
@@ -483,6 +489,12 @@ resource "aws_iam_role_policy" "host_analytics_prune" {
})
}
+resource "aws_iam_role_policy_attachment" "host_analytics_prune" {
+ count = var.enable_analytics_version_prune ? 1 : 0
+ role = aws_iam_role.host.name
+ policy_arn = aws_iam_policy.host_analytics_prune[0].arn
+}
+
# The agent's memwatch duty (internal/host/memwatch.go) publishes
# MemAvailablePercent and MemoryPressureSome60 once a minute. PutMetricData
# has no resource ARN; the namespace condition is the whole bound — this
@@ -1011,20 +1023,26 @@ resource "aws_iam_role_policy" "host_store_scan" {
# HeadBucket (s3:ListBucket) and GetBucketLocation are already in host_store,
# which exists whenever this policy can.
#
-# The Deny is the same care host_store takes: a prefix such as "carlos-" also
-# matches carlos-<deployment>-<acct>, and a lifecycle write replaces the whole
-# configuration, so an uploads enable aimed at the wrong bucket would wipe the
-# deployment bucket's retention rules. Bucket-level writes only, and nothing
-# on this role sets these on those buckets. An empty ARN is left out, and with
-# no bucket to name the statement is too: IAM refuses an empty Resource.
+# A lifecycle write replaces the whole configuration, so this grant must never
+# match platform storage. No root can list every bucket a broad prefix such as
+# "carlos-" would match (other regions' data buckets, tofu state), so a broad
+# prefix is refused outright: the switch needs a dedicated one ending in
+# -store-, and none of the buckets this module knows may start with it.
+#
+# The Deny is defence in depth behind that refusal. Bucket-level writes only,
+# and nothing on this role sets these on those buckets. An empty ARN is left
+# out, and with no bucket to name the statement is too: IAM refuses an empty
+# Resource.
+#
+# A customer-managed policy, because the console role's inline policies are
+# close to IAM's 10,240-character total (managed_fleets.tf counts them).
locals {
store_conversion_denied_buckets = compact([var.deployment_bucket_arn, var.data_bucket_arn])
}
-resource "aws_iam_role_policy" "host_store_conversion" {
+resource "aws_iam_policy" "host_store_conversion" {
count = var.store_conversion ? 1 : 0
name = "${var.iam_name}-store-conversion"
- role = aws_iam_role.host.id
policy = jsonencode({
Version = "2012-10-17"
Statement = concat(
@@ -1059,9 +1077,26 @@ resource "aws_iam_role_policy" "host_store_conversion" {
condition = var.store_bucket_prefix != ""
error_message = "store_conversion needs store_bucket_prefix: with no prefix there are no object stores on this deployment to convert, and the grant would name every bucket in the account."
}
+ precondition {
+ condition = var.store_bucket_prefix == "" || can(regex("^[a-z0-9][a-z0-9-]*-store-$", var.store_bucket_prefix))
+ error_message = "store_conversion needs a dedicated store_bucket_prefix: lower-case letters, digits and hyphens, ending in -store-, such as carlos-store-. A broader prefix such as carlos- can match the platform's own buckets, and this grant can replace the CORS, lifecycle and tag settings of every bucket it matches."
+ }
+ precondition {
+ condition = var.store_bucket_prefix == "" || !anytrue([
+ for arn in concat(local.store_conversion_denied_buckets, var.sibling_staging_arns) :
+ startswith(replace(arn, "/^arn:[^:]*:s3:::/", ""), var.store_bucket_prefix)
+ ])
+ error_message = "store_conversion refuses a store_bucket_prefix that matches the deployment, data or sibling staging buckets, because this grant can replace the CORS, lifecycle and tag settings of every bucket it matches. Choose a prefix none of those bucket names start with."
+ }
}
}
+resource "aws_iam_role_policy_attachment" "host_store_conversion" {
+ count = var.store_conversion ? 1 : 0
+ role = aws_iam_role.host.name
+ policy_arn = aws_iam_policy.host_store_conversion[0].arn
+}
+
# --- Termination evidence (region HA spec 1 §5.9) ---------------------------
# EC2 tells EventBridge when an instance in this account and region reaches
# "terminated"; the rule forwards that to a queue the region's boxes read.
diff --git a/infra/modules/region-host/managed_fleets.tf b/infra/modules/region-host/managed_fleets.tf
index f5fc3732c..249180d4a 100644
--- a/infra/modules/region-host/managed_fleets.tf
+++ b/infra/modules/region-host/managed_fleets.tf
@@ -284,16 +284,18 @@ locals {
Version = "2012-10-17"
Statement = local.managed_console_statements
})
- # Every other inline policy this role can carry in fixed mode. IAM caps
- # their total at 10,240 characters without whitespace (jsonencode writes
- # none), and the console host already carries most of them.
+ # Every other inline policy on this role. IAM caps their total at 10,240
+ # characters without whitespace (jsonencode writes none), and the console
+ # host already carries most of them. A new inline policy on this role
+ # belongs here; customer-managed policies do not count toward the total.
host_inline_policies = concat(
[aws_iam_role_policy.host_buckets.policy, aws_iam_role_policy.host_metrics.policy, aws_iam_role_policy.host_terminations.policy],
- aws_iam_role_policy.host_route53[*].policy, aws_iam_role_policy.host_steering[*].policy,
- aws_iam_role_policy.host_costexplorer[*].policy, aws_iam_role_policy.host_ssm_parameter_scope[*].policy,
+ aws_iam_role_policy.host_route53[*].policy, aws_iam_role_policy.host_ingress_drain[*].policy,
+ aws_iam_role_policy.host_steering[*].policy, aws_iam_role_policy.host_costexplorer[*].policy,
+ aws_iam_role_policy.host_ssm_parameter_scope[*].policy,
aws_iam_role_policy.host_release_key_deny[*].policy, aws_iam_role_policy.host_store[*].policy,
aws_iam_role_policy.host_console_mail[*].policy, aws_iam_role_policy.host_store_scan[*].policy,
- aws_iam_role_policy.host_store_conversion[*].policy, aws_iam_role_policy.alert_publish[*].policy,
+ aws_iam_role_policy.alert_publish[*].policy, aws_iam_role_policy.group[*].policy,
)
}
diff --git a/infra/modules/region-host/tests/analytics_prune.tftest.hcl b/infra/modules/region-host/tests/analytics_prune.tftest.hcl
index 568d99bbe..a7f4ed532 100644
--- a/infra/modules/region-host/tests/analytics_prune.tftest.hcl
+++ b/infra/modules/region-host/tests/analytics_prune.tftest.hcl
@@ -1,5 +1,5 @@
# Plan-time tests for the console's analytics version-prune grant
-# (aws_iam_role_policy.host_analytics_prune). mock_provider means no AWS
+# (aws_iam_policy.host_analytics_prune). mock_provider means no AWS
# account and no credentials: these assert what the module WOULD create.
mock_provider "aws" {}
@@ -20,6 +20,11 @@ override_resource {
values = { arn = "arn:aws:sqs:eu-west-1:123456789012:carlos-test-host-terminations" }
}
+override_resource {
+ target = aws_iam_policy.host_analytics_prune
+ values = { arn = "arn:aws:iam::123456789012:policy/carlos-test-host-analytics-prune" }
+}
+
variables {
name = "carlos-test-ie"
sg_description = "carlos test host"
@@ -35,9 +40,13 @@ variables {
run "off_by_default" {
command = plan
assert {
- condition = length(aws_iam_role_policy.host_analytics_prune) == 0
+ condition = length(aws_iam_policy.host_analytics_prune) == 0
error_message = "enable_analytics_version_prune defaults to false and must create no policy"
}
+ assert {
+ condition = length(aws_iam_role_policy_attachment.host_analytics_prune) == 0
+ error_message = "enable_analytics_version_prune defaults to false and must attach no policy"
+ }
# Off, the host's bucket policy still grants no version action on the
# deployment bucket at all.
assert {
@@ -56,18 +65,26 @@ run "on_grants_exactly_the_analytics_day_versions" {
enable_analytics_version_prune = true
}
assert {
- condition = length(aws_iam_role_policy.host_analytics_prune) == 1
+ condition = length(aws_iam_policy.host_analytics_prune) == 1
error_message = "enable_analytics_version_prune = true must create one policy"
}
assert {
- condition = aws_iam_role_policy.host_analytics_prune[0].name == "carlos-test-host-analytics-prune"
+ condition = aws_iam_policy.host_analytics_prune[*].name == ["carlos-test-host-analytics-prune"]
error_message = "the policy is named <iam_name>-analytics-prune"
}
+ assert {
+ condition = aws_iam_role_policy_attachment.host_analytics_prune[*].role == ["carlos-test-host"]
+ error_message = "the policy is attached to this host's role"
+ }
+ assert {
+ condition = aws_iam_role_policy_attachment.host_analytics_prune[*].policy_arn == ["arn:aws:iam::123456789012:policy/carlos-test-host-analytics-prune"]
+ error_message = "the attachment names the analytics prune policy"
+ }
# The whole policy, literally: version listing on the deployment bucket
# held to s3:prefix apps/*, version deletion on exactly the analytics day
# file pattern, and nothing else.
assert {
- condition = jsonencode(jsondecode(aws_iam_role_policy.host_analytics_prune[0].policy).Statement) == jsonencode([
+ condition = jsonencode(flatten([for p in aws_iam_policy.host_analytics_prune : jsondecode(p.policy).Statement])) == jsonencode([
{
Action = ["s3:ListBucketVersions"]
Condition = { StringLike = { "s3:prefix" = "apps/*" } }
@@ -87,7 +104,7 @@ run "on_grants_exactly_the_analytics_day_versions" {
# Nothing in it reaches the data bucket.
assert {
condition = alltrue([
- for s in jsondecode(aws_iam_role_policy.host_analytics_prune[0].policy).Statement :
+ for s in flatten([for p in aws_iam_policy.host_analytics_prune : jsondecode(p.policy).Statement]) :
alltrue([for r in flatten([s.Resource]) : !startswith(r, "arn:aws:s3:::carlos-test-ie")])
])
error_message = "the prune grant must not name the data bucket"
diff --git a/infra/modules/region-host/tests/managed_fleets/managed_fleets.tftest.hcl b/infra/modules/region-host/tests/managed_fleets/managed_fleets.tftest.hcl
index 358bef50e..325c3c915 100644
--- a/infra/modules/region-host/tests/managed_fleets/managed_fleets.tftest.hcl
+++ b/infra/modules/region-host/tests/managed_fleets/managed_fleets.tftest.hcl
@@ -45,6 +45,16 @@ override_resource {
values = { arn = "arn:aws:iam::123456789012:policy/carlos-test-host-store-boundary" }
}
+override_resource {
+ target = aws_iam_policy.host_analytics_prune
+ values = { arn = "arn:aws:iam::123456789012:policy/carlos-test-host-analytics-prune" }
+}
+
+override_resource {
+ target = aws_iam_policy.host_store_conversion
+ values = { arn = "arn:aws:iam::123456789012:policy/carlos-test-host-store-conversion" }
+}
+
variables {
name = "carlos-test-ie"
sg_description = "carlos test host"
@@ -461,10 +471,10 @@ run "role_inline_limit" {
expect_failures = [aws_iam_role_policy.host_managed_fleets]
}
-# The media conversion grant counts toward the same total. The policy_union
-# console fits without it and goes over with it, so the plan refuses rather
-# than IAM refusing the apply half-way.
-run "role_inline_limit_counts_store_conversion" {
+# The complete console: every grant this role can carry in fixed mode, media
+# conversion included. It must fit IAM's quotas, and the module's own count
+# must agree with this list of every inline policy on the role.
+run "complete_console_role_fits_iam_quotas" {
command = plan
variables {
managed_fleets_enabled = true
@@ -476,16 +486,86 @@ run "role_inline_limit_counts_store_conversion" {
instance_types = ["t4g.small", "t4g.medium"]
}
}
- store_bucket_prefix = "carlos-store-test-"
- store_scanning = true
- store_conversion = true
- enable_cost_explorer = true
- console_mail_from = "noreply@example.com"
- console_mail_region = "eu-west-1"
- route53_zone_ids = ["Z0123456789ABCDEFGHIJ"]
- steering_zone_ids = ["Z0123456789ABCDEFGHIJ"]
- steering_record_names = ["*.example.com"]
- alerts_email = "ops@example.com"
+ store_bucket_prefix = "carlos-store-"
+ store_scanning = true
+ store_conversion = true
+ enable_cost_explorer = true
+ enable_analytics_version_prune = true
+ console_mail_from = "noreply@example.com"
+ console_mail_region = "eu-west-1"
+ route53_zone_ids = ["Z0123456789ABCDEFGHIJ"]
+ steering_zone_ids = ["Z0123456789ABCDEFGHIJ"]
+ steering_record_names = ["*.example.com"]
+ alerts_email = "ops@example.com"
+ ingress_target_group_arns = ["arn:aws:elasticloadbalancing:eu-west-1:123456789012:targetgroup/carlos-test/0123456789abcdef"]
+ }
+ assert {
+ condition = sum([
+ for doc in [
+ aws_iam_role_policy.host_buckets.policy, aws_iam_role_policy.host_route53[0].policy,
+ aws_iam_role_policy.host_ingress_drain[0].policy, aws_iam_role_policy.host_steering[0].policy,
+ aws_iam_role_policy.host_costexplorer[0].policy,
+ aws_iam_role_policy.host_metrics.policy, aws_iam_role_policy.host_ssm_parameter_scope[0].policy,
+ aws_iam_role_policy.host_release_key_deny[0].policy, aws_iam_role_policy.host_store[0].policy,
+ aws_iam_role_policy.host_console_mail[0].policy, aws_iam_role_policy.host_store_scan[0].policy,
+ aws_iam_role_policy.host_terminations.policy, aws_iam_role_policy.alert_publish[0].policy,
+ aws_iam_role_policy.host_managed_fleets[0].policy,
+ ] : length(doc)
+ ]) < 10240
+ error_message = "the complete console role's inline policies must total under IAM's 10,240 characters"
+ }
+ assert {
+ condition = sum([for doc in local.host_inline_policies : length(doc)]) == sum([
+ for doc in [
+ aws_iam_role_policy.host_buckets.policy, aws_iam_role_policy.host_route53[0].policy,
+ aws_iam_role_policy.host_ingress_drain[0].policy, aws_iam_role_policy.host_steering[0].policy,
+ aws_iam_role_policy.host_costexplorer[0].policy,
+ aws_iam_role_policy.host_metrics.policy, aws_iam_role_policy.host_ssm_parameter_scope[0].policy,
+ aws_iam_role_policy.host_release_key_deny[0].policy, aws_iam_role_policy.host_store[0].policy,
+ aws_iam_role_policy.host_console_mail[0].policy, aws_iam_role_policy.host_store_scan[0].policy,
+ aws_iam_role_policy.host_terminations.policy, aws_iam_role_policy.alert_publish[0].policy,
+ ] : length(doc)
+ ])
+ error_message = "the module's inline total must count every inline policy on the role, and no managed policy"
+ }
+ # IAM caps a customer-managed policy at 6,144 characters.
+ assert {
+ condition = alltrue([
+ for doc in concat(aws_iam_policy.host_store_conversion[*].policy, aws_iam_policy.host_analytics_prune[*].policy, aws_iam_policy.store_user_boundary[*].policy) : length(doc) < 6144
+ ]) && length(aws_iam_policy.host_store_conversion) == 1 && length(aws_iam_policy.host_analytics_prune) == 1 && length(aws_iam_policy.store_user_boundary) == 1
+ error_message = "each customer-managed policy must fit IAM's 6,144-character limit"
+ }
+}
+
+# The same complete console with longer bucket names, the kind a real
+# deployment may have, goes over the total. The plan must refuse it: IAM would
+# otherwise refuse the apply half-way.
+run "complete_console_role_with_long_names_is_refused_at_plan" {
+ command = plan
+ variables {
+ managed_fleets_enabled = true
+ managed_fleets_regions = {
+ "eu-west-1" = {
+ image = "ami-0123456789abcdef0"
+ subnets = ["subnet-0123456789abcdef0", "subnet-0123456789abcdef1"]
+ security_groups = ["sg-0123456789abcdef0"]
+ instance_types = ["t4g.small", "t4g.medium"]
+ }
+ }
+ deployment_bucket_arn = "arn:aws:s3:::carlos-a-long-deployment-bucket-name-for-this-limit-test"
+ data_bucket_arn = "arn:aws:s3:::carlos-a-long-regional-data-bucket-name-for-this-limit-test"
+ store_bucket_prefix = "carlos-store-"
+ store_scanning = true
+ store_conversion = true
+ enable_cost_explorer = true
+ enable_analytics_version_prune = true
+ console_mail_from = "noreply@example.com"
+ console_mail_region = "eu-west-1"
+ route53_zone_ids = ["Z0123456789ABCDEFGHIJ"]
+ steering_zone_ids = ["Z0123456789ABCDEFGHIJ"]
+ steering_record_names = ["*.example.com"]
+ alerts_email = "ops@example.com"
+ ingress_target_group_arns = ["arn:aws:elasticloadbalancing:eu-west-1:123456789012:targetgroup/carlos-test/0123456789abcdef"]
}
expect_failures = [aws_iam_role_policy.host_managed_fleets]
}
@@ -504,14 +584,14 @@ run "store_conversion_leaves_the_fleet_boundary_alone" {
instance_types = ["t4g.small", "t4g.medium"]
}
}
- store_bucket_prefix = "carlos-store-test-"
+ store_bucket_prefix = "carlos-test-store-"
store_conversion = true
}
assert {
condition = jsondecode(aws_iam_policy.store_user_boundary[0].policy).Statement == concat(
[
- { Effect = "Allow", Action = ["s3:ListBucket", "s3:GetBucketLocation"], Resource = "arn:aws:s3:::carlos-store-test-*" },
- { Effect = "Allow", Action = ["s3:GetObject", "s3:PutObject", "s3:DeleteObject", "s3:AbortMultipartUpload", "s3:ListMultipartUploadParts"], Resource = "arn:aws:s3:::carlos-store-test-*/*" },
+ { Effect = "Allow", Action = ["s3:ListBucket", "s3:GetBucketLocation"], Resource = "arn:aws:s3:::carlos-test-store-*" },
+ { Effect = "Allow", Action = ["s3:GetObject", "s3:PutObject", "s3:DeleteObject", "s3:AbortMultipartUpload", "s3:ListMultipartUploadParts"], Resource = "arn:aws:s3:::carlos-test-store-*/*" },
],
jsondecode(file("../../../internal/awsmanaged/testdata/managed-boundary-policy.json")).Statement,
)
@@ -519,6 +599,87 @@ run "store_conversion_leaves_the_fleet_boundary_alone" {
}
}
+# The conversion prefix rules hold with managed fleets on. The fleet prefix here
+# leaves carlos- free, so only the conversion rule can refuse it.
+run "conversion_refuses_a_broad_prefix_with_fleets_on" {
+ command = plan
+ variables {
+ managed_fleets_enabled = true
+ managed_fleet_bucket_prefix = "fleets-test-"
+ managed_fleets_regions = {
+ "eu-west-1" = {
+ image = "ami-0123456789abcdef0"
+ subnets = ["subnet-0123456789abcdef0", "subnet-0123456789abcdef1"]
+ security_groups = ["sg-0123456789abcdef0"]
+ instance_types = ["t4g.small", "t4g.medium"]
+ }
+ }
+ store_bucket_prefix = "carlos-"
+ store_conversion = true
+ }
+ expect_failures = [aws_iam_policy.host_store_conversion]
+}
+
+# carlos- also matches this suite's deployment buckets, so the overlap rule can
+# refuse it alone. This prefix matches none of them: only the format rule can.
+run "conversion_refuses_a_prefix_not_ending_in_store_with_fleets_on" {
+ command = plan
+ variables {
+ managed_fleets_enabled = true
+ managed_fleets_regions = {
+ "eu-west-1" = {
+ image = "ami-0123456789abcdef0"
+ subnets = ["subnet-0123456789abcdef0", "subnet-0123456789abcdef1"]
+ security_groups = ["sg-0123456789abcdef0"]
+ instance_types = ["t4g.small", "t4g.medium"]
+ }
+ }
+ store_bucket_prefix = "carlos-media-"
+ store_conversion = true
+ }
+ expect_failures = [aws_iam_policy.host_store_conversion]
+}
+
+run "conversion_refuses_a_deployment_bucket_under_the_prefix_with_fleets_on" {
+ command = plan
+ variables {
+ managed_fleets_enabled = true
+ managed_fleets_regions = {
+ "eu-west-1" = {
+ image = "ami-0123456789abcdef0"
+ subnets = ["subnet-0123456789abcdef0", "subnet-0123456789abcdef1"]
+ security_groups = ["sg-0123456789abcdef0"]
+ instance_types = ["t4g.small", "t4g.medium"]
+ }
+ }
+ store_bucket_prefix = "carlos-store-"
+ deployment_bucket_arn = "arn:aws:s3:::carlos-store-platform"
+ store_conversion = true
+ }
+ expect_failures = [aws_iam_policy.host_store_conversion]
+}
+
+run "conversion_off_accepts_a_broad_prefix_with_fleets_on" {
+ command = plan
+ variables {
+ managed_fleets_enabled = true
+ managed_fleet_bucket_prefix = "fleets-test-"
+ managed_fleets_regions = {
+ "eu-west-1" = {
+ image = "ami-0123456789abcdef0"
+ subnets = ["subnet-0123456789abcdef0", "subnet-0123456789abcdef1"]
+ security_groups = ["sg-0123456789abcdef0"]
+ instance_types = ["t4g.small", "t4g.medium"]
+ }
+ }
+ store_bucket_prefix = "carlos-"
+ }
+ assert {
+ condition = length(aws_iam_policy.host_store_conversion) == 0 && length(aws_iam_role_policy.host_store) == 1
+ error_message = "with the switch off a broad prefix plans as it did before, fleets on"
+ }
+}
+
# ---- refused configurations: each fails the plan before any resource ----
run "bad_empty_deployment" {
diff --git a/infra/modules/region-host/tests/store_conversion.tftest.hcl b/infra/modules/region-host/tests/store_conversion.tftest.hcl
index 7e2f1e9e3..52383e62c 100644
--- a/infra/modules/region-host/tests/store_conversion.tftest.hcl
+++ b/infra/modules/region-host/tests/store_conversion.tftest.hcl
@@ -17,6 +17,11 @@ override_resource {
values = { arn = "arn:aws:sqs:eu-west-1:123456789012:carlos-test-host-terminations" }
}
+override_resource {
+ target = aws_iam_policy.host_store_conversion
+ values = { arn = "arn:aws:iam::123456789012:policy/carlos-test-host-store-conversion" }
+}
+
variables {
name = "carlos-test-ie"
sg_description = "carlos test host"
@@ -27,7 +32,7 @@ variables {
deployment_bucket_arn = "arn:aws:s3:::carlos-test-platform"
data_bucket_arn = "arn:aws:s3:::carlos-test-ie"
ssm_parameter_arn = "arn:aws:ssm:eu-west-1:123456789012:parameter/carlos/*"
- store_bucket_prefix = "carlos-store-test-"
+ store_bucket_prefix = "carlos-test-store-"
}
# Off, the boundary must render the very bytes it rendered before the switch
@@ -35,11 +40,15 @@ variables {
run "off_by_default" {
command = plan
assert {
- condition = length(aws_iam_role_policy.host_store_conversion) == 0
+ condition = length(aws_iam_policy.host_store_conversion) == 0
error_message = "store_conversion defaults to false and must create nothing"
}
assert {
- condition = aws_iam_policy.store_user_boundary[0].policy == "{\"Statement\":[{\"Action\":[\"s3:ListBucket\",\"s3:GetBucketLocation\"],\"Effect\":\"Allow\",\"Resource\":\"arn:aws:s3:::carlos-store-test-*\"},{\"Action\":[\"s3:GetObject\",\"s3:PutObject\",\"s3:DeleteObject\"],\"Effect\":\"Allow\",\"Resource\":\"arn:aws:s3:::carlos-store-test-*/*\"}],\"Version\":\"2012-10-17\"}"
+ condition = length(aws_iam_role_policy_attachment.host_store_conversion) == 0
+ error_message = "store_conversion defaults to false and must attach nothing"
+ }
+ assert {
+ condition = aws_iam_policy.store_user_boundary[0].policy == "{\"Statement\":[{\"Action\":[\"s3:ListBucket\",\"s3:GetBucketLocation\"],\"Effect\":\"Allow\",\"Resource\":\"arn:aws:s3:::carlos-test-store-*\"},{\"Action\":[\"s3:GetObject\",\"s3:PutObject\",\"s3:DeleteObject\"],\"Effect\":\"Allow\",\"Resource\":\"arn:aws:s3:::carlos-test-store-*/*\"}],\"Version\":\"2012-10-17\"}"
error_message = "with store_conversion off the boundary must stay byte for byte what it was"
}
}
@@ -50,16 +59,24 @@ run "on_grants_exactly_the_nine_bucket_settings_actions" {
store_conversion = true
}
assert {
- condition = length(aws_iam_role_policy.host_store_conversion) == 1
+ condition = length(aws_iam_policy.host_store_conversion) == 1
error_message = "store_conversion = true must create the conversion policy"
}
assert {
- condition = aws_iam_role_policy.host_store_conversion[*].name == ["carlos-test-host-store-conversion"]
+ condition = aws_iam_policy.host_store_conversion[*].name == ["carlos-test-host-store-conversion"]
error_message = "the policy is named <iam_name>-store-conversion"
}
+ assert {
+ condition = aws_iam_role_policy_attachment.host_store_conversion[*].role == ["carlos-test-host"]
+ error_message = "the policy is attached to this host's role"
+ }
+ assert {
+ condition = aws_iam_role_policy_attachment.host_store_conversion[*].policy_arn == ["arn:aws:iam::123456789012:policy/carlos-test-host-store-conversion"]
+ error_message = "the attachment names the conversion policy"
+ }
assert {
condition = [
- for st in flatten([for p in aws_iam_role_policy.host_store_conversion : jsondecode(p.policy).Statement]) : st if st.Effect == "Allow"
+ for st in flatten([for p in aws_iam_policy.host_store_conversion : jsondecode(p.policy).Statement]) : st if st.Effect == "Allow"
] == [{
Effect = "Allow"
Action = [
@@ -73,20 +90,19 @@ run "on_grants_exactly_the_nine_bucket_settings_actions" {
"s3:GetBucketTagging",
"s3:PutBucketTagging",
]
- Resource = "arn:aws:s3:::carlos-store-test-*"
+ Resource = "arn:aws:s3:::carlos-test-store-*"
}]
error_message = "one Allow: exactly the nine actions internal/mediaaws calls, on buckets under the store prefix and nothing else"
}
assert {
condition = length(flatten([
- for st in flatten([for p in aws_iam_role_policy.host_store_conversion : jsondecode(p.policy).Statement]) : st.Action if st.Effect == "Allow"
+ for st in flatten([for p in aws_iam_policy.host_store_conversion : jsondecode(p.policy).Statement]) : st.Action if st.Effect == "Allow"
])) == 9
error_message = "the grant is nine actions"
}
}
-# A prefix such as carlos- also matches carlos-<deployment>-<acct>, and a
-# lifecycle write replaces the whole configuration, so the deployment's own
+# Defence in depth behind the prefix refusals below: the deployment's own
# buckets are refused by name.
run "on_denies_settings_writes_on_the_deployment_buckets" {
command = plan
@@ -95,7 +111,7 @@ run "on_denies_settings_writes_on_the_deployment_buckets" {
}
assert {
condition = [
- for st in flatten([for p in aws_iam_role_policy.host_store_conversion : jsondecode(p.policy).Statement]) : st if st.Effect == "Deny"
+ for st in flatten([for p in aws_iam_policy.host_store_conversion : jsondecode(p.policy).Statement]) : st if st.Effect == "Deny"
] == [{
Effect = "Deny"
Action = ["s3:PutBucketCORS", "s3:PutLifecycleConfiguration", "s3:PutBucketTagging"]
@@ -105,26 +121,6 @@ run "on_denies_settings_writes_on_the_deployment_buckets" {
}
}
-run "the_deny_holds_when_the_prefix_matches_the_deployment_buckets" {
- command = plan
- variables {
- store_conversion = true
- store_bucket_prefix = "carlos-"
- }
- assert {
- condition = [
- for st in flatten([for p in aws_iam_role_policy.host_store_conversion : jsondecode(p.policy).Statement]) : st.Resource if st.Effect == "Allow"
- ] == ["arn:aws:s3:::carlos-*"]
- error_message = "the Allow follows the store prefix"
- }
- assert {
- condition = [
- for st in flatten([for p in aws_iam_role_policy.host_store_conversion : jsondecode(p.policy).Statement]) : st.Resource if st.Effect == "Deny"
- ] == [["arn:aws:s3:::carlos-test-platform", "arn:aws:s3:::carlos-test-ie"]]
- error_message = "a prefix that matches the deployment's buckets must still be refused on them"
- }
-}
-
# An empty ARN in a Resource list is a policy IAM refuses, so it is left out,
# and a Deny with nothing to name is left out whole.
run "the_deny_skips_an_empty_deployment_bucket" {
@@ -135,7 +131,7 @@ run "the_deny_skips_an_empty_deployment_bucket" {
}
assert {
condition = [
- for st in flatten([for p in aws_iam_role_policy.host_store_conversion : jsondecode(p.policy).Statement]) : st.Resource if st.Effect == "Deny"
+ for st in flatten([for p in aws_iam_policy.host_store_conversion : jsondecode(p.policy).Statement]) : st.Resource if st.Effect == "Deny"
] == [["arn:aws:s3:::carlos-test-ie"]]
error_message = "with no deployment bucket the Deny names the data bucket alone"
}
@@ -149,7 +145,7 @@ run "the_deny_skips_an_empty_data_bucket" {
}
assert {
condition = [
- for st in flatten([for p in aws_iam_role_policy.host_store_conversion : jsondecode(p.policy).Statement]) : st.Resource if st.Effect == "Deny"
+ for st in flatten([for p in aws_iam_policy.host_store_conversion : jsondecode(p.policy).Statement]) : st.Resource if st.Effect == "Deny"
] == [["arn:aws:s3:::carlos-test-platform"]]
error_message = "with no data bucket the Deny names the deployment bucket alone"
}
@@ -164,13 +160,13 @@ run "no_deny_when_there_is_no_bucket_to_name" {
}
assert {
condition = length([
- for st in flatten([for p in aws_iam_role_policy.host_store_conversion : jsondecode(p.policy).Statement]) : st if st.Effect == "Deny"
+ for st in flatten([for p in aws_iam_policy.host_store_conversion : jsondecode(p.policy).Statement]) : st if st.Effect == "Deny"
]) == 0
error_message = "a Deny with an empty Resource list is not a valid policy"
}
assert {
condition = length([
- for st in flatten([for p in aws_iam_role_policy.host_store_conversion : jsondecode(p.policy).Statement]) : st if st.Effect == "Allow"
+ for st in flatten([for p in aws_iam_policy.host_store_conversion : jsondecode(p.policy).Statement]) : st if st.Effect == "Allow"
]) == 1
error_message = "the Allow stays when there is nothing to deny"
}
@@ -184,7 +180,7 @@ run "on_adds_only_the_multipart_actions_to_the_boundary" {
store_conversion = true
}
assert {
- condition = aws_iam_policy.store_user_boundary[0].policy == "{\"Statement\":[{\"Action\":[\"s3:ListBucket\",\"s3:GetBucketLocation\"],\"Effect\":\"Allow\",\"Resource\":\"arn:aws:s3:::carlos-store-test-*\"},{\"Action\":[\"s3:GetObject\",\"s3:PutObject\",\"s3:DeleteObject\",\"s3:AbortMultipartUpload\",\"s3:ListMultipartUploadParts\"],\"Effect\":\"Allow\",\"Resource\":\"arn:aws:s3:::carlos-store-test-*/*\"}],\"Version\":\"2012-10-17\"}"
+ condition = aws_iam_policy.store_user_boundary[0].policy == "{\"Statement\":[{\"Action\":[\"s3:ListBucket\",\"s3:GetBucketLocation\"],\"Effect\":\"Allow\",\"Resource\":\"arn:aws:s3:::carlos-test-store-*\"},{\"Action\":[\"s3:GetObject\",\"s3:PutObject\",\"s3:DeleteObject\",\"s3:AbortMultipartUpload\",\"s3:ListMultipartUploadParts\"],\"Effect\":\"Allow\",\"Resource\":\"arn:aws:s3:::carlos-test-store-*/*\"}],\"Version\":\"2012-10-17\"}"
error_message = "with store_conversion on the boundary is the old document plus the two multipart actions on the object statement"
}
}
@@ -195,5 +191,149 @@ run "refused_without_a_store_prefix" {
store_conversion = true
store_bucket_prefix = ""
}
- expect_failures = [aws_iam_role_policy.host_store_conversion]
+ expect_failures = [aws_iam_policy.host_store_conversion]
+}
+
+# The switch needs a prefix used only for stores. No root can list every bucket
+# a broad prefix would match, so a broad prefix is refused outright.
+run "accepts_a_dedicated_store_prefix" {
+ command = plan
+ variables {
+ store_conversion = true
+ store_bucket_prefix = "carlos-store-"
+ }
+ assert {
+ condition = [
+ for st in flatten([for p in aws_iam_policy.host_store_conversion : jsondecode(p.policy).Statement]) : st.Resource if st.Effect == "Allow"
+ ] == ["arn:aws:s3:::carlos-store-*"]
+ error_message = "carlos-store- is a dedicated store prefix and the Allow follows it"
+ }
+}
+
+run "refuses_a_broad_prefix" {
+ command = plan
+ variables {
+ store_conversion = true
+ store_bucket_prefix = "carlos-"
+ }
+ expect_failures = [aws_iam_policy.host_store_conversion]
+}
+
+run "refuses_a_prefix_with_store_in_the_middle" {
+ command = plan
+ variables {
+ store_conversion = true
+ store_bucket_prefix = "carlos-store-test-"
+ }
+ expect_failures = [aws_iam_policy.host_store_conversion]
+}
+
+run "refuses_a_prefix_with_no_name_before_store" {
+ command = plan
+ variables {
+ store_conversion = true
+ store_bucket_prefix = "-store-"
+ }
+ expect_failures = [aws_iam_policy.host_store_conversion]
+}
+
+run "refuses_a_wildcard_prefix" {
+ command = plan
+ variables {
+ store_conversion = true
+ store_bucket_prefix = "carlos-*-store-"
+ }
+ expect_failures = [aws_iam_policy.host_store_conversion]
+}
+
+run "refuses_a_single_character_wildcard_prefix" {
+ command = plan
+ variables {
+ store_conversion = true
+ store_bucket_prefix = "carlos-?-store-"
+ }
+ expect_failures = [aws_iam_policy.host_store_conversion]
+}
+
+run "refuses_a_policy_variable_prefix" {
+ command = plan
+ variables {
+ store_conversion = true
+ store_bucket_prefix = "$${aws:username}-store-"
+ }
+ expect_failures = [aws_iam_policy.host_store_conversion]
+}
+
+run "refuses_an_upper_case_prefix" {
+ command = plan
+ variables {
+ store_conversion = true
+ store_bucket_prefix = "Carlos-store-"
+ }
+ expect_failures = [aws_iam_policy.host_store_conversion]
+}
+
+run "refuses_a_deployment_bucket_under_the_prefix" {
+ command = plan
+ variables {
+ store_conversion = true
+ store_bucket_prefix = "carlos-store-"
+ deployment_bucket_arn = "arn:aws:s3:::carlos-store-platform"
+ }
+ expect_failures = [aws_iam_policy.host_store_conversion]
+}
+
+run "refuses_a_data_bucket_under_the_prefix" {
+ command = plan
+ variables {
+ store_conversion = true
+ store_bucket_prefix = "carlos-store-"
+ data_bucket_arn = "arn:aws:s3:::carlos-store-ie"
+ }
+ expect_failures = [aws_iam_policy.host_store_conversion]
+}
+
+run "refuses_a_sibling_staging_bucket_under_the_prefix" {
+ command = plan
+ variables {
+ store_conversion = true
+ store_bucket_prefix = "carlos-store-"
+ sibling_staging_arns = ["arn:aws:s3:::carlos-store-uk"]
+ }
+ expect_failures = [aws_iam_policy.host_store_conversion]
+}
+
+# Off, the prefix rules do not apply: every prefix plans as it did before.
+run "off_accepts_a_broad_prefix" {
+ command = plan
+ variables {
+ store_bucket_prefix = "carlos-"
+ }
+ assert {
+ condition = length(aws_iam_policy.host_store_conversion) == 0 && length(aws_iam_role_policy.host_store) == 1
+ error_message = "with the switch off a broad prefix keeps working and gets no conversion grant"
+ }
+}
+
+run "off_accepts_a_wildcard_prefix" {
+ command = plan
+ variables {
+ store_bucket_prefix = "carlos-*"
+ }
+ assert {
+ condition = length(aws_iam_policy.host_store_conversion) == 0 && length(aws_iam_role_policy.host_store) == 1
+ error_message = "with the switch off and no managed fleets a wildcard prefix plans as it did before"
+ }
+}
+
+run "off_accepts_a_deployment_bucket_under_the_prefix" {
+ command = plan
+ variables {
+ store_bucket_prefix = "carlos-store-"
+ deployment_bucket_arn = "arn:aws:s3:::carlos-store-platform"
+ }
+ assert {
+ condition = length(aws_iam_policy.host_store_conversion) == 0 && length(aws_iam_role_policy.host_store) == 1
+ error_message = "with the switch off an overlapping deployment bucket plans as it did before"
+ }
}
diff --git a/infra/modules/region-host/variables.tf b/infra/modules/region-host/variables.tf
index b06b44e71..4d7c7e585 100644
--- a/infra/modules/region-host/variables.tf
+++ b/infra/modules/region-host/variables.tf
@@ -301,7 +301,7 @@ variable "store_scanning" {
}
variable "store_conversion" {
- description = "Let this host's role check and set the bucket settings media conversion and browser uploads rely on, for buckets under store_bucket_prefix, and let store users list and abort their multipart uploads. Off by default: the role gets no new grant and the store user boundary does not change. Set it only on the box that runs the console, together with CARLOS_STORE_CONVERSION=1 on the console. Requires store_bucket_prefix."
+ description = "Let this host's role check and set the bucket settings media conversion and browser uploads rely on, for buckets under store_bucket_prefix, and let store users list and abort their multipart uploads. Off by default: the role gets no new grant and the store user boundary does not change. Set it only on the box that runs the console, together with CARLOS_STORE_CONVERSION=1 on the console. Requires a dedicated store_bucket_prefix ending in -store-, such as carlos-store-, that none of the deployment, data or sibling staging buckets start with."
type = bool
default = false
}