gunk-dev/the-valley

main.go

sourced9f7bacd171bmain's published tip history
8.7 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
59Each approval signers file opens with one line naming what it renders:
60
61 # valley-approval-authority-v1 ref=REF commit=OID day=YYYY-MM-DD
62
63REF is --ref, OID is the commit REF resolved to, which every document was
64read from, and the day is the one expiry was judged on: --now, or today
65in UTC. Every reader of verifier keys reads the line as a comment. The
66signers publication leaves it out, so a compilation that changes only the
67stamp changes nothing published.
68
69approval-signers lists every key of every unexpired human holding the
70approve grant. approval-signers-hardware lists the keys from it whose
71entry carries a YubiKey attestation that verifies and whose touch policy
72is always: the YubiKey asks for a touch before every signature.
73
74An attestation is two PEM certificates: the one the YubiKey wrote for the
75PIV slot holding the key, and the YubiKey's own f9 certificate, which
76signed it. It verifies when the slot certificate chains through the f9
77certificate and Yubico's PIV intermediates to Yubico Attestation Root 1,
78and the key it names is the entry's public key. This program carries that
79root and those intermediates, and a registry cannot add others. An
80attestation that does not verify fails the render. So does one stating a
81PIN or touch policy other than the values Yubico specifies: 1, 2 and 3.
82Each attestation that verifies is reported on stderr with the YubiKey's
83serial number, firmware, PIN policy and touch policy, for whoever reviews
84the enrollment.
85
86A key that approves belongs to one principal: one also listed under
87another principal fails the render.
88
89The registry is read with git's replacement refs off and with no GIT_
90variable or user or system configuration inherited, so the registry
91compiled is the one the tip holds and not an object something else has
92been made to stand in for.
93
94Every key of the registry's export is read spelled exactly as the schema
95spells its field. A key the schema does not give fails the render, and so
96does a key that appears twice in one object. A registry may reopen a
97schema definition and add a field to it, such as Grants beside grants, and
98Go's JSON decoder alone would read that field as grants.
99
100Expiry is enforced here, because a document has no clock. An entry whose
101expiry has arrived is omitted from every artifact and noted on stderr, and
102the rest of the registry still compiles — so access ends at the first
103convergence after expiry.
104
105One key authorized as two principals is refused too, whether both entries
106are the registry's or one is the host's own (--declared-keys). sshd
107authorizes a key by the first entry that names it, so which principal such
108a key pushes as would depend on the order of lines and files.
109
110One state is refused rather than compiled: a registry where, after the
111expiry cut, no remaining principal holds a grant at a boundary of kind
112"registry". Nobody would govern the registry's own stream, so the whole
113render fails with an error naming governanceOrphaned and the last good
114artifacts stand. The rule is here and not in the schema for the same reason
115expiry is — the state is reached by the clock, and a document has none.
116
117A render that fails leaves the last good artifacts exactly as they were.
118All of them are computed in full before any is written, and each is
119replaced by rename. A schema violation, an unreachable repository or a bug
120here costs the compilation and never the git user's access.
121
122After writing, the compiler reads the ref and the day again, and compiles
123again until a reading matches what it compiled, so a push landing on the
124ref while it compiles is compiled before it exits. A compilation that
125fails ends the run only when the reading after it matches what it
126compiled. The retry budget is three compilations, two with --now: room
127for the ref to move once and the day to turn once. A run that still reads
128a change after spending it fails.
129
130flags:
131 --repo DIR the bare instance repository carrying the registry
132 --ref REF the ref whose tip is read, a full ref name read by
133 that exact name (default refs/heads/main). An
134 instance repository with no such ref has no
135 registry, and the compilation fails
136 --dir DIR the registry's directory in that tip (default
137 identity)
138 --schema FILE the identity schema (default
139 $VALLEY_IDENTITY_SCHEMA)
140 --known-signers FILE where the verifier keys are written
141 --authorized-keys FILE where the tagged authorized_keys is written
142 --grants FILE where the grants the pre-receive hook checks are
143 written
144 --approval-signers FILE
145 where the verifier keys of every approval key are
146 written. None is written without it.
147 --approval-signers-hardware FILE
148 where the verifier keys of the hardware approval
149 keys are written. None is written without it.
150 --declared-keys FILE the authorized_keys lines the host declares by
151 hand. A registry key that is also declared under
152 another tag, or untagged, fails the render: sshd
153 reads the declared entry first, so the key would
154 never push as its registry principal.
155 --now YYYY-MM-DD the day expiry, and the validity of an
156 attestation's certificates, are judged against
157 (default: today, UTC)
158`
159
160func main() {
161 if len(os.Args) < 2 {
162 fmt.Fprint(os.Stderr, usage)
163 os.Exit(2)
164 }
165 var err error
166 switch os.Args[1] {
167 case "compile":
168 err = compile(os.Args[2:])
169 case "help", "-h", "--help":
170 fmt.Print(usage)
171 return
172 default:
173 fmt.Fprintf(os.Stderr, "identity: unknown command %q\n\n%s", os.Args[1], usage)
174 os.Exit(2)
175 }
176 if err != nil {
177 fmt.Fprintf(os.Stderr, "identity: %v\n", err)
178 os.Exit(1)
179 }
180}