gunk-dev/the-valley

hooks.nix

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