|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! `host.openshell.internal` names the host from inside a sandbox, on every |
| 5 | +//! compute driver. |
| 6 | +//! |
| 7 | +//! The test starts an HTTP server where the tests run, which is the host the |
| 8 | +//! gateway runs its sandboxes on, and runs a client in a sandbox from the |
| 9 | +//! Python test image ([`testkit::python_image`]) that requests a page from that |
| 10 | +//! server through the name. |
| 11 | +
|
| 12 | +use testkit::{Context, Error, Result, ResultExt as _}; |
| 13 | +use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _}; |
| 14 | +use tokio::net::{TcpListener, TcpStream}; |
| 15 | +use tokio::task::JoinHandle; |
| 16 | + |
| 17 | +/// The image's system Python, which needs nothing beyond its standard library. |
| 18 | +/// The policy names its real path, `/usr/bin/python3.*`, because a rule matches |
| 19 | +/// the executable behind a symlink. |
| 20 | +const PYTHON: &str = "/usr/bin/python3"; |
| 21 | + |
| 22 | +/// Requests `/` from `host.openshell.internal` on the port in `argv[1]` and |
| 23 | +/// prints the response. A failed connection is retried for 20 seconds, so a |
| 24 | +/// slow start doesn't fail the test, and reported on stderr. |
| 25 | +const CLIENT: &str = r#" |
| 26 | +import socket, sys, time |
| 27 | +
|
| 28 | +host, port = "host.openshell.internal", int(sys.argv[1]) |
| 29 | +deadline = time.monotonic() + 20 |
| 30 | +while True: |
| 31 | + try: |
| 32 | + conn = socket.create_connection((host, port), timeout=10) |
| 33 | + break |
| 34 | + except OSError as error: |
| 35 | + print(f"connect {host}:{port}: {error}", file=sys.stderr, flush=True) |
| 36 | + if time.monotonic() > deadline: |
| 37 | + sys.exit(1) |
| 38 | + time.sleep(1) |
| 39 | +with conn: |
| 40 | + conn.sendall(f"GET / HTTP/1.1\r\nHost: {host}:{port}\r\nConnection: close\r\n\r\n".encode()) |
| 41 | + response = b"" |
| 42 | + while chunk := conn.recv(4096): |
| 43 | + response += chunk |
| 44 | +print(response.decode(errors="replace")) |
| 45 | +"#; |
| 46 | + |
| 47 | +/// A sandbox from the Python test image resolves `host.openshell.internal` |
| 48 | +/// and gets a response from a server the test runs on the host. The default |
| 49 | +/// policy denies all network access, so the test passes one that lets only the |
| 50 | +/// image's Python reach the server's port. |
| 51 | +#[testkit::test] |
| 52 | +async fn python_sandbox_reaches_host_server(cx: &mut Context) -> testkit::Result { |
| 53 | + let marker = format!("host-gateway-{}", cx.run_id()); |
| 54 | + let server = HostServer::start(marker.clone()).await?; |
| 55 | + let policy = PolicyFile::new(&policy(server.port))?; |
| 56 | + let port = server.port.to_string(); |
| 57 | + let image = testkit::python_image(); |
| 58 | + |
| 59 | + // The client runs through `sandbox exec` in a Ready sandbox, not as the |
| 60 | + // main process. A gateway with several replicas (k3s-ha) can refuse to |
| 61 | + // attach to a main process that already finished with "sandbox is not |
| 62 | + // ready" when the request reaches a replica that doesn't hold the |
| 63 | + // sandbox's session. |
| 64 | + let name = cx.name("hg"); |
| 65 | + cx.track(&name); |
| 66 | + cx.cli([ |
| 67 | + "sandbox", |
| 68 | + "create", |
| 69 | + "--name", |
| 70 | + &name, |
| 71 | + "--from", |
| 72 | + &image, |
| 73 | + "--policy", |
| 74 | + policy.path()?, |
| 75 | + "--detach", |
| 76 | + "--no-tty", |
| 77 | + "--", |
| 78 | + "sh", |
| 79 | + "-c", |
| 80 | + "exec sleep infinity", |
| 81 | + ]) |
| 82 | + .await? |
| 83 | + .success() |
| 84 | + .context(format!("create a sandbox from {image}"))?; |
| 85 | + cx.adopt(&name).await?.wait_ready().await?; |
| 86 | + |
| 87 | + let client = cx |
| 88 | + .cli([ |
| 89 | + "sandbox", "exec", "--name", &name, "--no-tty", "--", PYTHON, "-c", CLIENT, &port, |
| 90 | + ]) |
| 91 | + .await? |
| 92 | + .success() |
| 93 | + .context("run the client in the sandbox")?; |
| 94 | + testkit::ensure!( |
| 95 | + client.stdout.contains(&marker), |
| 96 | + "the client did not get the host server's response:\n{}{}", |
| 97 | + client.stdout, |
| 98 | + client.stderr |
| 99 | + ); |
| 100 | + Ok(()) |
| 101 | +} |
| 102 | + |
| 103 | +/// A sandbox policy that allows the image's system Python ([`PYTHON`]) to |
| 104 | +/// connect to `host.openshell.internal` on `port`. The endpoint names only the |
| 105 | +/// host and port, as the policy docs describe, so the driver's address for the |
| 106 | +/// name is what makes it reachable, not an `allowed_ips` range. |
| 107 | +fn policy(port: u16) -> String { |
| 108 | + format!( |
| 109 | + r#"version: 1 |
| 110 | +
|
| 111 | +filesystem_policy: |
| 112 | + include_workdir: true |
| 113 | + read_only: [/usr, /lib, /lib64, /proc, /dev/urandom, /etc] |
| 114 | + read_write: [/sandbox, /tmp, /dev/null] |
| 115 | +landlock: |
| 116 | + compatibility: best_effort |
| 117 | +
|
| 118 | +process: |
| 119 | + run_as_user: sandbox |
| 120 | + run_as_group: sandbox |
| 121 | +
|
| 122 | +network_policies: |
| 123 | + host_server: |
| 124 | + name: host_server |
| 125 | + endpoints: |
| 126 | + - host: host.openshell.internal |
| 127 | + port: {port} |
| 128 | + binaries: |
| 129 | + - path: /usr/bin/python3.* |
| 130 | +"# |
| 131 | + ) |
| 132 | +} |
| 133 | + |
| 134 | +/// An HTTP server on the host that answers every request with `body`. It |
| 135 | +/// stops when dropped. |
| 136 | +struct HostServer { |
| 137 | + port: u16, |
| 138 | + task: JoinHandle<()>, |
| 139 | +} |
| 140 | + |
| 141 | +impl HostServer { |
| 142 | + /// Listen on every host address, since drivers map |
| 143 | + /// `host.openshell.internal` to different ones. |
| 144 | + async fn start(body: String) -> Result<Self> { |
| 145 | + let listener = TcpListener::bind(("0.0.0.0", 0)) |
| 146 | + .await |
| 147 | + .map_err(|error| Error::from_error(&error)) |
| 148 | + .context("bind the host server")?; |
| 149 | + let port = listener |
| 150 | + .local_addr() |
| 151 | + .map_err(|error| Error::from_error(&error)) |
| 152 | + .context("read the host server's address")? |
| 153 | + .port(); |
| 154 | + let task = tokio::spawn(async move { |
| 155 | + while let Ok((stream, _)) = listener.accept().await { |
| 156 | + tokio::spawn(respond(stream, body.clone())); |
| 157 | + } |
| 158 | + }); |
| 159 | + Ok(Self { port, task }) |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +impl Drop for HostServer { |
| 164 | + fn drop(&mut self) { |
| 165 | + self.task.abort(); |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +/// Read one request's headers from `stream` and answer with `body`. |
| 170 | +async fn respond(mut stream: TcpStream, body: String) { |
| 171 | + let _ = stream.set_nodelay(true); |
| 172 | + let mut request = Vec::new(); |
| 173 | + let mut buf = [0_u8; 1024]; |
| 174 | + while !request.windows(4).any(|window| window == b"\r\n\r\n") { |
| 175 | + match stream.read(&mut buf).await { |
| 176 | + Ok(read) if read > 0 && request.len() < 64 * 1024 => { |
| 177 | + request.extend_from_slice(&buf[..read]); |
| 178 | + } |
| 179 | + _ => return, |
| 180 | + } |
| 181 | + } |
| 182 | + let response = format!( |
| 183 | + "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", |
| 184 | + body.len() |
| 185 | + ); |
| 186 | + let _ = stream.write_all(response.as_bytes()).await; |
| 187 | + let _ = stream.shutdown().await; |
| 188 | +} |
| 189 | + |
| 190 | +/// A sandbox policy written to a temporary file for `sandbox create --policy`. |
| 191 | +struct PolicyFile(tempfile::NamedTempFile); |
| 192 | + |
| 193 | +impl PolicyFile { |
| 194 | + fn new(contents: &str) -> Result<Self> { |
| 195 | + let file = tempfile::NamedTempFile::new() |
| 196 | + .map_err(|error| Error::from_error(&error)) |
| 197 | + .context("create policy file")?; |
| 198 | + std::fs::write(file.path(), contents) |
| 199 | + .map_err(|error| Error::from_error(&error)) |
| 200 | + .context(format!("write {}", file.path().display()))?; |
| 201 | + Ok(Self(file)) |
| 202 | + } |
| 203 | + |
| 204 | + fn path(&self) -> Result<&str> { |
| 205 | + let path = self.0.path(); |
| 206 | + path.to_str() |
| 207 | + .ok_or_else(|| Error::msg(format!("{} is not UTF-8", path.display()))) |
| 208 | + } |
| 209 | +} |
0 commit comments