| 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 not |
| 7 | # declare, though the system's git configuration trusts every directory. The |
| 8 | # unit receives the bearer token as a systemd credential while the token |
| 9 | # file on disk stays readable by root alone. The unit can connect to its |
| 10 | # allowed IP address. The unit's ExecStartPre egress check passes inside |
| 11 | # the unit (allowing the probe to start) and fails when run by root outside |
| 12 | # the unit (where root's UDP datagram reaches the canary listener). Inside |
| 13 | # the unit, the C library resolves a host name through nscd from a DNS |
| 14 | # server on a disallowed address. module-eval holds the unit's dynamic |
| 15 | # user, address filter and credential, which systemd enforces. |
| 16 | # |
| 17 | # The read links' wrapper, valley-web-link, runs as root against |
| 18 | # repositories the git user owns, though root's git trusts none of them |
| 19 | # without the wrapper's entries. tmpfiles makes the links lock beside the |
| 20 | # credential file, root's, and root refuses one it does not own. The |
| 21 | # wrapper issues a link for a keyless principal into a credential file only |
| 22 | # root writes and the publisher's group reads, and, with no receiver |
| 23 | # listening, says the link is pending and exits 1. A |
| 24 | # revoke of every link fails its delivery the same way; once a receiver |
| 25 | # listens, the same revoke again delivers it, and a link issued then |
| 26 | # reaches the receiver, read by the policy unit through its group, until a |
| 27 | # revoke of every link removes it there. |
| 28 | { |
| 29 | pkgs, |
| 30 | lib, |
| 31 | self, |
| 32 | packages, |
| 33 | ... |
| 34 | }: |
| 35 | let |
| 36 | declaration = pkgs.writeText "valley-web-publisher-e2e.cue" '' |
| 37 | package valley |
| 38 | |
| 39 | groups: core: name: "tools" |
| 40 | projects: { |
| 41 | // The instance repository, whose main the host module requires |
| 42 | // protected. |
| 43 | "valley": protection: refs: ["refs/heads/main"] |
| 44 | "app": group: "core" |
| 45 | } |
| 46 | ''; |
| 47 | |
| 48 | # The instance's main: an identity registry granting alice, who holds |
| 49 | # no key, an edge read of every project at example.org, and app's |
| 50 | # policy/read, which lets alice read it. |
| 51 | registry = pkgs.writeText "registry.cue" '' |
| 52 | package identity |
| 53 | |
| 54 | boundaries: { |
| 55 | registry: kind: "registry" |
| 56 | web: {kind: "edge-read", destination: "example.org"} |
| 57 | } |
| 58 | genesis: "operator" |
| 59 | principals: operator: { |
| 60 | kind: "human" |
| 61 | keys: [{class: "ssh-ed25519", bound: "hardware", public: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEDVqaZm+1Xx4noNlTYckB32kUMMUnS8eN74T/Luxo+L"}] |
| 62 | grants: govern: boundary: "registry" |
| 63 | } |
| 64 | principals: alice: {kind: "human", keys: [], grants: read: {boundary: "web", projects: "all"}} |
| 65 | anyone: grants: read: {boundary: "web", projects: {app: "9da6809f038df92138c131cd16bed76d"}} |
| 66 | ''; |
| 67 | appRead = pkgs.writeText "read.cue" '' |
| 68 | package read |
| 69 | |
| 70 | project: "app" |
| 71 | identity: "9da6809f038df92138c131cd16bed76d" |
| 72 | readers: {anyone: true, alice: true} |
| 73 | ''; |
| 74 | # Commits the files to a repository's main as the git user, by a fetch |
| 75 | # into the bare repository, which runs no hook. |
| 76 | seed = pkgs.writeShellApplication { |
| 77 | name = "seed"; |
| 78 | runtimeInputs = [ |
| 79 | pkgs.git |
| 80 | pkgs.coreutils |
| 81 | ]; |
| 82 | text = '' |
| 83 | work=$(mktemp -d) |
| 84 | git -C "$work" init --quiet |
| 85 | git -C "$work" config user.name operator |
| 86 | git -C "$work" config user.email operator@example.org |
| 87 | mkdir -p "$work/policy/instance" "$work/policy/read" "$work/identity" |
| 88 | echo 'package verification' > "$work/policy/instance/floor.cue" |
| 89 | cp ${registry} "$work/identity/registry.cue" |
| 90 | git -C "$work" add --all |
| 91 | git -C "$work" commit --quiet -m "the instance" |
| 92 | git -C /srv/git/valley.git fetch --quiet "$work" +HEAD:refs/heads/main |
| 93 | list=$(printf 'app\nvalley\n' | git -C /srv/git/valley.git hash-object -w --stdin) |
| 94 | git -C /srv/git/valley.git update-ref refs/the-valley/projects "$list" |
| 95 | rm -rf "$work" |
| 96 | work=$(mktemp -d) |
| 97 | git -C "$work" init --quiet |
| 98 | git -C "$work" config user.name operator |
| 99 | git -C "$work" config user.email operator@example.org |
| 100 | mkdir -p "$work/policy/read" |
| 101 | cp ${appRead} "$work/policy/read/read.cue" |
| 102 | echo /srv/git/valley.git > "$work/policy/valley" |
| 103 | git -C "$work" add --all |
| 104 | git -C "$work" commit --quiet -m "app" |
| 105 | git -C /srv/git/app.git fetch --quiet "$work" +HEAD:refs/heads/main |
| 106 | rm -rf "$work" |
| 107 | ''; |
| 108 | }; |
| 109 | |
| 110 | # What the unit runs in place of the publisher. Each check is one claim |
| 111 | # about the sandbox, and the probe says which one failed. |
| 112 | probe = pkgs.writeShellApplication { |
| 113 | name = "valley-web-publish-probe"; |
| 114 | runtimeInputs = [ |
| 115 | pkgs.bash |
| 116 | pkgs.git |
| 117 | pkgs.coreutils |
| 118 | pkgs.findutils |
| 119 | pkgs.getent |
| 120 | pkgs.gnugrep |
| 121 | ]; |
| 122 | text = '' |
| 123 | fail() { |
| 124 | echo "probe: $*" >&2 |
| 125 | exit 1 |
| 126 | } |
| 127 | git -C /srv/git/app.git rev-parse --git-dir > /dev/null || |
| 128 | fail "a repository the host serves could not be read" |
| 129 | if git -C /srv/git/stray.git rev-parse --git-dir 2> "$STATE_DIRECTORY/stray"; then |
| 130 | fail "a repository the host does not declare was read" |
| 131 | fi |
| 132 | grep -q "dubious ownership" "$STATE_DIRECTORY/stray" || |
| 133 | fail "the undeclared repository was refused for another reason: $(cat "$STATE_DIRECTORY/stray")" |
| 134 | if touch /srv/git/app.git/probe 2> /dev/null; then |
| 135 | fail "a repository was written" |
| 136 | fi |
| 137 | if git -C /srv/git/app.git update-ref refs/heads/probe refs/heads/main 2> /dev/null; then |
| 138 | fail "a repository's ref was written" |
| 139 | fi |
| 140 | grep -qx "edge-token-0123456789abcdef0123456789" "$CREDENTIALS_DIRECTORY/receiver-token" || |
| 141 | fail "the receiver's token did not reach the unit as a credential" |
| 142 | for key in /srv/git/.ssh/id_ed25519 /run/keys/backup-ssh-key /run/keys/web-receiver-token; do |
| 143 | if cat "$key" > /dev/null 2>&1; then |
| 144 | fail "$key was read" |
| 145 | fi |
| 146 | done |
| 147 | # The same alternate layout the publisher writes must resolve host |
| 148 | # objects with the exact safe.directory list and read-only mounts. |
| 149 | shared="$CACHE_DIRECTORY/shared.git" |
| 150 | git init --quiet --bare --template= "$shared" |
| 151 | git -C /srv/git/app.git rev-parse --path-format=absolute --git-path objects > "$shared/objects/info/alternates" |
| 152 | tip=$(git -C /srv/git/app.git rev-parse refs/heads/main) |
| 153 | git -C "$shared" update-ref refs/heads/main "$tip" |
| 154 | git -C "$shared" cat-file -e "$tip^{commit}" || fail "shared objects could not be read" |
| 155 | test "$(find "$shared/objects" -type f | wc -l)" -eq 1 || fail "objects were copied" |
| 156 | # The CLI uses a worktree-shaped cache with the same borrowed objects. |
| 157 | repos="$CACHE_DIRECTORY/repos" |
| 158 | mkdir "$repos" |
| 159 | cli="$repos/app" |
| 160 | git init --quiet --template= "$cli" |
| 161 | cp "$shared/objects/info/alternates" "$cli/.git/objects/info/alternates" |
| 162 | git -C "$cli" config remote.origin.url /srv/git/app.git |
| 163 | git -C "$cli" config core.logallrefupdates false |
| 164 | chmod 0500 "$repos" |
| 165 | if mkdir "$repos/unlisted" 2> /dev/null; then |
| 166 | fail "an unstaged CLI cache was created" |
| 167 | fi |
| 168 | git -C "$cli" update-ref refs/remotes/origin/main "$tip" |
| 169 | git -C "$cli" cat-file -e "$tip^{commit}" || fail "CLI shared objects could not be read" |
| 170 | test "$(find "$cli/.git/objects" -type f | wc -l)" -eq 1 || fail "CLI objects were copied" |
| 171 | chmod 0700 "$repos" |
| 172 | # Egress: the unit can connect to the allowed receiver address. |
| 173 | # shellcheck disable=SC2016 |
| 174 | test "$(timeout 5 bash -c 'exec 3<> /dev/tcp/192.0.2.1/8080 && head -n 1 <&3')" = "listener allowed" || |
| 175 | fail "the allowed listener at 192.0.2.1 could not be reached" |
| 176 | # DNS: the C library resolves names through nscd outside the unit, |
| 177 | # from a DNS server at an address the unit may not reach. |
| 178 | test "''${GODEBUG-}" = netdns=cgo || fail "Go's lookups are not pinned to the C library" |
| 179 | getent hosts receiver.example.org > "$STATE_DIRECTORY/getent" || |
| 180 | fail "receiver.example.org did not resolve through the C library" |
| 181 | grep -q "^192\.0\.2\.1 " "$STATE_DIRECTORY/getent" || |
| 182 | fail "receiver.example.org resolved to $(cat "$STATE_DIRECTORY/getent")" |
| 183 | touch "$STATE_DIRECTORY/written" || fail "the state directory could not be written" |
| 184 | touch "$CACHE_DIRECTORY/written" || fail "the cache directory could not be written" |
| 185 | echo "probe: confined" |
| 186 | ''; |
| 187 | }; |
| 188 | in |
| 189 | { |
| 190 | web-publisher-e2e = pkgs.testers.runNixOSTest { |
| 191 | name = "valley-web-publisher-e2e"; |
| 192 | nodes.host = { |
| 193 | imports = [ |
| 194 | self.nixosModules.default |
| 195 | self.nixosModules.web-publisher |
| 196 | ]; |
| 197 | # The system configuration trusts every directory, so only the |
| 198 | # entries in the unit's environment can keep a repository out. |
| 199 | environment.etc.gitconfig.text = '' |
| 200 | [safe] |
| 201 | directory = * |
| 202 | ''; |
| 203 | services.valley = { |
| 204 | enable = true; |
| 205 | config = declaration; |
| 206 | # The host module requires a key for the git user. Nobody holds |
| 207 | # this one's private half, and nothing in the test pushes. |
| 208 | authorizedKeys = [ |
| 209 | "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEDVqaZm+1Xx4noNlTYckB32kUMMUnS8eN74T/Luxo+L operator" |
| 210 | ]; |
| 211 | integrator.instanceProject = "valley"; |
| 212 | backup.sshKeyFile = "/run/keys/backup-ssh-key"; |
| 213 | webPublisher = { |
| 214 | enable = true; |
| 215 | destination = "example.org"; |
| 216 | receiver = "http://192.0.2.1"; |
| 217 | receiverTokenFile = "/run/keys/web-receiver-token"; |
| 218 | allowedAddresses = [ "192.0.2.1" ]; |
| 219 | }; |
| 220 | }; |
| 221 | systemd.services.valley-web-publish.serviceConfig.ExecStart = lib.mkForce (lib.getExe probe); |
| 222 | # Local DNS server on a disallowed IP address; answers queries for |
| 223 | # receiver.example.org. |
| 224 | networking.nameservers = [ "192.0.2.53" ]; |
| 225 | environment.systemPackages = [ |
| 226 | pkgs.socat |
| 227 | pkgs.dnsmasq |
| 228 | pkgs.dig |
| 229 | ]; |
| 230 | }; |
| 231 | |
| 232 | testScript = |
| 233 | { nodes, ... }: |
| 234 | let |
| 235 | check = nodes.host.systemd.services.valley-web-publish.serviceConfig.ExecStartPre; |
| 236 | in |
| 237 | '' |
| 238 | host.wait_for_unit("multi-user.target") |
| 239 | host.wait_until_succeeds("test -d /srv/git/app.git && test -d /srv/git/valley.git") |
| 240 | |
| 241 | # A repository the host does not declare, as the git user would |
| 242 | # leave one, and two keys the publisher must not read. Each is |
| 243 | # readable by the git group or by anyone, so only the unit's |
| 244 | # confinement keeps the probe from it. |
| 245 | host.succeed( |
| 246 | "runuser -u git -- git init --quiet --bare /srv/git/stray.git", |
| 247 | "install -d -m 0750 -o git -g git /srv/git/.ssh", |
| 248 | "echo secret | install -m 0640 -o git -g git /dev/stdin /srv/git/.ssh/id_ed25519", |
| 249 | "install -d -m 0755 /run/keys", |
| 250 | "echo secret | install -m 0644 /dev/stdin /run/keys/backup-ssh-key", |
| 251 | "echo edge-token-0123456789abcdef0123456789 | install -m 0400 /dev/stdin /run/keys/web-receiver-token", |
| 252 | ) |
| 253 | |
| 254 | # Start a TCP listener on the allowed receiver address, and verify |
| 255 | # that root can reach it. |
| 256 | host.succeed( |
| 257 | "ip addr add 192.0.2.1/32 dev lo", |
| 258 | "ip addr add 198.51.100.1/32 dev lo", |
| 259 | "ip addr add 192.0.2.53/32 dev lo", |
| 260 | ) |
| 261 | host.succeed("systemd-run --unit=listener-allowed socat TCP4-LISTEN:8080,bind=192.0.2.1,reuseaddr,fork SYSTEM:'echo listener allowed'") |
| 262 | host.wait_until_succeeds("timeout 5 bash -c 'exec 3<> /dev/tcp/192.0.2.1/8080 && head -n 1 <&3' | grep -qx 'listener allowed'", timeout=30) |
| 263 | |
| 264 | # UDP listener on the canary address (198.51.100.1:9); appends any |
| 265 | # datagram it receives to /run/canary. |
| 266 | host.succeed("systemd-run --unit=canary socat -u UDP4-RECV:9,bind=198.51.100.1 OPEN:/run/canary,creat,append") |
| 267 | host.wait_until_succeeds("ss -Hulnp | grep -q '198.51.100.1:9 '", timeout=30) |
| 268 | |
| 269 | # Start the DNS server on 192.0.2.53 and verify that root can query it. |
| 270 | host.succeed("systemd-run --unit=dns dnsmasq --keep-in-foreground --no-resolv --no-hosts --bind-interfaces --listen-address=192.0.2.53 --address=/receiver.example.org/192.0.2.1") |
| 271 | host.wait_until_succeeds("dig +short +time=2 +tries=1 @192.0.2.53 receiver.example.org | grep -qx 192.0.2.1", timeout=30) |
| 272 | |
| 273 | # Run the unit's egress check as root outside the unit's cgroup: |
| 274 | # the datagram reaches the canary listener, so the check fails. |
| 275 | status, refused = host.execute("${check} 2>&1") |
| 276 | print(refused) |
| 277 | assert status != 0, "the egress check passed outside the unit" |
| 278 | assert "a datagram to 198.51.100.1 port 9 was sent" in refused, "the egress check refused for another reason" |
| 279 | host.wait_until_succeeds("grep -qx canary /run/canary", timeout=30) |
| 280 | |
| 281 | host.succeed("runuser -u git -- ${lib.getExe seed}") |
| 282 | |
| 283 | host.succeed("systemctl start valley-web-publish.service") |
| 284 | journal = host.succeed("journalctl -u valley-web-publish.service --no-pager") |
| 285 | print(journal) |
| 286 | assert "refused a datagram to 198.51.100.1 port 9 (EPERM)" in journal, "the egress check did not pass inside the unit" |
| 287 | assert host.succeed("cat /run/canary") == "canary", "a datagram from the unit's check reached the canary" |
| 288 | assert "probe: confined" in journal, "the probe did not finish" |
| 289 | host.succeed("test -e /var/lib/valley-web-publish/written") |
| 290 | host.fail("test -e /srv/git/app.git/probe") |
| 291 | |
| 292 | # Read links. Root's git refuses the git user's repositories once |
| 293 | # the system's trust is reset, as the wrapper's entries reset it. |
| 294 | refused = host.fail("GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=safe.directory GIT_CONFIG_VALUE_0= git -C /srv/git/valley.git rev-parse HEAD 2>&1") |
| 295 | assert "dubious ownership" in refused, f"root's git refused the instance for another reason: {refused}" |
| 296 | assert host.succeed("stat -c '%U:%G %a %F' /var/lib/valley-web-links /var/lib/valley-web-links/links /var/lib/valley-web-links/lock") == "root:valley-web-links 750 directory\nroot:valley-web-links 640 regular empty file\nroot:valley-web-links 640 regular empty file\n", "the credential file's or the links lock's owners, modes or types are wrong" |
| 297 | |
| 298 | # Root refuses a links lock it does not own, and changes nothing. |
| 299 | host.succeed("chown nobody /var/lib/valley-web-links/lock") |
| 300 | status, refused = host.execute("valley-web-link revoke --all 2>&1") |
| 301 | print(refused) |
| 302 | assert status == 1 and "/var/lib/valley-web-links/lock is owned by UID" in refused, "root took a links lock it does not own" |
| 303 | host.succeed("test ! -s /var/lib/valley-web-links/links") |
| 304 | host.succeed("chown root /var/lib/valley-web-links/lock") |
| 305 | |
| 306 | # With no receiver listening, an issue as root writes the link and |
| 307 | # prints it once, and the delivery fails: the link is pending. The |
| 308 | # test never prints the link. |
| 309 | status, _ = host.execute("valley-web-link issue --principal alice --note vm > /root/link 2> /root/issue.err") |
| 310 | issued = host.succeed("cat /root/issue.err") |
| 311 | print(issued) |
| 312 | assert status == 1, f"an undelivered issue exited {status}" |
| 313 | assert "dubious ownership" not in issued, "root's git refused a repository through the wrapper" |
| 314 | host.succeed("grep -Eqx 'https://example\\.org/-/redeem#lk-[0-9a-f]{16}\\.[A-Za-z0-9_-]{43}' /root/link") |
| 315 | assert ": issued here; the edge does not accept it until a pass delivers it" in issued, "the issue did not report the link pending" |
| 316 | assert host.succeed("stat -c '%U:%G %a' /var/lib/valley-web-links/links") == "root:valley-web-links 640\n", "the link command wrote the credential file with other owners or another mode" |
| 317 | host.fail("valley-web-link list --check") |
| 318 | |
| 319 | # A revoke of every link whose delivery fails exits 1; once a |
| 320 | # receiver listens, the same revoke again delivers it. |
| 321 | status, revoked = host.execute("valley-web-link revoke --all 2>&1") |
| 322 | print(revoked) |
| 323 | assert status == 1 and "revoked here; the edge may accept it" in revoked, "an undelivered revoke --all did not report the revocation pending" |
| 324 | host.succeed( |
| 325 | "install -d -m 0700 /var/lib/receiver", |
| 326 | "systemd-run --unit=receiver ${lib.getExe' packages.valley-web-receive "valley-web-receive"} --state /var/lib/receiver/store --listen 192.0.2.1:80 --instance host --destination example.org --verifiers /var/lib/valley-web-publish/verifier --token-file /run/keys/web-receiver-token", |
| 327 | ) |
| 328 | host.wait_until_succeeds("ss -Htln | grep -q '192.0.2.1:80 '", timeout=30) |
| 329 | retried = host.succeed("valley-web-link revoke --all 2>&1") |
| 330 | print(retried) |
| 331 | assert "the receiver holds every link's current state" in retried, "a repeated revoke --all did not deliver" |
| 332 | host.fail("grep -q '^binding ' /var/lib/receiver/v2/authorization") |
| 333 | |
| 334 | # A link issued now reaches the receiver: the policy unit reads the |
| 335 | # credential file through its group as it signs. |
| 336 | host.succeed("valley-web-link issue --principal alice > /root/link") |
| 337 | lk = host.succeed("sed 's/.*#\\(lk-[0-9a-f]*\\)\\..*/\\1/' /root/link").strip() |
| 338 | host.succeed(f"grep -Eq '^binding {lk} alice [0-9a-f]{{64}}$' /var/lib/receiver/v2/authorization") |
| 339 | |
| 340 | # A revoke of every link now removes the issued link at the receiver. |
| 341 | host.succeed("valley-web-link revoke --all") |
| 342 | host.fail(f"grep -q '^binding {lk} ' /var/lib/receiver/v2/authorization") |
| 343 | ''; |
| 344 | }; |
| 345 | } |