gunk-dev/the-valley

main.go

source1c37c8d22741main's published tip history
7.8 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. So does one stating a
71PIN or touch policy other than the values Yubico specifies: 1, 2 and 3.
72Each attestation that verifies is reported on stderr with the YubiKey's
73serial number, firmware, PIN policy and touch policy, for whoever reviews
74the enrollment.
75
76A key that approves belongs to one principal: one also listed under
77another principal fails the render.
78
79The registry is read with git's replacement refs off and with no GIT_
80variable or user or system configuration inherited, so the registry
81compiled is the one the tip holds and not an object something else has
82been made to stand in for.
83
84Every key of the registry's export is read spelled exactly as the schema
85spells its field. A key the schema does not give fails the render, and so
86does a key that appears twice in one object. A registry may reopen a
87schema definition and add a field to it, such as Grants beside grants, and
88Go's JSON decoder alone would read that field as grants.
89
90Expiry is enforced here, because a document has no clock. An entry whose
91expiry has arrived is omitted from every artifact and noted on stderr, and
92the rest of the registry still compiles — so access ends at the first
93convergence after expiry.
94
95One key authorized as two principals is refused too, whether both entries
96are the registry's or one is the host's own (--declared-keys). sshd
97authorizes a key by the first entry that names it, so which principal such
98a key pushes as would depend on the order of lines and files.
99
100One state is refused rather than compiled: a registry where, after the
101expiry cut, no remaining principal holds a grant at a boundary of kind
102"registry". Nobody would govern the registry's own stream, so the whole
103render fails with an error naming governanceOrphaned and the last good
104artifacts stand. The rule is here and not in the schema for the same reason
105expiry is — the state is reached by the clock, and a document has none.
106
107A render that fails leaves the last good artifacts exactly as they were.
108All of them are computed in full before any is written, and each is
109replaced by rename. A schema violation, an unreachable repository or a bug
110here costs the compilation and never the git user's access.
111
112flags:
113 --repo DIR the bare instance repository carrying the registry
114 --ref REF the ref whose tip is read, a full ref name read by
115 that exact name (default refs/heads/main). An
116 instance repository with no such ref has no
117 registry, and the compilation fails
118 --dir DIR the registry's directory in that tip (default
119 identity)
120 --schema FILE the identity schema (default
121 $VALLEY_IDENTITY_SCHEMA)
122 --known-signers FILE where the verifier keys are written
123 --authorized-keys FILE where the tagged authorized_keys is written
124 --grants FILE where the grants the pre-receive hook checks are
125 written
126 --approval-signers FILE
127 where the verifier keys of every approval key are
128 written. None is written without it.
129 --approval-signers-hardware FILE
130 where the verifier keys of the hardware approval
131 keys are written. None is written without it.
132 --declared-keys FILE the authorized_keys lines the host declares by
133 hand. A registry key that is also declared under
134 another tag, or untagged, fails the render: sshd
135 reads the declared entry first, so the key would
136 never push as its registry principal.
137 --now YYYY-MM-DD the day expiry, and the validity of an
138 attestation's certificates, are judged against
139 (default: today, UTC)
140`
141
142func main() {
143 if len(os.Args) < 2 {
144 fmt.Fprint(os.Stderr, usage)
145 os.Exit(2)
146 }
147 var err error
148 switch os.Args[1] {
149 case "compile":
150 err = compile(os.Args[2:])
151 case "help", "-h", "--help":
152 fmt.Print(usage)
153 return
154 default:
155 fmt.Fprintf(os.Stderr, "identity: unknown command %q\n\n%s", os.Args[1], usage)
156 os.Exit(2)
157 }
158 if err != nil {
159 fmt.Fprintf(os.Stderr, "identity: %v\n", err)
160 os.Exit(1)
161 }
162}