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//!
1517use host_server_support:: { HttpRecorder , TEST_HOST } ;
1618use sandbox_clients_support:: { Request , http} ;
1719use 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]
2325async 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