gunk-dev/the-valley

main.go

source52be41028750main's published tip history
5.4 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.
11package main
12
13import (
14 "fmt"
15 "os"
16)
17
18const usage = `usage: identity <command> [flags]
19
20 compile render the registry at a repository's tip into the artifacts
21 the enforcement boundaries check
22 help print this message
23
24The registry is one declared CUE document in the instance repository
25(dcr-b87f6e8), read from that repository's integrated tip and never from a
26working tree: an edit in a branch or a worktree governs nothing until it
27lands. Three artifacts come out of it.
28
29The known-signers file is every attestation-capable key, written as the
30note format's verifier keys. It is both the integrator's acceptance list
31and any reader's ` + "`attest verify --known-keys`" + `, so it holds every key that
32signs — the integrator's own included. A key's signing name is recorded per
33key, because the note format's key hash binds the name to the key: the same
34key published under a second name is a second, unrelated verifier key.
35
36The authorized_keys file is every key of every principal holding a grant at
37a boundary of kind "git-push", each line tagging its key with the
38principal's name. The git user's shell reads the tag off the entry of the
39key sshd authenticated and hands it to the pre-receive hook, which is the
40only thing that can tell one pusher from another over a shared git user.
41
42The grants file is every grant the pre-receive hook checks, one line per
43holder: "request <principal>" for each principal holding the approve grant,
44at a boundary of kind "approve" or of kind "request", and "restack
45<principal>" for each holding one at a boundary of kind "restack". The two
46kinds are one grant: the approve grant, also accepted as request. A
47principal holding both is written once, and a registry using either kind
48compiles the same file. The hook takes writes to the integration request
49namespace only from a holder of the approve grant. It takes a version
50added to another principal's ready change, and a restack report, only from
51one named under restack.
52
53The registry is read with git's replacement refs off and with no GIT_
54variable or user or system configuration inherited, so the registry
55compiled is the one the tip holds and not an object something else has
56been made to stand in for.
57
58Expiry is enforced here, because a document has no clock. An entry whose
59expiry has arrived is omitted from every artifact and noted on stderr, and
60the rest of the registry still compiles — so access ends at the first
61convergence after expiry.
62
63One key authorized as two principals is refused too, whether both entries
64are the registry's or one is the host's own (--declared-keys). sshd
65authorizes a key by the first entry that names it, so which principal such
66a key pushes as would depend on the order of lines and files.
67
68One state is refused rather than compiled: a registry where, after the
69expiry cut, no remaining principal holds a grant at a boundary of kind
70"registry". Nobody would govern the registry's own stream, so the whole
71render fails with an error naming governanceOrphaned and the last good
72artifacts stand. The rule is here and not in the schema for the same reason
73expiry is — the state is reached by the clock, and a document has none.
74
75A render that fails leaves the last good artifacts exactly as they were.
76All of them are computed in full before any is written, and each is
77replaced by rename. A schema violation, an unreachable repository or a bug
78here costs the compilation and never the git user's access.
79
80flags:
81 --repo DIR the bare instance repository carrying the registry
82 --ref REF the ref whose tip is read (default refs/heads/main)
83 --dir DIR the registry's directory in that tip (default
84 identity)
85 --schema FILE the identity schema (default
86 $VALLEY_IDENTITY_SCHEMA)
87 --known-signers FILE where the verifier keys are written
88 --authorized-keys FILE where the tagged authorized_keys is written
89 --grants FILE where the grants the pre-receive hook checks are
90 written
91 --declared-keys FILE the authorized_keys lines the host declares by
92 hand. A registry key that is also declared under
93 another tag, or untagged, fails the render: sshd
94 reads the declared entry first, so the key would
95 never push as its registry principal.
96 --now YYYY-MM-DD the day expiry is judged against (default: today,
97 UTC)
98`
99
100func main() {
101 if len(os.Args) < 2 {
102 fmt.Fprint(os.Stderr, usage)
103 os.Exit(2)
104 }
105 var err error
106 switch os.Args[1] {
107 case "compile":
108 err = compile(os.Args[2:])
109 case "help", "-h", "--help":
110 fmt.Print(usage)
111 return
112 default:
113 fmt.Fprintf(os.Stderr, "identity: unknown command %q\n\n%s", os.Args[1], usage)
114 os.Exit(2)
115 }
116 if err != nil {
117 fmt.Fprintf(os.Stderr, "identity: %v\n", err)
118 os.Exit(1)
119 }
120}