DEV Community

Anton Brilliantov
Anton Brilliantov

Posted on

A Cage You Cannot Wire By Hand

A Cage You Cannot Wire By Hand - the cover: Gus the quartermaster with a scroll labelled service.yaml lays one tidy cable to an enclosure, while Biscuit holds a sparking tangle of hand-made wires under a red flag that reads file:line, and Nora raises an eyebrow

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.

A four-panel comic: Biscuit next to a lamppost says my own wire, quick, and Nora asks where it goes; Gus next to a sign plan - service.yaml says wires go where the plan says; Pickle next to a sign file:line says this file, this line, and Biscuit says busted; Gus says your doorbell is not a cage wire and Nora says thank you

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.

Three tiles with faces: the service manifest, proud; the config snapshot, happy; the connection, happy; labels read declared resource, and checked at start; a pill reads tested

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>
Enter fullscreen mode Exit fullscreen mode

Gus the quartermaster holds his wiring plan - a manifest board with the infra block, postgres main and rabbitmq events, each with an image line - and one tidy cable runs from it to an enclosure

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
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode
config snapshot at start:
ErrConnectionKeyByHand: ORDERS_POSTGRES_URL at internal/config/config.go:<line>
Enter fullscreen mode Exit fullscreen mode

A service-yard scene, no code: one tidy cable comes from the service.yaml blueprint, a hand-tied wire sneaks in from the side, and the guard post stops it with an alarm ErrConnectionKeyByHand and a tag pointing at file:line; a pill reads tested

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.

An animation: a letter travels under four tiles - the manifest, config keys, a hand-made key, start - and the pill changes from declared resources to keys from the plan: fine, then ErrConnectionKeyByHand at file:line, then start refused until the key goes to the plan

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

Bars: places a connection may be declared 1; ways a hand-made wire is caught 2; what the error points at - file, line - 2; Pickle, proud, says I know the exact line; a pill reads counted from the guard's description

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.

Columns: keys the guard stops - first, any _URL key, wide; now, only keys with a resource type in the name, narrow; Gus, happy, says doorbells pass now; a pill reads a shape, not a measurement

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.

Before and after: a key by hand - hidden, an address nobody declared; drift, the plan and the code disagree - and a key by plan - declared, the manifest names the resource; caught, file and line at start; a pill reads tested

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.

Service tiles with faces: the manifest proud, the config snapshot and the declared connection happy, a hand-made key angry; a callback URL happy, the first guard worried, the narrowed guard happy, other services happy; pills read tested, and narrowed after it blocked another service's keys

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)