gunk-dev/the-valley

webui-publisher-e2e.nix

sourced03575e2a892main's published tip history
4.5 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.gnugrep
35 ];
36 text = ''
37 fail() {
38 echo "probe: $*" >&2
39 exit 1
40 }
41 git -C /srv/git/app.git rev-parse --git-dir > /dev/null ||
42 fail "a repository the host serves could not be read"
43 if git -C /srv/git/stray.git rev-parse --git-dir 2> "$STATE_DIRECTORY/stray"; then
44 fail "a repository the host does not declare was read"
45 fi
46 grep -q "dubious ownership" "$STATE_DIRECTORY/stray" ||
47 fail "the undeclared repository was refused for another reason: $(cat "$STATE_DIRECTORY/stray")"
48 if touch /srv/git/app.git/probe 2> /dev/null; then
49 fail "a repository was written"
50 fi
51 if git -C /srv/git/app.git update-ref refs/heads/probe refs/heads/main 2> /dev/null; then
52 fail "a repository's ref was written"
53 fi
54 for key in /srv/git/.ssh/id_ed25519 /run/keys/backup-ssh-key; do
55 if cat "$key" > /dev/null 2>&1; then
56 fail "$key was read"
57 fi
58 done
59 touch "$STATE_DIRECTORY/written" || fail "the state directory could not be written"
60 touch "$CACHE_DIRECTORY/written" || fail "the cache directory could not be written"
61 echo "probe: confined"
62 '';
63 };
64in
65{
66 webui-publisher-e2e = pkgs.testers.runNixOSTest {
67 name = "valley-webui-publisher-e2e";
68 nodes.host = {
69 imports = [
70 self.nixosModules.default
71 self.nixosModules.webui-publisher
72 ];
73 # The system configuration trusts every directory, so only the
74 # entries in the unit's environment can keep a repository out.
75 environment.etc.gitconfig.text = ''
76 [safe]
77 directory = *
78 '';
79 services.valley = {
80 enable = true;
81 config = declaration;
82 # The host module requires a key for the git user. Nobody holds
83 # this one's private half, and nothing in the test pushes.
84 authorizedKeys = [
85 "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEDVqaZm+1Xx4noNlTYckB32kUMMUnS8eN74T/Luxo+L operator"
86 ];
87 integrator.instanceProject = "valley";
88 backup.sshKeyFile = "/run/keys/backup-ssh-key";
89 webPublisher = {
90 enable = true;
91 destination = "example.org";
92 receiver = "http://192.0.2.1";
93 };
94 };
95 systemd.services.valley-web-publish.serviceConfig.ExecStart = lib.mkForce (lib.getExe probe);
96 };
97
98 testScript = ''
99 host.wait_for_unit("multi-user.target")
100 host.wait_until_succeeds("test -d /srv/git/app.git && test -d /srv/git/valley.git")
101
102 # A repository the host does not declare, as the git user would
103 # leave one, and two keys the publisher must not read. Each is
104 # readable by the git group or by anyone, so only the unit's
105 # confinement keeps the probe from it.
106 host.succeed(
107 "runuser -u git -- git init --quiet --bare /srv/git/stray.git",
108 "install -d -m 0750 -o git -g git /srv/git/.ssh",
109 "echo secret | install -m 0640 -o git -g git /dev/stdin /srv/git/.ssh/id_ed25519",
110 "install -d -m 0755 /run/keys",
111 "echo secret | install -m 0644 /dev/stdin /run/keys/backup-ssh-key",
112 )
113
114 host.succeed("systemctl start valley-web-publish.service")
115 journal = host.succeed("journalctl -u valley-web-publish.service --no-pager")
116 print(journal)
117 assert "probe: confined" in journal, "the probe did not finish"
118 host.succeed("test -e /var/lib/valley-web-publish/written")
119 host.fail("test -e /srv/git/app.git/probe")
120 '';
121 };
122}