carlos / platform Public

infra: keep conversion off fleet buckets, move the fleet grant to managed

With store_conversion on, refuse a store_bucket_prefix that overlaps a
non-empty managed_fleet_bucket_prefix in either direction, whatever
managed_fleets_enabled says: fleet buckets outlive that switch. The rule
sits with the other conversion prefix refusals.

host_managed_fleets becomes a customer-managed policy attached to the
console role, with the same statements. A temporary
managed_fleets_inline_retired input (default false) keeps the old inline
copy beside it, so the first apply only adds; a second apply with it true
removes the copy. Conversion with fleets on requires it. The inline total
counts the copy while it exists, and a plan-time check holds the managed
fleet policy within 6,144 characters.

The complete console role now totals 5,830 inline characters. The plan's
rollout gains a read-only role check before R1 and the two-apply
retirement.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 78945add5ce905f6037cc61d61cb4fc25a1fb103 parent 86093e1
8 files changed, +319 −80
  • docs/superpowers/notes/media-conversion-copy-deck.md +6 −0
  • docs/superpowers/plans/2026-11-16-media-conversion-plan-3-engine.md +8 −3
  • infra/modules/region-host/README.md +12 −1
  • infra/modules/region-host/main.tf +13 −0
  • infra/modules/region-host/managed_fleets.tf +33 −4
  • infra/modules/region-host/tests/managed_fleets/managed_fleets.tftest.hcl +183 −72
  • infra/modules/region-host/tests/store_conversion.tftest.hcl +57 −0
  • infra/modules/region-host/variables.tf +7 −0
