r/devops • u/Sure_Abbreviations28 • Sep 08 '26
Ops / Incidents A hardening script I wrote took a load balancer offline and exited 0
Disclosure: the hardening script and the audit tool I mention are both mine. The audit tool is MIT and free, there's no paid product behind this post.
Wrote a script to retrofit a default-deny posture onto existing ALBs: force HTTPS, drop invalid headers, defensive desync mitigation, and make the HTTPS default action a 403 so only host-header rules you define can forward.
Tested it against an HTTP-only ALB — one listener on :80 forwarding to a target group. Extremely common shape if the stack is old or TLS terminates somewhere else.
Output:
Created HTTPS listener: arn:aws:elasticloadbalancing:...
WARNING: No default forward target group found. Add an allowed-host forward rule manually.
ALB hardening complete. Validate hostname routing and health before production use.Exit code 0. Application completely unreachable.
$ curl -sk -o /dev/null -w "%{http_code}\n" -H "Host: allowed.example.com" https://$ALB/
403
$ aws elbv2 describe-target-groups --target-group-arns $TG --query 'TargetGroups[0].LoadBalancerArns'
[]The bug is ordering, not logic. The script discovered the existing forward target group from the HTTPS listener:
CURRENT=$(aws elbv2 describe-listeners --listener-arns "$HTTPS_ARN" --output json)
TG=$(jq -r '.Listeners[0].DefaultActions[]? | select(.Type=="forward") | .TargetGroupArn // empty' <<<"$CURRENT")On an HTTP-only ALB there wasn't one — the script had created it seconds earlier with a 403 fixed-response default. So TG was empty, the branch that creates the host-header forward rule was skipped, and execution continued straight into the line that makes the 403 default permanent.
So the sequence was: create a listener that denies, look at it to find out what to allow, find nothing, print a warning, make the denial permanent, report success.
Three things I'd generalise:
Discover before you mutate. It read state after it had already replaced that state. Any discovery has to happen before the first write, not partway through.
A warning after the damage is a log line, not a safeguard. "Add the rule manually" is good advice one minute earlier. Printed after the default is already 403, it just narrates an outage.
Exit code 0 was the actually dangerous part. In a pipeline that's a green step and everything downstream proceeds. First real signal would've been customers.
Fix was three changes: read the target group from :80 as a fallback and capture it before touching anything, create the allow rule before flipping the default to deny, and abort outright if no target group can be found rather than black-holing traffic. Plus a --dry-run, which should have existed first.
| request | before | after |
|---|---|---|
| HTTP, allowed host | 301 | 301 |
| HTTPS, allowed host | 403 (outage) | 503 (forwarded, no targets) |
| HTTPS, unknown host | 403 | 403 |
| target group | orphaned | attached |
What bugs me is that nothing static would have caught this. ShellCheck clean, valid bash, every AWS API call succeeded and returned what it should. The ALB ended up in exactly the state the code described — and that state was an outage.
The only thing that found it was running it against an infra shape I hadn't designed for, then checking the result from outside as a user instead of checking that my commands returned 0.
Anyone got a good approach for testing this class of thing? Every idea I have is basically "spin up the ugly version of prod in a sandbox and curl it from outside", which works but doesn't scale to every permutation.
The read-only audit half of it is up free if useful: github.com/vamsiatluri/aws-baseline-audit — single file, every call is a Describe/Get so it can't change anything. Full writeup of the outage is in docs/the-outage.md in that repo.
1
u/Sure_Abbreviations28 Sep 09 '26
The thing that still bothers me is how ordinary each individual step was.
Create an HTTPS listener with a safe default. Look up the current forward target.
Add a host rule. Flip the default to deny. Every one of those is correct in
isolation, and I'd have approved all five in a code review.
The failure was entirely in the sequence — step 2 read the state that step 1 had
just replaced. There's no line you can point at and call wrong.
Which is why I don't think more linting would have caught it, and I'm not sure
what would, short of running it against infra shaped differently than you
designed for.
1
u/lastfinaldev Sep 09 '26
The generalisation I'd add: the exit code should come from probing the invariant, not from the mutations succeeding. "Exit 0 = every AWS call returned 200" is what bit you; what you actually care about is "traffic still flows for allowed hosts and is denied for everything else." That's a black-box check you can run from inside the script — curl the LB for one allowed host and one unknown host, compare against the expected matrix (basically the before/after table you posted), and that determines the exit code. Bonus: capture the previous default action before flipping it, and auto-revert if the probe fails within N seconds. Then the same bug becomes a self-healing 30-second blip that exits 1.
On "what would have caught it": a plan/apply split. Phase 1 reads everything and computes the full desired end state with zero mutations; phase 2 applies it. Your sequence bug can't exist in that shape — the plan for an HTTP-only ALB would have printed "default: deny, allow rules: (none)", which is visibly an outage before anything ran. It's also why the deny flip should be the last mutation, the way you bring up a firewall only after the allowlist is loaded.
You're right that linting can't see it — it's a property of the sequence against a particular starting state, so the only detectors are (a) asserting the end state from outside, or (b) running the plan against fixture stacks shaped unlike your design assumptions (an HTTP-only ALB fixture would have been exactly the one).
1
u/Several_Assistant685 Sep 09 '26
Thanks for writing this up. The ordering trap you describe is one I have hit in a different shape when changing listeners on a live ALB.
Two habits that saved me since then. First the script never flips the default action to deny until a curl through the ALB with the allowed Host header returns 200 from the new rule. Second any lookup that can legally return empty is treated as a hard failure with a non zero exit and a printed reason. A warning that scrolls past and exit 0 is what turns a small bug into an outage.
For retrofits on old HTTP only stacks I also run the whole thing in a plan mode first that only prints the aws cli calls it would make. Reading that list takes a minute and it would have shown the empty target group before anything changed.
1
u/Equivalent-Common115 Sep 20 '26
api returning 200 just means your json was parsed, nothing about infra
a warning that describes an outage is a fatal error not a warning note
you should assert on observed behavior to not kill prod again
1
u/[deleted] Sep 09 '26
[removed] — view removed comment