Your first policy
About 1112 words
This tutorial builds a small tailnet policy from catalog entities: one tailnet group with one person in it, one tag, a grant, a Tailscale SSH rule and two tests. At the end you'll have the rendered policy file in the console and, if you like, checked on your machine with backstage-tailscale-diff.
It assumes you've installed the plugins and allowed kind: Tailscale from a location. The examples use a repository acme/network with the Tailscale entities under tailscale/policy/ and the people in a separate file the catalog already reads.
1. The policy
Every policy is one entity of spec.type: policy. It holds what belongs to the file as a whole. Here that's just the tailnet's name, which the console and the file's header comment show.
# tailscale/policy/policy.yaml
apiVersion: specht-labs.de/v1alpha1
kind: Tailscale
metadata:
name: my-tailnet
title: Our tailnet
spec:
type: policy
owner: group:platform
tailnet: example.ts.netspec.owner is a Backstage entity reference, as on any other entity. It's the team that owns the policy in the catalog, and it doesn't appear in the file.
2. The people
Groups and users come from the catalog. A Backstage Group becomes a tailnet group through the specht-labs.de/tailnet-group annotation, and a User gets a Tailscale login through specht-labs.de/tailnet-login:
# people.yaml, wherever your Groups and Users live
apiVersion: backstage.io/v1alpha1
kind: Group
metadata:
name: tailnet-admins
description: People who administer the tailnet
annotations:
specht-labs.de/tailnet-group: group:admins
spec:
type: team
children: []
---
apiVersion: backstage.io/v1alpha1
kind: User
metadata:
name: alice
annotations:
specht-labs.de/tailnet-login: alice@github
spec:
profile:
displayName: Alice
memberOf: [tailnet-admins]Use the login exactly as your tailnet knows it, such as alice@github or [email protected].
3. A tag
A tag has to be declared before a rule can use it. One Tailscale entity of spec.type: tag declares one tag and says who may give it to a device:
# tailscale/policy/tags.yaml
apiVersion: specht-labs.de/v1alpha1
kind: Tailscale
metadata:
name: tag-server
title: Servers
spec:
type: tag
target: tailscale:my-tailnet
owner: group:platform
tag: tag:server
owners: [group:admins]spec.target names the policy this piece belongs to. Every Tailscale entity except the policy itself needs one.
4. A grant, an SSH rule and tests
# tailscale/policy/access.yaml
apiVersion: specht-labs.de/v1alpha1
kind: Tailscale
metadata:
name: grant-admins-grafana
title: Admins use Grafana
spec:
type: grant
target: tailscale:my-tailnet
owner: group:platform
src: [group:admins]
dst: [svc:grafana]
ip: [tcp:443]
---
apiVersion: specht-labs.de/v1alpha1
kind: Tailscale
metadata:
name: ssh-admins-servers
title: Admins log in to servers
spec:
type: ssh
target: tailscale:my-tailnet
owner: group:platform
action: check
checkPeriod: 12h
src: [group:admins]
dst: [tag:server]
users: [root, autogroup:nonroot]
---
apiVersion: specht-labs.de/v1alpha1
kind: Tailscale
metadata:
name: test-admins
title: Alice can't reach servers on 443, and SSH as root asks her to sign in again
spec:
type: test
target: tailscale:my-tailnet
owner: group:platform
tests:
- src: user:alice
deny: ['tag:server:443']
sshTests:
src: user:alice
dst: [tag:server]
check: [root]Apart from type, target, owner, system and appliesTo, the fields are Tailscale's own and mean what they mean in a policy file. Two things are different from writing the file by hand:
group:adminsmust be a group the catalog knows, andtag:servera tag a tag entity in this policy declares. Otherwise the console reports an error.user:aliceis a reference to the Backstage Useralice. It renders as her login,alice@github.
5. Look at the result
Commit the files and let the catalog pick them up. Open /tailscale, or the Policy tab on the my-tailnet entity, and go to Policy file. Backstage renders this:
// 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-tailnet
{
"groups": {
// tailnet-admins
"group:admins": [
"alice@github",
],
},
"tagOwners": {
// Servers
"tag:server": ["group:admins"],
},
"ssh": [
// Admins log in to servers
{
"action": "check",
"checkPeriod": "12h",
"src": ["group:admins"],
"dst": ["tag:server"],
"users": ["root", "autogroup:nonroot"],
},
],
"grants": [
// Admins use Grafana
{
"src": ["group:admins"],
"dst": ["svc:grafana"],
"ip": ["tcp:443"],
},
],
"tests": [
// Alice can't reach servers on 443, and SSH as root asks her to sign in again
{
"src": "alice@github",
"deny": ["tag:server:443"],
},
],
"sshTests": [
// Alice can't reach servers on 443, and SSH as root asks her to sign in again
{
"src": "alice@github",
"dst": ["tag:server"],
"check": ["root"],
},
],
}Each entity's title (and description, if it has one) becomes the comment above what it renders to. The third line links back to the policy entity; it uses app.baseUrl and is missing when that isn't set.
The Overview section counts what's in each section and draws the access map. Its Health card should say there are no problems. If it lists some, Problems explains each message.
The catalog relates each entity as well: tag-server is partOfPolicy of my-tailnet and ownedBy group:platform, and both get the reverse relation. Relations lists them all.
6. Check it on your machine (optional)
You don't have to wait for the catalog. backstage-tailscale-diff renders the same file from a local checkout and compares it with a policy file. Every directory you pass must be in a git working tree. Compare with an empty policy the first time:
$ echo '{}' > /tmp/empty.hujson
$ npx -p @spechtlabs/backstage-plugin-tailscale-backend backstage-tailscale-diff \
--out /tmp/rendered.hujson /tmp/empty.hujson ~/src/acme/network ~/src/acme/catalog
Our tailnet: 7 entities, 5 parts, 52 lines -> /tmp/rendered.hujson
Services: svc:grafana
grants:
+ group:admins -> svc:grafana tcp:443 (only rendered)
groups:
+ group:admins has alice@github (only rendered)
tagOwners:
+ tag:server owned by group:admins (only rendered)
ssh:
+ {"action":"check","checkPeriod":"12h","dst":["tag:server"],"src":["group:admins"],"users":["autogroup:nonroot","root"]} (only rendered)
tests:
+ alice@github deny tag:server:443 (only rendered)
sshTests:
+ alice@github -> ["tag:server"] check root (only rendered)
6 differencesThe first line counts every catalog entity it read in the directories, and the parts of the policy, the policy entity included. The exit status is 1 because the files differ. Run it against your current policy file instead of the empty one to see what moving your tailnet into the catalog would change. Check a change locally has the details.
Next
- Grants from the catalog lets the catalog's APIs and Components write grants for you.
- Publish to GitHub sends the file to the repository that applies it.
- kind: Tailscale lists every type and field.
