gunk-dev/the-valley

main.go

source9d8f53c82aaemain's published tip history
5.2 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 a grant at a
44boundary of kind "request", and "restack <principal>" for each holding one
45at a boundary of kind "restack". The hook takes writes to the integration
46request namespace only from a principal named under request. It takes a
47version added to another principal's ready change, and a restack report,
48only from one named under restack.
49
50The registry is read with git's replacement refs off and with no GIT_
51variable or user or system configuration inherited, so the registry
52compiled is the one the tip holds and not an object something else has
53been made to stand in for.
54
55Expiry is enforced here, because a document has no clock. An entry whose
56expiry has arrived is omitted from every artifact and noted on stderr, and
57the rest of the registry still compiles — so access ends at the first
58convergence after expiry.
59
60One key authorized as two principals is refused too, whether both entries
61are the registry's or one is the host's own (--declared-keys). sshd
62authorizes a key by the first entry that names it, so which principal such
63a key pushes as would depend on the order of lines and files.
64
65One state is refused rather than compiled: a registry where, after the
66expiry cut, no remaining principal holds a grant at a boundary of kind
67"registry". Nobody would govern the registry's own stream, so the whole
68render fails with an error naming governanceOrphaned and the last good
69artifacts stand. The rule is here and not in the schema for the same reason
70expiry is — the state is reached by the clock, and a document has none.
71
72A render that fails leaves the last good artifacts exactly as they were.
73All of them are computed in full before any is written, and each is
74replaced by rename. A schema violation, an unreachable repository or a bug
75here costs the compilation and never the git user's access.
76
77flags:
78 --repo DIR the bare instance repository carrying the registry
79 --ref REF the ref whose tip is read (default refs/heads/main)
80 --dir DIR the registry's directory in that tip (default
81 identity)
82 --schema FILE the identity schema (default
83 $VALLEY_IDENTITY_SCHEMA)
84 --known-signers FILE where the verifier keys are written
85 --authorized-keys FILE where the tagged authorized_keys is written
86 --grants FILE where the grants the pre-receive hook checks are
87 written
88 --declared-keys FILE the authorized_keys lines the host declares by
89 hand. A registry key that is also declared under
90 another tag, or untagged, fails the render: sshd
91 reads the declared entry first, so the key would
92 never push as its registry principal.
93 --now YYYY-MM-DD the day expiry is judged against (default: today,
94 UTC)
95`
96
97func main() {
98 if len(os.Args) < 2 {
99 fmt.Fprint(os.Stderr, usage)
100 os.Exit(2)
101 }
102 var err error
103 switch os.Args[1] {
104 case "compile":
105 err = compile(os.Args[2:])
106 case "help", "-h", "--help":
107 fmt.Print(usage)
108 return
109 default:
110 fmt.Fprintf(os.Stderr, "identity: unknown command %q\n\n%s", os.Args[1], usage)
111 os.Exit(2)
112 }
113 if err != nil {
114 fmt.Fprintf(os.Stderr, "identity: %v\n", err)
115 os.Exit(1)
116 }
117}