gunk-dev/the-valley

hooks.nix

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