From catalog to policy file
About 966 words
This page follows a policy from the YAML in your repositories to the file in a pull request: who checks what, when, and why the work is split the way it is.
In the catalog: shapes and relations
The catalog module registers kind: Tailscale with the catalog. When the catalog processes a Tailscale entity, the module checks its shape: that spec.type is one of the seven types, that required fields are there, that lists are lists of strings, that a tag looks like tag:<name>, that a capability looks like <domain>/cap/<name>, that ports parse. A malformed entity fails processing with a message that names each problem, and the catalog doesn't take that version of it. The backend checks the shape again when it renders, in case the catalog still holds an older, invalid copy.
It doesn't check what the entity refers to. Whether group:admins exists, or tag:server is declared, depends on other entities that may not be processed yet, or that live in another repository. Those checks wait until the whole catalog is in view.
The module also emits relations: from each entity to its owner, its system, its policy (partOfPolicy) and the entities it applies to (appliesTo), each with its reverse. That's what puts a policy's parts on the policy's page and a rule on its owner's page. Relations lists them.
In the backend: one pass over the catalog
The backend plugin asks the catalog for every Tailscale entity, Group, User, Component and API, and every Resource of spec.type: kubernetes-cluster. It fetches only the fields it reads: kind, name, namespace, title, description, annotations, spec and relations. It does this with its own service credentials, at most once a minute for the console, and fresh for each publishing run.
Then it works through the policies one by one, in order of name:
- Collect the parts. Each Tailscale entity joins the policy its
spec.targetnames. A part whose target isn't a policy is an orphan; it's reported, and rendered nowhere. - Declare the tags. Every tag entity of the policy declares one tag. A second entity for the same tag is an error, and only the first one counts.
- Resolve every name. Each source, destination, owner, approver and test target is checked against the catalog and the policy: groups against Backstage Groups,
user:references against Users and their logins, tags against the tag entities, hosts againstspec.hosts, ipsets againstsettings.ipsets. What resolves becomes the name the file uses; what doesn't is an error, and is written as it was. - Make the catalog grants. With
catalogGrants: true, the backend reads the tailnet annotations of APIs and Components, finds the consumptions that cross the tailnet, drops the ones a grant entity already allows, and merges the rest. How catalog grants are merged has the details. - Check ports and classify destinations. Grants on every port to a Service or with capabilities get a warning. Every grant destination gets a class, and a device without a reason gets a warning.
- Collect the groups. Every annotated Group, and every other Group a rule used, with its members' logins.
- Render. The file is written section by section, with each entity's title and description as a comment.
The rendered file
The backend writes the file in the style of Tailscale's own policy files: HuJSON with tabs, trailing commas, short lists on one line, and comments above what they describe. The sections always come in the same order: groups, hosts, tagOwners, nodeAttrs, autoApprovers, ssh, grants, tests, sshTests, then the policy's settings. An empty section is left out.
While writing, it counts lines. Every entity knows which lines it rendered to, which is how the console highlights an entity's lines and how the entity card shows its excerpt.
The file starts with a comment that says where it came from:
// Our tailnet (example.ts.net). Rendered by Backstage from 5 Tailscale entities
// and the catalog's users and groups; change those, not this file.
// https://backstage.example.com/catalog/default/tailscale/my-tailnetProblems don't block
Neither the console nor publishing waits for a clean policy. A name that doesn't resolve is written as it was, a member without a login is written as their entity reference, and the problem goes on the list. The console shows the list in Health, the pull request in its Problems section, and backstage-tailscale-diff on its output and in its exit status.
So one broken entity in one team's repository doesn't hold back everyone else's changes on their way to the policy repository. The place that decides whether a file goes live is the policy repository, where a person merges the pull request and the tailnet's own validation runs when the file is applied.
Publishing
The publishing task runs every hour. For each policy with spec.publish whose repository tailscale.allowedRepos lists, it compares the rendered file with the one in the repository and, if they differ, updates the pull request or pushes. Publish to GitHub covers both flows.
The plugin never talks to the Tailscale API. What's live on the tailnet is whatever the policy repository last applied.
