Skip to content

Commit 26134dc

Browse files
authored
feat(gateway): add hashed service-routing hostnames for multi-gateway clusters (ai-openshell#4842)
* feat(gateway): add hashed service-routing hostnames for multi-gateway clusters Closes ai-openshell#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 <jjaggars@redhat.com> * fix(lint): satisfy clippy doc lints on main-landed sandbox_runtime code The shared-supervisor landing (9a85667) predates the last Branch Checks run on main, so its doc-comment style was never gated. Split the three overlong first doc paragraphs so workspace clippy -D warnings passes; behavior is unchanged. Signed-off-by: Jesse Jaggars <jjaggars@redhat.com> * refactor(gateway): address hashed-hostname review feedback Three changes from PR review: Identity model: replace service_routing_gateway_name with an explicit service_hostname_format mode (readable, the default, or hashed). In hashed mode the discriminator hashed into exposed-service hostnames is the existing gateway installation name ([openshell.gateway].name, operator- or platform-assigned, defaulting to the Helm release name), so no second per-gateway name is introduced. The name must be a DNS label and unique among gateways sharing the service base domain — an operational contract held by the deploying platform, documented in the configuration reference. gateway_jwt.gateway_id remains separate and untouched; renaming the installation now changes every minted URL. Request-path amplification: miss-triggered service route index rebuilds are debounced to one attempt per five seconds. Unknown hash labels reach the rebuild path before authentication, and without the debounce each request carrying a random well-formed label triggered a full all-workspaces endpoint scan. Startup hydration and explicit rebuilds bypass the debounce. OCSF regression: cross-origin rejections for hashed service hosts emitted no security event because the logging path used the readable-host parser only. The emitter now resolves hashed hosts through the in-memory route index (never the miss-triggered rebuild path) and still emits for unknown hash-shaped labels, attributed to the raw label. Also updates the debug-openshell-cluster skill for the new gateway TOML fields, per the repository instructions for gateway config changes. Signed-off-by: Jesse Jaggars <jjaggars@redhat.com> --------- Signed-off-by: Jesse Jaggars <jjaggars@redhat.com>
1 parent ac7f615 commit 26134dc

19 files changed

Lines changed: 1312 additions & 30 deletions

File tree

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

Lines changed: 164 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,68 @@ 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+
/// Format used for newly generated service hostnames.
351+
///
352+
/// `Readable` (the default) generates
353+
/// `<workspace>--<sandbox>[--<service>]` hostnames. `Hashed` generates a
354+
/// stable, one-label opaque hash derived from the gateway installation
355+
/// name plus the workspace, sandbox, and service — see
356+
/// [`Config::service_routing_discriminator`].
357+
pub service_hostname_format: ServiceHostnameFormat,
358+
}
359+
360+
/// Format of exposed-service hostnames minted by the gateway.
361+
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
362+
#[serde(rename_all = "snake_case")]
363+
pub enum ServiceHostnameFormat {
364+
/// Readable `<workspace>--<sandbox>[--<service>]` hostnames.
365+
#[default]
366+
Readable,
367+
/// Opaque one-label hashed hostnames, discriminated by the gateway
368+
/// installation name.
369+
Hashed,
370+
}
371+
372+
impl FromStr for ServiceHostnameFormat {
373+
type Err = String;
374+
375+
fn from_str(value: &str) -> Result<Self, Self::Err> {
376+
match value {
377+
"readable" => Ok(Self::Readable),
378+
"hashed" => Ok(Self::Hashed),
379+
_ => Err(format!(
380+
"invalid service hostname format '{value}'; expected readable or hashed"
381+
)),
382+
}
383+
}
384+
}
385+
386+
impl ServiceRoutingConfig {
387+
/// Validate the service-routing configuration against the gateway
388+
/// installation name.
389+
///
390+
/// When [`ServiceHostnameFormat::Hashed`] is selected, the installation
391+
/// name becomes the hash discriminator and must be a DNS-1123 label —
392+
/// 1–63 ASCII bytes of lowercase `a-z`, `0-9`, or `-`, with no leading or
393+
/// trailing `-`. It must also be unique among all gateways sharing the
394+
/// service base domain; that uniqueness is an operational contract the
395+
/// deploying platform is responsible for, since a single gateway cannot
396+
/// observe its siblings.
397+
pub fn validate(&self, installation_name: &str) -> crate::error::Result<()> {
398+
if self.service_hostname_format == ServiceHostnameFormat::Hashed
399+
&& !is_dns_label(installation_name)
400+
{
401+
return Err(crate::error::Error::Config {
402+
message: format!(
403+
"hashed service hostnames require the gateway installation name to be \
404+
a DNS label (1-63 lowercase ASCII alphanumeric or '-' bytes, no \
405+
leading/trailing '-'): {installation_name:?}"
406+
),
407+
});
408+
}
409+
Ok(())
410+
}
349411
}
350412

