| 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. |
| 11 | package main |
| 12 | |
| 13 | import ( |
| 14 | "fmt" |
| 15 | "os" |
| 16 | ) |
| 17 | |
| 18 | const 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 | |
| 24 | The registry is one declared CUE document in the instance repository |
| 25 | (dcr-b87f6e8), read from that repository's integrated tip and never from a |
| 26 | working tree: an edit in a branch or a worktree governs nothing until it |
| 27 | lands. Three artifacts come out of it. |
| 28 | |
| 29 | The known-signers file is every attestation-capable key, written as the |
| 30 | note format's verifier keys. It is both the integrator's acceptance list |
| 31 | and any reader's ` + "`attest verify --known-keys`" + `, so it holds every key that |
| 32 | signs — the integrator's own included. A key's signing name is recorded per |
| 33 | key, because the note format's key hash binds the name to the key: the same |
| 34 | key published under a second name is a second, unrelated verifier key. |
| 35 | |
| 36 | The authorized_keys file is every key of every principal holding a grant at |
| 37 | a boundary of kind "git-push", each line tagging its key with the |
| 38 | principal's name. The git user's shell reads the tag off the entry of the |
| 39 | key sshd authenticated and hands it to the pre-receive hook, which is the |
| 40 | only thing that can tell one pusher from another over a shared git user. |
| 41 | |
| 42 | The grants file is every grant the pre-receive hook checks, one line per |
| 43 | holder: "request <principal>" for each principal holding the approve grant, |
| 44 | at 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 |
| 46 | kinds are one grant: the approve grant, also accepted as request. A |
| 47 | principal holding both is written once, and a registry using either kind |
| 48 | compiles the same file. The hook takes writes to the integration request |
| 49 | namespace only from a holder of the approve grant. It takes a version |
| 50 | added to another principal's ready change, and a restack report, only from |
| 51 | one named under restack. |
| 52 | |
| 53 | The registry is read with git's replacement refs off and with no GIT_ |
| 54 | variable or user or system configuration inherited, so the registry |
| 55 | compiled is the one the tip holds and not an object something else has |
| 56 | been made to stand in for. |
| 57 | |
| 58 | Expiry is enforced here, because a document has no clock. An entry whose |
| 59 | expiry has arrived is omitted from every artifact and noted on stderr, and |
| 60 | the rest of the registry still compiles — so access ends at the first |
| 61 | convergence after expiry. |
| 62 | |
| 63 | One key authorized as two principals is refused too, whether both entries |
| 64 | are the registry's or one is the host's own (--declared-keys). sshd |
| 65 | authorizes a key by the first entry that names it, so which principal such |
| 66 | a key pushes as would depend on the order of lines and files. |
| 67 | |
| 68 | One state is refused rather than compiled: a registry where, after the |
| 69 | expiry 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 |
| 71 | render fails with an error naming governanceOrphaned and the last good |
| 72 | artifacts stand. The rule is here and not in the schema for the same reason |
| 73 | expiry is — the state is reached by the clock, and a document has none. |
| 74 | |
| 75 | A render that fails leaves the last good artifacts exactly as they were. |
| 76 | All of them are computed in full before any is written, and each is |
| 77 | replaced by rename. A schema violation, an unreachable repository or a bug |
| 78 | here costs the compilation and never the git user's access. |
| 79 | |
| 80 | flags: |
| 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 | |
| 100 | func 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 | } |