Skip to content

Commit 3f1fd80

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 05050a6 commit 3f1fd80

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
@@ -316,6 +316,51 @@ pub struct ServiceRoutingConfig {
316316
/// Enable TLS-enabled loopback gateway listeners to also accept plaintext
317317
/// HTTP for sandbox service hostnames.
318318
pub enable_loopback_service_http: bool,
319+
320+
/// Optional gateway name used to derive opaque hashed service route
321+
/// labels.
322+
///
323+
/// When `None` (the default), the gateway keeps generating readable
324+
/// `<workspace>--<sandbox>[--<service>]` service hostnames. When set,
325+
/// newly generated service hostnames use a stable, one-label hash derived
326+
/// from this name plus the workspace, sandbox, and service instead.
327+
///
328+
/// This value is unrelated to [`Config::name`] and
329+
/// [`GatewayJwtConfig::gateway_id`]; it is never derived from them.
330+
pub service_routing_gateway_name: Option<String>,
331+
}
332+
333+
impl ServiceRoutingConfig {
334+
/// Validate the service-routing configuration.
335+
///
336+
/// Currently validates [`Self::service_routing_gateway_name`]: `None` is
337+
/// always valid, while `Some(name)` must be a DNS-1123 label — 1–63 ASCII
338+
/// bytes of lowercase `a-z`, `0-9`, or `-`, with no leading or trailing
339+
/// `-`. An empty string is rejected; the setting is represented as
340+
/// disabled only by `None`.
341+
pub fn validate(&self) -> crate::error::Result<()> {
342+
Self::validate_gateway_name(self.service_routing_gateway_name.as_deref())
343+
}
344+
345+
/// Validate an optional service-routing gateway name.
346+
///
347+
/// `None` is always valid. `Some(name)` must be a DNS-1123 label — 1–63
348+
/// ASCII bytes of lowercase `a-z`, `0-9`, or `-`, with no leading or
349+
/// trailing `-`. An empty string is rejected; the setting is represented
350+
/// as disabled only by `None`.
351+
pub fn validate_gateway_name(name: Option<&str>) -> crate::error::Result<()> {
352+
if let Some(name) = name
353+
&& !is_dns_label(name)
354+
{
355+
return Err(crate::error::Error::Config {
356+
message: format!(
357+
"service_routing_gateway_name must be a DNS label (1-63 lowercase \
358+
ASCII alphanumeric or '-' bytes, no leading/trailing '-'): {name:?}"
359+
),
360+
});
361+
}
362+
Ok(())
363+
}
319364
}
320365

321366
/// TLS configuration.
@@ -1094,6 +1139,17 @@ impl Config {
10941139
self
10951140
}
10961141

1142+
/// Set the service-routing gateway name used to derive hashed service
1143+
/// route labels.
1144+
///
1145+
/// `None` preserves readable service hostnames. `Some(name)` must pass
1146+
/// [`ServiceRoutingConfig::validate`].
1147+
#[must_use]
1148+
pub fn with_service_routing_gateway_name(mut self, name: Option<impl Into<String>>) -> Self {
1149+
self.service_routing.service_routing_gateway_name = name.map(Into::into);
1150+
self
1151+
}
1152+
10971153
/// Enable or disable plaintext HTTP routing for loopback sandbox service
10981154
/// hostnames on TLS-enabled gateway listeners.
10991155
#[must_use]
@@ -1115,6 +1171,7 @@ impl Default for ServiceRoutingConfig {
11151171
Self {
11161172
base_domains: default_service_routing_domains(),
11171173
enable_loopback_service_http: default_enable_loopback_service_http(),
1174+
service_routing_gateway_name: None,
11181175
}
11191176
}
11201177
}
@@ -1655,6 +1712,67 @@ mod tests {
16551712
assert!(!cfg.service_routing.enable_loopback_service_http);
16561713
}
16571714

1715+
#[test]
1716+
fn service_routing_gateway_name_defaults_to_none() {
1717+
let cfg = Config::new(None);
1718+
assert!(cfg.service_routing.service_routing_gateway_name.is_none());
1719+
cfg.service_routing.validate().expect("unset is valid");
1720+
}
1721+
1722+
#[test]
1723+
fn service_routing_gateway_name_accepts_dns_label() {
1724+
for name in ["gateway-west", "g", &"g".repeat(63), "gateway-1-west"] {
1725+
let cfg = Config::new(None).with_service_routing_gateway_name(Some(name));
1726+
assert_eq!(
1727+
cfg.service_routing.service_routing_gateway_name.as_deref(),
1728+
Some(name)
1729+
);
1730+
cfg.service_routing
1731+
.validate()
1732+
.unwrap_or_else(|e| panic!("{name:?} must be valid: {e}"));
1733+
}
1734+
}
1735+
1736+
#[test]
1737+
fn service_routing_gateway_name_rejects_empty_uppercase_invalid_hyphen_and_overlong_values() {
1738+
for name in [
1739+
"",
1740+
"Gateway-West",
1741+
"-gateway",
1742+
"gateway-",
1743+
"gateway_west",
1744+
"gateway west",
1745+
&"g".repeat(64),
1746+
] {
1747+
let cfg = Config::new(None).with_service_routing_gateway_name(Some(name));
1748+
let error = cfg
1749+
.service_routing
1750+
.validate()
1751+
.expect_err(&format!("{name:?} must be rejected"));
1752+
let message = error.to_string();
1753+
assert!(message.contains("service_routing_gateway_name"), "{name:?}");
1754+
}
1755+
}
1756+
1757+
#[test]
1758+
fn server_sans_update_preserves_service_routing_gateway_name() {
1759+
let cfg = Config::new(None)
1760+
.with_service_routing_gateway_name(Some("gateway-west"))
1761+
.with_server_sans(["*.apps.example.com"]);
1762+
1763+
assert_eq!(
1764+
cfg.service_routing.service_routing_gateway_name.as_deref(),
1765+
Some("gateway-west")
1766+
);
1767+
assert_eq!(
1768+
cfg.service_routing.base_domains,
1769+
vec![
1770+
"apps.example.com".to_string(),
1771+
DEFAULT_SERVICE_ROUTING_DOMAIN.to_string()
1772+
]
1773+
);
1774+
}
1775+
16581776
#[test]
16591777
fn service_routing_domains_are_derived_from_wildcard_server_sans() {
16601778
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
@@ -282,6 +282,12 @@ struct RunArgs {
282282
)]
283283
server_sans: Vec<String>,
284284

285+
/// Gateway name used to derive opaque hashed sandbox service route
286+
/// labels. Unset keeps readable `<workspace>--<sandbox>[--<service>]`
287+
/// service hostnames.
288+
#[arg(long, env = "OPENSHELL_SERVICE_ROUTING_GATEWAY_NAME")]
289+
service_routing_gateway_name: Option<String>,
290+
285291
/// Enable plaintext HTTP routing for loopback sandbox service URLs.
286292
#[arg(
287293
long,
@@ -564,8 +570,13 @@ fn prepare_server_config_with_drivers(
564570
.unwrap_or_default(),
565571
)
566572
.with_server_sans(args.server_sans.clone())
573+
.with_service_routing_gateway_name(args.service_routing_gateway_name.clone())
567574
.with_loopback_service_http(args.enable_loopback_service_http)
568575
.with_websocket_tunnel(args.enable_websocket_tunnel);
576+
config
577+
.service_routing
578+
.validate()
579+
.map_err(|error| miette::miette!("invalid service routing configuration: {error}"))?;
569580
if let Some(sources) = file
570581
.as_ref()
571582
.and_then(|file| file.openshell.gateway.provider_profile_sources.clone())
@@ -1015,6 +1026,10 @@ fn validate_preflight_semantics(
10151026
args.grpc_rate_limit_requests,
10161027
args.grpc_rate_limit_window_seconds,
10171028
)?;
1029+
openshell_core::config::ServiceRoutingConfig::validate_gateway_name(
1030+
args.service_routing_gateway_name.as_deref(),
1031+
)
1032+
.map_err(|error| miette::miette!("invalid service routing configuration: {error}"))?;
10181033
GuestTlsPaths::validate_configuration(Some(gateway), args.disable_tls)
10191034
.map_err(|error| miette::miette!("invalid gateway guest TLS configuration: {error}"))?;
10201035

@@ -1223,6 +1238,12 @@ fn merge_file_into_args(args: &mut RunArgs, file: &GatewayFileSection, matches:
12231238
{
12241239
args.server_sans.clone_from(sans);
12251240
}
1241+
if let Some(name) = &file.service_routing_gateway_name
1242+
&& args.service_routing_gateway_name.is_none()
1243+
&& arg_defaulted(matches, "service_routing_gateway_name")
1244+
{
1245+
args.service_routing_gateway_name = Some(name.clone());
1246+
}
12261247
if let Some(enabled) = file.enable_loopback_service_http
12271248
&& arg_defaulted(matches, "enable_loopback_service_http")
12281249
{
@@ -2403,6 +2424,10 @@ mod tests {
24032424
"rate-limit",
24042425
"[openshell]\nversion = 2\n[openshell.gateway]\nname = 'secret-semantic-marker'\ngrpc_rate_limit_requests = 10\n",
24052426
),
2427+
(
2428+
"service-routing",
2429+
"[openshell]\nversion = 2\n[openshell.gateway]\nservice_routing_gateway_name = 'Not A Label'\n",
2430+
),
24062431
(
24072432
"guest-tls",
24082433
"[openshell]\nversion = 2\n[openshell.gateway]\nguest_tls_ca = '/tls/ca.pem'\ndisable_tls = true\n",
@@ -3551,6 +3576,131 @@ enable_loopback_service_http = false
35513576
});
35523577
}
35533578

3579+
#[test]
3580+
fn file_populates_service_routing_gateway_name() {
3581+
Environment::new()
3582+
.remove("OPENSHELL_SERVICE_ROUTING_GATEWAY_NAME")
3583+
.run(|| {
3584+
let (mut args, matches) =
3585+
parse_with_args(&["openshell-gateway", "--db-url", "sqlite::memory:"]);
3586+
let file = config_file_from_toml(
3587+
"[openshell.gateway]\nservice_routing_gateway_name = \"gateway-west\"\n",
3588+
);
3589+
merge_file_into_args(&mut args, &file.openshell.gateway, &matches);
3590+
3591+
assert_eq!(
3592+
args.service_routing_gateway_name.as_deref(),
3593+
Some("gateway-west")
3594+
);
3595+
});
3596+
}
3597+
3598+
#[test]
3599+
fn service_routing_gateway_name_cli_and_env_override_file_value() {
3600+
// CLI flag wins over the file value.
3601+
let (mut args, matches) = parse_with_args(&[
3602+
"openshell-gateway",
3603+
"--db-url",
3604+
"sqlite::memory:",
3605+
"--service-routing-gateway-name",
3606+
"gateway-cli",
3607+
]);
3608+
let file = config_file_from_toml(
3609+
"[openshell.gateway]\nservice_routing_gateway_name = \"gateway-west\"\n",
3610+
);
3611+
merge_file_into_args(&mut args, &file.openshell.gateway, &matches);
3612+
assert_eq!(
3613+
args.service_routing_gateway_name.as_deref(),
3614+
Some("gateway-cli"),
3615+
"CLI flag must win over file value"
3616+
);
3617+
3618+
// Env var wins over the file value.
3619+
Environment::new()
3620+
.set("OPENSHELL_SERVICE_ROUTING_GATEWAY_NAME", "gateway-env")
3621+
.run(|| {
3622+
let (mut args, matches) =
3623+
parse_with_args(&["openshell-gateway", "--db-url", "sqlite::memory:"]);
3624+
let file = config_file_from_toml(
3625+
"[openshell.gateway]\nservice_routing_gateway_name = \"gateway-west\"\n",
3626+
);
3627+
merge_file_into_args(&mut args, &file.openshell.gateway, &matches);
3628+
assert_eq!(
3629+
args.service_routing_gateway_name.as_deref(),
3630+
Some("gateway-env"),
3631+
"env var must win over file value"
3632+
);
3633+
});
3634+
}
3635+
3636+
#[test]
3637+
fn server_config_preparation_applies_and_validates_service_routing_gateway_name() {
3638+
let state = tempfile::tempdir().unwrap();
3639+
let local_tls = tempfile::tempdir().unwrap();
3640+
3641+
Environment::new()
3642+
.set("XDG_STATE_HOME", state.path().to_str().unwrap())
3643+
.set(
3644+
"OPENSHELL_LOCAL_TLS_DIR",
3645+
local_tls.path().to_str().unwrap(),
3646+
)
3647+
.remove("OPENSHELL_SERVICE_ROUTING_GATEWAY_NAME")
3648+
.run(|| {
3649+
let config_path = state.path().join("gateway.toml");
3650+
std::fs::write(
3651+
&config_path,
3652+
"[openshell]\nversion = 2\n\n[openshell.gateway]\nservice_routing_gateway_name = \"gateway-west\"\n",
3653+
)
3654+
.unwrap();
3655+
3656+
let (mut args, matches) = parse_with_args(&[
3657+
"openshell-gateway",
3658+
"--config",
3659+
config_path.to_str().unwrap(),
3660+
"--db-url",
3661+
"sqlite::memory:",
3662+
"--compute-driver",
3663+
"local",
3664+
"--disable-tls",
3665+
]);
3666+
3667+
let prepared = super::prepare_server_config(&mut args, &matches)
3668+
.expect("server config is prepared");
3669+
assert_eq!(
3670+
prepared
3671+
.config
3672+
.service_routing
3673+
.service_routing_gateway_name
3674+
.as_deref(),
3675+
Some("gateway-west")
3676+
);
3677+
3678+
let invalid_path = state.path().join("invalid-gateway-name.toml");
3679+
std::fs::write(
3680+
&invalid_path,
3681+
"[openshell]\nversion = 2\n\n[openshell.gateway]\nservice_routing_gateway_name = \"Gateway West\"\n",
3682+
)
3683+
.unwrap();
3684+
let (mut args, matches) = parse_with_args(&[
3685+
"openshell-gateway",
3686+
"--config",
3687+
invalid_path.to_str().unwrap(),
3688+
"--db-url",
3689+
"sqlite::memory:",
3690+
"--compute-driver",
3691+
"local",
3692+
"--disable-tls",
3693+
]);
3694+
let error = super::prepare_server_config(&mut args, &matches)
3695+
.err()
3696+
.expect("invalid routing gateway name must fail preparation");
3697+
assert!(
3698+
error.to_string().contains("service_routing_gateway_name"),
3699+
"unexpected error: {error}"
3700+
);
3701+
});
3702+
}
3703+
35543704
#[test]
35553705
fn canonical_file_driver_selector_populates_cli_args() {
35563706
let (mut args, matches) =

0 commit comments

Comments
 (0)