351413
/// TLS configuration.
@@ -1126,6 +1188,36 @@ impl Config {
11261188
self
11271189
}
11281190

1191+
/// Select the hostname format for exposed sandbox services.
1192+
///
1193+
/// [`ServiceHostnameFormat::Hashed`] derives its discriminator from the
1194+
/// gateway installation name ([`Config::name`]); see
1195+
/// [`Self::service_routing_discriminator`].
1196+
#[must_use]
1197+
pub fn with_service_hostname_format(mut self, format: ServiceHostnameFormat) -> Self {
1198+
self.service_routing.service_hostname_format = format;
1199+
self
1200+
}
1201+
1202+
/// The discriminator hashed into exposed-service hostnames when
1203+
/// [`ServiceHostnameFormat::Hashed`] is selected: the gateway
1204+
/// installation name.
1205+
///
1206+
/// Returns `None` while readable hostnames are in use, in which case no
1207+
/// hashed service hosts exist. The name is an operator- or
1208+
/// platform-assigned value; it must be unique among all gateways sharing
1209+
/// the service base domain, an operational contract enforced by the
1210+
/// deploying platform rather than the gateway itself. Renaming the
1211+
/// installation changes every minted hostname.
1212+
///
1213+
/// Unrelated to [`GatewayJwtConfig::gateway_id`], which carries security
1214+
/// and Kubernetes-ownership semantics.
1215+
#[must_use]
1216+
pub fn service_routing_discriminator(&self) -> Option<&str> {
1217+
(self.service_routing.service_hostname_format == ServiceHostnameFormat::Hashed)
1218+
.then_some(self.name.as_str())
1219+
}
1220+
11291221
/// Enable or disable plaintext HTTP routing for loopback sandbox service
11301222
/// hostnames on TLS-enabled gateway listeners.
11311223
#[must_use]
@@ -1147,6 +1239,7 @@ impl Default for ServiceRoutingConfig {
11471239
Self {
11481240
base_domains: default_service_routing_domains(),
11491241
enable_loopback_service_http: default_enable_loopback_service_http(),
1242+
service_hostname_format: ServiceHostnameFormat::default(),
11501243
}
11511244
}
11521245
}
@@ -1225,8 +1318,8 @@ mod tests {
12251318
AppArmorProfile, Config, ConfigDeliveryMode, DEFAULT_SERVICE_ROUTING_DOMAIN,
12261319
GatewayInterceptorBindingPolicy, GatewayInterceptorConfig, GatewayInterceptorFailurePolicy,
12271320
GatewayJwtConfig, GatewayProviderProfileSourceConfig, ImagePullPolicy,
1228-
PolicyValidationFailureMode, UpstreamProxyConfig, default_sandbox_pids_limit,
1229-
normalize_compute_driver_name,
1321+
PolicyValidationFailureMode, ServiceHostnameFormat, UpstreamProxyConfig,
1322+
default_sandbox_pids_limit, normalize_compute_driver_name,
12301323
};
12311324
use std::net::SocketAddr;
12321325
use std::time::Duration;
@@ -1701,6 +1794,75 @@ mod tests {
17011794
assert!(!cfg.service_routing.enable_loopback_service_http);
17021795
}
17031796

