One toolkit, two estates
My infrastructure tool is nearly three hundred Python scripts behind around eight hundred make targets, with one wrapper that fetches credentials for the named account and runs the right script. It was written for one AWS organization, and that organization lives in the GovCloud partition. Budgets, alerting, CloudTrail, GuardDuty, Security Hub, Route 53, the audit and remediation roles. All of it keyed by an account name.
Then I had a second organization to run: five accounts in the commercial partition. I did not want a second copy of the tool. A fork is a second thing to maintain, and every fix for one estate would have to be carried to the other by hand. I wanted the same checkout, the same targets, and one untracked file that says which estate I am in.
It failed differently in every script
The tool knew its first estate in ways nobody had written down. Pointed at a commercial account, it produced a different error per script.
- The SNS targets:
InvalidClientTokenId. The script defaulted its region argument to a GovCloud region, the wrapper passed none, and commercial credentials went to a GovCloud endpoint. - CloudTrail status:
UnrecognizedClientException. Same cause, different literal. - The Organizations targets:
ProfileNotFound. Six targets omitted the account argument entirely, so every call went to the original management account. - Anything that printed the account id:
KeyError. The credential loader never set the id on the single-sign-on path, and the first script to read it crashed.
None of these said "wrong estate". Each looked like a credentials problem. When I searched for the region literal I found it in 31 scripts, and hanging off the same literal: a partition constant in three remediation scripts, the standard ARNs for Security Hub, a dry-run topic ARN, the default region list for a health notifier, and the partition-specific API regions for the Health and Organizations services. Four make recipes had the region typed into them as well.
Three layers for estate identity
Three values are the estate: the management account name, the account that administers the security services, and the address that alerts go to. Fifteen argparse defaults in twelve scripts carried the account names. Two scripts carried the address. I replaced every one with a single resolver.
def env_default(name, fallback):
value = os.environ.get(f"TOOL_{name}")
if value:
return value
try:
import local_accounts as local # untracked, optional
if getattr(local, name, None):
return str(getattr(local, name))
except ImportError:
pass
return fallback
MGMT_ACCOUNT = env_default("MGMT_ACCOUNT", "original-mgmt")
ALERT_EMAIL = env_default("ALERT_EMAIL", "original@example.com")
The order is the point. An environment variable wins, so a one-off run can point anywhere. An untracked local file comes second, so my second estate is a few lines on disk that never enter the repository. The original literal is the fallback, so a checkout with neither set behaves exactly as it did before. The first estate's behaviour did not change and its tests did not need to.
The make side mirrors it. The variables are assigned with
?=, which yields to the environment or to an included
file, and the last line of the Makefile includes an optional
untracked file.
MGMT_ACCOUNT ?= original-mgmt
ORG_ID ?= o-original
# ... eight hundred targets ...
-include local.mk
The same local file appends my accounts to the account map, one row per account with its id and home region. The map is recursively expanded, so an append at the bottom of the file applies to every recipe above it.
Region from the wrapper, partition derived
The region should never have been in a script. The wrapper already
reads the account's home region from the account map and exports it
for every run. So each of the 31 scripts now resolves its region
through one function: an explicit --region first, then
a fixed endpoint region where the service has one, then the
account's home region. The result is checked against the account's
partition and refused if it does not match, so commercial
credentials cannot reach a GovCloud endpoint and die with a token
error that points the wrong way.
The partition is never typed. It is one line, a function of the account.
def partition_of(account):
return "aws-us-gov" if account in GOVCLOUD_ACCOUNTS else "aws"
Every ARN that used to carry a partition literal now builds from that call. The GovCloud account set it reads is itself overridable, with one ordering rule I got wrong once: the override has to be assigned after the canonical definition, because assignment order in the module is the override mechanism. Put it first and the canonical set silently wins.
Merge the id table, replace the account list
Two tables, two rules. The account list is replaced by the override, because a personal estate should see only its own accounts when a target loops over all of them. The account id table is merged, personal rows added and original rows kept, because a family of GuardDuty scripts validates the account argument against that table and the original unit tests pin the original rows. Replace the id table and those tests fail. Merge the list and "all accounts" starts iterating an estate I cannot reach.
try:
import local_accounts as local
if getattr(local, "ACCOUNT_IDS", None):
ACCOUNT_IDS = {**ACCOUNT_IDS, **local.ACCOUNT_IDS}
except ImportError:
pass
What it costs
The fix is a 31-file diff against the tool's original tree, and it will conflict with any later edit of those lines until the same change is made there. That is the price of not forking. The diff is small per file and wide across files, and it is mine to carry until it lands.
The unit suite tells the truth about the overrides. With my local file in place, four tests fail by construction: two that pin the original credential contexts and id table, two that assume an account that is not in my GovCloud set. The baseline is 419 tests, 4 expected failures, 0 errors, and the rule is written next to the number: any other failure is real. A separate environment switch turns every hook off so the original suite runs clean when I need it to.
The mistake. When I fixed the first region literal, I listed the
other 31 with a one-line sed and deliberately did not
apply it, to keep the change small. Weeks later I wanted a CloudTrail
status and found the whole status family unusable in the second
estate, for the reason I had already written down. A small diff is a
good thing. A known literal in 31 files is not a small diff waiting
to happen. It is 31 bugs waiting for a target I have not run yet.
The rules I run by now
- Estate identity lives in three layers: environment variable, untracked local file, original literal. The fallback keeps the original estate unchanged.
- Region comes from the per-account environment the wrapper exports. A script never defaults it.
- Partition is derived from the account. It is never typed into an ARN.
- Tables that drive loops are replaced by the override. Tables that validate are merged.
- Record the expected test failures under the override, and treat anything beyond that number as real.