gunk-dev/the-valley

hooks.nix

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