gunk-dev/the-valley

webui-publisher-e2e.nix

sourceec8d3e69c534main's published tip history
6.3 KiB
1# The web publisher's sandbox, in a NixOS machine running the host module
2# and the publisher's module beside it. The unit is the one the module
3# renders, with a probe in place of the publisher, so the probe runs as the
4# unit's dynamic user under the unit's own confinement. It must read the
5# repositories the host serves and nothing more: it writes no repository,
6# reads no key outside its state, and reads no repository the host does
7# not declare, though the system's git configuration trusts every
8# directory.
9{
10 pkgs,
11 lib,
12 self,
13 ...
14}:
15let
16 declaration = pkgs.writeText "valley-webui-publisher-e2e.cue" ''
17 package valley
18
19 projects: {
20 // The instance repository, whose main the host module requires
21 // protected.
22 "valley": protection: refs: ["refs/heads/main"]
23 "app": {}
24 }
25 '';
26
27 # What the unit runs in place of the publisher. Each check is one claim
28 # about the sandbox, and the probe says which one failed.
29 probe = pkgs.writeShellApplication {
30 name = "valley-web-publish-probe";
31 runtimeInputs = [
32 pkgs.git
33 pkgs.coreutils
34 pkgs.findutils
35 pkgs.gnugrep
36 ];
37 text = ''
38 fail() {
39 echo "probe: $*" >&2
40 exit 1
41 }
42 git -C /srv/git/app.git rev-parse --git-dir > /dev/null ||
43 fail "a repository the host serves could not be read"
44 if git -C /srv/git/stray.git rev-parse --git-dir 2> "$STATE_DIRECTORY/stray"; then
45 fail "a repository the host does not declare was read"
46 fi
47 grep -q "dubious ownership" "$STATE_DIRECTORY/stray" ||
48 fail "the undeclared repository was refused for another reason: $(cat "$STATE_DIRECTORY/stray")"
49 if touch /srv/git/app.git/probe 2> /dev/null; then
50 fail "a repository was written"
51 fi
52 if git -C /srv/git/app.git update-ref refs/heads/probe refs/heads/main 2> /dev/null; then
53 fail "a repository's ref was written"
54 fi
55 for key in /srv/git/.ssh/id_ed25519 /run/keys/backup-ssh-key; do
56 if cat "$key" > /dev/null 2>&1; then
57 fail "$key was read"
58 fi
59 done
60 # The same alternate layout the publisher writes must resolve host
61 # objects with the exact safe.directory list and read-only mounts.
62 shared="$CACHE_DIRECTORY/shared.git"
63 git init --quiet --bare --template= "$shared"
64 git -C /srv/git/app.git rev-parse --path-format=absolute --git-path objects > "$shared/objects/info/alternates"
65 tip=$(git -C /srv/git/app.git rev-parse refs/heads/main)
66 git -C "$shared" update-ref refs/heads/main "$tip"
67 git -C "$shared" cat-file -e "$tip^{commit}" || fail "shared objects could not be read"
68 test "$(find "$shared/objects" -type f | wc -l)" -eq 1 || fail "objects were copied"
69 # The CLI uses a worktree-shaped cache with the same borrowed objects.
70 repos="$CACHE_DIRECTORY/repos"
71 mkdir "$repos"
72 cli="$repos/app"
73 git init --quiet --template= "$cli"
74 cp "$shared/objects/info/alternates" "$cli/.git/objects/info/alternates"
75 git -C "$cli" config remote.origin.url /srv/git/app.git
76 git -C "$cli" config core.logallrefupdates false
77 chmod 0500 "$repos"
78 if mkdir "$repos/unlisted" 2> /dev/null; then
79 fail "an unstaged CLI cache was created"
80 fi
81 git -C "$cli" update-ref refs/remotes/origin/main "$tip"
82 git -C "$cli" cat-file -e "$tip^{commit}" || fail "CLI shared objects could not be read"
83 test "$(find "$cli/.git/objects" -type f | wc -l)" -eq 1 || fail "CLI objects were copied"
84 chmod 0700 "$repos"
85 touch "$STATE_DIRECTORY/written" || fail "the state directory could not be written"
86 touch "$CACHE_DIRECTORY/written" || fail "the cache directory could not be written"
87 echo "probe: confined"
88 '';
89 };
90in
91{
92 webui-publisher-e2e = pkgs.testers.runNixOSTest {
93 name = "valley-webui-publisher-e2e";
94 nodes.host = {
95 imports = [
96 self.nixosModules.default
97 self.nixosModules.webui-publisher
98 ];
99 # The system configuration trusts every directory, so only the
100 # entries in the unit's environment can keep a repository out.
101 environment.etc.gitconfig.text = ''
102 [safe]
103 directory = *
104 '';
105 services.valley = {
106 enable = true;
107 config = declaration;
108 # The host module requires a key for the git user. Nobody holds
109 # this one's private half, and nothing in the test pushes.
110 authorizedKeys = [
111 "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEDVqaZm+1Xx4noNlTYckB32kUMMUnS8eN74T/Luxo+L operator"
112 ];
113 integrator.instanceProject = "valley";
114 backup.sshKeyFile = "/run/keys/backup-ssh-key";
115 webPublisher = {
116 enable = true;
117 destination = "example.org";
118 receiver = "http://192.0.2.1";
119 };
120 };
121 systemd.services.valley-web-publish.serviceConfig.ExecStart = lib.mkForce (lib.getExe probe);
122 };
123
124 testScript = ''
125 host.wait_for_unit("multi-user.target")
126 host.wait_until_succeeds("test -d /srv/git/app.git && test -d /srv/git/valley.git")
127
128 # A repository the host does not declare, as the git user would
129 # leave one, and two keys the publisher must not read. Each is
130 # readable by the git group or by anyone, so only the unit's
131 # confinement keeps the probe from it.
132 host.succeed(
133 "runuser -u git -- git init --quiet --bare /srv/git/stray.git",
134 "install -d -m 0750 -o git -g git /srv/git/.ssh",
135 "echo secret | install -m 0640 -o git -g git /dev/stdin /srv/git/.ssh/id_ed25519",
136 "install -d -m 0755 /run/keys",
137 "echo secret | install -m 0644 /dev/stdin /run/keys/backup-ssh-key",
138 )
139
140 host.succeed("runuser -u git -- sh -c 'tree=$(git -C /srv/git/app.git mktree < /dev/null); tip=$(echo initial | git -C /srv/git/app.git -c user.name=operator -c user.email=operator@example.org commit-tree \"$tree\"); git -C /srv/git/app.git update-ref refs/heads/main \"$tip\"'")
141
142 host.succeed("systemctl start valley-web-publish.service")
143 journal = host.succeed("journalctl -u valley-web-publish.service --no-pager")
144 print(journal)
145 assert "probe: confined" in journal, "the probe did not finish"
146 host.succeed("test -e /var/lib/valley-web-publish/written")
147 host.fail("test -e /srv/git/app.git/probe")
148 '';
149 };
150}