Skip to content

Commit 28551bd

Browse files
committed
feat(gateway): add hashed service-routing hostnames for multi-gateway clusters
Closes #4840 Add an optional service_routing_gateway_name gateway setting (TOML, CLI, env, Helm) that disambiguates exposed service URLs when multiple gateways share a cluster, ingress, and wildcard certificate. When configured, exposed-service hostnames become a deterministic 16-character lowercase base32 SHA-256 label of (gateway name, workspace, sandbox, service) under the same base domain, keeping every hostname exactly one label deep for wildcard-certificate compatibility. Unset gateways preserve the existing readable hostname format byte-for-byte, and configured gateways still accept legacy hosts during migration. Hashed hosts resolve through an in-memory route index hydrated from persisted ServiceEndpoint records at startup, refreshed on miss for multi-replica deployments, maintained across expose, delete, and sandbox cleanup, and enforced for collisions before endpoint persistence. Signed-off-by: Jesse Jaggars <[email protected]>
1 parent 9a85667 commit 28551bd

17 files changed

Lines changed: 962 additions & 21 deletions

File tree

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

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,51 @@ pub struct ServiceRoutingConfig {
346346
/// Enable TLS-enabled loopback gateway listeners to also accept plaintext
347347
/// HTTP for sandbox service hostnames.
348348
pub enable_loopback_service_http: bool,
349+
350+
/// Optional gateway name used to derive opaque hashed service route
351+
/// labels.
352+
///
353+
/// When `None` (the default), the gateway keeps generating readable
354+
/// `<workspace>--<sandbox>[--<service>]` service hostnames. When set,
355+
/// newly generated service hostnames use a stable, one-label hash derived
356+
/// from this name plus the workspace, sandbox, and service instead.
357+
///
358+
/// This value is unrelated to [`Config::name`] and
359+
/// [`GatewayJwtConfig::gateway_id`]; it is never derived from them.
360+
pub service_routing_gateway_name: Option<String>,
361+
}
362+
363+
impl ServiceRoutingConfig {
364+
/// Validate the service-routing configuration.
365+
///
366+
/// Currently validates [`Self::service_routing_gateway_name`]: `None` is
367+
/// always valid, while `Some(name)` must be a DNS-1123 label — 1–63 ASCII
368+
/// bytes of lowercase `a-z`, `0-9`, or `-`, with no leading or trailing
369+
/// `-`. An empty string is rejected; the setting is represented as
370+
/// disabled only by `None`.
371+
pub fn validate(&self) -> crate::error::Result<()> {
372+
Self::validate_gateway_name(self.service_routing_gateway_name.as_deref())
373+
}
374+
375+
/// Validate an optional service-routing gateway name.
376+
///
377+
/// `None` is always valid. `Some(name)` must be a DNS-1123 label — 1–63
378+
/// ASCII bytes of lowercase `a-z`, `0-9`, or `-`, with no leading or
379+
/// trailing `-`. An empty string is rejected; the setting is represented
380+
/// as disabled only by `None`.
381+
pub fn validate_gateway_name(name: Option<&str>) -> crate::error::Result<()> {
382+
if let Some(name) = name
383+
&& !is_dns_label(name)
384+
{
385+
return Err(crate::error::Error::Config {
386+
message: format!(
387+
"service_routing_gateway_name must be a DNS label (1-63 lowercase \
388+
ASCII alphanumeric or '-' bytes, no leading/trailing '-'): {name:?}"
389+
),
390+
});
391+
}
392+
Ok(())
393+
}
349394
}
350395

351396
/// TLS configuration.
@@ -1125,6 +1170,17 @@ impl Config {
11251170
self
11261171
}
11271172

1173+
/// Set the service-routing gateway name used to derive hashed service
1174+
/// route labels.
1175+
///
1176+
/// `None` preserves readable service hostnames. `Some(name)` must pass
1177+
/// [`ServiceRoutingConfig::validate`].
1178+
#[must_use]
1179+
pub fn with_service_routing_gateway_name(mut self, name: Option<impl Into<String>>) -> Self {
1180+
self.service_routing.service_routing_gateway_name = name.map(Into::into);
1181+
self
1182+
}
1183+
11281184
/// Enable or disable plaintext HTTP routing for loopback sandbox service
11291185
/// hostnames on TLS-enabled gateway listeners.
11301186
#[must_use]
@@ -1146,6 +1202,7 @@ impl Default for ServiceRoutingConfig {
11461202
Self {
11471203
base_domains: default_service_routing_domains(),
11481204
enable_loopback_service_http: default_enable_loopback_service_http(),
1205+
service_routing_gateway_name: None,
11491206
}
11501207
}
11511208
}
@@ -1700,6 +1757,67 @@ mod tests {
17001757
assert!(!cfg.service_routing.enable_loopback_service_http);
17011758
}
17021759

1760+
#[test]
1761+
fn service_routing_gateway_name_defaults_to_none() {
1762+
let cfg = Config::new(None);
1763+
assert!(cfg.service_routing.service_routing_gateway_name.is_none());
1764+
cfg.service_routing.validate().expect("unset is valid");
1765+
}
1766+
1767+
#[test]
1768+
fn service_routing_gateway_name_accepts_dns_label() {
1769+
for name in ["gateway-west", "g", &"g".repeat(63), "gateway-1-west"] {
1770+
let cfg = Config::new(None).with_service_routing_gateway_name(Some(name));
1771+
assert_eq!(
1772+
cfg.service_routing.service_routing_gateway_name.as_deref(),
1773+
Some(name)
1774+
);
1775+
cfg.service_routing
1776+
.validate()
1777+
.unwrap_or_else(|e| panic!("{name:?} must be valid: {e}"));
1778+
}
1779+
}
1780+
1781+
#[test]
1782+
fn service_routing_gateway_name_rejects_empty_uppercase_invalid_hyphen_and_overlong_values() {
1783+
for name in [
1784+
"",
1785+
"Gateway-West",
1786+
"-gateway",
1787+
"gateway-",
1788+
"gateway_west",
1789+
"gateway west",
1790+
&"g".repeat(64),
1791+
] {
1792+
let cfg = Config::new(None).with_service_routing_gateway_name(Some(name));
1793+
let error = cfg
1794+
.service_routing
1795+
.validate()
1796+
.expect_err(&format!("{name:?} must be rejected"));
1797+
let message = error.to_string();
1798+
assert!(message.contains("service_routing_gateway_name"), "{name:?}");
1799+
}
1800+
}
1801+
1802+
#[test]
1803+
fn server_sans_update_preserves_service_routing_gateway_name() {
1804+
let cfg = Config::new(None)
1805+
.with_service_routing_gateway_name(Some("gateway-west"))
1806+
.with_server_sans(["*.apps.example.com"]);
1807+
1808+
assert_eq!(
1809+
cfg.service_routing.service_routing_gateway_name.as_deref(),
1810+
Some("gateway-west")
1811+
);
1812+
assert_eq!(
1813+
cfg.service_routing.base_domains,
1814+
vec![
1815+
"apps.example.com".to_string(),
1816+
DEFAULT_SERVICE_ROUTING_DOMAIN.to_string()
1817+
]
1818+
);
1819+
}
1820+
17031821
#[test]
17041822
fn service_routing_domains_are_derived_from_wildcard_server_sans() {
17051823
let cfg = Config::new(None).with_server_sans([

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

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,12 @@ struct RunArgs {
286286
)]
287287
server_sans: Vec<String>,
288288

289+
/// Gateway name used to derive opaque hashed sandbox service route
290+
/// labels. Unset keeps readable `<workspace>--<sandbox>[--<service>]`
291+
/// service hostnames.
292+
#[arg(long, env = "OPENSHELL_SERVICE_ROUTING_GATEWAY_NAME")]
293+
service_routing_gateway_name: Option<String>,
294+
289295
/// Enable plaintext HTTP routing for loopback sandbox service URLs.
290296
#[arg(
291297
long,
@@ -568,8 +574,13 @@ fn prepare_server_config_with_drivers(
568574
.unwrap_or_default(),
569575
)
570576
.with_server_sans(args.server_sans.clone())
577+
.with_service_routing_gateway_name(args.service_routing_gateway_name.clone())
571578
.with_loopback_service_http(args.enable_loopback_service_http)
572579
.with_websocket_tunnel(args.enable_websocket_tunnel);
580+
config
581+
.service_routing
582+
.validate()
583+
.map_err(|error| miette::miette!("invalid service routing configuration: {error}"))?;
573584
if let Some(sources) = file
574585
.as_ref()
575586
.and_then(|file| file.openshell.gateway.provider_profile_sources.clone())
@@ -1044,6 +1055,10 @@ fn validate_preflight_semantics(
10441055
args.grpc_rate_limit_requests,
10451056
args.grpc_rate_limit_window_seconds,
10461057
)?;
1058+
openshell_core::config::ServiceRoutingConfig::validate_gateway_name(
1059+
args.service_routing_gateway_name.as_deref(),
1060+
)
1061+
.map_err(|error| miette::miette!("invalid service routing configuration: {error}"))?;
10471062
GuestTlsPaths::validate_configuration(Some(gateway), args.disable_tls)
10481063
.map_err(|error| miette::miette!("invalid gateway guest TLS configuration: {error}"))?;
10491064

@@ -1252,6 +1267,12 @@ fn merge_file_into_args(args: &mut RunArgs, file: &GatewayFileSection, matches:
12521267
{
12531268
args.server_sans.clone_from(sans);
12541269
}
1270+
if let Some(name) = &file.service_routing_gateway_name
1271+
&& args.service_routing_gateway_name.is_none()
1272+
&& arg_defaulted(matches, "service_routing_gateway_name")
1273+
{
1274+
args.service_routing_gateway_name = Some(name.clone());
1275+
}
12551276
if let Some(enabled) = file.enable_loopback_service_http
12561277
&& arg_defaulted(matches, "enable_loopback_service_http")
12571278
{
@@ -2509,6 +2530,10 @@ mod tests {
25092530
"rate-limit",
25102531
"[openshell]\nversion = 2\n[openshell.gateway]\nname = 'secret-semantic-marker'\ngrpc_rate_limit_requests = 10\n",
25112532
),
2533+
(
2534+
"service-routing",
2535+
"[openshell]\nversion = 2\n[openshell.gateway]\nservice_routing_gateway_name = 'Not A Label'\n",
2536+
),
25122537
(
25132538
"guest-tls",
25142539
"[openshell]\nversion = 2\n[openshell.gateway]\nguest_tls_ca = '/tls/ca.pem'\ndisable_tls = true\n",
@@ -3657,6 +3682,131 @@ enable_loopback_service_http = false
36573682
});
36583683
}
36593684

3685+
#[test]
3686+
fn file_populates_service_routing_gateway_name() {
3687+
Environment::new()
3688+
.remove("OPENSHELL_SERVICE_ROUTING_GATEWAY_NAME")
3689+
.run(|| {
3690+
let (mut args, matches) =
3691+
parse_with_args(&["openshell-gateway", "--db-url", "sqlite::memory:"]);
3692+
let file = config_file_from_toml(
3693+
"[openshell.gateway]\nservice_routing_gateway_name = \"gateway-west\"\n",
3694+
);
3695+
merge_file_into_args(&mut args, &file.openshell.gateway, &matches);
3696+
3697+
assert_eq!(
3698+
args.service_routing_gateway_name.as_deref(),
3699+
Some("gateway-west")
3700+
);
3701+
});
3702+
}
3703+
3704+
#[test]
3705+
fn service_routing_gateway_name_cli_and_env_override_file_value() {
3706+
// CLI flag wins over the file value.
3707+
let (mut args, matches) = parse_with_args(&[
3708+
"openshell-gateway",
3709+
"--db-url",
3710+
"sqlite::memory:",
3711+
"--service-routing-gateway-name",
3712+
"gateway-cli",
3713+
]);
3714+
let file = config_file_from_toml(
3715+
"[openshell.gateway]\nservice_routing_gateway_name = \"gateway-west\"\n",
3716+
);
3717+
merge_file_into_args(&mut args, &file.openshell.gateway, &matches);
3718+
assert_eq!(
3719+
args.service_routing_gateway_name.as_deref(),
3720+
Some("gateway-cli"),
3721+
"CLI flag must win over file value"
3722+
);
3723+
3724+
// Env var wins over the file value.
3725+
Environment::new()
3726+
.set("OPENSHELL_SERVICE_ROUTING_GATEWAY_NAME", "gateway-env")
3727+
.run(|| {
3728+
let (mut args, matches) =
3729+
parse_with_args(&["openshell-gateway", "--db-url", "sqlite::memory:"]);
3730+
let file = config_file_from_toml(
3731+
"[openshell.gateway]\nservice_routing_gateway_name = \"gateway-west\"\n",
3732+
);
3733+
merge_file_into_args(&mut args, &file.openshell.gateway, &matches);
3734+
assert_eq!(
3735+
args.service_routing_gateway_name.as_deref(),
3736+
Some("gateway-env"),
3737+
"env var must win over file value"
3738+
);
3739+
});
3740+
}
3741+
3742+
#[test]
3743+
fn server_config_preparation_applies_and_validates_service_routing_gateway_name() {
3744+
let state = tempfile::tempdir().unwrap();
3745+
let local_tls = tempfile::tempdir().unwrap();
3746+
3747+
Environment::new()
3748+
.set("XDG_STATE_HOME", state.path().to_str().unwrap())
3749+
.set(
3750+
"OPENSHELL_LOCAL_TLS_DIR",
3751+
local_tls.path().to_str().unwrap(),
3752+
)
3753+
.remove("OPENSHELL_SERVICE_ROUTING_GATEWAY_NAME")
3754+
.run(|| {
3755+
let config_path = state.path().join("gateway.toml");
3756+
std::fs::write(
3757+
&config_path,
3758+
"[openshell]\nversion = 2\n\n[openshell.gateway]\nservice_routing_gateway_name = \"gateway-west\"\n",
3759+
)
3760+
.unwrap();
3761+
3762+
let (mut args, matches) = parse_with_args(&[
3763+
"openshell-gateway",
3764+
"--config",
3765+
config_path.to_str().unwrap(),
3766+
"--db-url",
3767+
"sqlite::memory:",
3768+
"--compute-driver",
3769+
"local",
3770+
"--disable-tls",
3771+
]);
3772+
3773+
let prepared = super::prepare_server_config(&mut args, &matches)
3774+
.expect("server config is prepared");
3775+
assert_eq!(
3776+
prepared
3777+
.config
3778+
.service_routing
3779+
.service_routing_gateway_name
3780+
.as_deref(),
3781+
Some("gateway-west")
3782+
);
3783+
3784+
let invalid_path = state.path().join("invalid-gateway-name.toml");
3785+
std::fs::write(
3786+
&invalid_path,
3787+
"[openshell]\nversion = 2\n\n[openshell.gateway]\nservice_routing_gateway_name = \"Gateway West\"\n",
3788+
)
3789+
.unwrap();
3790+
let (mut args, matches) = parse_with_args(&[
3791+
"openshell-gateway",
3792+
"--config",
3793+
invalid_path.to_str().unwrap(),
3794+
"--db-url",
3795+
"sqlite::memory:",
3796+
"--compute-driver",
3797+
"local",
3798+
"--disable-tls",
3799+
]);
3800+
let error = super::prepare_server_config(&mut args, &matches)
3801+
.err()
3802+
.expect("invalid routing gateway name must fail preparation");
3803+
assert!(
3804+
error.to_string().contains("service_routing_gateway_name"),
3805+
"unexpected error: {error}"
3806+
);
3807+
});
3808+
}
3809+
36603810
#[test]
36613811
fn canonical_file_driver_selector_populates_cli_args() {
36623812
let (mut args, matches) =

0 commit comments

Comments
 (0)