Skip to content

Commit b82757c

Browse files
test(features): find the host address from the host server in routable-host-ip
Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
1 parent 42c97e4 commit b82757c

2 files changed

Lines changed: 45 additions & 19 deletions

File tree

‎TESTING.md‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -657,10 +657,11 @@ the node's address.
657657

658658
The `routable-host-ip` feature suite (`tests/suites/features/routable-host-ip`)
659659
checks that a policy endpoint whose host is an IP literal reaches that address,
660-
with or without `allowed_ips`. The sandbox uses the address
661-
`host.openshell.internal` resolves to, which must be one the supervisor lets an
662-
IP literal reach. Rootless Podman's is link-local and always blocked, so CI runs
663-
the suite on Docker rootful, Podman rootful, and the `k3s` and `k3s-ha`
660+
with or without `allowed_ips`. The test reaches a host server by
661+
`host.openshell.internal`, then uses the address that request arrived at, which
662+
must be one the supervisor lets an IP literal reach. Rootless Podman's is
663+
link-local and always blocked, and Docker Desktop's is loopback, so CI runs the
664+
suite on Docker rootful, Podman rootful, and the `k3s` and `k3s-ha`
664665
installers.
665666

666667
The `connect-ssh-supervision` feature suite

‎tests/suites/features/routable-host-ip/tests/routable_host_ip.rs‎

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
//! A policy endpoint whose host is an IP literal reaches that address
55
//! transparently, whether `allowed_ips` names it or not.
66
//!
7-
//! The servers run on the test host, and the sandbox reaches them by the
8-
//! address `host.openshell.internal` resolves to. Not conformance, because
7+
//! The servers run on the test host. The sandbox can't learn the host's
8+
//! address, since policy DNS answers it with a synthetic one, so the test
9+
//! reaches a server by `host.openshell.internal` first and takes the address
10+
//! the server saw the request arrive at. Not conformance, because
911
//! that address must be one the supervisor lets an IP literal reach: rootless
1012
//! Podman's is link-local, which is always blocked.
1113
//!
@@ -15,31 +17,54 @@
1517
use host_server_support::{HttpRecorder, TEST_HOST};
1618
use sandbox_clients_support::{Request, http};
1719
use testkit::policy;
18-
use testkit::{Context, PYTHON_BINARIES, ResultExt as _};
20+
use testkit::{Context, PYTHON_BINARIES};
1921

2022
/// Requests reach a server by an IP literal endpoint with `allowed_ips` for
2123
/// that address, and another by an IP literal endpoint without it.
2224
#[testkit::test]
2325
async fn ip_literal_endpoints_reach_the_host(cx: &mut Context) -> testkit::Result {
2426
let explicit = HttpRecorder::start("explicit").await?;
2527
let implicit = HttpRecorder::start("implicit").await?;
28+
let mut name_policy = policy::base();
29+
policy::allow(
30+
&mut name_policy,
31+
"by_name",
32+
TEST_HOST,
33+
explicit.port,
34+
PYTHON_BINARIES,
35+
);
2636
let sandbox = cx
2737
.sandbox("hstip")
28-
.policy(policy::base())
38+
.policy(name_policy)
2939
.image(testkit::python_image())
3040
.command(["sleep", "infinity"])
3141
.create_ready()
3242
.await?;
33-
let ip = sandbox
34-
.run_python(
35-
"import socket, sys; print(socket.gethostbyname(sys.argv[1]))",
36-
&[TEST_HOST],
37-
)
38-
.await
39-
.context(format!("resolve {TEST_HOST} in the sandbox"))?
40-
.stdout
41-
.trim()
42-
.to_string();
43+
let reply = http(
44+
&sandbox,
45+
TEST_HOST,
46+
explicit.port,
47+
&Request::get("/by-name"),
48+
)
49+
.await?;
50+
testkit::ensure!(
51+
reply.status() == Some(200),
52+
"request to {TEST_HOST}:{} failed: {reply:?}",
53+
explicit.port
54+
);
55+
let host_ip = explicit
56+
.requests()
57+
.first()
58+
.and_then(|request| request.local_addr)
59+
.ok_or_else(|| testkit::Error::msg("the host server didn't record its address"))?
60+
.ip();
61+
// Docker Desktop relays the host gateway through a host-side proxy.
62+
testkit::ensure!(
63+
!host_ip.is_loopback(),
64+
"requests to {TEST_HOST} arrive at the host on {host_ip}, which a sandbox can't reach"
65+
);
66+
let ip = host_ip.to_string();
67+
let prefix = if host_ip.is_ipv4() { 32 } else { 128 };
4368

4469
let mut ip_policy = policy::base();
4570
policy::allow(
@@ -49,7 +74,7 @@ async fn ip_literal_endpoints_reach_the_host(cx: &mut Context) -> testkit::Resul
4974
explicit.port,
5075
PYTHON_BINARIES,
5176
)
52-
.allowed_ips = vec![format!("{ip}/32")];
77+
.allowed_ips = vec![format!("{ip}/{prefix}")];
5378
policy::allow(
5479
&mut ip_policy,
5580
"implicit",

0 commit comments

Comments
 (0)