gunk-dev/the-valley

hooks.nix

sourceb160a3fca731main's published tip history
8.5 KiB
1# The hooks the module renders, run against real git. Each check follows the
2# store paths out of a rendered valley-init script, so what it drives is what
3# a host would execute — never a copy of it written for the test.
4#
5# A check that pushes through the pre-receive hook runs the real valleyhook,
6# and takes the hosts that run it. mirror-e2e, bus-e2e and init-e2e run none
7# of this flake's programs: they drive what the module renders around them,
8# so they take the hosts rendered over stand-ins (nix/checks/hosts.nix), and
9# an edit to a program does not change them.
10{
11 pkgs,
12 packages,
13 hosts,
14 stubbedHosts,
15 ...
16}:
17{
18 # What a mirror ends up holding, end to end and unmocked: a bare
19 # repo wired with the real rendered hooks, pushed to for real, the
20 # real drain run as its unit would run it, and a second bare repo
21 # standing in for the mirror. The push is easy to get subtly
22 # wrong — it must publish main and the tags, delete the tags the
23 # primary deleted, and leave every other ref on the mirror as it
24 # is — and only running git can tell.
25 # Relative mirror URLs (mirrorHost) resolve against the pushing
26 # repo, so the shipped scripts run with no substitution: this is
27 # exactly what a host executes.
28 #
29 # It also stalls a push in the window where git has read the local
30 # refs and not yet the mirror's. Two pushers at once rewind the
31 # mirror there; the drain, the only publisher, leaves the newer
32 # move queued and publishes it next.
33 mirror-e2e = pkgs.runCommand "valley-mirror-e2e" {
34 nativeBuildInputs = [ pkgs.git ];
35 initScript = stubbedHosts.mirrorHost.config.systemd.services.valley-init.script;
36 publishUnit = stubbedHosts.mirrorHost.config.systemd.units."valley-publish@.service".text;
37 passAsFile = [
38 "initScript"
39 "publishUnit"
40 ];
41 } (builtins.readFile ./mirror-e2e.sh);
42
43 # The pre-receive hook's policy, rule by rule where a rule is
44 # subtle: the glob matching of a declared pattern, the order in which
45 # the rules are tried, and every transition of a change record, each
46 # refusal against real git objects. What the hook does to a real push
47 # is protect-e2e's, valley-ready's for the records the CLI writes, and
48 # knots-e2e's for the plan graph.
49 valleyhook-unit = packages.valleyhook.unit;
50
51 # The note envelope every reader of an attestation shares (note/).
52 note-unit = packages.note.unit;
53
54 # The strict JSON reading the hook reads its push policy with, the
55 # identity compiler its registry, and read access a policy/read and the
56 # registry (exactjson/).
57 exactjson-unit = packages.exactjson.unit;
58
59 # The change record the hook guards and the CLI writes (change/): its
60 # strict reading, and the fuzz target's seed corpus.
61 change-unit = packages.change.unit;
62
63 # The one structural invariant, driven for real: bare repos wired
64 # with the rendered pre-receive hook, pushed to as each principal.
65 # What sshd would set from a key's authorized_keys entry, these
66 # pushes set in the environment directly — that rendering is pinned
67 # by module-eval, and everything the hook decides after it is what
68 # this check exercises: protected refs, attestations and the names
69 # they may take, integration requests and the approve grant under both
70 # its names, the changes of a change record's state, the restack
71 # grant's one version and each write it does not authorise, restack
72 # reports and their history, named grants, symbolic refs, refs named
73 # like other refs and the one-off report of them, a composed hook, and
74 # the namespaces every push is refused. An
75 # attestation's tree written by hand, with the genuine note at a name
76 # holding a slash, is refused. Projects with and without protection are
77 # both served. Last, the rendered init script wires a repository that
78 # already has history, and the hook it installs refuses an untagged key
79 # and takes a declared writer's push: the check begins where init-e2e
80 # ends.
81 protect-e2e = pkgs.runCommand "valley-protect-e2e" {
82 nativeBuildInputs = [
83 pkgs.git
84 pkgs.openssh
85 # Real attestations, made the way a contributor makes them, for the
86 # namespace the hook checks the contents of.
87 packages.attest
88 ];
89 initScript = hosts.protectedHost.config.systemd.services.valley-init.script;
90 passAsFile = [ "initScript" ];
91 } (builtins.readFile ./protect-e2e.sh);
92
93 # The plan graph, refs/the-valley/knots, through the hook the protected
94 # host renders. The real valley knot writes it as two principals, and
95 # every write lands; then crafted pushes break each rule the hook holds a
96 # push to the graph to, and each is refused with the ref left where it
97 # was: an untagged key, a non-fast-forward update, a merge, two commits,
98 # a parent that is not the old tip, a tag, another committer, a missing
99 # trailer, a deleted comment, an edit that records no edit, names
100 # another editor, half its record or no UTC time, changes the author, or
101 # comes from a principal without the approve grant, a new comment that
102 # records an edit, a deleted knot, a changed id,
103 # type or created_by, a forged author or creator, a symlink, an
104 # executable, a file or directory outside the layout, a non-canonical
105 # knot, an oversized one, a shared-subtree bomb, a dangling edge, a
106 # cycle, a deletion of the ref, and refs that only begin with its name.
107 # Trees written by hand are refused too: a root file named
108 # ideas/<id>-<slug>.md, every file flattened into names with slashes, a
109 # directory in the mode 040000, and entries named ".", ".." and "", the
110 # last sent to receive-pack directly, since git push will not send it.
111 # A refused write from the CLI runs the hook once. A create with a
112 # parent is refused and one without is admitted, and a valid graph
113 # update carries no invalid change record, attestation or integration
114 # request through with it. Comments are edited by the real CLI and by
115 # crafted pushes: by their author, and by holders of the approve grant
116 # under each of its names, while another principal is refused, and
117 # valley knot log and show --at give back every earlier text.
118 knots-e2e = pkgs.runCommand "valley-knots-e2e" {
119 nativeBuildInputs = [
120 pkgs.git
121 pkgs.jq
122 packages.valley-go
123 ];
124 cliScript = ../../bin/valley;
125 initScript = hosts.protectedHost.config.systemd.services.valley-init.script;
126 passAsFile = [ "initScript" ];
127 } (builtins.readFile ./knots-e2e.sh);
128
129 # valley-init itself, over repositories that already exist. The other
130 # hook checks wire a bare repo by hand; this one runs the rendered init
131 # script and holds it to converging on what the declaration says — the
132 # managed hook installed where a project had none, re-pointed where it
133 # had drifted to another store path, removed where the declaration no
134 # longer protects the project, and a hand-written hook of the same name
135 # left alone. The hooks run a stand-in for valleyhook, so nothing is
136 # pushed through them here: protect-e2e pushes through the hook this
137 # script converges on, with the real valleyhook. Relocating the data
138 # directory is the only edit to the script — the sandbox cannot write
139 # /srv.
140 init-e2e = pkgs.runCommand "valley-init-e2e" {
141 nativeBuildInputs = [ pkgs.git ];
142 initScript = stubbedHosts.protectedHost.config.systemd.services.valley-init.script;
143 integratorInitScript = stubbedHosts.integratorHost.config.systemd.services.valley-init.script;
144 passAsFile = [
145 "initScript"
146 "integratorInitScript"
147 ];
148 } (builtins.readFile ./init-e2e.sh);
149
150 # Phase 1's exit criteria, end to end and unmocked: a push to a
151 # bare repo wired with the real rendered hooks produces a
152 # ref-updated event on a real JetStream server within seconds,
153 # visible in `valley tail`; replaying the repo's refs is
154 # deterministic; and a dead bus never fails a push. The hook and
155 # stream-init scripts are followed from the rendered unit scripts,
156 # so the check exercises exactly what a host would run — only the
157 # server invocation differs (the sandbox cannot write /srv), same
158 # binary and flags, relocated storage.
159 bus-e2e = pkgs.runCommand "valley-bus-e2e" {
160 nativeBuildInputs = [
161 pkgs.git
162 pkgs.natscli
163 pkgs.nats-server
164 pkgs.jq
165 pkgs.cue
166 packages.valley-script
167 ];
168 initScript = stubbedHosts.busHost.config.systemd.services.valley-init.script;
169 busInitScript = stubbedHosts.busHost.config.systemd.services.valley-bus-init.script;
170 eventSchema = ../../schema/events.cue;
171 passAsFile = [
172 "initScript"
173 "busInitScript"
174 ];
175 } (builtins.readFile ./bus-e2e.sh);
176}