carlos / platform Public

fix: refuse wildcard store namespaces with managed fleets on

Task 5 fix round 1 (Astra R1 P1-1, P2-1; Fable P3-1, P3-9).

- With managed fleets on and object storage on, store_bucket_prefix and
  store_user_path must be literal: no *, ? or ${. IAM reads those as
  wildcards or policy variables, so "carlos-*" or "/carlos-*/" would let
  the store grant delete fleet buckets or fleet users' policies despite
  the literal disjointness checks. Object-storage-only plans unchanged.
- Group-mode refusal now has a witness: tests/managed_fleets/
  group_refusal.tftest.hcl, group inputs with the module's own ingress
  (no keyed overrides) and a control run. The managed fleet tests moved
  to tests/managed_fleets/ so they run in-tree with
  -test-directory=tests/managed_fleets, past the group suites that
  OpenTofu 1.12.5 cannot parse.
- The boundary comment states the shared ceiling. A boundary first made
  for managed fleets gets its own description; an existing store
  boundary keeps its text, and ignore_changes on description stops a
  later feature switch from replacing a boundary users are bound to.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 81c84b249a5eddccdcbf61f1c2e9ee63741d0495 parent ea233b2
5 files changed, +244 −6
  • infra/modules/region-host/main.tf +15 −5
  • infra/modules/region-host/managed_fleets.tf +3 −1
  • infra/modules/region-host/tests/managed_fleets/group_refusal.tftest.hcl +140 −0
  • infra/modules/region-host/tests/{ => managed_fleets}/managed_fleets.tftest.hcl +73 −0
  • infra/modules/region-host/variables.tf +13 −0
diff --git a/infra/modules/region-host/main.tf b/infra/modules/region-host/main.tf
index 535ebb02c..5ebe466f0 100644
--- a/infra/modules/region-host/main.tf
+++ b/infra/modules/region-host/main.tf
@@ -684,8 +684,12 @@ data "aws_caller_identity" "current" {}
# The ceiling. Every store user is created under this boundary (objstore's
# EnsureUser stamps it, and the role policy below REFUSES CreateUser without
-# it), so a store credential can never do more than S3 on this deployment's
-# own store buckets — no matter what inline policy the console writes.
+# it), so a store credential can never do more than this boundary's S3 grants,
+# whatever inline policy the console writes: on the store buckets, and, when
+# managed fleets are also on, the fleet data actions on the fleet buckets
+# too, because the ceiling is shared with managed fleet users. What a store
+# credential can actually do is its inline policy within that ceiling, which
+# objstore writes for its one bucket only.
#
# This is the answer to the escalation the grant would otherwise open: a role
# that may CreateUser and PutUserPolicy can mint a principal wider than
@@ -696,8 +700,10 @@ data "aws_caller_identity" "current" {}
# so it exists when either feature is on. Its statements are concatenated:
# the object-storage pair only with a store prefix, the fleet statements only
# with managed fleets, so an object-storage-only deployment renders it byte
-# for byte as before. Name and description stay fixed: changing either
-# replaces a policy every existing store user is bound to.
+# for byte as before. The name stays fixed, and the description is set only
+# at creation: IAM cannot change a policy's description in place, so a change
+# would replace a policy every existing user is bound to. An existing store
+# boundary keeps its text; one first created for managed fleets says so.
locals {
store_boundary_statements = [
{
@@ -716,7 +722,7 @@ locals {
resource "aws_iam_policy" "store_user_boundary" {
count = var.store_bucket_prefix != "" || var.managed_fleets_enabled ? 1 : 0
name = "${var.iam_name}-store-boundary"
- description = "Permissions ceiling for CARLOS object-store users. Intersected with each user's inline policy, which narrows it to that user's one bucket."
+ description = var.store_bucket_prefix != "" ? "Permissions ceiling for CARLOS object-store users. Intersected with each user's inline policy, which narrows it to that user's one bucket." : "Permissions ceiling for CARLOS managed fleet users, and for object-store users if object storage is switched on. Intersected with each user's inline policy, which narrows it to that user's one bucket."
policy = jsonencode({
Version = "2012-10-17"
Statement = concat(
@@ -724,6 +730,10 @@ resource "aws_iam_policy" "store_user_boundary" {
[for st in local.managed_boundary_statements : st if var.managed_fleets_enabled],
)
})
+ lifecycle {
+ # Switching the other feature on later must not replace the boundary.
+ ignore_changes = [description]
+ }
}
resource "aws_iam_role_policy" "host_store" {
diff --git a/infra/modules/region-host/managed_fleets.tf b/infra/modules/region-host/managed_fleets.tf
index 53da2f65c..7c9f2294c 100644
--- a/infra/modules/region-host/managed_fleets.tf
+++ b/infra/modules/region-host/managed_fleets.tf
@@ -20,7 +20,9 @@
#
# The rendered documents are pinned in internal/awsmanaged/testdata
# (managed-console-policy.json, managed-boundary-policy.json) by
-# tests/managed_fleets.tftest.hcl, and their shape by
+# tests/managed_fleets/managed_fleets.tftest.hcl (run it with
+# -test-directory=tests/managed_fleets; see group_refusal.tftest.hcl there),
+# and their shape by
# TestManagedFleetRenderedPolicyIsolation. Neither is a live IAM verdict.
#
# Read scope, stated plainly: host_buckets already allows ec2:DescribeInstances
diff --git a/infra/modules/region-host/tests/managed_fleets/group_refusal.tftest.hcl b/infra/modules/region-host/tests/managed_fleets/group_refusal.tftest.hcl
new file mode 100644
index 000000000..b8610ff55
--- /dev/null
+++ b/infra/modules/region-host/tests/managed_fleets/group_refusal.tftest.hcl
@@ -0,0 +1,140 @@
+# Managed fleets are refused in group mode (variables.tf
+# managed_fleets_enabled). The inputs are group_iam.tftest.hcl's, with the
+# module's own ingress instead of an existing one; the control run shows they
+# plan cleanly with managed fleets off, so the refusal run fails for exactly
+# one reason.
+#
+# Run from the module directory with this directory as the test directory,
+# which keeps tofu from parsing the group suites in tests/:
+# tofu init -backend=false -test-directory=tests/managed_fleets
+# tofu test -test-directory=tests/managed_fleets
+mock_provider "aws" {
+ mock_resource "aws_launch_template" { defaults = { id = "lt-0123456789abcdef0" } }
+ mock_resource "aws_security_group" { defaults = { id = "sg-0123456789abcdef1" } }
+ # Defaults for the ingress child module's own lookups (both group subnets
+ # are listed for every zone; each zone's check needs only its own).
+ mock_resource "aws_lb" { defaults = { arn = "arn:aws:elasticloadbalancing:eu-west-1:123456789012:loadbalancer/net/test/1234", arn_suffix = "net/test/1234" } }
+ mock_resource "aws_lb_target_group" { defaults = { arn = "arn:aws:elasticloadbalancing:eu-west-1:123456789012:targetgroup/test/1234" } }
+ mock_data "aws_vpc" { defaults = { cidr_block = "10.0.0.0/16", cidr_block_associations = [] } }
+ mock_data "aws_subnets" { defaults = { ids = ["subnet-00000000000000001", "subnet-00000000000000002"] } }
+}
+override_data {
+ target = data.aws_region.current
+ values = { region = "eu-west-1" }
+}
+override_data {
+ target = data.aws_partition.current
+ values = { partition = "aws", dns_suffix = "amazonaws.com" }
+}
+override_data {
+ target = data.aws_caller_identity.current
+ values = { account_id = "123456789012" }
+}
+override_data {
+ target = data.aws_vpc.host
+ values = { cidr_block = "10.0.0.0/16", cidr_block_associations = [] }
+}
+override_data {
+ target = data.aws_subnets.group
+ values = { ids = ["subnet-00000000000000001", "subnet-00000000000000002"] }
+}
+override_data {
+ target = data.aws_route_table.group
+ values = { routes = [{ cidr_block = "0.0.0.0/0", gateway_id = "igw-0123456789abcdef0", carrier_gateway_id = "", core_network_arn = "", destination_prefix_list_id = "", egress_only_gateway_id = "", instance_id = "", ipv6_cidr_block = "", local_gateway_id = "", nat_gateway_id = "", network_interface_id = "", odb_network_arn = "", transit_gateway_id = "", vpc_endpoint_id = "", vpc_peering_connection_id = "" }] }
+}
+override_data {
+ target = data.aws_lb.group
+ values = {
+ vpc_id = "vpc-0123456789abcdef0"
+ internal = false
+ load_balancer_type = "network"
+ ip_address_type = "ipv4"
+ enable_cross_zone_load_balancing = true
+ security_groups = ["sg-0123456789abcdef0"]
+ subnet_mapping = [
+ { subnet_id = "subnet-00000000000000001", allocation_id = "eipalloc-00000000000000001", ipv6_address = "", outpost_id = "", private_ipv4_address = "" },
+ { subnet_id = "subnet-00000000000000002", allocation_id = "eipalloc-00000000000000002", ipv6_address = "", outpost_id = "", private_ipv4_address = "" }
+ ]
+ }
+}
+override_data {
+ target = data.aws_lb_target_group.group_http
+ values = { vpc_id = "vpc-0123456789abcdef0", port = 80, protocol = "TCP", target_type = "instance", load_balancer_arns = ["arn:aws:elasticloadbalancing:eu-west-1:123456789012:loadbalancer/net/test/1234"], preserve_client_ip = "true" }
+}
+override_data {
+ target = data.aws_lb_target_group.group_https
+ values = { vpc_id = "vpc-0123456789abcdef0", port = 443, protocol = "TCP", target_type = "instance", load_balancer_arns = ["arn:aws:elasticloadbalancing:eu-west-1:123456789012:loadbalancer/net/test/1234"], preserve_client_ip = "true" }
+}
+override_data {
+ target = data.aws_ami.group
+ values = { root_device_type = "ebs", root_device_name = "/dev/xvda" }
+}
+override_resource {
+ target = aws_sqs_queue.terminations
+ values = { arn = "arn:aws:sqs:eu-west-1:123456789012:carlos-dev-ie-group-terminations", id = "https://sqs.eu-west-1.amazonaws.com/123456789012/carlos-dev-ie-group-terminations" }
+}
+override_resource {
+ target = aws_cloudwatch_event_rule.terminations
+ values = { arn = "arn:aws:events:eu-west-1:123456789012:rule/carlos-dev-ie-group-terminations" }
+}
+override_resource {
+ target = aws_iam_role.host
+ values = { id = "carlos-dev-ie-group", arn = "arn:aws:iam::123456789012:role/carlos-dev-ie-group" }
+}
+override_resource {
+ target = aws_sns_topic.alerts
+ values = { arn = "arn:aws:sns:eu-west-1:123456789012:carlos-dev-ie-group-alerts" }
+}
+variables {
+ alerts_email = "ops@example.com"
+ mode = "group"
+ name = "carlos-dev-ie-group"
+ iam_name = "carlos-dev-ie-group"
+ sg_description = "test group"
+ instance_type = "t4g.micro"
+ root_volume_size = 20
+ user_data = ""
+ deployment_bucket_arn = "arn:aws:s3:::carlos-dev-platform"
+ data_bucket_arn = "arn:aws:s3:::carlos-dev-ie"
+ ssm_parameter_arn = ""
+ vpc_id = "vpc-0123456789abcdef0"
+ box_count = 2
+ group_config = {
+ deployment = "dev"
+ region = "ie"
+ placement_region = "ie"
+ pools = ["default"]
+ ami_id = "ami-0123456789abcdef0"
+ release = "v0.100.0"
+ configuration_kms_key_arn = "arn:aws:kms:eu-west-1:123456789012:key/00000000-0000-0000-0000-000000000001"
+ }
+ group_zones = {
+ eu-west-1a = { subnet_id = "subnet-00000000000000001", allocation_id = "eipalloc-00000000000000001" }
+ eu-west-1b = { subnet_id = "subnet-00000000000000002", allocation_id = "eipalloc-00000000000000002" }
+ }
+ # The module builds its own ingress, so no keyed data.aws_eip override
+ # is needed: OpenTofu 1.12.5 refuses keyed override targets, which is why
+ # the group suites in tests/ do not run under it.
+ group_ingress = null
+}
+
+run "group_mode_plans_without_managed_fleets" {
+ command = plan
+ assert {
+ condition = length(aws_iam_role_policy.host_managed_fleets) == 0 && length(aws_iam_role_policy.group) == 1
+ error_message = "the group harness must plan a group role and no managed fleet grant"
+ }
+}
+
+run "bad_group_mode" {
+ command = plan
+ variables {
+ managed_fleets_enabled = true
+ managed_fleets_deployment = "dev"
+ managed_fleet_bucket_prefix = "carlos-fleet-"
+ managed_fleets_regions = {
+ "eu-west-1" = { image = "ami-0123456789abcdef0", subnets = ["subnet-00000000000000001"], security_groups = ["sg-0123456789abcdef9"], instance_types = ["t4g.small"] }
+ }
+ }
+ expect_failures = [var.managed_fleets_enabled]
+}
diff --git a/infra/modules/region-host/tests/managed_fleets.tftest.hcl b/infra/modules/region-host/tests/managed_fleets/managed_fleets.tftest.hcl
similarity index 91%
rename from infra/modules/region-host/tests/managed_fleets.tftest.hcl
rename to infra/modules/region-host/tests/managed_fleets/managed_fleets.tftest.hcl
index 18d8774e5..db56d347f 100644
--- a/infra/modules/region-host/tests/managed_fleets.tftest.hcl
+++ b/infra/modules/region-host/tests/managed_fleets/managed_fleets.tftest.hcl
@@ -159,6 +159,10 @@ run "managed_only" {
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"
}
+ assert {
+ condition = aws_iam_policy.store_user_boundary[0].description == "Permissions ceiling for CARLOS managed fleet users, and for object-store users if object storage is switched on. Intersected with each user's inline policy, which narrows it to that user's one bucket."
+ error_message = "a boundary first created for managed fleets says whom it bounds"
+ }
assert {
condition = alltrue([
for st in jsondecode(aws_iam_policy.store_user_boundary[0].policy).Statement :
@@ -370,6 +374,10 @@ run "policy_union" {
)
error_message = "with both features on the boundary is the unchanged app statements followed by the fleet fixture"
}
+ assert {
+ condition = aws_iam_policy.store_user_boundary[0].description == "Permissions ceiling for CARLOS object-store users. Intersected with each user's inline policy, which narrows it to that user's one bucket."
+ error_message = "an object storage boundary keeps its description when managed fleets are switched on"
+ }
# No older grant on this role already allows a new fleet mutation, and the
# ones they share with object storage stay on the disjoint store prefix and
# path.
@@ -636,3 +644,68 @@ run "bad_prefix_inside_store_prefix" {
}
expect_failures = [var.managed_fleet_bucket_prefix]
}
+
+# IAM reads *, ? and ${...} in a resource as wildcards or policy variables,
+# so a literal prefix comparison proves nothing about them. "carlos-*" would
+# give the store grant DeleteBucket on arn:aws:s3:::carlos-** (fleet buckets
+# included); "/carlos-*/" would give it iam:DeleteUserPolicy on fleet users.
+run "bad_store_prefix_star_with_managed" {
+ command = plan
+ variables {
+ managed_fleets_enabled = true
+ store_bucket_prefix = "carlos-*"
+ }
+ expect_failures = [var.store_bucket_prefix]
+}
+
+run "bad_store_prefix_question_with_managed" {
+ command = plan
+ variables {
+ managed_fleets_enabled = true
+ store_bucket_prefix = "carlos-?leet-"
+ }
+ expect_failures = [var.store_bucket_prefix]
+}
+
+run "bad_store_prefix_policy_variable_with_managed" {
+ command = plan
+ variables {
+ managed_fleets_enabled = true
+ store_bucket_prefix = "carlos-$${aws:username}"
+ }
+ expect_failures = [var.store_bucket_prefix]
+}
+
+run "bad_store_path_star_with_managed" {
+ command = plan
+ variables {
+ managed_fleets_enabled = true
+ store_bucket_prefix = "carlos-store-test-"
+ store_user_path = "/carlos-*/"
+ }
+ expect_failures = [var.store_user_path]
+}
+
+run "bad_store_path_question_with_managed" {
+ command = plan
+ variables {
+ managed_fleets_enabled = true
+ store_bucket_prefix = "carlos-store-test-"
+ store_user_path = "/carlos-?anaged-fleet/"
+ }
+ expect_failures = [var.store_user_path]
+}
+
+# Object storage alone keeps its old behaviour: the new checks apply only
+# with managed fleets on.
+run "store_only_wildcards_unchanged" {
+ command = plan
+ variables {
+ store_bucket_prefix = "carlos-*"
+ store_user_path = "/carlos-*/"
+ }
+ assert {
+ condition = length(aws_iam_role_policy.host_store) == 1 && length(aws_iam_role_policy.host_managed_fleets) == 0
+ error_message = "object storage alone must plan exactly as before"
+ }
+}
diff --git a/infra/modules/region-host/variables.tf b/infra/modules/region-host/variables.tf
index 259a094e3..7de97fd8d 100644
--- a/infra/modules/region-host/variables.tf
+++ b/infra/modules/region-host/variables.tf
@@ -253,12 +253,25 @@ variable "store_bucket_prefix" {
description = "Bucket-name prefix this deployment mints object stores under (CARLOS_STORE_BUCKET_PREFIX, e.g. \"carlos-\"). Set it and the host role gains the object-storage provisioning grant: bucket lifecycle plus the IAM user lifecycle, both scoped to this prefix and to the store user path, and the module creates the permissions boundary those users are made under. Empty (the default): no store grant and no boundary, which is every deployment that has not switched object storage on."
type = string
default = ""
+ # Only with managed fleets on: an IAM wildcard or policy variable here (or
+ # in store_user_path) would reach fleet buckets or users through the store
+ # grant, whatever the literal comparisons in managed_fleet_bucket_prefix and
+ # managed_fleet_user_path say. The managed prefix and path already admit no
+ # such character. Object-storage-only plans are unchanged.
+ validation {
+ condition = !var.managed_fleets_enabled || var.store_bucket_prefix == "" || !can(regex("[*?]|\\$\\{", var.store_bucket_prefix))
+ error_message = "With managed fleets on, store_bucket_prefix must be a literal prefix: no *, ? or $${ (IAM reads them as wildcards or policy variables, which could reach fleet buckets)."
+ }
}
variable "store_user_path" {
description = "IAM path every store principal lives under (objstore.UserPath). It is what scopes the role's user-lifecycle grant to store users and nothing else, so it must match the platform constant exactly — change it here without changing it there and the console can create a user it then cannot give a key to."
type = string
default = "/carlos-store/"
+ validation {
+ condition = !var.managed_fleets_enabled || var.store_bucket_prefix == "" || !can(regex("[*?]|\\$\\{", var.store_user_path))
+ error_message = "With managed fleets on, store_user_path must be a literal IAM path: no *, ? or $${ (IAM reads them as wildcards or policy variables, which could reach fleet users)."
+ }
}
variable "store_scanning" {