1797+
#[test]
1798+
fn service_hostname_format_defaults_to_readable() {
1799+
let cfg = Config::new(None);
1800+
assert_eq!(
1801+
cfg.service_routing.service_hostname_format,
1802+
ServiceHostnameFormat::Readable
1803+
);
1804+
assert_eq!(cfg.service_routing_discriminator(), None);
1805+
cfg.service_routing
1806+
.validate("any name")
1807+
.expect("readable is valid");
1808+
}
1809+
1810+
#[test]
1811+
fn hashed_format_discriminates_on_the_installation_name() {
1812+
let cfg = Config::new(None)
1813+
.with_name("gateway-west")
1814+
.with_service_hostname_format(ServiceHostnameFormat::Hashed);
1815+
assert_eq!(cfg.service_routing_discriminator(), Some("gateway-west"));
1816+
cfg.service_routing
1817+
.validate("gateway-west")
1818+
.expect("DNS-label installation name is valid");
1819+
}
1820+
1821+
#[test]
1822+
fn hashed_format_requires_a_dns_label_installation_name() {
1823+
for name in [
1824+
"",
1825+
"Gateway-West",
1826+
"-gateway",
1827+
"gateway-",
1828+
"gateway_west",
1829+
"gateway west",
1830+
&"g".repeat(64),
1831+
] {
1832+
let cfg = Config::new(None)
1833+
.with_name(name)
1834+
.with_service_hostname_format(ServiceHostnameFormat::Hashed);
1835+
let error = cfg
1836+
.service_routing
1837+
.validate(name)
1838+
.expect_err(&format!("{name:?} must be rejected"));
1839+
let message = error.to_string();
1840+
assert!(
1841+
message.contains("hashed service hostnames require"),
1842+
"{name:?}: {message}"
1843+
);
1844+
}
1845+
}
1846+
1847+
#[test]
1848+
fn server_sans_update_preserves_service_hostname_format() {
1849+
let cfg = Config::new(None)
1850+
.with_service_hostname_format(ServiceHostnameFormat::Hashed)
1851+
.with_server_sans(["*.apps.example.com"]);
1852+
1853+
assert_eq!(
1854+
cfg.service_routing.service_hostname_format,
1855+
ServiceHostnameFormat::Hashed
1856+
);
1857+
assert_eq!(
1858+
cfg.service_routing.base_domains,
1859+
vec![
1860+
"apps.example.com".to_string(),
1861+
DEFAULT_SERVICE_ROUTING_DOMAIN.to_string()
1862+
]
1863+
);
1864+
}
1865+
17041866
#[test]
17051867
fn service_routing_domains_are_derived_from_wildcard_server_sans() {
17061868
let cfg = Config::new(None).with_server_sans([

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ pub const SUPERVISOR_AUTH_BUNDLE_KEY: &str = "auth.json";
4040
pub const PROXY_CA_CERTIFICATE_KEY: &str = "proxy-ca.crt";
4141
pub const PROXY_CA_PRIVATE_KEY: &str = "proxy-ca.key";
4242
/// Optional bootstrap-Secret key holding the operator's corporate proxy CA
43-
/// bundle (`proxy_ca_bundle`). Distinct from [`PROXY_CA_CERTIFICATE_KEY`],
44-
/// which is the sandbox's own generated TLS-interception CA.
43+
/// bundle (`proxy_ca_bundle`).
44+
///
45+
/// Distinct from [`PROXY_CA_CERTIFICATE_KEY`], which is the sandbox's own
46+
/// generated TLS-interception CA.
4547
pub const UPSTREAM_PROXY_CA_BUNDLE_KEY: &str = "upstream-proxy-ca.pem";
4648
/// JSON sandbox instance registration of a generation on the shared supervisor.
4749
pub const SHARED_INSTANCE_KEY: &str = "instance.json";
@@ -205,8 +207,10 @@ pub fn workload_fence(
205207
}
206208

207209
/// Per-sandbox Sandbox Protocol endpoint. Its stable `ClusterIP` is allocated
208-
/// before either Pod runs. Each connection mode has a separate Service name
209-
/// and fixed selector, so legacy gateways keep their workload endpoint.
210+
/// before either Pod runs.
211+
///
212+
/// Each connection mode has a separate Service name and fixed selector, so
213+
/// legacy gateways keep their workload endpoint.
210214
#[must_use]
211215
pub fn boundary_service(
212216
namespace: &str,
@@ -692,7 +696,9 @@ pub fn supervisor_bootstrap_secret(
692696

693697
/// Durable launch material of one shared-supervisor sandbox instance, under
694698
/// the generation's supervisor Secret name so the existing generation cleanup
695-
/// deletes it. The gateway reads it back to rebuild the host's assignments.
699+
/// deletes it.
700+
///
701+
/// The gateway reads it back to rebuild the host's assignments.
696702
#[must_use]
697703
pub fn shared_instance_secret(
698704
namespace: &str,

0 commit comments

Comments
 (0)