gunk-dev/the-valley

hooks.nix

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