gunk-dev/the-valley

web-publisher-e2e.nix

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