backstage-tailscale-diff
About 612 words
Renders a tailnet policy from local catalog files, the way Backstage would, and compares it with a policy file. It ships in @spechtlabs/backstage-plugin-tailscale-backend. Check a change locally shows it in use.
Synopsis
backstage-tailscale-diff [--policy <name>] [--out <file>] [--json <file>] <policy.hujson> <catalog dir>...Run it without installing:
npx -p @spechtlabs/backstage-plugin-tailscale-backend backstage-tailscale-diff …Arguments
| Argument | Meaning |
|---|---|
<policy.hujson> | The policy file to compare with, usually the one your tailnet has applied. HuJSON: comments and trailing commas are fine. |
<catalog dir>... | One or more directories with catalog YAML. Each must be inside a git working tree. |
Relative paths resolve against the directory you run the command from. Under yarn they resolve against INIT_CWD, the directory yarn was started in.
Flags
| Flag | Meaning |
|---|---|
--policy <name> | The metadata.name of the policy entity to render. Default: the first policy by name. |
--out <file> | Write the rendered policy file here. |
--json <file> | Write the rendered overview here, as GET /api/tailscale/overview serves it but without publish. |
-h, --help | Print the usage line and exit with status 2. |
What it reads
In each directory, every file git ls-files --cached --others --exclude-standard lists that ends in .yaml or .yml. That's tracked and untracked files, without the ones git ignores. A file is only parsed if it has a line starting with apiVersion: backstage.io/ or apiVersion: specht-labs.de/.
Each YAML document with such an apiVersion, a kind and a metadata.name is an entity, except kind: Location. metadata.namespace defaults to default. A document that doesn't parse is skipped with a warning, and so is the second definition of an entity.
It then adds the relations the catalog would: consumesApi and apiConsumedBy from spec.consumesApis, providesApi and apiProvidedBy from spec.providesApis, dependsOn from spec.dependsOn, memberOf and hasMember from a User's spec.memberOf, and the Tailscale relations.
Output
<policy title>: <n> entities, <n> parts, <n> lines[ -> <out file>]
Services: …
peer meshes: …
capability targets: …
autogroups: …
devices: <name>[ (<reason>)], …
everything: *
<error|warning>: <entity ref>: <message>
<section>:
- <entry> (only in <policy file name>)
+ <entry> (only rendered)
<n> differencesDestination classes with no members are left out. The problems are the orphans and the chosen policy's errors and warnings. The sections compared are grants, groups, hosts, tagOwners, nodeAttrs, autoApprovers, ssh, tests, sshTests and settings; a section without differences isn't printed.
Without differences, the last line is No differences: the rendered policy allows what <policy file name> allows.
How entries compare
| Section | One entry is |
|---|---|
grants | <src> -> <dst> <port> for every source, destination and ip entry, followed by app …, via … and posture … when present. A grant without ip uses (none) as the port. A grant to a Service on * becomes one entry per port the catalog's APIs list for that Service. |
groups | <group> has <member> |
hosts | <name> = <address> |
tagOwners | <tag> owned by <owner> |
autoApprovers | <svc> served by <approver>, <cidr> routed by <approver>, exit node <approver> |
nodeAttrs, ssh | the whole entry, with keys sorted and list order ignored |
tests | <src>[ <proto>] <accept or deny> <target> |
sshTests | <src> -> <dst> <accept, check or deny> <user> |
settings | <key>: <value> for every other top-level key |
Exit status
| Status | Meaning |
|---|---|
| 0 | No differences, and the policy has no errors. |
| 1 | The files differ, or the policy (or an orphan) has an error. Warnings don't count. |
| 2 | Bad arguments: none given, a path that doesn't exist, a catalog path that isn't a directory, a policy file that doesn't parse, a --policy that doesn't exist, or an unexpected failure. |
