Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/suites/conformance/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ nextest run the tests as the library's unit tests. The crate also builds the
| `cli` | The same lifecycle through the `openshell` CLI: status, create, get, list, exec, stop, start, delete |
| `persistent-workspace` | Files in `/sandbox` survive stop and start |
| `policy-advisor` | `policy.local` accepts a proposed rule; a denied address or hostname yields one draft rule |
| `policy-dns` | A client that keeps a policy DNS address can open new connections to it after the lookup |
| `file-transfer` | `sandbox upload` and `download`: round trips, file modes and symlinks outside the workspace, `.gitignore` filtering, workspace-escape and option-looking names, a large upload |
| `create-upload` | `sandbox create --upload`: directory basename and nested files, multiple uploads, a single file |
| `credential-gating` | Provider credentials: admission of credentialed endpoints, REST body placeholders and rewriting, WebSocket binary frames, live policy binding |
Expand Down
1 change: 1 addition & 0 deletions tests/suites/conformance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod labels;
mod lifecycle;
mod persistent_workspace;
mod policy_advisor;
mod policy_dns;
mod process;
mod settings;
mod smoke;
Expand Down
139 changes: 139 additions & 0 deletions tests/suites/conformance/src/policy_dns.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! Policy DNS answers a sandbox's lookup of an allowed name with a synthetic
//! address. Clients keep resolved addresses in their own DNS caches (libcurl
//! for 60 s, the JDK for 30 s). The network rules docs promise that a new
//! connection to a cached address still works after its DNS mapping expires:
//! OpenShell refreshes the destination, and the address stays the same while
//! the endpoint policy is unchanged.
//!
//! The upstream server runs on the test host ([`host_server_support`]) and
//! the sandbox reaches it as `host.openshell.internal`.

use std::time::Duration;

use host_server_support::HttpRecorder;
use testkit::policy;
use testkit::sdk::raw::proto;
use testkit::{Context, PYTHON_BINARIES, Result, ResultExt as _, Sandbox};

const TEST_HOST: &str = "host.openshell.internal";

/// How long the client keeps the address before connecting again.
///
/// Policy DNS gives a host alias such as `TEST_HOST` a mapping that expires
/// exactly 30 s after the lookup (`MAX_MAPPING_TTL` in
/// `openshell-supervisor-network`), with no grace period. Waiting 33 s
/// outlasts that mapping by a few seconds whatever the timing of the exec
/// calls, so the second connection has to go through the refresh. The wait
/// starts after the first connection returns, so the address is older still
/// when the test reuses it.
///
/// A shorter wait would need a mapping with a shorter lifetime. Other names
/// take their upstream DNS TTL, but a conformance test can't control the
/// sandbox's upstream resolver, and the host server is reachable only through
/// the host alias.
///
/// A longer wait adds nothing: the docs set no limit on how long a cached
/// address works, and once the mapping has expired every connection takes the
/// same refresh path.
const CACHED_ADDRESS_AGE: Duration = Duration::from_secs(33);

/// Resolves `TEST_HOST` the way a client does and prints the synthetic IPv4
/// address policy DNS handed out.
const RESOLVE: &str = r#"
import socket, sys
answers = socket.getaddrinfo(sys.argv[1], int(sys.argv[2]), socket.AF_INET, socket.SOCK_STREAM)
synthetic = [a[4][0] for a in answers if a[4][0].startswith(("198.18.", "198.19."))]
if not synthetic:
sys.exit(f"no policy DNS address for {sys.argv[1]}: {answers}")
print(synthetic[0])
"#;

/// Connects to the address in `argv[1]` on the port in `argv[2]`, as a client
/// with a cached answer does, and prints the response.
const CONNECT: &str = r#"
import socket, sys
address, port = sys.argv[1], int(sys.argv[2])
with socket.create_connection((address, port), timeout=10) as conn:
conn.sendall(f"GET / HTTP/1.1\r\nHost: host.openshell.internal:{port}\r\nConnection: close\r\n\r\n".encode())
response = b""
while chunk := conn.recv(4096):
response += chunk
print(response.decode(errors="replace"))
"#;

/// A new connection to a policy DNS address, made after the client has kept
/// the address for [`CACHED_ADDRESS_AGE`], reaches the allowed server while
/// the policy still allows the name. An address policy DNS never handed out
/// is refused. Regression test for NVIDIA/OpenShell#4674.
#[testkit::test]
async fn cached_address_takes_new_connections(cx: &mut Context) -> testkit::Result {
let marker = format!("policy-dns-{}", cx.run_id());
let server = HttpRecorder::start(marker.clone()).await?;
let port = server.port.to_string();
let sandbox = cx
.sandbox("pdns")
.image(testkit::python_image())
.policy(policy(server.port))
.command(["sleep", "infinity"])
.create_ready()
.await?;

let address = python(&sandbox, RESOLVE, TEST_HOST, &port)
.await?
.success()
.context("resolve the host through policy DNS")?
.stdout
.trim()
.to_string();

expect_response(&sandbox, &address, &port, &marker)
.await
.context(format!("connect to {address} right after the lookup"))?;

tokio::time::sleep(CACHED_ADDRESS_AGE).await;
expect_response(&sandbox, &address, &port, &marker)
.await
.context(format!(
"connect to {address} {}s after the lookup, without a new lookup",
CACHED_ADDRESS_AGE.as_secs()
))?;

// The last address in the synthetic range, which a sandbox with one
// allowed name is never handed.
python(&sandbox, CONNECT, "198.19.255.254", &port)
.await?
.failure()
.context("connect to a synthetic address policy DNS never handed out")?;
Ok(())
}

async fn python(
sandbox: &Sandbox,
script: &str,
target: &str,
port: &str,
) -> Result<testkit::ExecOutput> {
sandbox.exec(["python3", "-c", script, target, port]).await
}

/// Fail unless a new connection to `address` gets the server's response.
async fn expect_response(sandbox: &Sandbox, address: &str, port: &str, marker: &str) -> Result {
let output = python(sandbox, CONNECT, address, port).await?.success()?;
testkit::ensure!(
output.stdout.contains(marker),
"the client did not get the host server's response:\n{}{}",
output.stdout,
output.stderr
);
Ok(())
}

/// Let the image's Python reach `TEST_HOST` on `port`.
fn policy(port: u16) -> proto::SandboxPolicy {
let mut policy = policy::base();
policy::allow(&mut policy, "host_server", TEST_HOST, port, PYTHON_BINARIES);
policy
}
Loading