Skip to content

fix(ecs-service): make /tmp writable for non-root apps, add grace period and ECS Exec - #184

Merged
Alexanderamiri merged 1 commit into
mainfrom
fix/ecs-tmp-writable
Oct 31, 2026
Merged

Alexanderamiri merged 1 commit into
mainfrom
fix/ecs-tmp-writable

Conversation

@Alexanderamiri

Copy link
Copy Markdown
Member

Problem

  • /tmp is not writable (javazoneweb is down). Every app container runs as uid 1000 with readonlyRootFilesystem = true. The only writable path is the empty task volume tmp at /tmp. Fargate creates that volume owned by root with mode 0755, so a non-root user cannot write to it. Live log from javazoneweb-javazone-2027 (nginx-unprivileged): nginx: [emerg] mkdir() "/tmp/proxy_temp" failed (13: Permission denied). Changing compute.user would not help, because the volume belongs to root whatever the user is.
  • New tasks get almost no time to start. The target group checks every 10 s and marks a target unhealthy after 2 failures, and the service had no health_check_grace_period_seconds. A task therefore had about 20-30 s to become healthy, which is too short for JVM apps.
  • Developers cannot shell into a task. ECS Exec was not enabled anywhere.

Change

  • modules/ecs-service
    • New init container platform_tmp_init. It is non-essential, runs as root with a read-only root filesystem, runs chmod 1777 /tmp on the shared volume and exits. The image is busybox 1.37 from public.ecr.aws, pinned by digest (tmp_init_image).
    • The app container dependsOn the init container with condition SUCCESS.
    • The init container's name contains underscores, which app names cannot (NAME_RE), so the names can never clash.
    • container_user now has a validation that rejects 0 and root (also as 0:x / root:x).
    • New health_check_grace_period_seconds (default 120). It is set only when there is a target group, because AWS rejects it without a load balancer.
    • New enable_execute_command (default false).
  • Generator
    • New compute.health_check_grace_period (whole seconds, 0-3600, default 120).
    • New compute.exec (bool). It sets enable_execute_command and adds an ecs-exec task-role policy with the four ssmmessages:*Channel actions.
    • compute.exec requires readonly_root_filesystem: false, because AWS does not support ECS Exec with a read-only root filesystem.
    • compute.user is validated: name, uid, user:group or uid:gid. Root is rejected. The default stays "1000" and compute.user overrides it, as before; this is now documented.
  • team-provisioner
    • The javabin-team-{team} inline policy gets AllowExecOnPlatformCluster, which allows ecs:ExecuteCommand on the shared javabin-platform cluster. The cluster is tagged team=shared, so the existing tag-scoped allow did not cover it.
    • The task side is still only allowed through AllowTeamResources (the task's propagated team tag).
    • The rendered guardrails (DenyXTeam) still deny other teams' tasks, so a member can exec only into their own team's tasks.
    • No human-applied change (terraform/org, boundary) is needed.
  • monitoring
    • New Security Hub automation rule(s) that suppress ECS.20 on task-definition/{team}-* for registered teams (one rule per 20 teams, the per-criterion limit).
    • ECS.20 fails any task definition that has a root container. Without the rule, every app's task definition would flip from PASSED to FAILED.
    • The legacy CDK task definitions keep their findings.
  • Docs: app-yaml-reference.md (compute.user, /tmp, grace period, exec), reusable-modules.md, platform-modules.md, lambda-functions.md.

Checks behind the design

  • Networking: the platform VPC javabin-vpc has a NAT gateway (nat-0e9cc9e27cc6598db). Its only VPC endpoint is GuardDuty, and there are no ECR interface endpoints (that is documented as intentional). Tasks therefore reach public.ecr.aws through NAT. Public ECR pulls are anonymous, so the per-team execution role needs no new permissions.
  • Guardrails:
    • Nothing in the developer boundary, the apply gate or plan-review inspects container definitions, so a root init container or a second container is not rejected.
    • ECS.20 would flag it; this PR suppresses that finding as described above.
    • ECS.5 (read-only root filesystem) and ECS.4 (privileged) still pass for the init container.
  • Live task definitions: all five platform services (testteam-platform-test-app, cupcake-cupcake-backend, cupcake-cupcake, jzwatch-jz-watch, javazoneweb-javazone-2027) have one container with user "1000", readonlyRootFilesystem true, the tmp host volume at /tmp, enableExecuteCommand false and grace period 0. This change matches that shape and adds the init container.

Verification

  • python3 -m unittest discover -s scripts/tests: 142 tests OK. New ComputeTest covers:
    • the default user and its override
    • root and malformed users rejected
    • grace period default, override, range check, and omission for workers
    • exec off by default; on, it adds the policy
    • exec rejected with a read-only root filesystem
  • python3 -m unittest discover -s terraform/lambda-src/tests: 219 tests OK. The team permission set test now checks the exec statement and the tag-scoped allow.
  • terraform fmt -check -recursive and terraform validate pass for terraform/platform and modules/ecs-service.
  • Ran terraform plan (read-only) on a scratch root that calls ecs-service:
    • the rendered container definitions have the init container (user 0, chmod 1777 /tmp, same mount) and the app container with dependsOn SUCCESS
    • with no target group, the grace period is omitted
    • container_user = "root:0" fails validation
  • Got the busybox digest from public.ecr.aws/v2/docker/library/busybox/manifests/1.37 (multi-arch index).

Follow-ups

  • javazoneweb: comes back up once this is merged and the app regenerates and applies. Its next tf-apply creates a new task definition revision.
  • javabin-cli: a javabin exec wrapper around aws ecs execute-command is not in this PR.
  • app-template: its app.yaml and CLAUDE.md comments could mention health_check_grace_period and exec.

🤖 Generated with Claude Code

…iod and ECS Exec

Fargate creates the empty "tmp" task volume root-owned with mode 0755, so an
app running as the default uid 1000 cannot write to /tmp (javazoneweb's nginx
fails with mkdir() "/tmp/proxy_temp" ... Permission denied). A non-essential
init container (busybox pinned by digest, root, read-only rootfs) now runs
chmod 1777 /tmp, and the app container depends on it with condition SUCCESS.
ECS.20 is suppressed for team task definitions; root app users are rejected
in the generator and the module.

Also:
- compute.health_check_grace_period (default 120s) on services with a target group
- compute.exec turns on ECS Exec and grants the task role ssmmessages channels;
  team permission sets may ecs:ExecuteCommand on the shared cluster, tasks
  stay limited to the team's own tag

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Alexanderamiri
Alexanderamiri requested a review from a team as a code owner October 31, 2026 12:08
@github-actions

Copy link
Copy Markdown

Terraform Plan

🚧 Changes detected — Plan: 1 to add, 1 to change, 0 to destroy.

Plan output
aws_ssm_parameter.atlassian_api_key: Refreshing state... [id=/javabin/platform/atlassian-api-key]
aws_ssm_parameter.atlassian_org_id: Refreshing state... [id=/javabin/platform/atlassian-org-id]

Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  + create
  ~ update in-place

Terraform will perform the following actions:

  # module.lambdas.aws_lambda_function.team_provisioner will be updated in-place
  ~ resource "aws_lambda_function" "team_provisioner" {
        id                             = "javabin-team-provisioner"
      ~ last_modified                  = "2026-10-26T14:09:21.000+0000" -> (known after apply)
      ~ source_code_hash               = "i+0n0A3gn0SSOJWZx1C2Aq4/eSSczvrK8hXWYOqCpqs=" -> "3I4td1T2ENiRLUUEjeBLPinRaiXc/sZrwOT+Ai83lOY="
        tags                           = {}
        # (21 unchanged attributes hidden)

        # (4 unchanged blocks hidden)
    }

  # module.monitoring.aws_securityhub_automation_rule.ecs_tmp_init_root[0] will be created
  + resource "aws_securityhub_automation_rule" "ecs_tmp_init_root" {
      + arn         = (known after apply)
      + description = "Platform /tmp init container in app task definitions runs as root (accepted)."
      + id          = (known after apply)
      + is_terminal = false
      + rule_name   = "javabin-accepted-ecs-tmp-init-root-0"
      + rule_order  = 3
      + rule_status = "ENABLED"
      + tags_all    = {
          + "environment" = "production"
          + "managed-by"  = "terraform"
          + "repo"        = "javaBin/platform"
          + "service"     = "platform"
          + "team"        = "platform"
        }

      + actions {
          + type = "FINDING_FIELDS_UPDATE"

          + finding_fields_update {
              + note {
                  + text       = "Accepted: the platform_tmp_init container runs as root only to chmod 1777 the /tmp volume; the app container runs as a non-root user (enforced by the generator)."
                  + updated_by = "javabin-platform-terraform"
                }
              + workflow {
                  + status = "SUPPRESSED"
                }
            }
        }

      + criteria {
          + compliance_security_control_id {
              + comparison = "EQUALS"
              + value      = "ECS.20"
            }
          + resource_id {
              + comparison = "PREFIX"
              + value      = "arn:aws:ecs:eu-central-1:553637109631:task-definition/cupcake-"
            }
          + resource_id {
              + comparison = "PREFIX"
              + value      = "arn:aws:ecs:eu-central-1:553637109631:task-definition/javazoneweb-"
            }
          + resource_id {
              + comparison = "PREFIX"
              + value      = "arn:aws:ecs:eu-central-1:553637109631:task-definition/jzwatch-"
            }
          + resource_id {
              + comparison = "PREFIX"
              + value      = "arn:aws:ecs:eu-central-1:553637109631:task-definition/testteam-"
            }
        }
    }

Plan: 1 to add, 1 to change, 0 to destroy.

─────────────────────────────────────────────────────────────────────────────

Saved the plan to: tfplan

To perform exactly these actions, run the following command to apply:
    terraform apply "tfplan"

LLM Review

Risk: 🟢 LOW

Plan updates the team provisioner Lambda code in place (source hash change only) and creates a Security Hub automation rule that suppresses a known, accepted ECS.20 finding for platform /tmp init containers in specific task definitions.

  • ✅ [routine] aws_lambda_function.team_provisioner (module.lambdas) is updated in place. Only source_code_hash changes (new deployment package); no configuration, role, or environment changes are shown. Routine code deploy.
  • 🔒 [security] New aws_securityhub_automation_rule (module.monitoring) suppresses ECS.20 findings (ECS task definition with root-running init container) for task definitions with prefixes cupcake-, javazoneweb-, jzwatch-, and testteam-. The rule is documented as an accepted risk: the root init container only chmods /tmp, and the app container runs as non-root. Worth confirming the accepted-risk justification still holds and that the prefixes do not match unintended task definitions. Suppression hides findings rather than fixing them, but it is scoped and not an internet exposure.
  • 💰 [cost] No new billable infrastructure. Security Hub automation rules carry no meaningful direct cost.
  • 💥 [destruction] No resources are destroyed (0 to destroy). No data loss risk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant