@@ -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) ]
550524mod 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 ) ]
0 commit comments