diff --git a/docs/superpowers/notes/media-conversion-copy-deck.md b/docs/superpowers/notes/media-conversion-copy-deck.md
index c9c5b55ab..d66cb9511 100644
--- a/docs/superpowers/notes/media-conversion-copy-deck.md
+++ b/docs/superpowers/notes/media-conversion-copy-deck.md
@@ -723,4 +723,10 @@ refusal, for whoever deploys it.
| `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.region_host.store_conversion.refused_fleet_overlap` | `store_conversion refuses a store_bucket_prefix that overlaps managed_fleet_bucket_prefix, even with managed fleets off, because fleet buckets outlive the switch and this grant can replace the CORS, lifecycle and tag settings of every bucket it matches. Choose prefixes where neither starts with the other.` (pending Luna pass; plan 3 Task 12 fix 2) |
+| `infra.region_host.store_conversion.refused_inline_fleets` | `store_conversion with managed fleets on needs managed_fleets_inline_retired = true, so the inline copy of the managed fleet grant stops taking up the console role's limited inline policy space. Apply once with it false to attach the managed copy, then set it true and apply again.` (pending Luna pass; plan 3 Task 12 fix 2) |
+| `infra.region_host.managed_fleets.refused_size` | `The managed fleet grant exceeds IAM's 6,144-character limit for a customer-managed policy. Name fewer managed fleet regions, subnets or security groups.` (pending Luna pass; plan 3 Task 12 fix 2) |
+| `infra.region_host.managed_fleets.refused_inline_total` | `This host role's inline policies exceed IAM's 10,240-character total. Set managed_fleets_inline_retired once the managed fleet grant is attached, or turn off a grant this host does not need.` (pending Luna pass; plan 3 Task 12 fix 2) |
+| `infra.region_host.managed_fleets_inline_retired.description` | The `description` of `variable "managed_fleets_inline_retired"` in `infra/modules/region-host/variables.tf` (pending Luna pass; plan 3 Task 12 fix 2) |
+| `infra.region_host.readme.managed_fleets_move` | The `Moving the managed fleet grant` section of `infra/modules/region-host/README.md`, and the fleet prefix sentences in `Media conversion and browser uploads` (pending Luna pass; plan 3 Task 12 fix 2) |
| `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 451d685e9..783e1260f 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
@@ -270,10 +270,10 @@ Depends on plan 1 Task 3 Step 4: the muxer rules and the parser's accepted box s
**Interfaces:**
- 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.
+- 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; and it does not overlap a non-empty `managed_fleet_bucket_prefix` in either direction, whatever `managed_fleets_enabled` says, because fleet buckets outlive that switch. With managed fleets on, the switch also requires `managed_fleets_inline_retired = true`. With the switch off every prefix plans as before.
+- The console role's inline total: `host_analytics_prune` and `host_managed_fleets` become customer-managed attached policies with the same statements and the same switches, and `host_inline_policies` counts every inline policy this module puts on the role. The managed fleet grant moves without a gap: a temporary `managed_fleets_inline_retired` (default false) keeps the old inline copy beside the attached managed one until a second apply sets it true, and the total counts that copy while it exists. Plan-time refusals: the module's inline policies over 10,240 characters, and the managed fleet policy over 6,144 (it grows with each managed region). The complete console role, every feature on, fits; the module cannot see inline policies a root adds, which the pre-rollout check (Rollout, R0) covers.
-- [ ] **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 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, with a platform bucket under the prefix, or with a fleet prefix overlapping it in either direction (fleets off included) fails its precondition; on with fleets on and the inline copy not retired fails; off accepts every prefix as before, with fleets off and on; the complete console role fits IAM's quotas and reports its inline total; extra policy text over the inline total, and a managed fleet policy over 6,144 characters, 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.
@@ -312,6 +312,11 @@ Depends on plan 1 Task 3 Step 4: the muxer rules and the parser's accepted box s
Ask each time with the account, region, resources, cost, duration, traffic effect and cleanup (AGENTS.md). Dev first.
+### R0: Console role check, read only, before R1
+
+- [ ] Ask Paul for a read-only look at each console role (profile `kass` for dev, `keymail` for flagship): `aws iam list-role-policies` and `get-role-policy` for every inline policy, and `list-attached-role-policies`. Total the inline documents without whitespace, including the ones the root adds itself, which the module cannot count. Confirm that the total, after this release moves `host_analytics_prune` and retires the inline copy of `host_managed_fleets`, fits under 10,240 characters, and that the attachments stay within the role's quota. Record the numbers. Nothing is written.
+- [ ] Record the two-apply retirement for every root with managed fleets on: apply this release with `managed_fleets_inline_retired = false`, which only adds the attached managed fleet policy; confirm it is attached; then set `managed_fleets_inline_retired = true` and apply again to remove the inline copy. `store_conversion = true` comes after the second apply. Dev in R2, flagship in R6, each on Paul's go.
+
### R1: Release and image
- [ ] Ask Paul. Cut a release carrying the branch (release recipe; push the tag to the GitHub mirror). `make media-image` from the release tag; record `image.json`. Set up the weekly lock refresh the spike report chose.
diff --git a/infra/modules/region-host/README.md b/infra/modules/region-host/README.md
index ca2a4165a..b521ae44a 100644
--- a/infra/modules/region-host/README.md
+++ b/infra/modules/region-host/README.md
@@ -110,10 +110,21 @@ looks exactly like a deploy that did nothing.
`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 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, or if the prefix and `managed_fleet_bucket_prefix` overlap. That last check applies even with managed fleets off, because fleet buckets stay after the switch is turned off. With managed fleets on, the switch also needs `managed_fleets_inline_retired = true` (see below).
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.
+## Moving the managed fleet grant
+
+The managed fleet grant is a customer-managed policy attached to the console’s role, so it does not count toward IAM’s 10,240-character limit on the role’s inline policies. A role set up by an earlier release holds it as an inline policy. To move it without a moment where the role lacks the grant, apply twice:
+
+1. Upgrade and apply with `managed_fleets_inline_retired` left at `false`. The role keeps its inline copy and gains the managed one.
+2. Set `managed_fleets_inline_retired = true` and apply again. The inline copy goes.
+
+`store_conversion` with managed fleets on is refused until step 2. `managed_fleets_inline_retired` is temporary, and goes once every root has set it.
+
+A customer-managed policy holds at most 6,144 characters, which is about two managed regions; with more, the plan fails. The plan also fails if this module’s inline policies on the role pass 10,240 characters. It cannot see inline policies your root adds to the same role, so if you add any, check the role’s total yourself.
+
## Checking it worked
```sh
diff --git a/infra/modules/region-host/main.tf b/infra/modules/region-host/main.tf
index 9edd23396..e3f8660d9 100644
--- a/infra/modules/region-host/main.tf
+++ b/infra/modules/region-host/main.tf
@@ -1088,6 +1088,19 @@ resource "aws_iam_policy" "host_store_conversion" {
])
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."
}
+ # Whatever managed_fleets_enabled says: switching the fleet grant off
+ # leaves the fleet buckets it made.
+ precondition {
+ condition = var.store_bucket_prefix == "" || var.managed_fleet_bucket_prefix == "" || !(
+ startswith(var.store_bucket_prefix, var.managed_fleet_bucket_prefix) ||
+ startswith(var.managed_fleet_bucket_prefix, var.store_bucket_prefix)
+ )
+ error_message = "store_conversion refuses a store_bucket_prefix that overlaps managed_fleet_bucket_prefix, even with managed fleets off, because fleet buckets outlive the switch and this grant can replace the CORS, lifecycle and tag settings of every bucket it matches. Choose prefixes where neither starts with the other."
+ }
+ precondition {
+ condition = !var.managed_fleets_enabled || var.managed_fleets_inline_retired
+ error_message = "store_conversion with managed fleets on needs managed_fleets_inline_retired = true, so the inline copy of the managed fleet grant stops taking up the console role's limited inline policy space. Apply once with it false to attach the managed copy, then set it true and apply again."
+ }
}
}
diff --git a/infra/modules/region-host/managed_fleets.tf b/infra/modules/region-host/managed_fleets.tf
index 249180d4a..29b4938a3 100644
--- a/infra/modules/region-host/managed_fleets.tf
+++ b/infra/modules/region-host/managed_fleets.tf
@@ -296,20 +296,49 @@ locals {
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.alert_publish[*].policy, aws_iam_role_policy.group[*].policy,
+ aws_iam_role_policy.host_managed_fleets[*].policy,
)
}
-resource "aws_iam_role_policy" "host_managed_fleets" {
+# The console grant is a customer-managed policy attached to the role, so it
+# stays outside the role's inline total; it grows with each managed region.
+#
+# Moving it must not leave a live role without the grant, so the move takes
+# two applies. While managed_fleets_inline_retired is false the module keeps
+# the old inline copy and attaches the managed one beside it, so the first
+# apply only adds. Setting it true removes the inline copy. The inline total
+# counts that copy while it exists.
+resource "aws_iam_policy" "host_managed_fleets" {
count = var.managed_fleets_enabled ? 1 : 0
name = "${var.iam_name}-managed-fleets"
- role = aws_iam_role.host.id
policy = local.managed_console_policy
lifecycle {
# Fail the plan, not the middle of an apply. Unknown lengths (a policy
# naming a resource this apply creates) are checked at apply instead.
precondition {
- condition = sum(concat([length(local.managed_console_policy)], [for p in local.host_inline_policies : length(p)])) <= 10240
- error_message = "With the managed fleet grant this host role's inline policies exceed IAM's 10,240-character total. Name fewer managed fleet regions, subnets or security groups, or turn off a grant this host does not need."
+ condition = length(local.managed_console_policy) <= 6144
+ error_message = "The managed fleet grant exceeds IAM's 6,144-character limit for a customer-managed policy. Name fewer managed fleet regions, subnets or security groups."
+ }
+ # Only this module's inline policies: a root that puts its own inline
+ # policy on this role must check the role's total itself.
+ precondition {
+ condition = sum([for p in local.host_inline_policies : length(p)]) <= 10240
+ error_message = "This host role's inline policies exceed IAM's 10,240-character total. Set managed_fleets_inline_retired once the managed fleet grant is attached, or turn off a grant this host does not need."
}
}
}
+
+resource "aws_iam_role_policy_attachment" "host_managed_fleets" {
+ count = var.managed_fleets_enabled ? 1 : 0
+ role = aws_iam_role.host.name
+ policy_arn = aws_iam_policy.host_managed_fleets[0].arn
+}
+
+# The inline copy, kept until managed_fleets_inline_retired. Temporary: it
+# and the variable go once every root has retired it.
+resource "aws_iam_role_policy" "host_managed_fleets" {
+ count = var.managed_fleets_enabled && !var.managed_fleets_inline_retired ? 1 : 0
+ name = "${var.iam_name}-managed-fleets"
+ role = aws_iam_role.host.id
+ policy = local.managed_console_policy
+}
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 325c3c915..02ae53f43 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,11 @@ override_resource {
values = { arn = "arn:aws:iam::123456789012:policy/carlos-test-host-store-boundary" }
}
+override_resource {
+ target = aws_iam_policy.host_managed_fleets
+ values = { arn = "arn:aws:iam::123456789012:policy/carlos-test-host-managed-fleets" }
+}
+
override_resource {
target = aws_iam_policy.host_analytics_prune
values = { arn = "arn:aws:iam::123456789012:policy/carlos-test-host-analytics-prune" }
@@ -92,6 +97,10 @@ run "off_by_default" {
condition = length(aws_iam_role_policy.host_managed_fleets) == 0
error_message = "managed_fleets_enabled defaults to false and must create no grant"
}
+ assert {
+ condition = length(aws_iam_policy.host_managed_fleets) == 0 && length(aws_iam_role_policy_attachment.host_managed_fleets) == 0
+ error_message = "managed_fleets_enabled defaults to false and must create or attach no managed grant"
+ }
assert {
condition = length(aws_iam_policy.store_user_boundary) == 0
error_message = "with object storage and managed fleets both off there is no boundary"
@@ -158,13 +167,30 @@ run "managed_only" {
managed_fleets_enabled = true
}
assert {
- condition = length(aws_iam_role_policy.host_managed_fleets) == 1 && aws_iam_role_policy.host_managed_fleets[0].name == "carlos-test-host-managed-fleets"
+ condition = aws_iam_role_policy.host_managed_fleets[*].name == ["carlos-test-host-managed-fleets"]
error_message = "one grant, named <iam_name>-managed-fleets"
}
assert {
- condition = jsondecode(aws_iam_role_policy.host_managed_fleets[0].policy) == jsondecode(file("../../../internal/awsmanaged/testdata/managed-console-policy.json"))
+ condition = [for p in aws_iam_policy.host_managed_fleets : jsondecode(p.policy)] == [jsondecode(file("../../../internal/awsmanaged/testdata/managed-console-policy.json"))]
error_message = "the console grant differs from internal/awsmanaged/testdata/managed-console-policy.json"
}
+ # Before the inline copy is retired, the managed copy is attached beside it
+ # with the same text, so the first apply after upgrading only adds.
+ assert {
+ condition = length(aws_iam_policy.host_managed_fleets) == 1 && aws_iam_policy.host_managed_fleets[*].policy == aws_iam_role_policy.host_managed_fleets[*].policy
+ error_message = "the managed copy must hold exactly the inline copy's statements"
+ }
+ assert {
+ condition = aws_iam_policy.host_managed_fleets[*].name == ["carlos-test-host-managed-fleets"]
+ error_message = "the managed copy is named <iam_name>-managed-fleets"
+ }
+ assert {
+ condition = (
+ aws_iam_role_policy_attachment.host_managed_fleets[*].role == ["carlos-test-host"] &&
+ aws_iam_role_policy_attachment.host_managed_fleets[*].policy_arn == ["arn:aws:iam::123456789012:policy/carlos-test-host-managed-fleets"]
+ )
+ error_message = "the managed copy is attached to this host's role"
+ }
assert {
condition = length(aws_iam_policy.store_user_boundary) == 1 && jsondecode(aws_iam_policy.store_user_boundary[0].policy) == jsondecode(file("../../../internal/awsmanaged/testdata/managed-boundary-policy.json"))
error_message = "the boundary differs from internal/awsmanaged/testdata/managed-boundary-policy.json"
@@ -182,7 +208,7 @@ run "managed_only" {
}
assert {
condition = alltrue([
- for doc in [aws_iam_role_policy.host_managed_fleets[0].policy, aws_iam_policy.store_user_boundary[0].policy] :
+ for doc in concat(aws_iam_policy.host_managed_fleets[*].policy, [aws_iam_policy.store_user_boundary[0].policy]) :
alltrue([for st in jsondecode(doc).Statement : alltrue([
for r in flatten([st.Resource]) : !contains(["arn:aws:s3:::*", "arn:aws:s3:::*/*"], r)
])])
@@ -191,7 +217,7 @@ run "managed_only" {
}
assert {
condition = alltrue([
- for st in jsondecode(aws_iam_role_policy.host_managed_fleets[0].policy).Statement :
+ for st in flatten([for p in aws_iam_policy.host_managed_fleets : jsondecode(p.policy).Statement]) :
st.Sid == "ReadManagedRegions" if contains(flatten([st.Resource]), "*")
])
error_message = "only the region-bound Describe statement may name Resource *"
@@ -216,7 +242,7 @@ run "enabled_shape" {
# nonempty fleet, label and operation tags, and no other tag key.
assert {
condition = alltrue([
- for st in jsondecode(aws_iam_role_policy.host_managed_fleets[0].policy).Statement :
+ for st in flatten([for p in aws_iam_policy.host_managed_fleets : jsondecode(p.policy).Statement]) :
st.Condition.StringEquals["aws:RequestTag/carlos:deployment"] == "production" &&
st.Condition.StringLike == {
"aws:RequestTag/carlos:fleet" = "?*"
@@ -232,7 +258,7 @@ run "enabled_shape" {
# on the two resource types the launch tags.
assert {
condition = alltrue([
- for st in jsondecode(aws_iam_role_policy.host_managed_fleets[0].policy).Statement :
+ for st in flatten([for p in aws_iam_policy.host_managed_fleets : jsondecode(p.policy).Statement]) :
st.Condition.StringEquals["ec2:CreateAction"] == "RunInstances" &&
alltrue([for r in st.Resource : can(regex("^arn:aws:ec2:[a-z0-9-]+:123456789012:(instance|volume)/\\*$", r))])
if contains(st.Action, "ec2:CreateTags")
@@ -243,7 +269,7 @@ run "enabled_shape" {
# identity tags present.
assert {
condition = alltrue([
- for st in jsondecode(aws_iam_role_policy.host_managed_fleets[0].policy).Statement :
+ for st in flatten([for p in aws_iam_policy.host_managed_fleets : jsondecode(p.policy).Statement]) :
st.Condition.StringEquals == { "aws:ResourceTag/carlos:deployment" = "production" } &&
st.Condition.Null == {
"aws:ResourceTag/carlos:deployment" = "false"
@@ -259,7 +285,7 @@ run "enabled_shape" {
# (group.tf group_tags). It must lack a tag both actions require.
assert {
condition = alltrue([
- for st in jsondecode(aws_iam_role_policy.host_managed_fleets[0].policy).Statement :
+ for st in flatten([for p in aws_iam_policy.host_managed_fleets : jsondecode(p.policy).Statement]) :
!alltrue([for k, v in st.Condition.Null : contains(["carlos:deployment", "carlos:region"], trimprefix(k, "aws:ResourceTag/"))])
if contains(st.Action, "ec2:TerminateInstances")
])
@@ -269,7 +295,7 @@ run "enabled_shape" {
# RunInstances resource types that supports them.
assert {
condition = alltrue([
- for st in jsondecode(aws_iam_role_policy.host_managed_fleets[0].policy).Statement :
+ for st in flatten([for p in aws_iam_policy.host_managed_fleets : jsondecode(p.policy).Statement]) :
st.Condition.StringEquals["ec2:MetadataHttpTokens"] == "required" &&
st.Condition.NumericEquals["ec2:MetadataHttpPutResponseHopLimit"] == "1" &&
st.Condition.Null["ec2:InstanceProfile"] == "true" &&
@@ -280,7 +306,7 @@ run "enabled_shape" {
}
assert {
condition = alltrue([
- for st in jsondecode(aws_iam_role_policy.host_managed_fleets[0].policy).Statement :
+ for st in flatten([for p in aws_iam_policy.host_managed_fleets : jsondecode(p.policy).Statement]) :
!anytrue([for k in keys(try(st.Condition, {})) : true])
if contains(st.Action, "ec2:RunInstances") && anytrue([for r in st.Resource : can(regex(":(image|subnet|security-group)/", r))])
])
@@ -288,7 +314,7 @@ run "enabled_shape" {
}
assert {
condition = alltrue(flatten([
- for st in jsondecode(aws_iam_role_policy.host_managed_fleets[0].policy).Statement : [
+ for st in flatten([for p in aws_iam_policy.host_managed_fleets : jsondecode(p.policy).Statement]) : [
for a in st.Action :
!can(regex("^(iam:(CreateRole|PassRole|AttachUserPolicy|AttachRolePolicy|PutRolePolicy|PutUserPermissionsBoundary|DeleteUserPermissionsBoundary|UpdateUser|DeleteUserPolicy|CreatePolicyVersion|SetDefaultPolicyVersion|AddUserToGroup|CreateLoginProfile|UpdateLoginProfile|CreateInstanceProfile|AddRoleToInstanceProfile)|ssm:.*|ssmmessages:.*|ec2messages:.*|ec2:(DeleteTags|AssociateIamInstanceProfile|ReplaceIamInstanceProfileAssociation|ModifyInstanceAttribute|CreateSecurityGroup|AuthorizeSecurityGroupIngress|CreateSubnet|CreateVpc|CreateKeyPair|ImportKeyPair)|s3:(DeleteBucket|PutBucketPolicy|PutBucketTagging|DeleteObjectVersion|ListBucketMultipartUploads))$", a)) && !strcontains(a, "*")
]
@@ -297,7 +323,7 @@ run "enabled_shape" {
}
assert {
condition = anytrue([
- for st in jsondecode(aws_iam_role_policy.host_managed_fleets[0].policy).Statement :
+ for st in flatten([for p in aws_iam_policy.host_managed_fleets : jsondecode(p.policy).Statement]) :
st.Effect == "Allow" && contains(st.Action, "s3:CreateBucket") && contains(st.Action, "s3:TagResource") && st.Resource == ["arn:aws:s3:::carlos-fleet-*"]
])
error_message = "creating a tagged bucket needs s3:TagResource beside s3:CreateBucket, on the managed prefix"
@@ -307,7 +333,7 @@ run "enabled_shape" {
# delete: the inventory reads and lease release depend on those.
assert {
condition = anytrue([
- for st in jsondecode(aws_iam_role_policy.host_managed_fleets[0].policy).Statement :
+ for st in flatten([for p in aws_iam_policy.host_managed_fleets : jsondecode(p.policy).Statement]) :
st.Effect == "Deny" &&
toset(st.Action) == toset(["s3:CreateBucket", "s3:TagResource", "s3:PutBucketVersioning", "s3:PutEncryptionConfiguration", "s3:PutBucketPublicAccessBlock", "s3:PutLifecycleConfiguration"]) &&
toset(st.Resource) == toset(["arn:aws:s3:::carlos-test-platform", "arn:aws:s3:::carlos-test-ie"])
@@ -316,7 +342,7 @@ run "enabled_shape" {
}
assert {
condition = alltrue([
- for st in jsondecode(aws_iam_role_policy.host_managed_fleets[0].policy).Statement :
+ for st in flatten([for p in aws_iam_policy.host_managed_fleets : jsondecode(p.policy).Statement]) :
!anytrue([for a in st.Action : startswith(a, "s3:Get") || startswith(a, "s3:List") || startswith(a, "s3:DeleteObject")])
if st.Effect == "Deny"
])
@@ -331,7 +357,7 @@ run "existing_user_boundary" {
}
assert {
condition = alltrue([
- for st in jsondecode(aws_iam_role_policy.host_managed_fleets[0].policy).Statement :
+ for st in flatten([for p in aws_iam_policy.host_managed_fleets : jsondecode(p.policy).Statement]) :
st.Condition == { StringEquals = { "iam:PermissionsBoundary" = "arn:aws:iam::123456789012:policy/carlos-test-host-store-boundary" } } &&
st.Resource == ["arn:aws:iam::123456789012:user/carlos-managed-fleet/*"]
if contains(st.Action, "iam:CreateUser") || contains(st.Action, "iam:PutUserPolicy")
@@ -340,7 +366,7 @@ run "existing_user_boundary" {
}
assert {
condition = alltrue([
- for st in jsondecode(aws_iam_role_policy.host_managed_fleets[0].policy).Statement :
+ for st in flatten([for p in aws_iam_policy.host_managed_fleets : jsondecode(p.policy).Statement]) :
st.Resource == ["arn:aws:iam::123456789012:user/carlos-managed-fleet/*"]
if anytrue([for a in st.Action : startswith(a, "iam:")])
])
@@ -437,47 +463,42 @@ run "policy_union" {
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
+ ]) + sum(concat([0], [
+ for doc in aws_iam_role_policy.host_managed_fleets[*].policy : length(doc)
+ ])) <= 10240
error_message = "the host role's inline policies together exceed IAM's 10,240-character limit"
}
}
-# The same console with four managed regions is over IAM's inline total: the
-# plan refuses rather than an apply failing half-way.
-run "role_inline_limit" {
+# The managed fleet grant grows with each managed region. Past IAM's 6,144
+# characters for a customer-managed policy the plan refuses rather than an
+# apply failing half-way.
+run "managed_fleet_policy_size_limit" {
command = plan
variables {
- managed_fleets_enabled = true
+ managed_fleets_enabled = true
+ managed_fleets_inline_retired = true
managed_fleets_regions = {
- for i, r in ["eu-west-1", "eu-central-1", "us-east-1", "us-west-2"] : r => {
+ for i, r in ["eu-west-1", "eu-central-1", "us-east-1"] : r => {
image = "ami-0123456789abcdef${i}"
subnets = ["subnet-0123456789abcde${i}0", "subnet-0123456789abcde${i}1"]
security_groups = ["sg-0123456789abcde${i}0"]
instance_types = ["t4g.small", "t4g.medium"]
}
}
- store_bucket_prefix = "carlos-store-test-"
- store_scanning = 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"
}
- expect_failures = [aws_iam_role_policy.host_managed_fleets]
+ expect_failures = [aws_iam_policy.host_managed_fleets]
}
-# 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" {
+# Extra policy text, here many sibling staging buckets in the bucket grant,
+# takes the role's inline policies over IAM's 10,240-character total, and the
+# plan refuses.
+run "role_inline_limit" {
command = plan
variables {
- managed_fleets_enabled = true
+ managed_fleets_enabled = true
+ managed_fleets_inline_retired = true
managed_fleets_regions = {
"eu-west-1" = {
image = "ami-0123456789abcdef0"
@@ -498,21 +519,36 @@ run "complete_console_role_fits_iam_quotas" {
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"]
+ sibling_staging_arns = [for i in range(17) : "arn:aws:s3:::carlos-test-sibling-${i}"]
}
- 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"
+ expect_failures = [aws_iam_policy.host_managed_fleets]
+}
+
+# The first apply after upgrading: the inline copy is still there, and the
+# module's total counts it.
+run "complete_console_role_counts_the_inline_copy" {
+ 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-"
+ store_scanning = 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 local.host_inline_policies : length(doc)]) == sum([
@@ -525,25 +561,60 @@ run "complete_console_role_fits_iam_quotas" {
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"
+ ]) + sum(concat([0], [
+ for doc in aws_iam_role_policy.host_managed_fleets[*].policy : length(doc)
+ ]))
+ error_message = "while the inline copy of the managed fleet grant exists, the module's total must count it"
+ }
+}
+
+# Conversion with fleets on needs the inline copy retired first.
+run "conversion_with_fleets_needs_the_inline_copy_retired" {
+ 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-"
+ store_conversion = true
+ }
+ expect_failures = [aws_iam_policy.host_store_conversion]
+}
+
+# The second apply: the inline copy goes and the attached managed copy stays.
+run "retiring_the_inline_copy" {
+ command = plan
+ variables {
+ managed_fleets_enabled = true
+ managed_fleets_inline_retired = true
}
- # 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"
+ condition = length(aws_iam_role_policy.host_managed_fleets) == 0
+ error_message = "managed_fleets_inline_retired = true removes the inline copy"
+ }
+ assert {
+ condition = (
+ [for p in aws_iam_policy.host_managed_fleets : jsondecode(p.policy)] == [jsondecode(file("../../../internal/awsmanaged/testdata/managed-console-policy.json"))] &&
+ aws_iam_role_policy_attachment.host_managed_fleets[*].policy_arn == ["arn:aws:iam::123456789012:policy/carlos-test-host-managed-fleets"]
+ )
+ error_message = "the attached managed copy still grants exactly the fixture"
}
}
-# 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" {
+# 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
+ managed_fleets_inline_retired = true
+ managed_fleets_enabled = true
managed_fleets_regions = {
"eu-west-1" = {
image = "ami-0123456789abcdef0"
@@ -552,8 +623,6 @@ run "complete_console_role_with_long_names_is_refused_at_plan" {
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
@@ -567,7 +636,45 @@ run "complete_console_role_with_long_names_is_refused_at_plan" {
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]
+ 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,
+ ] : 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"
+ }
+ assert {
+ condition = length(aws_iam_role_policy.host_managed_fleets) == 0 && length(aws_iam_role_policy_attachment.host_managed_fleets) == 1
+ error_message = "with the inline copy retired only the attached managed copy grants managed fleets"
+ }
+ # IAM caps a customer-managed policy at 6,144 characters.
+ assert {
+ condition = alltrue([
+ for doc in concat(aws_iam_policy.host_managed_fleets[*].policy, 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_managed_fleets) == 1 && 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"
+ }
}
# Fleet users share the boundary. The conversion switch adds the two multipart
@@ -575,7 +682,8 @@ run "complete_console_role_with_long_names_is_refused_at_plan" {
run "store_conversion_leaves_the_fleet_boundary_alone" {
command = plan
variables {
- managed_fleets_enabled = true
+ managed_fleets_inline_retired = true
+ managed_fleets_enabled = true
managed_fleets_regions = {
"eu-west-1" = {
image = "ami-0123456789abcdef0"
@@ -604,8 +712,9 @@ run "store_conversion_leaves_the_fleet_boundary_alone" {
run "conversion_refuses_a_broad_prefix_with_fleets_on" {
command = plan
variables {
- managed_fleets_enabled = true
- managed_fleet_bucket_prefix = "fleets-test-"
+ managed_fleets_inline_retired = true
+ managed_fleets_enabled = true
+ managed_fleet_bucket_prefix = "fleets-test-"
managed_fleets_regions = {
"eu-west-1" = {
image = "ami-0123456789abcdef0"
@@ -625,7 +734,8 @@ run "conversion_refuses_a_broad_prefix_with_fleets_on" {
run "conversion_refuses_a_prefix_not_ending_in_store_with_fleets_on" {
command = plan
variables {
- managed_fleets_enabled = true
+ managed_fleets_inline_retired = true
+ managed_fleets_enabled = true
managed_fleets_regions = {
"eu-west-1" = {
image = "ami-0123456789abcdef0"
@@ -643,7 +753,8 @@ run "conversion_refuses_a_prefix_not_ending_in_store_with_fleets_on" {
run "conversion_refuses_a_deployment_bucket_under_the_prefix_with_fleets_on" {
command = plan
variables {
- managed_fleets_enabled = true
+ managed_fleets_inline_retired = true
+ managed_fleets_enabled = true
managed_fleets_regions = {
"eu-west-1" = {
image = "ami-0123456789abcdef0"
diff --git a/infra/modules/region-host/tests/store_conversion.tftest.hcl b/infra/modules/region-host/tests/store_conversion.tftest.hcl
index 52383e62c..d538d602d 100644
--- a/infra/modules/region-host/tests/store_conversion.tftest.hcl
+++ b/infra/modules/region-host/tests/store_conversion.tftest.hcl
@@ -303,7 +303,64 @@ run "refuses_a_sibling_staging_bucket_under_the_prefix" {
expect_failures = [aws_iam_policy.host_store_conversion]
}
+# Fleet buckets outlive the fleet switch, so a fleet prefix that overlaps the
+# store prefix is refused with managed fleets off too, in either direction.
+run "refuses_a_fleet_prefix_equal_to_the_store_prefix_with_fleets_off" {
+ command = plan
+ variables {
+ store_conversion = true
+ store_bucket_prefix = "carlos-store-"
+ managed_fleet_bucket_prefix = "carlos-store-"
+ }
+ expect_failures = [aws_iam_policy.host_store_conversion]
+}
+
+run "refuses_a_fleet_prefix_inside_the_store_prefix_with_fleets_off" {
+ command = plan
+ variables {
+ store_conversion = true
+ store_bucket_prefix = "carlos-store-"
+ managed_fleet_bucket_prefix = "carlos-store-fleet-"
+ }
+ expect_failures = [aws_iam_policy.host_store_conversion]
+}
+
+run "refuses_a_store_prefix_inside_the_fleet_prefix_with_fleets_off" {
+ command = plan
+ variables {
+ store_conversion = true
+ store_bucket_prefix = "carlos-fleet-media-store-"
+ managed_fleet_bucket_prefix = "carlos-fleet-"
+ }
+ expect_failures = [aws_iam_policy.host_store_conversion]
+}
+
+run "accepts_separate_fleet_and_store_prefixes_with_fleets_off" {
+ command = plan
+ variables {
+ store_conversion = true
+ store_bucket_prefix = "carlos-store-"
+ managed_fleet_bucket_prefix = "carlos-fleet-"
+ }
+ assert {
+ condition = length(aws_iam_policy.host_store_conversion) == 1
+ error_message = "a fleet prefix that does not overlap the store prefix is accepted"
+ }
+}
+
# Off, the prefix rules do not apply: every prefix plans as it did before.
+run "off_accepts_an_overlapping_fleet_prefix" {
+ command = plan
+ variables {
+ store_bucket_prefix = "carlos-store-"
+ managed_fleet_bucket_prefix = "carlos-store-"
+ }
+ assert {
+ condition = length(aws_iam_policy.host_store_conversion) == 0 && length(aws_iam_role_policy.host_store) == 1
+ error_message = "with the switch and managed fleets off an overlapping fleet prefix plans as it did before"
+ }
+}
+
run "off_accepts_a_broad_prefix" {
command = plan
variables {
diff --git a/infra/modules/region-host/variables.tf b/infra/modules/region-host/variables.tf
index 4d7c7e585..b45b42c51 100644
--- a/infra/modules/region-host/variables.tf
+++ b/infra/modules/region-host/variables.tf
@@ -501,6 +501,13 @@ variable "managed_fleets_regions" {
}
}
+variable "managed_fleets_inline_retired" {
+ description = "Temporary, for moving the managed fleet grant to a customer-managed policy without a gap. Leave it false for the first apply after upgrading: the role keeps its inline copy of the grant and gains the managed one. Then set it true and apply again to remove the inline copy. store_conversion with managed fleets on requires it to be true. This variable goes once every root has set it."
+ type = bool
+ default = false
+ nullable = false
+}
+
variable "managed_fleet_bucket_prefix" {
description = "Bucket-name prefix reserved for managed fleet buckets (each bucket is this prefix plus 24 hex characters), the console's fleet bucket prefix setting. The grant creates and configures buckets under it only. It must not cover the deployment or data buckets and must not overlap store_bucket_prefix. Required when managed_fleets_enabled is true."
type = string