gunk-dev/the-valley

main.go

sourcedee3830391efmain's published tip history
7.1 KiB
1// identity compiles a valley instance's identity registry — the declared
2// CUE document naming who the instance grants something to (dcr-b87f6e8) —
3// into the artifacts its enforcement boundaries check.
4//
5// It renders, and enforces nothing itself. sshd refuses a key that is not
6// in the authorized_keys it writes, the integrator refuses evidence signed
7// by nobody in the known-signers it writes, and the pre-receive hook
8// refuses an integration request or a restack write from anybody not in
9// the grants it writes; this program is the step between the document and
10// those three gates. It also writes the two approval signers files, which
11// no gate reads.
12package main
13
14import (
15 "fmt"
16 "os"
17)
18
19const usage = `usage: identity <command> [flags]
20
21 compile render the registry at a repository's tip into the artifacts
22 the enforcement boundaries check
23 help print this message
24
25The registry is one declared CUE document in the instance repository
26(dcr-b87f6e8), read from that repository's integrated tip and never from a
27working tree: an edit in a branch or a worktree governs nothing until it
28lands. Three artifacts come out of it, and two approval signers files when
29asked for.
30
31The known-signers file is every attestation-capable key, written as the
32note format's verifier keys. It is both the integrator's acceptance list
33and any reader's ` + "`attest verify --known-keys`" + `, so it holds every key that
34signs — the integrator's own included. A key's signing name is recorded per
35key, because the note format's key hash binds the name to the key: the same
36key published under a second name is a second, unrelated verifier key.
37
38The authorized_keys file is every key of every principal holding a grant at
39a boundary of kind "git-push", each line tagging its key with the
40principal's name. The git user's shell reads the tag off the entry of the
41key sshd authenticated and hands it to the pre-receive hook, which is the
42only thing that can tell one pusher from another over a shared git user.
43
44The grants file is every grant the pre-receive hook checks, one line per
45holder: "request <principal>" for each principal holding the approve grant,
46at a boundary of kind "approve" or of kind "request", and "restack
47<principal>" for each holding one at a boundary of kind "restack". The two
48kinds are one grant: the approve grant, also accepted as request. A
49principal holding both is written once, and a registry using either kind
50compiles the same file. The hook takes writes to the integration request
51namespace only from a holder of the approve grant. It takes a version
52added to another principal's ready change, and a restack report, only from
53one named under restack.
54
55The two approval signers files hold the keys whose signed approvals
56count. They are written as the note format's verifier keys, and each line
57is named by the principal who approves with its key. No gate reads them.
58
59approval-signers lists every key of every unexpired human holding the
60approve grant. approval-signers-hardware lists the keys from it whose
61entry carries a YubiKey attestation that verifies and whose touch policy
62is always: the YubiKey asks for a touch before every signature.
63
64An attestation is two PEM certificates: the one the YubiKey wrote for the
65PIV slot holding the key, and the YubiKey's own f9 certificate, which
66signed it. It verifies when the slot certificate chains through the f9
67certificate and Yubico's PIV intermediates to Yubico Attestation Root 1,
68and the key it names is the entry's public key. This program carries that
69root and those intermediates, and a registry cannot add others. An
70attestation that does not verify fails the render. Each one that verifies
71is reported on stderr with the YubiKey's serial number, firmware, PIN
72policy and touch policy, for whoever reviews the enrollment.
73
74A key that approves belongs to one principal: one also listed under
75another principal fails the render.
76
77The registry is read with git's replacement refs off and with no GIT_
78variable or user or system configuration inherited, so the registry
79compiled is the one the tip holds and not an object something else has
80been made to stand in for.
81
82Expiry is enforced here, because a document has no clock. An entry whose
83expiry has arrived is omitted from every artifact and noted on stderr, and
84the rest of the registry still compiles — so access ends at the first
85convergence after expiry.
86
87One key authorized as two principals is refused too, whether both entries
88are the registry's or one is the host's own (--declared-keys). sshd
89authorizes a key by the first entry that names it, so which principal such
90a key pushes as would depend on the order of lines and files.
91
92One state is refused rather than compiled: a registry where, after the
93expiry cut, no remaining principal holds a grant at a boundary of kind
94"registry". Nobody would govern the registry's own stream, so the whole
95render fails with an error naming governanceOrphaned and the last good
96artifacts stand. The rule is here and not in the schema for the same reason
97expiry is — the state is reached by the clock, and a document has none.
98
99A render that fails leaves the last good artifacts exactly as they were.
100All of them are computed in full before any is written, and each is
101replaced by rename. A schema violation, an unreachable repository or a bug
102here costs the compilation and never the git user's access.
103
104flags:
105 --repo DIR the bare instance repository carrying the registry
106 --ref REF the ref whose tip is read (default refs/heads/main)
107 --dir DIR the registry's directory in that tip (default
108 identity)
109 --schema FILE the identity schema (default
110 $VALLEY_IDENTITY_SCHEMA)
111 --known-signers FILE where the verifier keys are written
112 --authorized-keys FILE where the tagged authorized_keys is written
113 --grants FILE where the grants the pre-receive hook checks are
114 written
115 --approval-signers FILE
116 where the verifier keys of every approval key are
117 written. None is written without it.
118 --approval-signers-hardware FILE
119 where the verifier keys of the hardware approval
120 keys are written. None is written without it.
121 --declared-keys FILE the authorized_keys lines the host declares by
122 hand. A registry key that is also declared under
123 another tag, or untagged, fails the render: sshd
124 reads the declared entry first, so the key would
125 never push as its registry principal.
126 --now YYYY-MM-DD the day expiry is judged against (default: today,
127 UTC)
128`
129
130func main() {
131 if len(os.Args) < 2 {
132 fmt.Fprint(os.Stderr, usage)
133 os.Exit(2)
134 }
135 var err error
136 switch os.Args[1] {
137 case "compile":
138 err = compile(os.Args[2:])
139 case "help", "-h", "--help":
140 fmt.Print(usage)
141 return
142 default:
143 fmt.Fprintf(os.Stderr, "identity: unknown command %q\n\n%s", os.Args[1], usage)
144 os.Exit(2)
145 }
146 if err != nil {
147 fmt.Fprintf(os.Stderr, "identity: %v\n", err)
148 os.Exit(1)
149 }
150}