Scope optional console IAM grants to analytics version pruning
Keep the grant off by default and reject it in group mode. Allow version listing under apps/ and version deletion only under analytics days in the deployment bucket. Keep data-bucket permissions unchanged.
4 files changed,
+178
−1
infra/modules/region-host/main.tf+36 −1infra/modules/region-host/tests/analytics_prune.tftest.hcl+131 −0infra/modules/region-host/tests/group.tftest.hcl+5 −0infra/modules/region-host/variables.tf+6 −0
Intent
Attributed to this commit: 5.66 g CO₂e (608,638 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on stats-prune-400, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/infra/modules/region-host/main.tf b/infra/modules/region-host/main.tf| index 0f9e36518..abf3fb8c5 100644 |
| --- a/infra/modules/region-host/main.tf |
| +++ b/infra/modules/region-host/main.tf |
| @@ -146,7 +146,7 @@ resource "aws_iam_role" "host" { |
| var.root_volume_size >= 1 && floor(var.root_volume_size) == var.root_volume_size && |
| var.user_data == "" && var.ssm_parameter_arn == "" && |
| (var.group_ingress == null || alltrue([for zone in var.group_zones : zone.allocation_id != null])) && |
| - !var.relay && length(var.extra_ingress) == 0 && !var.enable_cost_explorer && |
| + !var.relay && length(var.extra_ingress) == 0 && !var.enable_cost_explorer && !var.enable_analytics_version_prune && |
| var.console_mail_from == "" && var.console_mail_region == "" && |
| var.store_bucket_prefix == "" && !var.store_scanning && |
| var.scope_ssm_parameter_reads && var.deny_release_key_reads && |
| @@ -446,6 +446,41 @@ resource "aws_iam_role_policy" "host_costexplorer" { |
| }) |
| } |
| +# Analytics version prune (the console's daily 400-day statistics prune, |
| +# internal/analytics): the deployment bucket is versioned and every day file |
| +# is rewritten on each usage flush, so deleting an expired day file for good |
| +# means deleting each of its versions and delete markers. Off by default: |
| +# only the box that runs the console needs it. Version listing is held to |
| +# the apps/ prefix and version deletion to exactly the analytics day files, |
| +# apps/<acct>/<app>/analytics/<box>/days/<file>; nothing else in the |
| +# deployment bucket loses a version through this grant, and the data 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" { |
| + 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 = [ |
| + { |
| + Sid = "AnalyticsDayVersionList" |
| + Effect = "Allow" |
| + Action = ["s3:ListBucketVersions"] |
| + Resource = [var.deployment_bucket_arn] |
| + Condition = { StringLike = { "s3:prefix" = "apps/*" } } |
| + }, |
| + { |
| + Sid = "AnalyticsDayVersionDelete" |
| + Effect = "Allow" |
| + Action = ["s3:DeleteObjectVersion"] |
| + Resource = ["${var.deployment_bucket_arn}/apps/*/*/analytics/*/days/*"] |
| + }, |
| + ] |
| + }) |
| +} |
| + |
| # 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 |
diff --git a/infra/modules/region-host/tests/analytics_prune.tftest.hcl b/infra/modules/region-host/tests/analytics_prune.tftest.hcl| new file mode 100644 |
| index 000000000..568d99bbe |
| --- /dev/null |
| +++ b/infra/modules/region-host/tests/analytics_prune.tftest.hcl |
| @@ -0,0 +1,131 @@ |
| +# Plan-time tests for the console's analytics version-prune grant |
| +# (aws_iam_role_policy.host_analytics_prune). mock_provider means no AWS |
| +# account and no credentials: these assert what the module WOULD create. |
| +mock_provider "aws" {} |
| + |
| +override_data { |
| + target = data.aws_vpcs.default |
| + values = { ids = ["vpc-0aaaaaaaaaaaaaaaa"] } |
| +} |
| + |
| +override_data { |
| + target = data.aws_caller_identity.current |
| + values = { account_id = "123456789012" } |
| +} |
| + |
| +# The termination queue's ARN feeds an EventBridge target, which the provider |
| +# validates as an ARN; the mock's random string is not one. |
| +override_resource { |
| + target = aws_sqs_queue.terminations |
| + values = { arn = "arn:aws:sqs:eu-west-1:123456789012:carlos-test-host-terminations" } |
| +} |
| + |
| +variables { |
| + name = "carlos-test-ie" |
| + sg_description = "carlos test host" |
| + iam_name = "carlos-test-host" |
| + instance_type = "t4g.small" |
| + root_volume_size = 20 |
| + user_data = "#!/bin/sh\n" |
| + 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/*" |
| +} |
| + |
| +run "off_by_default" { |
| + command = plan |
| + assert { |
| + condition = length(aws_iam_role_policy.host_analytics_prune) == 0 |
| + error_message = "enable_analytics_version_prune defaults to false and must create no policy" |
| + } |
| + # Off, the host's bucket policy still grants no version action on the |
| + # deployment bucket at all. |
| + assert { |
| + condition = alltrue([ |
| + for s in jsondecode(aws_iam_role_policy.host_buckets.policy).Statement : |
| + !(contains(flatten([s.Action]), "s3:ListBucketVersions") || contains(flatten([s.Action]), "s3:DeleteObjectVersion")) || |
| + !anytrue([for r in flatten([s.Resource]) : startswith(r, "arn:aws:s3:::carlos-test-platform")]) |
| + ]) |
| + error_message = "with the prune grant off, no deployment-bucket version action may appear in host_buckets" |
| + } |
| +} |
| + |
| +run "on_grants_exactly_the_analytics_day_versions" { |
| + command = plan |
| + variables { |
| + enable_analytics_version_prune = true |
| + } |
| + assert { |
| + condition = length(aws_iam_role_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" |
| + error_message = "the policy is named <iam_name>-analytics-prune" |
| + } |
| + # 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([ |
| + { |
| + Action = ["s3:ListBucketVersions"] |
| + Condition = { StringLike = { "s3:prefix" = "apps/*" } } |
| + Effect = "Allow" |
| + Resource = ["arn:aws:s3:::carlos-test-platform"] |
| + Sid = "AnalyticsDayVersionList" |
| + }, |
| + { |
| + Action = ["s3:DeleteObjectVersion"] |
| + Effect = "Allow" |
| + Resource = ["arn:aws:s3:::carlos-test-platform/apps/*/*/analytics/*/days/*"] |
| + Sid = "AnalyticsDayVersionDelete" |
| + }, |
| + ]) |
| + error_message = "the prune grant must be exactly ListBucketVersions (s3:prefix apps/*) on the deployment bucket and DeleteObjectVersion on apps/*/*/analytics/*/days/*" |
| + } |
| + # Nothing in it reaches the data bucket. |
| + assert { |
| + condition = alltrue([ |
| + for s in jsondecode(aws_iam_role_policy.host_analytics_prune[0].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" |
| + } |
| + # The data-bucket version grants in host_buckets are unchanged by the |
| + # switch: the same three statements, the same resources. |
| + assert { |
| + condition = jsonencode([ |
| + for s in jsondecode(aws_iam_role_policy.host_buckets.policy).Statement : s |
| + if contains(flatten([s.Action]), "s3:GetObjectVersion") || contains(flatten([s.Action]), "s3:DeleteObjectVersion") || contains(flatten([s.Action]), "s3:ListBucketVersions") |
| + ]) == jsonencode([ |
| + { |
| + Action = ["s3:GetObjectVersion"] |
| + Effect = "Allow" |
| + Resource = ["arn:aws:s3:::carlos-test-ie/*"] |
| + Sid = "PullDoorObjectVersions" |
| + }, |
| + { |
| + Action = ["s3:DeleteObjectVersion"] |
| + Effect = "Allow" |
| + Resource = ["arn:aws:s3:::carlos-test-ie/*"] |
| + }, |
| + { |
| + Action = ["s3:ListBucketVersions"] |
| + Effect = "Allow" |
| + Resource = ["arn:aws:s3:::carlos-test-ie"] |
| + Sid = "PullDoorVersionList" |
| + }, |
| + ]) |
| + error_message = "the data bucket's version grants in host_buckets must be unchanged with the prune grant on" |
| + } |
| + # And the object grants on both buckets are the same as ever. |
| + assert { |
| + condition = jsonencode(jsondecode(aws_iam_role_policy.host_buckets.policy).Statement[0]) == jsonencode({ |
| + Action = ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"] |
| + Effect = "Allow" |
| + Resource = ["arn:aws:s3:::carlos-test-platform/*", "arn:aws:s3:::carlos-test-ie/*"] |
| + }) |
| + error_message = "the bucket object grants must be unchanged with the prune grant on" |
| + } |
| +} |
diff --git a/infra/modules/region-host/tests/group.tftest.hcl b/infra/modules/region-host/tests/group.tftest.hcl| index 15533596c..80562c839 100644 |
| --- a/infra/modules/region-host/tests/group.tftest.hcl |
| +++ b/infra/modules/region-host/tests/group.tftest.hcl |
| @@ -241,6 +241,11 @@ run "invalid_group_console" { |
| variables { enable_cost_explorer = true } |
| expect_failures = [aws_iam_role.host] |
| } |
| +run "invalid_group_analytics_prune" { |
| + command = plan |
| + variables { enable_analytics_version_prune = true } |
| + expect_failures = [aws_iam_role.host] |
| +} |
| run "invalid_group_relay" { |
| command = plan |
| variables { relay = true } |
diff --git a/infra/modules/region-host/variables.tf b/infra/modules/region-host/variables.tf| index fd0bff04e..106195f32 100644 |
| --- a/infra/modules/region-host/variables.tf |
| +++ b/infra/modules/region-host/variables.tf |
| @@ -206,6 +206,12 @@ variable "enable_cost_explorer" { |
| default = false |
| } |
| +variable "enable_analytics_version_prune" { |
| + description = "Let this host's role list object versions under apps/ in the deployment bucket and delete versions of analytics day files only (apps/*/*/analytics/*/days/*), so the console's 400-day statistics prune removes every version of an expired day file. Off by default. Set it only on the box that runs the console, in fixed mode, before switching CONSOLE_ANALYTICS_PRUNE on." |
| + type = bool |
| + default = false |
| +} |
| + |
| variable "alerts_email" { |
| description = "Email address for a regional alerts topic created by this module. Leave empty when using alerts_topic_arn. If both inputs are empty, alarm notifications are disabled." |
| type = string |