Skip to content

Commit 00446a4

Browse files
committed
feat(auth): make gateway service authentication opt-in
Signed-off-by: Taylor Mutch <taylormutch@gmail.com>
1 parent 6d8a591 commit 00446a4

16 files changed

Lines changed: 411 additions & 135 deletions

File tree

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,10 @@ pub struct Config {
390390
/// Part of the programmatically-built [`Config`]; never deserialized directly.
391391
#[derive(Debug, Clone)]
392392
pub struct ServiceRoutingConfig {
393+
/// Opt in to gateway authentication for proxied application requests.
394+
/// Independent of the supervisor application listener.
395+
pub auth_enabled: bool,
396+
393397
/// Base domains accepted for `sandbox--service.<domain>` routes.
394398
/// The first domain is used when the gateway prints endpoint URLs.
395399
pub base_domains: Vec<String>,
@@ -1311,6 +1315,13 @@ impl Config {
13111315
.then_some(self.name.as_str())
13121316
}
13131317

1318+
/// Opt in to configured gateway authentication on application requests.
1319+
#[must_use]
1320+
pub const fn with_service_auth(mut self, enabled: bool) -> Self {
1321+
self.service_routing.auth_enabled = enabled;
1322+
self
1323+
}
1324+
13141325
/// Enable or disable plaintext HTTP routing for loopback sandbox service
13151326
/// hostnames on TLS-enabled gateway listeners.
13161327
#[must_use]
@@ -1330,6 +1341,7 @@ impl Config {
13301341
impl Default for ServiceRoutingConfig {
13311342
fn default() -> Self {
13321343
Self {
1344+
auth_enabled: false,
13331345
base_domains: default_service_routing_domains(),
13341346
enable_loopback_service_http: default_enable_loopback_service_http(),
13351347
service_hostname_format: ServiceHostnameFormat::default(),
@@ -1869,6 +1881,8 @@ mod tests {
18691881
vec![DEFAULT_SERVICE_ROUTING_DOMAIN.to_string()]
18701882
);
18711883
assert!(cfg.service_routing.enable_loopback_service_http);
1884+
assert!(!cfg.service_routing.auth_enabled);
1885+
assert!(cfg.with_service_auth(true).service_routing.auth_enabled);
18721886
}
18731887

18741888
#[test]

‎crates/openshell-server/README.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,13 @@ equality tokens, not resumable cursors or an ordering across gateway replicas.
342342

343343
## Gateway application authentication
344344

345-
Gateway-mediated application HTTP requests and WebSocket upgrades authenticate
345+
Gateway service authentication defaults to disabled, independently of the app
346+
listener. Disabled mode preserves application routing, reserved-looking paths,
347+
and the pre-existing header/cookie policy. CLI `--service-auth-enabled` and
348+
`OPENSHELL_SERVICE_AUTH_ENABLED` override the TOML setting.
349+
350+
With `[openshell.gateway].service_auth_enabled = true`, gateway-mediated
351+
application HTTP requests and WebSocket upgrades authenticate
346352
through the gateway chain or verified TLS client identity before endpoint lookup,
347353
independently of app-listener enablement. User access requires TCP-forwarding
348354
role/scope policy (`sandbox:write` when enforced) and current workspace membership;

‎crates/openshell-server/src/auth/http.rs‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,14 @@ fn sanitize_callback_host(raw: &Option<String>) -> (String, String) {
9090

9191
/// Create the auth router.
9292
pub fn router(state: Arc<ServerState>) -> Router {
93-
Router::new()
94-
.route(
93+
let mut router = Router::new();
94+
if state.config.service_routing.auth_enabled {
95+
router = router.route(
9596
"/auth/services",
9697
get(service_browser_login).post(service_browser_login),
97-
)
98+
);
99+
}
100+
router
98101
.route("/auth/connect", get(auth_connect))
99102
.route("/auth/oidc-config", get(oidc_config_handler))
100103
.route("/.well-known/jwks.json", get(gateway_jwks_handler))

‎crates/openshell-server/src/auth/service_session.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,7 @@ pub mod tests {
813813
let mut state = test_server_state().await;
814814
let inner = Arc::get_mut(&mut state).unwrap();
815815
inner.config.auth.allow_unauthenticated_users = false;
816+
inner.config.service_routing.auth_enabled = true;
816817
inner.config.service_routing.base_domains = vec!["services.example.com".into()];
817818
inner.config.oidc = Some(serde_json::from_value(serde_json::json!({"issuer":server.uri(),"audience":"openshell-cli","scopes_claim":"scope"})).unwrap());
818819
inner.admin_role = "openshell-admin".into();
@@ -1299,6 +1300,7 @@ pub mod tests {
12991300
let inner = Arc::get_mut(&mut state).unwrap();
13001301
inner.config.service_routing.base_domains = vec!["services.example.com".into()];
13011302
inner.config.auth.allow_unauthenticated_users = false;
1303+
inner.config.service_routing.auth_enabled = true;
13021304
inner.config.mtls_auth.enabled = true;
13031305
let peer = ServicePeer {
13041306
secure: true,

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,10 @@ struct RunArgs {
318318
)]
319319
server_sans: Vec<String>,
320320

321+
/// Authenticate sandbox application requests using configured gateway auth.
322+
#[arg(long, env = "OPENSHELL_SERVICE_AUTH_ENABLED", default_value_t = false, action = ArgAction::Set)]
323+
service_auth_enabled: bool,
324+
321325
/// Hostname format for exposed sandbox services: `readable` (default)
322326
/// keeps `<workspace>--<sandbox>[--<service>]` hostnames; `hashed` mints
323327
/// opaque one-label hostnames discriminated by the gateway installation
@@ -646,6 +650,7 @@ fn prepare_server_config_with_drivers(
646650
)
647651
.with_server_sans(args.server_sans.clone())
648652
.with_service_hostname_format(args.service_hostname_format.unwrap_or_default())
653+
.with_service_auth(args.service_auth_enabled)
649654
.with_loopback_service_http(args.enable_loopback_service_http)
650655
.with_websocket_tunnel(args.enable_websocket_tunnel);
651656
config
@@ -1593,6 +1598,11 @@ fn merge_file_into_args(args: &mut RunArgs, file: &GatewayFileSection, matches:
15931598
{
15941599
args.service_hostname_format = Some(*format);
15951600
}
1601+
if let Some(enabled) = file.service_auth_enabled
1602+
&& arg_defaulted(matches, "service_auth_enabled")
1603+
{
1604+
args.service_auth_enabled = enabled;
1605+
}
15961606
if let Some(enabled) = file.enable_loopback_service_http
15971607
&& arg_defaulted(matches, "enable_loopback_service_http")
15981608
{
@@ -4171,6 +4181,40 @@ enabled = false
41714181
});
41724182
}
41734183

4184+
#[test]
4185+
fn service_auth_is_opt_in_with_explicit_cli_environment_and_file_precedence() {
4186+
Environment::new()
4187+
.remove("OPENSHELL_SERVICE_AUTH_ENABLED")
4188+
.run(|| {
4189+
let file = toml::from_str::<ConfigFile>(
4190+
"[openshell.gateway]\nservice_auth_enabled = true\n",
4191+
)
4192+
.unwrap();
4193+
let (mut args, matches) = parse_with_args(&["openshell-gateway"]);
4194+
assert!(!args.service_auth_enabled);
4195+
merge_file_into_args(&mut args, &file.openshell.gateway, &matches);
4196+
assert!(args.service_auth_enabled);
4197+
let (mut args, matches) =
4198+
parse_with_args(&["openshell-gateway", "--service-auth-enabled", "false"]);
4199+
merge_file_into_args(&mut args, &file.openshell.gateway, &matches);
4200+
assert!(!args.service_auth_enabled);
4201+
});
4202+
Environment::new()
4203+
.set("OPENSHELL_SERVICE_AUTH_ENABLED", "false")
4204+
.run(|| {
4205+
let file = toml::from_str::<ConfigFile>(
4206+
"[openshell.gateway]\nservice_auth_enabled = true\n",
4207+
)
4208+
.unwrap();
4209+
let (mut args, matches) = parse_with_args(&["openshell-gateway"]);
4210+
merge_file_into_args(&mut args, &file.openshell.gateway, &matches);
4211+
assert!(!args.service_auth_enabled);
4212+
let (args, _) =
4213+
parse_with_args(&["openshell-gateway", "--service-auth-enabled", "true"]);
4214+
assert!(args.service_auth_enabled);
4215+
});
4216+
}
4217+
41744218
#[test]
41754219
fn operator_auth_is_opt_in_and_file_flag_precedence_is_explicit() {
41764220
Environment::new()

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ pub struct GatewayFileSection {
180180
pub config_delivery_mode: Option<openshell_core::config::ConfigDeliveryMode>,
181181

182182
// ── Service routing ──────────────────────────────────────────────────
183+
/// Opt in to gateway authentication for sandbox application requests.
184+
#[serde(default)]
185+
pub service_auth_enabled: Option<bool>,
183186
/// Subject Alternative Names configured on the gateway server certificate.
184187
/// Wildcard DNS SANs also enable sandbox service URLs under that domain.
185188
#[serde(default)]

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3753,12 +3753,14 @@ mod tests {
37533753
let client_cert = CertificateDer::from(client.der().to_vec());
37543754
let key_pem = client_key.serialize_pem().unwrap();
37553755
let client_key = PrivateKeyDer::from_pem_slice(key_pem.as_bytes()).unwrap();
3756-
for enabled in [false, true] {
3756+
for (enabled, auth_enabled) in [(false, false), (false, true), (true, false), (true, true)]
3757+
{
37573758
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
37583759
let addr = listener.local_addr().unwrap();
37593760
let mut state = test_state(addr, false).await;
37603761
let inner = Arc::get_mut(&mut state).unwrap();
37613762
inner.config.auth.allow_unauthenticated_users = false;
3763+
inner.config.service_routing.auth_enabled = auth_enabled;
37623764
inner.config.mtls_auth.enabled = true;
37633765
inner.config.supervisor_app_listener.enabled = enabled;
37643766
inner.config.tls = Some(openshell_core::TlsConfig {
@@ -3822,12 +3824,12 @@ mod tests {
38223824
.unwrap();
38233825
let response = String::from_utf8_lossy(&response);
38243826
assert!(
3825-
response.starts_with(if authenticated {
3827+
response.starts_with(if authenticated || !auth_enabled {
38263828
"HTTP/1.1 404"
38273829
} else {
38283830
"HTTP/1.1 401"
38293831
}),
3830-
"listener={enabled}, authenticated={authenticated}: {response}"
3832+
"listener={enabled}, auth={auth_enabled}, authenticated={authenticated}: {response}"
38313833
);
38323834
}
38333835
stop_listener(shutdown, handle).await;

0 commit comments

Comments
 (0)