A Cage You Cannot Wire By Hand
Only the quartermaster runs wires to the enclosures, and only by the plan. A hand-made wire is found at the next morning check, with the exact place it was spliced in. Tested in the shared library - and once too strict.
👋 Hi, I'm Anton - a software engineer. These days I work on a platform: contracts, generated code, data paths and the tooling around them. Running notes live on my GitHub: github.com/brilliant-almazov.
This part is about a tested guard in the shared library: connections to resources come only from the manifest. The narrowing in "What went wrong" is real; how many hand-made keys the guard has caught across services is not counted here.
This is part 14 of the series on what a shared platform does for every service, told as a zoo. Maybe you already do it better, maybe you'd do it another way - this is how it is going, and where it stops.
In real life
Biscuit needed light in his enclosure, so he ran a wire himself. "My own wire. Quick!" Nora looks at it and asks the only question that matters: "Where does it go?" Nobody knows. It works, it is not on any plan, and the next keeper who cuts a cable for repairs will not know it is there.
This part brings in a new resident: Gus, the quartermaster. Gus owns the plan of every wire in the zoo, and the rule is short - wires go where the plan says. At the morning check Pickle walks the wires, finds the hand-made one and points at the exact spot: "This file, this line." And the last panel is the part I like most, because it is a mistake: on the first day Gus treated Nora's doorbell as a cage wire and cut it. The rule was right; its reach was too wide. Gus apologised and narrowed it.
Addresses written by hand
A service in our platform declares its resources in one manifest: this pool of Postgres, that queue, this cache. From that the generator prints the typed config, the wiring and a config file that carries every key with its default and the name of its environment variable in a comment. The manifest is the plan.
The temptation is a field in the config struct with a hand-written tag - env:"ORDERS_POSTGRES_URL" - or a value swapped into the address before the connection opens. It is quick, it works on a laptop, and it splits the truth in two. The manifest says the service talks to one database; the code talks to another. Deploy tooling, health checks and the stand all read the manifest, so the hand-made wire is invisible to everything except the one process that uses it.
What the plan prints
The other half of the rule is what you get for following it. A resource is declared in the manifest - in the shape of it, names illustrative:
# service.yaml - declare the wire; the platform prints the config field, its wiring and its ENV key
infra:
postgres:
main:
image: postgres:<version>
rabbitmq:
events:
image: rabbitmq:<version>
Declare a resource, and the generator prints its typed config field, its wiring and its keys in the service's config file - every key with its default, and the name of its environment variable written next to it. In the shape of that file - values elided, names illustrative:
postgres:
main:
url: "" # ENV: MAIN_POSTGRES_URL
max_conns: <n> # ENV: MAIN_POSTGRES_MAX_CONNS
rabbitmq:
events:
url: "" # ENV: EVENTS_RABBITMQ_URL
So the plan is readable by a person who has never opened the code: which wires exist, what they are called in the environment, and what happens when nobody sets them. A hand-made field is the one thing missing from that file - which is exactly why it needs a guard.
The snapshot that checks the wires
The guard lives where every service already passes: the snapshot of the environment the library takes at start. A connection to a resource may be declared only in the manifest. A hand-written connection tag, or an address swapped by hand, fails the snapshot with a named error and the place it came from. In the shape of the code - names illustrative:
type Config struct {
Orders string `env:"ORDERS_POSTGRES_URL"` // a resource address by hand
Hook string `env:"CALLBACK_URL"` // no resource type in the name: passes
}
config snapshot at start:
ErrConnectionKeyByHand: ORDERS_POSTGRES_URL at internal/config/config.go:<line>
The file and the line are the point. A guard that says "something in your config is wrong" sends a person reading the whole package. A guard that says which key, in which file, on which line sends them to one place - and the fix is always the same: delete the hand-made field and declare the resource in the manifest, so the generator prints the field for you.
| What a service writes | What the snapshot says | What to do |
|---|---|---|
| a resource in the manifest | fine, the field is printed | nothing |
| a hand-written key named after a resource type |
ErrConnectionKeyByHand with file and line |
move it to the manifest |
| an address swapped in code | the same error | move it to the manifest |
a _URL key that is not a resource (a callback, a webhook) |
fine | nothing |
What went wrong
The first version of the guard was Gus cutting the doorbell. It treated every key ending in _URL as a connection to a resource. That is a reasonable first guess - most resource addresses do end in _URL - and it was wrong for one service in a very ordinary way. The gateway reads addresses that are not resources declared to the platform. Those keys end in _URL too, and the guard blocked them.
The fix was to narrow the rule, not to add exceptions. Keys whose name does not carry a resource type now pass; a key named after a resource type still fails. The difference matters to me. A list of allowed key names would have fixed the gateway that day and broken the next service tomorrow. A rule about what the name says fixes the class.
This narrowing was one of six complaints that three services brought to the library at once, and the last part of this series is about all six. The lesson that belongs here: a guard is a claim about the world, and its first version claims too much. Ship it, watch who it blocks, and narrow the rule rather than growing a list of exceptions.
Why at start
Why is this not a linter? It could be, and a linter would run earlier. The snapshot has one property a linter does not: it runs in every service, in every environment, on the code that actually starts, with no setup in the service. A service that skips the linter does not skip its own start. And the error at start says the same thing on a laptop, on a stand and in production - which is what I want from a rule about wires.
What I do not claim
- Not how many hand-made keys the guard has caught. That is not counted.
- Not that the narrowed rule is final. It is the second version of a claim.
- Not that a linter would be worse. It would be earlier; this is everywhere.
- Not the exact error text. The block above is a form; the error name is the library's.
- Not that the gateway was wrong. Its keys were never resources.
What of this is method
- One plan for every wire. A connection is declared in the manifest or it does not exist.
- Point at the line. An error with a file and a line is a fix, not a hunt.
- Narrow the rule, not the list. When a guard blocks the wrong thing, change what it means.
Zoo keeping, built in - Part 14. Next: The Empty Sign That Erased The Real One.
If your services read connection strings, where are they declared - in one place or in each struct? If you run a config guard, does it tell you the line? If a guard of yours blocked the wrong keys, did you add exceptions or change the rule? Which wire in your zoo is not on the plan?










Top comments (0)