carlos / platform Public

Add SSM boot channel and scoped policy for group roll operators

Install and require an active SSM agent before group bootstrap and edge startup, preserving the restart guards and unattended update masks.

Emit an operator-only IAM policy scoped to this group, its tagged instances, one approved session document and caller-owned sessions. Cover boot failure and ordering, exact policy grants and document validation with mock tests.
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 40138139585d1538ee43158a5f07237ee0dce17b parent f86e979
7 files changed, +416 −1
  • infra/modules/region-host/README.md +23 −0
  • infra/modules/region-host/group-init.sh.tftpl +10 −1
  • infra/modules/region-host/group_operator.tf +88 −0
  • infra/modules/region-host/tests/fixed_state_unchanged.tftest.hcl +4 −0
  • infra/modules/region-host/tests/group_operator.tftest.hcl +219 −0
  • infra/modules/region-host/tests/group_user_data.tftest.hcl +10 −0
  • scripts/region_group_ssm_boot_test.py +62 −0
diff --git a/infra/modules/region-host/README.md b/infra/modules/region-host/README.md
index 5088fe84b..e5926a395 100644
--- a/infra/modules/region-host/README.md
+++ b/infra/modules/region-host/README.md
@@ -139,3 +139,26 @@ Open item for region HA plan 1b: the box role's `ec2:DescribeInstances`
is unconditioned, because the infrastructure inventory reads every
region. The HA spec asks for an `ec2:Region` condition on the call the
placer makes. Decide there whether that is a second, narrower statement.
+
+## Attended group maintenance
+
+Group mode installs and starts `amazon-ssm-agent` before CARLOS bootstrap.
+If downloading, installing or starting it fails, boot stops before the edge
+starts. The initial launch hook then reaches its default `ABANDON` deadline.
+A running local agent does not prove that Session Manager can reach it. The
+roll operator must verify that the target is SSM-online before any AWS write.
+
+Attach `group_operator_policy_json` to the attended roll operator's identity.
+The module creates no operator IAM resource and returns an empty string in
+fixed mode. `group_operator_session_document_arn` selects one approved session
+document; its default is the regional AWS-managed
+`AWS-StartNonInteractiveCommand`. A custom document must belong to this
+account and region. The policy grants group-scoped maintenance actions and
+Session Manager access to this group's tagged instances. The operator tool
+must still enforce the instance, hook, transaction and `CONTINUE` result.
+Termination requires separate fault authority.
+
+Do not associate `AWS-UpdateSSMAgent`, patch baselines, or automatic patch or
+reboot jobs with group members. This module creates no State Manager
+association. Keep package and agent updates inside the attended Standby
+transaction, with one member held out of service at a time.
diff --git a/infra/modules/region-host/group-init.sh.tftpl b/infra/modules/region-host/group-init.sh.tftpl
index f858c5b53..39690c3ee 100644
--- a/infra/modules/region-host/group-init.sh.tftpl
+++ b/infra/modules/region-host/group-init.sh.tftpl
@@ -96,7 +96,7 @@ for path, body in files.items():
PY
export NEEDRESTART_MODE=l
apt-get -q update
-apt-get -y -q install nftables chrony awscli iproute2
+apt-get -y -q install nftables chrony awscli iproute2 curl
getent passwd carlos >/dev/null || useradd --system --home-dir /data --shell /usr/sbin/nologin carlos
CARLOS_UID=$(id -u carlos)
export CARLOS_UID
@@ -139,6 +139,15 @@ systemctl enable nftables.service
systemctl restart nftables.service
nft list table inet carlos_group_imds >/dev/null
+# install maintenance channel
+# Fail before edge startup if the attended maintenance channel cannot start.
+curl -fsSL --connect-timeout 5 --max-time 120 -o /var/lib/carlos-group-boot/amazon-ssm-agent.deb \
+ "https://s3.$AWS_REGION.amazonaws.com/amazon-ssm-$AWS_REGION/latest/debian_arm64/amazon-ssm-agent.deb"
+dpkg -i /var/lib/carlos-group-boot/amazon-ssm-agent.deb
+rm -f /var/lib/carlos-group-boot/amazon-ssm-agent.deb
+systemctl enable --now amazon-ssm-agent
+systemctl is-active --quiet amazon-ssm-agent
+
# configure chrony
printf '%s\n' 'server 169.254.169.123 prefer iburst minpoll 4 maxpoll 4' > /var/lib/carlos-group-boot/chrony.conf
install -o root -g root -m 0644 /var/lib/carlos-group-boot/chrony.conf /etc/chrony/conf.d/carlos-group.conf
diff --git a/infra/modules/region-host/group_operator.tf b/infra/modules/region-host/group_operator.tf
new file mode 100644
index 000000000..c55d48004
--- /dev/null
+++ b/infra/modules/region-host/group_operator.tf
@@ -0,0 +1,88 @@
+variable "group_operator_session_document_arn" {
+ description = "Session document approved for attended group maintenance. Null selects the regional AWS-StartNonInteractiveCommand document."
+ type = string
+ default = null
+ validation {
+ condition = var.group_operator_session_document_arn == null ? true : can(regex(
+ "^arn:aws(-[a-z]+)*:ssm:[a-z]{2}(-[a-z]+)+-[0-9]:([0-9]{12})?:document/[a-zA-Z0-9_.-]{3,128}$",
+ var.group_operator_session_document_arn
+ ))
+ error_message = "group_operator_session_document_arn must name one SSM document ARN without wildcards, or be null for the AWS default."
+ }
+}
+
+locals {
+ group_operator_ssm_arn = local.group_enabled ? "arn:${data.aws_partition.current[0].partition}:ssm:${data.aws_region.current.region}:" : ""
+ group_operator_document = local.group_enabled ? coalesce(
+ var.group_operator_session_document_arn,
+ "${local.group_operator_ssm_arn}:document/AWS-StartNonInteractiveCommand"
+ ) : ""
+}
+
+# Attach this output to the attended operator identity, never the instance role.
+# IAM cannot bind iid, hook, CONTINUE-only or transaction state. return_member in
+# scripts/region-group-roll.py must enforce those before lifecycle completion.
+output "group_operator_policy_json" {
+ description = "IAM policy for the attended group roll operator. Empty in fixed mode. Creates no IAM resource."
+ value = !local.group_enabled ? "" : jsonencode({
+ Version = "2012-10-17"
+ Statement = [
+ {
+ Sid = "GroupMaintenance"
+ Effect = "Allow"
+ Action = ["autoscaling:UpdateAutoScalingGroup", "autoscaling:EnterStandby", "autoscaling:ExitStandby", "autoscaling:CompleteLifecycleAction"]
+ Resource = aws_autoscaling_group.group[0].arn
+ Condition = { StringEquals = {
+ "aws:ResourceTag/carlos:deployment" = var.group_config.deployment
+ "aws:ResourceTag/carlos:region" = var.group_config.region
+ } }
+ },
+ {
+ Sid = "ObserveMaintenance"
+ Effect = "Allow"
+ Action = ["autoscaling:DescribeAutoScalingGroups", "autoscaling:DescribeAutoScalingInstances", "autoscaling:DescribeLifecycleHooks", "autoscaling:DescribeScalingActivities", "elasticloadbalancing:DescribeTargetHealth", "ec2:DescribeInstances", "ssm:DescribeInstanceInformation"]
+ # These describe actions do not support resource scoping. Only they
+ # receive Resource "*"; requests still stay in the group's AWS region.
+ Resource = "*"
+ Condition = { StringEquals = { "aws:RequestedRegion" = data.aws_region.current.region } }
+ },
+ {
+ Sid = "GroupSession"
+ Effect = "Allow"
+ Action = ["ssm:StartSession"]
+ Resource = "arn:${data.aws_partition.current[0].partition}:ec2:${data.aws_region.current.region}:${data.aws_caller_identity.current.account_id}:instance/*"
+ Condition = {
+ StringEquals = { "aws:ResourceTag/aws:autoscaling:groupName" = var.name }
+ # Require document authorization even when the caller omits its name.
+ # https://docs.aws.amazon.com/service-authorization/latest/reference/list_ssm.html
+ Bool = { "ssm:SessionDocumentAccessCheck" = "true" }
+ }
+ },
+ {
+ # Document authorization is separate: AWS-owned documents do not carry
+ # the instance's group tag. Both resources must authorize StartSession.
+ Sid = "ApprovedSessionDocument"
+ Effect = "Allow"
+ Action = ["ssm:StartSession"]
+ Resource = local.group_operator_document
+ },
+ {
+ Sid = "OwnSessions"
+ Effect = "Allow"
+ Action = ["ssm:TerminateSession", "ssm:ResumeSession"]
+ Resource = "${local.group_operator_ssm_arn}${data.aws_caller_identity.current.account_id}:session/*"
+ # AWS's session tag contains the caller's user ID, including the role
+ # session name for federation. Exact equality avoids prefix collisions.
+ # https://aws.amazon.com/blogs/mt/configuring-aws-systems-manager-session-manager-support-federated-users-using-session-tags/
+ Condition = { StringEquals = { "ssm:resourceTag/aws:ssmmessages:session-id" = "$${aws:userid}" } }
+ },
+ ]
+ })
+ precondition {
+ condition = !local.group_enabled ? true : (
+ local.group_operator_document == "${local.group_operator_ssm_arn}:document/AWS-StartNonInteractiveCommand" ||
+ startswith(local.group_operator_document, "${local.group_operator_ssm_arn}${data.aws_caller_identity.current.account_id}:document/")
+ )
+ error_message = "The operator session document must be AWS-StartNonInteractiveCommand in this region, or a document owned by this account in this region and partition."
+ }
+}
diff --git a/infra/modules/region-host/tests/fixed_state_unchanged.tftest.hcl b/infra/modules/region-host/tests/fixed_state_unchanged.tftest.hcl
index a6548d44c..d5da0fcbf 100644
--- a/infra/modules/region-host/tests/fixed_state_unchanged.tftest.hcl
+++ b/infra/modules/region-host/tests/fixed_state_unchanged.tftest.hcl
@@ -60,6 +60,10 @@ override_resource {
}
run "fixed_state_unchanged" {
command = plan
+ assert {
+ condition = output.group_operator_policy_json == ""
+ error_message = "fixed mode must not emit an operator policy"
+ }
assert {
condition = (
aws_instance.host[0].instance_type == "t4g.small" &&
diff --git a/infra/modules/region-host/tests/group_operator.tftest.hcl b/infra/modules/region-host/tests/group_operator.tftest.hcl
new file mode 100644
index 000000000..c95b4d1a2
--- /dev/null
+++ b/infra/modules/region-host/tests/group_operator.tftest.hcl
@@ -0,0 +1,219 @@
+mock_provider "aws" {
+ mock_resource "aws_launch_template" { defaults = { id = "lt-0123456789abcdef0" } }
+ mock_resource "aws_security_group" { defaults = { id = "sg-0123456789abcdef1" } }
+}
+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_eip.group["eu-west-1a"]
+ values = { public_ip = "198.51.100.10" }
+}
+override_data {
+ target = data.aws_eip.group["eu-west-1b"]
+ values = { public_ip = "198.51.100.11" }
+}
+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" }
+}
+variables {
+ 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" }
+ }
+ group_ingress = {
+ nlb_arn = "arn:aws:elasticloadbalancing:eu-west-1:123456789012:loadbalancer/net/test/1234"
+ nlb_security_group_id = "sg-0123456789abcdef0"
+ target_group_arns = {
+ "80" = "arn:aws:elasticloadbalancing:eu-west-1:123456789012:targetgroup/test-80/1234"
+ "443" = "arn:aws:elasticloadbalancing:eu-west-1:123456789012:targetgroup/test-443/1234"
+ }
+ public_ips = { eu-west-1a = "198.51.100.10", eu-west-1b = "198.51.100.11" }
+ }
+}
+
+override_data {
+ target = data.aws_ami.group
+ values = { id = "ami-0123456789abcdef0", root_device_name = "/dev/xvda", root_device_type = "ebs" }
+}
+override_resource {
+ target = aws_autoscaling_group.group
+ values = { arn = "arn:aws:autoscaling:eu-west-1:123456789012:autoScalingGroup:00000000-0000-0000-0000-000000000001:autoScalingGroupName/carlos-dev-ie-group" }
+}
+
+run "operator_policy" {
+ command = plan
+ assert {
+ condition = jsondecode(output.group_operator_policy_json) == jsondecode(jsonencode({
+ Version = "2012-10-17"
+ Statement = [
+ {
+ Sid = "GroupMaintenance"
+ Effect = "Allow"
+ Action = ["autoscaling:UpdateAutoScalingGroup", "autoscaling:EnterStandby", "autoscaling:ExitStandby", "autoscaling:CompleteLifecycleAction"]
+ Resource = aws_autoscaling_group.group[0].arn
+ Condition = { StringEquals = {
+ "aws:ResourceTag/carlos:deployment" = "dev"
+ "aws:ResourceTag/carlos:region" = "ie"
+ } }
+ },
+ {
+ Sid = "ObserveMaintenance"
+ Effect = "Allow"
+ Action = ["autoscaling:DescribeAutoScalingGroups", "autoscaling:DescribeAutoScalingInstances", "autoscaling:DescribeLifecycleHooks", "autoscaling:DescribeScalingActivities", "elasticloadbalancing:DescribeTargetHealth", "ec2:DescribeInstances", "ssm:DescribeInstanceInformation"]
+ Resource = "*"
+ Condition = { StringEquals = { "aws:RequestedRegion" = "eu-west-1" } }
+ },
+ {
+ Sid = "GroupSession"
+ Effect = "Allow"
+ Action = ["ssm:StartSession"]
+ Resource = "arn:aws:ec2:eu-west-1:123456789012:instance/*"
+ Condition = {
+ StringEquals = { "aws:ResourceTag/aws:autoscaling:groupName" = "carlos-dev-ie-group" }
+ Bool = { "ssm:SessionDocumentAccessCheck" = "true" }
+ }
+ },
+ {
+ Sid = "ApprovedSessionDocument"
+ Effect = "Allow"
+ Action = ["ssm:StartSession"]
+ Resource = "arn:aws:ssm:eu-west-1::document/AWS-StartNonInteractiveCommand"
+ },
+ {
+ Sid = "OwnSessions"
+ Effect = "Allow"
+ Action = ["ssm:TerminateSession", "ssm:ResumeSession"]
+ Resource = "arn:aws:ssm:eu-west-1:123456789012:session/*"
+ Condition = { StringEquals = { "ssm:resourceTag/aws:ssmmessages:session-id" = "$${aws:userid}" } }
+ },
+ ]
+ }))
+ error_message = "the operator policy must contain only the exact group writes, describes, tagged document-checked sessions and caller-owned session controls"
+ }
+ assert {
+ condition = !anytrue([for s in jsondecode(output.group_operator_policy_json).Statement :
+ anytrue([for a in s.Action : contains(["ec2:TerminateInstances", "autoscaling:TerminateInstanceInAutoScalingGroup", "autoscaling:SetDesiredCapacity", "autoscaling:SetInstanceHealth", "ssm:SendCommand"], a) || startswith(a, "ssm:GetParameter")])
+ ])
+ error_message = "the roll policy must not grant termination, direct capacity or health changes, Run Command or parameter reads"
+ }
+}
+
+run "custom_session_document" {
+ command = plan
+ variables { group_operator_session_document_arn = "arn:aws:ssm:eu-west-1:123456789012:document/shop-maintenance" }
+ assert {
+ condition = jsondecode(output.group_operator_policy_json).Statement[3].Resource == var.group_operator_session_document_arn
+ error_message = "only the configured session document may be authorized"
+ }
+}
+
+run "invalid_document_wildcard" {
+ command = plan
+ variables { group_operator_session_document_arn = "arn:aws:ssm:eu-west-1:123456789012:document/*" }
+ expect_failures = [var.group_operator_session_document_arn]
+}
+run "invalid_document_empty" {
+ command = plan
+ variables { group_operator_session_document_arn = "" }
+ expect_failures = [var.group_operator_session_document_arn]
+}
+run "invalid_document_region" {
+ command = plan
+ variables { group_operator_session_document_arn = "arn:aws:ssm:eu-west-2:123456789012:document/shop-maintenance" }
+ expect_failures = [output.group_operator_policy_json]
+}
+run "invalid_document_account" {
+ command = plan
+ variables { group_operator_session_document_arn = "arn:aws:ssm:eu-west-1:000000000000:document/shop-maintenance" }
+ expect_failures = [output.group_operator_policy_json]
+}
+run "invalid_document_partition" {
+ command = plan
+ variables { group_operator_session_document_arn = "arn:aws-us-gov:ssm:eu-west-1:123456789012:document/shop-maintenance" }
+ expect_failures = [output.group_operator_policy_json]
+}
+run "invalid_aws_shell_document" {
+ command = plan
+ variables { group_operator_session_document_arn = "arn:aws:ssm:eu-west-1::document/AWS-StartSSHSession" }
+ expect_failures = [output.group_operator_policy_json]
+}
+run "invalid_document_type" {
+ command = plan
+ variables { group_operator_session_document_arn = "arn:aws:ssm:eu-west-1:123456789012:parameter/shop-maintenance" }
+ expect_failures = [var.group_operator_session_document_arn]
+}
diff --git a/infra/modules/region-host/tests/group_user_data.tftest.hcl b/infra/modules/region-host/tests/group_user_data.tftest.hcl
index bf7ef1a99..4a5cdbc0a 100644
--- a/infra/modules/region-host/tests/group_user_data.tftest.hcl
+++ b/infra/modules/region-host/tests/group_user_data.tftest.hcl
@@ -117,6 +117,16 @@ variables {
run "real_dev_user_data" {
command = plan
+ assert {
+ condition = (
+ strcontains(local.group_init_script, "https://s3.$AWS_REGION.amazonaws.com/amazon-ssm-$AWS_REGION/latest/debian_arm64/amazon-ssm-agent.deb") &&
+ strcontains(local.group_init_script, "dpkg -i /var/lib/carlos-group-boot/amazon-ssm-agent.deb") &&
+ strcontains(local.group_init_script, "systemctl enable --now amazon-ssm-agent") &&
+ strcontains(local.group_init_script, "systemctl is-active --quiet amazon-ssm-agent") &&
+ can(regex("(?s)50-group[.]conf.*Restart=no.*nft list table inet carlos_group_imds.*dpkg -i.*systemctl enable --now amazon-ssm-agent.*# fetch verified bootstrap.*systemctl start carlos-edge.service", local.group_init_script))
+ )
+ error_message = "group boot must install and start SSM after the restart guard and metadata fence, before verified bootstrap and edge startup"
+ }
assert {
condition = length(aws_launch_template.group[0].user_data) <= 13107
error_message = "the encoded group user data must stay within 13107 characters"
diff --git a/scripts/region_group_ssm_boot_test.py b/scripts/region_group_ssm_boot_test.py
new file mode 100644
index 000000000..8766c9cb1
--- /dev/null
+++ b/scripts/region_group_ssm_boot_test.py
@@ -0,0 +1,62 @@
+import os
+import subprocess
+import tempfile
+import unittest
+from pathlib import Path
+
+TEMPLATE = Path(__file__).resolve().parent.parent / 'infra/modules/region-host/group-init.sh.tftpl'
+STAGING = '/var/lib/carlos-group-boot'
+
+
+class SSMBootTests(unittest.TestCase):
+ def test_install_order(self):
+ source = TEMPLATE.read_text()
+ steps = [
+ 'systemctl mask --now apt-daily.timer',
+ "'/etc/systemd/system/carlos-edge.service.d/50-group.conf':",
+ 'apt-get -y -q install nftables chrony awscli iproute2 curl',
+ 'nft list table inet carlos_group_imds >/dev/null',
+ '# install maintenance channel',
+ 'dpkg -i /var/lib/carlos-group-boot/amazon-ssm-agent.deb',
+ 'systemctl enable --now amazon-ssm-agent',
+ 'systemctl is-active --quiet amazon-ssm-agent',
+ '# fetch verified bootstrap',
+ 'python3 -I /var/lib/carlos-group-boot/carlos_group_boot_verify.py',
+ '( umask 022; exec /var/lib/carlos-group-boot/bootstrap system host-env write',
+ 'systemctl start carlos-edge.service',
+ ]
+ positions = [source.index(step) for step in steps]
+ self.assertEqual(positions, sorted(positions))
+ self.assertIn('https://s3.$AWS_REGION.amazonaws.com/amazon-ssm-$AWS_REGION/latest/debian_arm64/amazon-ssm-agent.deb', source)
+ self.assertNotIn('AWS-UpdateSSMAgent', source)
+
+ def test_install_failures_stop_boot(self):
+ source = TEMPLATE.read_text()
+ block = source.split('# install maintenance channel\n', 1)[1].split('# configure chrony', 1)[0]
+ with tempfile.TemporaryDirectory(dir=os.environ['TMPDIR']) as tmp:
+ root = Path(tmp)
+ for command in ('curl', 'dpkg', 'systemctl'):
+ stub = root / command
+ stub.write_text('#!/bin/sh\nprintf "%s %s %s\\n" "$(basename "$0")" "$(umask)" "$*" >> "$LOG"\n'
+ 'test "$(basename "$0") $1" != "$FAIL_AT"\n')
+ stub.chmod(0o700)
+ for failure in ('', 'curl -fsSL', 'dpkg -i', 'systemctl enable', 'systemctl is-active'):
+ with self.subTest(failure=failure):
+ log = root / 'log'
+ log.unlink(missing_ok=True)
+ env = dict(os.environ, PATH=str(root) + ':' + os.environ['PATH'],
+ AWS_REGION='eu-west-1', LOG=str(log), FAIL_AT=failure)
+ script = source.split('%{ for', 1)[0] + block.replace(STAGING, tmp)
+ script += '\nprintf "edge %s\\n" "$(umask)" >> "$LOG"\n'
+ result = subprocess.run(['sh', '-c', script], env=env, capture_output=True, text=True)
+ calls = log.read_text()
+ self.assertEqual(result.returncode == 0, not failure, result.stderr)
+ self.assertEqual('edge 0077' in calls, not failure)
+ if not failure:
+ self.assertIn('dpkg 0077 -i ', calls)
+ self.assertIn('systemctl 0077 enable --now amazon-ssm-agent', calls)
+ self.assertIn('systemctl 0077 is-active --quiet amazon-ssm-agent', calls)
+
+
+if __name__ == '__main__':
+ unittest.main()