Rewriting a firewall rollout script without breaking it
Some customers have one big firewall. Others have well over a hundred small ones, at remote sites that all look alike: the same VPN, the same objects, the same NAT rules, a few supplier-specific functions per site. Nobody configures that by hand in SmartConsole. There's a script.
This one had grown over the years. It worked, mostly. It was also one long file, with copy-pasted blocks per supplier, a publish after almost every step, and a few bugs that nobody had noticed because they only hit unusual sites. My job: make it maintainable and safer, without changing what it does.
The rules I set
- Same changes as before. Every object, group membership and NAT rule the old script created, the new one must create too, named the same.
- Fix only what is clearly a bug. Things that looked odd but might be intentional stay as they are, and go on a list of questions for the people who know the sites.
- Never test on production. Everything is proven first against a fake API, then against a lab copy of the management server.
The rewrite
The single file became a small package: one module for the menu, one per area (objects, NAT, VPN, gateways, policy installation, CSV import), and one for the supplier functions that differ per site.
The important change is how it talks to the management server:
- One session per menu action. All changes are collected, shown in a table at the end, and then you choose Publish or Discard everything. An error halfway never leaves a half-configured site behind.
- Safe to run twice. Objects, group memberships, VPN memberships and NAT rules that already exist are skipped. The NAT rulebase is read once per session, so the duplicate check is cheap.
- Dry run. It reads everything from the server and shows what it would change, but changes nothing.
- Polite logins. The management server refuses logins that come too fast after each other. The tool waits and tries again instead of crashing.
New features came along too: CSV imports with a header row and a preview that shows every line as OK or with the reason it will be skipped, and policy installation in batches: one install task per firewall, a configurable number at the same time, with a result table and a CSV report at the end.
Proving it does the same thing
This is the part I'm most happy with. I wrote a fake management API that records every call instead of executing it. Then a comparison script runs the old and the new tool through the same scenarios and compares the recorded calls one by one.
text25 scenarios
22 identical, call by call
3 different: exactly the three bug fixes, nothing else
The fixes it found were the kind you only catch this way: a third camera object that got the name of the second one, a subnet that was asked for twice but created from the first answer, and a site ID parser that turned some names into the wrong number and silently skipped those sites.
On top of that: unit tests for the new behaviour, and a smoke test that drives the real menus with simulated key presses. None of it needs a server.
Then the lab
Only after all of that did it touch a management server: a lab copy of production, with the same packages and tens of thousands of objects. Dry runs first, then a real publish on a test gateway, then the same function again to see "no changes". The lab test found two things the fake API couldn't: the login rate limit, and that picking one firewall out of a long list works better as "type part of the name first" than as one huge menu.
What I took from it
- The old code is the specification. Not the documentation, not what people remember. Record what it does and compare against that.
- A fake API is cheap and pays back fast. It took an afternoon and it made every later change safe.
- Write down the questions. Odd behaviour you don't understand is not yours to "fix". Ask.
- Batch everything that touches many devices. Which turned out to matter a lot more than I expected. That's another post.