@@ -13,66 +13,82 @@ use sha2::{Digest, Sha256};
1313pub struct AppListenerConfig {
1414 pub enabled : bool ,
1515 pub port : u16 ,
16- pub domain : String ,
17- /// Public HTTPS origin, independently of the supervisor's listener port.
18- pub public_url : Option < String > ,
16+ /// Public HTTPS origin used to derive service hostnames and URLs.
17+ pub base_url : Option < String > ,
1918}
2019
2120impl Default for AppListenerConfig {
2221 fn default ( ) -> Self {
2322 Self {
2423 enabled : false ,
2524 port : 8443 ,
26- domain : String :: new ( ) ,
27- public_url : None ,
25+ base_url : None ,
2826 }
2927 }
3028}
3129
30+ /// Validate a listener endpoint after deriving its domain from the base URL.
31+ fn validate_listener_endpoint ( port : u16 , domain : & str ) -> Result < ( ) , String > {
32+ if port < 1024 {
33+ return Err ( "supervisor.app_listener.port must be at least 1024" . into ( ) ) ;
34+ }
35+ if domain. is_empty ( )
36+ || domain. len ( ) > 253
37+ || !domain. split ( '.' ) . all ( |label| {
38+ !label. is_empty ( )
39+ && label. len ( ) <= 63
40+ && !label. starts_with ( '-' )
41+ && !label. ends_with ( '-' )
42+ && label
43+ . bytes ( )
44+ . all ( |b| b. is_ascii_lowercase ( ) || b. is_ascii_digit ( ) || b == b'-' )
45+ } )
46+ {
47+ return Err ( "app listener domain must be a lowercase DNS name" . into ( ) ) ;
48+ }
49+ Ok ( ( ) )
50+ }
51+
3252impl AppListenerConfig {
3353 pub fn validate ( & self ) -> Result < ( ) , String > {
3454 if !self . enabled {
35- if self . public_url . is_some ( ) {
36- return Err ( "supervisor.app_listener.public_url requires enabled = true" . into ( ) ) ;
37- }
3855 return Ok ( ( ) ) ;
3956 }
40- if self . port < 1024 {
41- return Err ( "supervisor.app_listener.port must be at least 1024" . into ( ) ) ;
42- }
43- if self . domain . len ( ) > 253
44- || !self . domain . split ( '.' ) . all ( |label| {
45- !label. is_empty ( )
46- && label. len ( ) <= 63
47- && !label. starts_with ( '-' )
48- && !label. ends_with ( '-' )
49- && label
50- . bytes ( )
51- . all ( |b| b. is_ascii_lowercase ( ) || b. is_ascii_digit ( ) || b == b'-' )
52- } )
57+ let origin = self . parsed_base_url ( ) ?;
58+ validate_listener_endpoint ( self . port , origin. host_str ( ) . unwrap_or_default ( ) )
59+ }
60+
61+ fn parsed_base_url ( & self ) -> Result < url:: Url , String > {
62+ let base = self . base_url . as_deref ( ) . ok_or_else ( || {
63+ "supervisor.app_listener.base_url is required when enabled" . to_string ( )
64+ } ) ?;
65+ let parsed = url:: Url :: parse ( base)
66+ . map_err ( |_| "supervisor.app_listener.base_url must be an HTTPS origin" . to_string ( ) ) ?;
67+ if parsed. scheme ( ) != "https"
68+ || !matches ! ( parsed. host( ) , Some ( url:: Host :: Domain ( _) ) )
69+ || !parsed. username ( ) . is_empty ( )
70+ || parsed. password ( ) . is_some ( )
71+ || parsed. path ( ) != "/"
72+ || parsed. query ( ) . is_some ( )
73+ || parsed. fragment ( ) . is_some ( )
74+ || parsed. port ( ) == Some ( 0 )
5375 {
54- return Err ( "supervisor.app_listener.domain must be a lowercase DNS name" . into ( ) ) ;
55- }
56- if let Some ( origin) = & self . public_url {
57- let parsed = url:: Url :: parse ( origin)
58- . map_err ( |_| "supervisor.app_listener.public_url must be an HTTPS origin" ) ?;
59- if parsed. scheme ( ) != "https"
60- || parsed. host_str ( ) != Some ( self . domain . as_str ( ) )
61- || !parsed. username ( ) . is_empty ( )
62- || parsed. password ( ) . is_some ( )
63- || parsed. path ( ) != "/"
64- || parsed. query ( ) . is_some ( )
65- || parsed. fragment ( ) . is_some ( )
66- || parsed. port ( ) == Some ( 0 )
67- {
68- return Err ( "supervisor.app_listener.public_url must be an HTTPS origin matching domain, without credentials, path, query, or fragment" . into ( ) ) ;
69- }
76+ return Err ( "supervisor.app_listener.base_url must be an HTTPS origin with a DNS hostname, without credentials, path, query, or fragment" . into ( ) ) ;
7077 }
71- Ok ( ( ) )
78+ validate_listener_endpoint ( self . port , parsed. host_str ( ) . unwrap_or_default ( ) ) ?;
79+ Ok ( parsed)
80+ }
81+
82+ /// Hostname suffix derived from the configured base URL. Unconfigured or
83+ /// invalid listener settings have no service domain.
84+ pub fn domain ( & self ) -> String {
85+ self . parsed_base_url ( )
86+ . ok ( )
87+ . and_then ( |origin| origin. host_str ( ) . map ( str:: to_string) )
88+ . unwrap_or_default ( )
7289 }
7390
7491 /// Canonical SNI/Host shared by URL publication, configuration and discovery.
75- /// A discriminator selects the gateway's hashed mode; otherwise use readable names.
7692 pub fn service_hostname (
7793 & self ,
7894 workspace : & str ,
@@ -81,7 +97,7 @@ impl AppListenerConfig {
8197 discriminator : Option < & str > ,
8298 ) -> String {
8399 let label = service_route_label ( discriminator, workspace, sandbox, service) ;
84- format ! ( "{label}.{}" , self . domain)
100+ format ! ( "{label}.{}" , self . domain( ) )
85101 }
86102
87103 /// Configured address only; this does not imply installation or readiness.
@@ -95,7 +111,7 @@ impl AppListenerConfig {
95111 if !self . enabled {
96112 return None ;
97113 }
98- let mut origin = url :: Url :: parse ( self . public_url . as_deref ( ) ? ) . ok ( ) ?;
114+ let mut origin = self . parsed_base_url ( ) . ok ( ) ?;
99115 origin
100116 . set_host ( Some ( & self . service_hostname (
101117 workspace,
@@ -198,8 +214,7 @@ mod tests {
198214 fn application_urls_share_gateway_hash_labels_and_preserve_public_origin ( ) {
199215 let config = AppListenerConfig {
200216 enabled : true ,
201- domain : "apps.example.com" . into ( ) ,
202- public_url : Some ( "https://apps.example.com:10443" . into ( ) ) ,
217+ base_url : Some ( "https://apps.example.com:10443" . into ( ) ) ,
203218 ..Default :: default ( )
204219 } ;
205220 assert_eq ! (
@@ -221,44 +236,62 @@ mod tests {
221236 }
222237
223238 #[ test]
224- fn public_origin_is_explicit_https_and_matches_listener_domain ( ) {
239+ fn base_url_defines_the_service_domain_and_public_port ( ) {
225240 let mut config = AppListenerConfig {
226241 enabled : true ,
227- domain : "services.example.com" . into ( ) ,
228- public_url : Some ( "https://services.example.com:10443" . into ( ) ) ,
242+ base_url : Some ( "https://services.example.com:10443" . into ( ) ) ,
229243 ..Default :: default ( )
230244 } ;
231245 config. validate ( ) . unwrap ( ) ;
246+ assert_eq ! ( config. domain( ) , "services.example.com" ) ;
232247 assert_eq ! (
233248 config
234249 . service_url( "default" , "demo" , "web" , None )
235250 . as_deref( ) ,
236251 Some ( "https://default--demo--web.services.example.com:10443/" )
237252 ) ;
238- config. public_url = Some ( "https://services.example.com:443/" . into ( ) ) ;
253+ config. base_url = Some ( "https://services.example.com:443/" . into ( ) ) ;
239254 assert_eq ! (
240255 config. service_url( "default" , "demo" , "" , None ) . as_deref( ) ,
241256 Some ( "https://default--demo.services.example.com/" )
242257 ) ;
243258 for bad in [
244259 "http://services.example.com" ,
245- "https://other.example.com" ,
260+ "https://127.0.0.1" ,
261+ "https://[::1]" ,
262+ "https://services.example.com." ,
246263 "https://user:password@services.example.com" ,
247264 "https://services.example.com/path" ,
248265 "https://services.example.com/?query" ,
249266 "https://services.example.com/#fragment" ,
250267 "https://services.example.com:0" ,
251268 "not-a-url" ,
252269 ] {
253- config. public_url = Some ( bad. into ( ) ) ;
270+ config. base_url = Some ( bad. into ( ) ) ;
254271 assert ! ( config. validate( ) . is_err( ) , "{bad}" ) ;
255272 }
256- config. public_url = Some ( "https://services.example.com" . into ( ) ) ;
273+ config. base_url = Some ( "https://services.example.com" . into ( ) ) ;
257274 config. enabled = false ;
258- assert ! ( config. validate( ) . is_err ( ) ) ;
275+ assert ! ( config. validate( ) . is_ok ( ) ) ;
259276 assert ! ( config. service_url( "default" , "demo" , "web" , None ) . is_none( ) ) ;
260277 }
261278
279+ #[ test]
280+ fn base_url_is_the_only_authored_service_domain ( ) {
281+ let config: AppListenerConfig = serde_json:: from_str (
282+ r#"{"enabled":true,"port":9443,"base_url":"https://services.example.com:10443"}"# ,
283+ )
284+ . unwrap ( ) ;
285+ config. validate ( ) . unwrap ( ) ;
286+ assert_eq ! ( config. domain( ) , "services.example.com" ) ;
287+ assert_eq ! ( config. port, 9443 ) ;
288+ for field in [ "domain" , "public_url" ] {
289+ let value = serde_json:: json!( { field: "services.example.com" } ) ;
290+ let error = serde_json:: from_value :: < AppListenerConfig > ( value) . unwrap_err ( ) ;
291+ assert ! ( error. to_string( ) . contains( field) ) ;
292+ }
293+ }
294+
262295 #[ test]
263296 fn only_explicit_enablement_requires_a_valid_endpoint ( ) {
264297 assert ! ( !AppListenerConfig :: default ( ) . enabled) ;
@@ -268,7 +301,7 @@ mod tests {
268301 ..Default :: default ( )
269302 } ;
270303 assert ! ( config. validate( ) . is_err( ) ) ;
271- config. domain = " services.example.com". into ( ) ;
304+ config. base_url = Some ( "https:// services.example.com". into ( ) ) ;
272305 assert ! ( config. validate( ) . is_ok( ) ) ;
273306 config. port = 0 ;
274307 assert ! ( config. validate( ) . is_err( ) ) ;
0 commit comments