Skip to content

Commit 1e069a1

Browse files
committed
refactor(services): derive app listener domain from base URL
Signed-off-by: Taylor Mutch <taylormutch@gmail.com>
1 parent 42c5d3b commit 1e069a1

17 files changed

Lines changed: 204 additions & 128 deletions

File tree

‎crates/openshell-core/src/app_listener.rs‎

Lines changed: 85 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,66 +13,82 @@ use sha2::{Digest, Sha256};
1313
pub 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

2120
impl 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+
3252
impl 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());

‎crates/openshell-driver-kubernetes/src/app_listener.rs‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ pub fn configure_supervisor_pod(mut pod: Pod, config: &KubernetesAppListenerConf
2828
"OPENSHELL_APP_LISTEN_ADDRESS",
2929
format!("0.0.0.0:{}", config.common.port),
3030
),
31-
("OPENSHELL_APP_SERVICE_DOMAIN", config.common.domain.clone()),
31+
(
32+
"OPENSHELL_APP_BASE_URL",
33+
config.common.base_url.clone().unwrap_or_default(),
34+
),
3235
("OPENSHELL_APP_TLS_CERT", format!("{TLS_PATH}/tls.crt")),
3336
("OPENSHELL_APP_TLS_KEY", format!("{TLS_PATH}/tls.key")),
3437
] {
@@ -86,4 +89,41 @@ mod tests {
8689
};
8790
assert_eq!(configure_supervisor_pod(pod.clone(), &config), pod);
8891
}
92+
93+
#[test]
94+
fn supervisor_receives_base_url_separately_from_its_listener_port() {
95+
let pod = Pod {
96+
spec: Some(k8s_openapi::api::core::v1::PodSpec {
97+
containers: vec![k8s_openapi::api::core::v1::Container {
98+
name: "supervisor".into(),
99+
..Default::default()
100+
}],
101+
..Default::default()
102+
}),
103+
..Default::default()
104+
};
105+
let config = KubernetesAppListenerConfig {
106+
common: openshell_core::app_listener::AppListenerConfig {
107+
enabled: true,
108+
port: 9443,
109+
base_url: Some("https://services.example.com:10443".into()),
110+
},
111+
tls_secret_name: "app-tls".into(),
112+
};
113+
config.common.validate().unwrap();
114+
let pod = configure_supervisor_pod(pod, &config);
115+
let env = pod.spec.unwrap().containers.remove(0).env.unwrap();
116+
let value = |name| {
117+
env.iter()
118+
.find(|entry| entry.name == name)
119+
.unwrap()
120+
.value
121+
.as_deref()
122+
};
123+
assert_eq!(
124+
value("OPENSHELL_APP_BASE_URL"),
125+
Some("https://services.example.com:10443")
126+
);
127+
assert_eq!(value("OPENSHELL_APP_LISTEN_ADDRESS"), Some("0.0.0.0:9443"));
128+
}
89129
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,10 +1178,9 @@ mod tests {
11781178
assert!(!config.app_listener.common.enabled);
11791179
assert!(config.validate().is_ok());
11801180
config.app_listener.common = openshell_core::app_listener::AppListenerConfig {
1181-
public_url: None,
1181+
base_url: Some("https://services.example.com".into()),
11821182
enabled: true,
11831183
port: 9443,
1184-
domain: "services.example.com".into(),
11851184
};
11861185
assert!(config.validate().is_ok());
11871186
config.app_listener.common.port = config.boundary_port;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ async fn run_from_args(
10231023

10241024
if prepared.config.supervisor_app_listener.enabled {
10251025
info!(
1026-
domain = %prepared.config.supervisor_app_listener.domain,
1026+
domain = %prepared.config.supervisor_app_listener.domain(),
10271027
port = prepared.config.supervisor_app_listener.port,
10281028
"App-listener-based service routing enabled"
10291029
);

‎crates/openshell-server/src/grpc/mutation_replay/tests.rs‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ async fn create_url_maps_follow_independent_routing_flags_and_replays_keep_recor
118118
config.service_routing.enabled = gateway_enabled;
119119
config.supervisor_app_listener = openshell_core::app_listener::AppListenerConfig {
120120
enabled: application_enabled,
121-
domain: "services.example.com".into(),
122-
public_url: application_enabled
123-
.then(|| "https://services.example.com:10443".into()),
121+
base_url: application_enabled.then(|| "https://services.example.com:10443".into()),
124122
..Default::default()
125123
};
126124
let request = CreateSandboxRequest {
@@ -168,7 +166,7 @@ async fn create_url_maps_follow_independent_routing_flags_and_replays_keep_recor
168166
let config = &mut Arc::get_mut(&mut state).unwrap().config;
169167
config.service_routing.enabled = !gateway_enabled;
170168
config.supervisor_app_listener.enabled = !application_enabled;
171-
config.supervisor_app_listener.public_url =
169+
config.supervisor_app_listener.base_url =
172170
(!application_enabled).then(|| "https://services.example.com:10443".into());
173171
let replay = run(&state, authed_request(request)).await.unwrap();
174172
assert_eq!(replay.metadata().get("openshell-replayed").unwrap(), "true");
@@ -213,8 +211,7 @@ async fn create_replay_preserves_recorded_urls_and_refreshes_explicit_origins()
213211
config.supervisor_app_listener = openshell_core::app_listener::AppListenerConfig {
214212
enabled: true,
215213
port: 9443,
216-
domain: "services.example.com".into(),
217-
public_url: Some("https://services.example.com:10443".into()),
214+
base_url: Some("https://services.example.com:10443".into()),
218215
};
219216
let replay = run(&state, authed_request(request)).await.unwrap();
220217
assert_eq!(replay.metadata().get("openshell-replayed").unwrap(), "true");
@@ -253,8 +250,7 @@ async fn expose_replay_preserves_recorded_url_when_origins_change_or_are_disable
253250
.supervisor_app_listener = openshell_core::app_listener::AppListenerConfig {
254251
enabled: true,
255252
port: 9443,
256-
domain: "old.example.com".into(),
257-
public_url: Some("https://old.example.com:10443".into()),
253+
base_url: Some("https://old.example.com:10443".into()),
258254
};
259255
run(
260256
&state,
@@ -286,8 +282,7 @@ async fn expose_replay_preserves_recorded_url_when_origins_change_or_are_disable
286282
let config = &mut Arc::get_mut(&mut state).unwrap().config;
287283
config.service_routing.base_domains = vec!["gateway.example.com".into()];
288284
config.bind_address.set_port(12443);
289-
config.supervisor_app_listener.domain = "new.example.com".into();
290-
config.supervisor_app_listener.public_url = Some("https://new.example.com:11443".into());
285+
config.supervisor_app_listener.base_url = Some("https://new.example.com:11443".into());
291286
let replay = run(&state, authed_request(request.clone())).await.unwrap();
292287
assert_eq!(replay.metadata().get("openshell-replayed").unwrap(), "true");
293288
assert_eq!(replay.get_ref().endpoint, original.endpoint);
@@ -304,7 +299,7 @@ async fn expose_replay_preserves_recorded_url_when_origins_change_or_are_disable
304299
assert_eq!(gateway.port(), Some(12443));
305300
let config = &mut Arc::get_mut(&mut state).unwrap().config;
306301
config.supervisor_app_listener.enabled = false;
307-
config.supervisor_app_listener.public_url = None;
302+
config.supervisor_app_listener.base_url = None;
308303
let without_application = run(&state, authed_request(request.clone())).await.unwrap();
309304
assert_eq!(
310305
without_application

0 commit comments

Comments
 (0)