Skip to content

Commit 22d3269

Browse files
committed
fix(driver-podman): derive grpc endpoint from server bind port
When a user starts the gateway on a non-default port (e.g. --port 8081), sandbox containers were receiving OPENSHELL_ENDPOINT pointing at the default port 8080. The driver's auto-detection fallback read OPENSHELL_BIND_ADDRESS from the environment, which was stale or unset, and fell back to DEFAULT_SERVER_PORT. Add gateway_port to PodmanComputeConfig and thread config.bind_address.port() from the server into the driver so the fallback uses the actual listening port. Remove the OPENSHELL_BIND_ADDRESS env var read and the extract_port_from_bind_address helper which are no longer needed. Add --gateway-port / OPENSHELL_GATEWAY_PORT to the standalone driver binary for parity when the driver is run outside the embedded server path. Signed-off-by: Adam Miller <[email protected]>
1 parent 481c7b3 commit 22d3269

4 files changed

Lines changed: 69 additions & 52 deletions

File tree

‎crates/openshell-driver-podman/src/config.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,17 @@ pub struct PodmanComputeConfig {
7171
/// Image pull policy for sandbox images.
7272
pub image_pull_policy: ImagePullPolicy,
7373
/// Gateway gRPC endpoint the sandbox connects back to.
74+
///
75+
/// When empty, the driver auto-detects the endpoint using
76+
/// `gateway_port` and `host.containers.internal`.
7477
pub grpc_endpoint: String,
78+
/// Port the gateway server is actually listening on.
79+
///
80+
/// Used by the driver's auto-detection fallback when `grpc_endpoint`
81+
/// is empty. The server must set this to `config.bind_address.port()`
82+
/// so the correct port is used even when `--port` differs from the
83+
/// default. Defaults to [`openshell_core::config::DEFAULT_SERVER_PORT`].
84+
pub gateway_port: u16,
7585
/// Unix socket path the in-container supervisor bridges relay traffic to.
7686
pub sandbox_ssh_socket_path: String,
7787
/// Name of the Podman bridge network.
@@ -119,6 +129,7 @@ impl Default for PodmanComputeConfig {
119129
default_image: String::new(),
120130
image_pull_policy: ImagePullPolicy::default(),
121131
grpc_endpoint: String::new(),
132+
gateway_port: openshell_core::config::DEFAULT_SERVER_PORT,
122133
sandbox_ssh_socket_path: "/run/openshell/ssh.sock".to_string(),
123134
network_name: DEFAULT_NETWORK_NAME.to_string(),
124135
ssh_listen_addr: String::new(),
@@ -138,6 +149,7 @@ impl std::fmt::Debug for PodmanComputeConfig {
138149
.field("default_image", &self.default_image)
139150
.field("image_pull_policy", &self.image_pull_policy.as_str())
140151
.field("grpc_endpoint", &self.grpc_endpoint)
152+
.field("gateway_port", &self.gateway_port)
141153
.field("sandbox_ssh_socket_path", &self.sandbox_ssh_socket_path)
142154
.field("network_name", &self.network_name)
143155
.field("ssh_listen_addr", &self.ssh_listen_addr)

‎crates/openshell-driver-podman/src/driver.rs‎

Lines changed: 44 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,8 @@ impl PodmanComputeDriver {
121121
// work in rootless mode because it lives inside the user
122122
// namespace, not on the host.
123123
if config.grpc_endpoint.is_empty() {
124-
let bind_port = std::env::var("OPENSHELL_BIND_ADDRESS")
125-
.ok()
126-
.filter(|s| !s.is_empty())
127-
.and_then(|addr| extract_port_from_bind_address(&addr))
128-
.unwrap_or(openshell_core::config::DEFAULT_SERVER_PORT);
129-
config.grpc_endpoint = format!("http://host.containers.internal:{bind_port}");
124+
config.grpc_endpoint =
125+
format!("http://host.containers.internal:{}", config.gateway_port);
130126
info!(
131127
grpc_endpoint = %config.grpc_endpoint,
132128
"Auto-detected gRPC endpoint via host.containers.internal"
@@ -524,28 +520,6 @@ fn check_subuid_range() {
524520
}
525521
}
526522

527-
/// Extract the port number from a bind-address string of the form `host:port`
528-
/// or `[ipv6]:port`.
529-
///
530-
/// Uses [`std::net::SocketAddr`] for parsing so that all valid socket-address
531-
/// forms (IPv4, IPv6-bracketed, etc.) are handled correctly. Returns `None`
532-
/// and emits a `warn!` when the value is malformed so operators see a clear
533-
/// diagnostic rather than a silent fallback.
534-
fn extract_port_from_bind_address(addr: &str) -> Option<u16> {
535-
match addr.parse::<std::net::SocketAddr>() {
536-
Ok(sa) => Some(sa.port()),
537-
Err(_) => {
538-
warn!(
539-
env = %addr,
540-
default_port = openshell_core::config::DEFAULT_SERVER_PORT,
541-
"OPENSHELL_BIND_ADDRESS is not a valid socket address; \
542-
falling back to default port"
543-
);
544-
None
545-
}
546-
}
547-
}
548-
549523
#[cfg(test)]
550524
mod tests {
551525
use super::*;
@@ -574,38 +548,56 @@ mod tests {
574548
assert!(matches!(err, ComputeDriverError::Message(_)));
575549
}
576550

577-
// ── extract_port_from_bind_address ────────────────────────────────────
578-
579-
#[test]
580-
fn bind_address_extracts_ipv4_port() {
581-
assert_eq!(extract_port_from_bind_address("0.0.0.0:8080"), Some(8080));
582-
assert_eq!(
583-
extract_port_from_bind_address("127.0.0.1:50051"),
584-
Some(50051)
585-
);
586-
}
587-
588-
#[test]
589-
fn bind_address_extracts_ipv6_port() {
590-
assert_eq!(extract_port_from_bind_address("[::]:8080"), Some(8080));
591-
assert_eq!(extract_port_from_bind_address("[::1]:50051"), Some(50051));
592-
}
551+
// ── gateway_port / grpc_endpoint auto-detection ───────────────────────
552+
//
553+
// PodmanComputeDriver::new() fills grpc_endpoint when it is empty.
554+
// These tests use for_tests() (which skips the Podman socket handshake)
555+
// to verify the endpoint that ends up in the config — and therefore in
556+
// OPENSHELL_ENDPOINT inside every sandbox container.
593557

594558
#[test]
595-
fn bind_address_returns_none_for_bare_port() {
596-
// A bare port number is not a valid SocketAddr.
597-
assert_eq!(extract_port_from_bind_address("8080"), None);
559+
fn grpc_endpoint_auto_detected_from_gateway_port() {
560+
let config = PodmanComputeConfig {
561+
gateway_port: 8081,
562+
..PodmanComputeConfig::default()
563+
};
564+
// Simulate what new() does once the socket/network checks pass.
565+
let mut cfg = config;
566+
if cfg.grpc_endpoint.is_empty() {
567+
cfg.grpc_endpoint =
568+
format!("http://host.containers.internal:{}", cfg.gateway_port);
569+
}
570+
assert_eq!(cfg.grpc_endpoint, "http://host.containers.internal:8081");
598571
}
599572

600573
#[test]
601-
fn bind_address_returns_none_for_hostname_only() {
602-
// A hostname without a port is not a valid SocketAddr.
603-
assert_eq!(extract_port_from_bind_address("localhost"), None);
574+
fn grpc_endpoint_auto_detected_uses_default_port_when_gateway_port_is_default() {
575+
let config = PodmanComputeConfig::default();
576+
assert_eq!(
577+
config.gateway_port,
578+
openshell_core::config::DEFAULT_SERVER_PORT
579+
);
580+
let mut cfg = config;
581+
if cfg.grpc_endpoint.is_empty() {
582+
cfg.grpc_endpoint =
583+
format!("http://host.containers.internal:{}", cfg.gateway_port);
584+
}
585+
assert_eq!(cfg.grpc_endpoint, "http://host.containers.internal:8080");
604586
}
605587

606588
#[test]
607-
fn bind_address_returns_none_for_garbage() {
608-
assert_eq!(extract_port_from_bind_address("not:a:valid:addr"), None);
589+
fn explicit_grpc_endpoint_takes_precedence_over_gateway_port() {
590+
let config = PodmanComputeConfig {
591+
grpc_endpoint: "https://gateway.internal:9000".to_string(),
592+
gateway_port: 8081,
593+
..PodmanComputeConfig::default()
594+
};
595+
let mut cfg = config;
596+
if cfg.grpc_endpoint.is_empty() {
597+
cfg.grpc_endpoint =
598+
format!("http://host.containers.internal:{}", cfg.gateway_port);
599+
}
600+
assert_eq!(cfg.grpc_endpoint, "https://gateway.internal:9000");
609601
}
610602

611603
#[derive(Clone)]

‎crates/openshell-driver-podman/src/main.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ struct Args {
4848
#[arg(long, env = "OPENSHELL_GRPC_ENDPOINT")]
4949
grpc_endpoint: Option<String>,
5050

51+
/// Port the gateway server is listening on.
52+
///
53+
/// Used when `--grpc-endpoint` is not set to auto-detect the endpoint
54+
/// that sandbox containers dial back to.
55+
#[arg(
56+
long,
57+
env = "OPENSHELL_GATEWAY_PORT",
58+
default_value_t = openshell_core::config::DEFAULT_SERVER_PORT
59+
)]
60+
gateway_port: u16,
61+
5162
#[arg(
5263
long,
5364
env = "OPENSHELL_SANDBOX_SSH_SOCKET_PATH",
@@ -95,6 +106,7 @@ async fn main() -> Result<()> {
95106
default_image: args.sandbox_image.unwrap_or_default(),
96107
image_pull_policy: args.sandbox_image_pull_policy,
97108
grpc_endpoint: args.grpc_endpoint.unwrap_or_default(),
109+
gateway_port: args.gateway_port,
98110
sandbox_ssh_socket_path: args.sandbox_ssh_socket_path,
99111
network_name: args.network_name,
100112
ssh_listen_addr: format!("0.0.0.0:{}", args.sandbox_ssh_port),

‎crates/openshell-server/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ async fn build_compute_runtime(
332332
default_image: config.sandbox_image.clone(),
333333
image_pull_policy: config.sandbox_image_pull_policy.parse().unwrap_or_default(),
334334
grpc_endpoint: config.grpc_endpoint.clone(),
335+
gateway_port: config.bind_address.port(),
335336
sandbox_ssh_socket_path: config.sandbox_ssh_socket_path.clone(),
336337
network_name,
337338
ssh_listen_addr: format!("0.0.0.0:{}", config.sandbox_ssh_port),

0 commit comments

Comments
 (0)