diff --git a/.github/scripts/runtime-tls-recovery.sh b/.github/scripts/runtime-tls-recovery.sh new file mode 100755 index 000000000..028b3b9cf --- /dev/null +++ b/.github/scripts/runtime-tls-recovery.sh @@ -0,0 +1,63 @@ +#!/usr/bin/env bash +# Read-only validators for a separately reviewed hosted TLS cleanup recovery. +set -euo pipefail + +check_baseline() { + jq -e ' + def group_identity: {TargetGroupName, TargetGroupArn, Protocol, Port, VpcId}; + . as $baseline | + .account == "925323749693" and .region == "us-east-1" and + (.state.lineage | type == "string" and test("^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$")) and + (.state.serial | type == "number" and . >= 0 and floor == .) and + (.state.versionId | type == "string" and length > 0 and . != "null" and (test("[\\r\\n]") | not)) and + (.state.sha256 | type == "string" and test("^[a-f0-9]{64}$")) and + (.httpGroups | type == "array" and length == 4) and + ([.httpGroups[].TargetGroupName] | sort) == + (["backend", "forms", "tracking", "realtime"] | map("warmbly-production-" + .) | sort) and + ([.httpGroups[].TargetGroupArn] | unique | length == 4) and + ([.httpGroups[].VpcId] | unique | length == 1) and + all(.httpGroups[]; + .Protocol == "HTTP" and + (.Port | type == "number" and . > 0 and . <= 65535 and floor == .) and + (.VpcId | type == "string" and test("^vpc-[a-f0-9]+$")) and + .TargetGroupArn == ("arn:aws:elasticloadbalancing:us-east-1:925323749693:targetgroup/" + .TargetGroupName + "/" + (.TargetGroupArn | split("/")[-1])) and + (.TargetGroupArn | split("/")[-1] | test("^[a-f0-9]{16}$")) and + (.LoadBalancerArns | type == "array" and length == 0) and + (.targets | type == "array" and length == 0)) and + (.state.httpGroups | type == "array" and length == 4) and + (.state.httpGroups | map(group_identity) | sort_by(.TargetGroupName)) == + ($baseline.httpGroups | map(group_identity) | sort_by(.TargetGroupName)) + ' "$1" >/dev/null || { echo 'Recovery requires four identified, drained HTTP groups matching reviewed state' >&2; return 1; } +} + +check_plan() { + check_baseline "$2" + jq -e --slurpfile baseline "$2" ' + (.resource_changes | type == "array") and + ( + [.resource_changes[]? | select(.change.actions != ["no-op"] and .change.actions != ["read"])] as $changes | + ($baseline[0].httpGroups | map({ + address:("aws_lb_target_group.service[\"" + (.TargetGroupName | ltrimstr("warmbly-production-")) + "\"]"), + group:. + })) as $expected | + ($changes | map(.address) | sort) == ($expected | map(.address) | sort) and + all($changes[]; + . as $change | + .change.actions == ["delete"] and (.change | has("after")) and .change.after == null and + any($expected[]; + .address == $change.address and + $change.change.before.arn == .group.TargetGroupArn and + $change.change.before.id == .group.TargetGroupArn and + $change.change.before.name == .group.TargetGroupName and + $change.change.before.protocol == .group.Protocol and + $change.change.before.port == .group.Port and + $change.change.before.vpc_id == .group.VpcId)) and + ([.resource_drift[]? | select(.change.actions != ["no-op"])] | length == 0)) + ' "$1" >/dev/null || { echo 'Recovery plan must contain only the four reviewed HTTP group deletions without drift' >&2; return 1; } +} + +case "${1:-}" in + check-baseline) check_baseline "${2:?}" ;; + check-plan) check_plan "${2:?}" "${3:?}" ;; + *) echo 'Only read-only recovery validation is supported' >&2; exit 1 ;; +esac diff --git a/.github/scripts/runtime-tls-recovery.test.sh b/.github/scripts/runtime-tls-recovery.test.sh new file mode 100755 index 000000000..27ab22d55 --- /dev/null +++ b/.github/scripts/runtime-tls-recovery.test.sh @@ -0,0 +1,70 @@ +#!/usr/bin/env bash +set -euo pipefail +cd "$(dirname "$0")/../.." +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT +guard=.github/scripts/runtime-tls-recovery.sh +jq -n ' + ["backend", "forms", "tracking", "realtime"] | + map({TargetGroupName:("warmbly-production-" + .), + TargetGroupArn:("arn:aws:elasticloadbalancing:us-east-1:925323749693:targetgroup/warmbly-production-" + . + "/0123456789abcdef"), + Protocol:"HTTP",Port:8080,VpcId:"vpc-0123456789abcdef0",LoadBalancerArns:[],targets:[]}) as $groups | + {account:"925323749693",region:"us-east-1",httpGroups:$groups, + state:{lineage:"01234567-89ab-cdef-0123-456789abcdef",serial:1,versionId:"reviewed-version",sha256:("a"*64),httpGroups:$groups}} +' >"$work/baseline.json" +jq '{resource_changes:[.httpGroups[] | { + address:("aws_lb_target_group.service[\"" + (.TargetGroupName | ltrimstr("warmbly-production-")) + "\"]"), + change:{actions:["delete"],after:null,before:{arn:.TargetGroupArn,id:.TargetGroupArn,name:.TargetGroupName,protocol:.Protocol,port:.Port,vpc_id:.VpcId}} +}]}' "$work/baseline.json" >"$work/plan.json" +bash "$guard" check-baseline "$work/baseline.json" +bash "$guard" check-plan "$work/plan.json" "$work/baseline.json" +jq '.httpGroups |= reverse' "$work/baseline.json" >"$work/reordered-baseline.json" +jq '.resource_changes |= reverse | + .resource_changes += [{address:"data.aws_caller_identity.current",change:{actions:["read"]}}]' "$work/plan.json" >"$work/reordered-plan.json" +bash "$guard" check-plan "$work/reordered-plan.json" "$work/reordered-baseline.json" +for change in '.account="123456789012"' '.region="us-west-2"' \ + '.httpGroups|=.[1:]' '.httpGroups[0]=.httpGroups[1]' \ + '.httpGroups[0].LoadBalancerArns=["still-referenced"]' \ + '.httpGroups[0].targets=[{Target:{Id:"still-registered"}}]' \ + '.httpGroups[0].Protocol="HTTPS"' '.httpGroups[0].Port=0' \ + '.httpGroups[0].VpcId="vpc-11111111"' \ + '.httpGroups[0].TargetGroupArn="arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/warmbly-production-backend/0123456789abcdef"' \ + '.httpGroups[0].targets=null' '.httpGroups[0].LoadBalancerArns=null' \ + '.state.httpGroups[0].TargetGroupArn="changed"' '.state.httpGroups|=.[1:]' \ + '.state.versionId=""' '.state.versionId="null"' '.state.sha256=""' \ + '.state.lineage=null' '.state.lineage=("-"*36)' '.state.serial=-1'; do + jq "$change" "$work/baseline.json" >"$work/changed.json" + if bash "$guard" check-plan "$work/plan.json" "$work/changed.json" 2>/dev/null; then + echo "Recovery accepted an invalid baseline: $change" >&2 + exit 1 + fi +done +for change in '.resource_changes=[]' '.resource_changes|=.[1:]' \ + '.resource_changes[0]=.resource_changes[1]' \ + '.resource_changes[0].change.actions=["no-op"]' \ + '.resource_changes[0].change.actions=["delete","create"]' \ + '.resource_changes[0].change.after={}' \ + 'del(.resource_changes[0].change.after)' \ + '.resource_changes[0].change.before.arn="changed"' \ + '.resource_changes[0].change.before.id="changed"' \ + '.resource_changes[0].change.before.port=465' \ + '.resource_changes[0].change.before.protocol="HTTPS"' \ + '.resource_changes[0].change.before.name="different"' \ + '.resource_changes[0].change.before.vpc_id="vpc-11111111"' \ + '.resource_changes[0].address="aws_lb_target_group.service[\"consumer\"]"' \ + '.resource_changes += [{address:"aws_ecs_service.service[\"backend\"]",change:{actions:["update"]}}]' \ + '.resource_changes += [{address:"aws_vpc_security_group_ingress_rule.internal_backend[0]",change:{actions:["delete"]}}]' \ + '.resource_drift=[{address:"aws_lb_target_group.service[\"backend\"]",change:{actions:["update"]}}]'; do + jq "$change" "$work/plan.json" >"$work/changed.json" + if bash "$guard" check-plan "$work/changed.json" "$work/baseline.json" 2>/dev/null; then + echo "Recovery accepted a different cleanup plan: $change" >&2 + exit 1 + fi +done +for action in plan apply delete; do + if bash "$guard" "$action" 2>/dev/null; then + echo 'Read-only recovery validator accepted an execution action' >&2 + exit 1 + fi +done +echo 'Read-only TLS recovery identity and scope guards passed' diff --git a/.github/workflows/runtime-tls.yml b/.github/workflows/runtime-tls.yml index a57cc1ffc..0606b67ac 100644 --- a/.github/workflows/runtime-tls.yml +++ b/.github/workflows/runtime-tls.yml @@ -44,6 +44,8 @@ jobs: persist-credentials: false - run: shellcheck .github/scripts/runtime-tls.sh .github/scripts/runtime-tls.test.sh - run: bash .github/scripts/runtime-tls.test.sh + - run: shellcheck .github/scripts/runtime-tls-recovery.sh .github/scripts/runtime-tls-recovery.test.sh + - run: bash .github/scripts/runtime-tls-recovery.test.sh plan: needs: check