gunk-dev/the-valley

hooks.nix

source6db7e2f30e24main's published tip history
8.4 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, and the
55 # identity compiler its registry (exactjson/).
56 exactjson-unit = packages.exactjson.unit;
57
58 # The change record the hook guards and the CLI writes (change/): its
59 # strict reading, and the fuzz target's seed corpus.
60 change-unit = packages.change.unit;
61
62 # The one structural invariant, driven for real: bare repos wired
63 # with the rendered pre-receive hook, pushed to as each principal.
64 # What sshd would set from a key's authorized_keys entry, these
65 # pushes set in the environment directly — that rendering is pinned
66 # by module-eval, and everything the hook decides after it is what
67 # this check exercises: protected refs, attestations and the names
68 # they may take, integration requests and the approve grant under both
69 # its names, the changes of a change record's state, the restack
70 # grant's one version and each write it does not authorise, restack
71 # reports and their history, named grants, symbolic refs, a composed
72 # hook, and the namespaces every push is refused. An
73 # attestation's tree written by hand, with the genuine note at a name
74 # holding a slash, is refused. Projects with and without protection are
75 # both served. Last, the rendered init script wires a repository that
76 # already has history, and the hook it installs refuses an untagged key
77 # and takes a declared writer's push: the check begins where init-e2e
78 # ends.
79 protect-e2e = pkgs.runCommand "valley-protect-e2e" {
80 nativeBuildInputs = [
81 pkgs.git
82 pkgs.openssh
83 # Real attestations, made the way a contributor makes them, for the
84 # namespace the hook checks the contents of.
85 packages.attest
86 ];
87 initScript = hosts.protectedHost.config.systemd.services.valley-init.script;
88 passAsFile = [ "initScript" ];
89 } (builtins.readFile ./protect-e2e.sh);
90
91 # The plan graph, refs/the-valley/knots, through the hook the protected
92 # host renders. The real valley knot writes it as two principals, and
93 # every write lands; then crafted pushes break each rule the hook holds a
94 # push to the graph to, and each is refused with the ref left where it
95 # was: an untagged key, a non-fast-forward update, a merge, two commits,
96 # a parent that is not the old tip, a tag, another committer, a missing
97 # trailer, a deleted comment, an edit that records no edit, names
98 # another editor, half its record or no UTC time, changes the author, or
99 # comes from a principal without the approve grant, a new comment that
100 # records an edit, a deleted knot, a changed id,
101 # type or created_by, a forged author or creator, a symlink, an
102 # executable, a file or directory outside the layout, a non-canonical
103 # knot, an oversized one, a shared-subtree bomb, a dangling edge, a
104 # cycle, a deletion of the ref, and refs that only begin with its name.
105 # Trees written by hand are refused too: a root file named
106 # ideas/<id>-<slug>.md, every file flattened into names with slashes, a
107 # directory in the mode 040000, and entries named ".", ".." and "", the
108 # last sent to receive-pack directly, since git push will not send it.
109 # A refused write from the CLI runs the hook once. A create with a
110 # parent is refused and one without is admitted, and a valid graph
111 # update carries no invalid change record, attestation or integration
112 # request through with it. Comments are edited by the real CLI and by
113 # crafted pushes: by their author, and by holders of the approve grant
114 # under each of its names, while another principal is refused, and
115 # valley knot log and show --at give back every earlier text.
116 knots-e2e = pkgs.runCommand "valley-knots-e2e" {
117 nativeBuildInputs = [
118 pkgs.git
119 pkgs.jq
120 packages.valley-go
121 ];
122 cliScript = ../../bin/valley;
123 initScript = hosts.protectedHost.config.systemd.services.valley-init.script;
124 passAsFile = [ "initScript" ];
125 } (builtins.readFile ./knots-e2e.sh);
126
127 # valley-init itself, over repositories that already exist. The other
128 # hook checks wire a bare repo by hand; this one runs the rendered init
129 # script and holds it to converging on what the declaration says — the
130 # managed hook installed where a project had none, re-pointed where it
131 # had drifted to another store path, removed where the declaration no
132 # longer protects the project, and a hand-written hook of the same name
133 # left alone. The hooks run a stand-in for valleyhook, so nothing is
134 # pushed through them here: protect-e2e pushes through the hook this
135 # script converges on, with the real valleyhook. Relocating the data
136 # directory is the only edit to the script — the sandbox cannot write
137 # /srv.
138 init-e2e = pkgs.runCommand "valley-init-e2e" {
139 nativeBuildInputs = [ pkgs.git ];
140 initScript = stubbedHosts.protectedHost.config.systemd.services.valley-init.script;
141 integratorInitScript = stubbedHosts.integratorHost.config.systemd.services.valley-init.script;
142 passAsFile = [
143 "initScript"
144 "integratorInitScript"
145 ];
146 } (builtins.readFile ./init-e2e.sh);
147
148 # Phase 1's exit criteria, end to end and unmocked: a push to a
149 # bare repo wired with the real rendered hooks produces a
150 # ref-updated event on a real JetStream server within seconds,
151 # visible in `valley tail`; replaying the repo's refs is
152 # deterministic; and a dead bus never fails a push. The hook and
153 # stream-init scripts are followed from the rendered unit scripts,
154 # so the check exercises exactly what a host would run — only the
155 # server invocation differs (the sandbox cannot write /srv), same
156 # binary and flags, relocated storage.
157 bus-e2e = pkgs.runCommand "valley-bus-e2e" {
158 nativeBuildInputs = [
159 pkgs.git
160 pkgs.natscli
161 pkgs.nats-server
162 pkgs.jq
163 pkgs.cue
164 packages.valley-script
165 ];
166 initScript = stubbedHosts.busHost.config.systemd.services.valley-init.script;
167 busInitScript = stubbedHosts.busHost.config.systemd.services.valley-bus-init.script;
168 eventSchema = ../../schema/events.cue;
169 passAsFile = [
170 "initScript"
171 "busInitScript"
172 ];
173 } (builtins.readFile ./bus-e2e.sh);
174}