gunk-dev/the-valley

webui-publisher-e2e.nix

sourcef5567cb33daemain's published tip history
6.1 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 cli="$CACHE_DIRECTORY/cli"
71 git init --quiet --template= "$cli"
72 cp "$shared/objects/info/alternates" "$cli/.git/objects/info/alternates"
73 git -C "$cli" config remote.origin.url /srv/git/app.git
74 git -C "$cli" config core.logallrefupdates false
75 git -C "$cli" update-ref refs/remotes/origin/main "$tip"
76 git -C "$cli" cat-file -e "$tip^{commit}" || fail "CLI shared objects could not be read"
77 test "$(find "$cli/.git/objects" -type f | wc -l)" -eq 1 || fail "CLI objects were copied"
78 touch "$STATE_DIRECTORY/written" || fail "the state directory could not be written"
79 touch "$CACHE_DIRECTORY/written" || fail "the cache directory could not be written"
80 echo "probe: confined"
81 '';
82 };
83in
84{
85 webui-publisher-e2e = pkgs.testers.runNixOSTest {
86 name = "valley-webui-publisher-e2e";
87 nodes.host = {
88 imports = [
89 self.nixosModules.default
90 self.nixosModules.webui-publisher
91 ];
92 # The system configuration trusts every directory, so only the
93 # entries in the unit's environment can keep a repository out.
94 environment.etc.gitconfig.text = ''
95 [safe]
96 directory = *
97 '';
98 services.valley = {
99 enable = true;
100 config = declaration;
101 # The host module requires a key for the git user. Nobody holds
102 # this one's private half, and nothing in the test pushes.
103 authorizedKeys = [
104 "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEDVqaZm+1Xx4noNlTYckB32kUMMUnS8eN74T/Luxo+L operator"
105 ];
106 integrator.instanceProject = "valley";
107 backup.sshKeyFile = "/run/keys/backup-ssh-key";
108 webPublisher = {
109 enable = true;
110 destination = "example.org";
111 receiver = "http://192.0.2.1";
112 };
113 };
114 systemd.services.valley-web-publish.serviceConfig.ExecStart = lib.mkForce (lib.getExe probe);
115 };
116
117 testScript = ''
118 host.wait_for_unit("multi-user.target")
119 host.wait_until_succeeds("test -d /srv/git/app.git && test -d /srv/git/valley.git")
120
121 # A repository the host does not declare, as the git user would
122 # leave one, and two keys the publisher must not read. Each is
123 # readable by the git group or by anyone, so only the unit's
124 # confinement keeps the probe from it.
125 host.succeed(
126 "runuser -u git -- git init --quiet --bare /srv/git/stray.git",
127 "install -d -m 0750 -o git -g git /srv/git/.ssh",
128 "echo secret | install -m 0640 -o git -g git /dev/stdin /srv/git/.ssh/id_ed25519",
129 "install -d -m 0755 /run/keys",
130 "echo secret | install -m 0644 /dev/stdin /run/keys/backup-ssh-key",
131 )
132
133 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\"'")
134
135 host.succeed("systemctl start valley-web-publish.service")
136 journal = host.succeed("journalctl -u valley-web-publish.service --no-pager")
137 print(journal)
138 assert "probe: confined" in journal, "the probe did not finish"
139 host.succeed("test -e /var/lib/valley-web-publish/written")
140 host.fail("test -e /srv/git/app.git/probe")
141 '';
142 };
143}