Skip to content

Commit 7224961

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

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
@@ -352,6 +352,10 @@ pub struct Config {
352352
/// Part of the programmatically-built [`Config`]; never deserialized directly.
353353
#[derive(Debug, Clone)]
354354
pub struct ServiceRoutingConfig {
355+
/// Opt in to gateway authentication for proxied application requests.
356+
/// Independent of the supervisor application listener.
357+
pub auth_enabled: bool,
358+
355359
/// Base domains accepted for `sandbox--service.<domain>` routes.
356360
/// The first domain is used when the gateway prints endpoint URLs.
357361
pub base_domains: Vec<String>,
@@ -1262,6 +1266,13 @@ impl Config {
12621266
.then_some(self.name.as_str())
12631267
}
12641268

1269+
/// Opt in to configured gateway authentication on application requests.
1270+
#[must_use]
1271+
pub const fn with_service_auth(mut self, enabled: bool) -> Self {
1272+
self.service_routing.auth_enabled = enabled;
1273+
self
1274+
}
1275+
12651276
/// Enable or disable plaintext HTTP routing for loopback sandbox service
12661277
/// hostnames on TLS-enabled gateway listeners.
12671278
#[must_use]
@@ -1281,6 +1292,7 @@ impl Config {
12811292
impl Default for ServiceRoutingConfig {
12821293
fn default() -> Self {
12831294
Self {
1295+
auth_enabled: false,
12841296
base_domains: default_service_routing_domains(),
12851297
enable_loopback_service_http: default_enable_loopback_service_http(),
12861298
service_hostname_format: ServiceHostnameFormat::default(),
@@ -1820,6 +1832,8 @@ mod tests {
18201832
vec![DEFAULT_SERVICE_ROUTING_DOMAIN.to_string()]
18211833
);
18221834
assert!(cfg.service_routing.enable_loopback_service_http);
1835+
assert!(!cfg.service_routing.auth_enabled);
1836+
assert!(cfg.with_service_auth(true).service_routing.auth_enabled);
18231837
}
18241838

18251839
#[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
@@ -312,6 +312,10 @@ struct RunArgs {
312312
)]
313313
server_sans: Vec<String>,
314314

315+
/// Authenticate sandbox application requests using configured gateway auth.
316+
#[arg(long, env = "OPENSHELL_SERVICE_AUTH_ENABLED", default_value_t = false, action = ArgAction::Set)]
317+
service_auth_enabled: bool,
318+
315319
/// Hostname format for exposed sandbox services: `readable` (default)
316320
/// keeps `<workspace>--<sandbox>[--<service>]` hostnames; `hashed` mints
317321
/// opaque one-label hostnames discriminated by the gateway installation
@@ -628,6 +632,7 @@ fn prepare_server_config_with_drivers(
628632
)
629633
.with_server_sans(args.server_sans.clone())
630634
.with_service_hostname_format(args.service_hostname_format.unwrap_or_default())
635+
.with_service_auth(args.service_auth_enabled)
631636
.with_loopback_service_http(args.enable_loopback_service_http)
632637
.with_websocket_tunnel(args.enable_websocket_tunnel);
633638
config
@@ -1508,6 +1513,11 @@ fn merge_file_into_args(args: &mut RunArgs, file: &GatewayFileSection, matches:
15081513
{
15091514
args.service_hostname_format = Some(*format);
15101515
}
1516+
if let Some(enabled) = file.service_auth_enabled
1517+
&& arg_defaulted(matches, "service_auth_enabled")
1518+
{
1519+
args.service_auth_enabled = enabled;
1520+
}
15111521
if let Some(enabled) = file.enable_loopback_service_http
15121522
&& arg_defaulted(matches, "enable_loopback_service_http")
15131523
{
@@ -4059,6 +4069,40 @@ enabled = false
40594069
});
40604070
}
40614071

4072+
#[test]
4073+
fn service_auth_is_opt_in_with_explicit_cli_environment_and_file_precedence() {
4074+
Environment::new()
4075+
.remove("OPENSHELL_SERVICE_AUTH_ENABLED")
4076+
.run(|| {
4077+
let file = toml::from_str::<ConfigFile>(
4078+
"[openshell.gateway]\nservice_auth_enabled = true\n",
4079+
)
4080+
.unwrap();
4081+
let (mut args, matches) = parse_with_args(&["openshell-gateway"]);
4082+
assert!(!args.service_auth_enabled);
4083+
merge_file_into_args(&mut args, &file.openshell.gateway, &matches);
4084+
assert!(args.service_auth_enabled);
4085+
let (mut args, matches) =
4086+
parse_with_args(&["openshell-gateway", "--service-auth-enabled", "false"]);
4087+
merge_file_into_args(&mut args, &file.openshell.gateway, &matches);
4088+
assert!(!args.service_auth_enabled);
4089+
});
4090+
Environment::new()
4091+
.set("OPENSHELL_SERVICE_AUTH_ENABLED", "false")
4092+
.run(|| {
4093+
let file = toml::from_str::<ConfigFile>(
4094+
"[openshell.gateway]\nservice_auth_enabled = true\n",
4095+
)
4096+
.unwrap();
4097+
let (mut args, matches) = parse_with_args(&["openshell-gateway"]);
4098+
merge_file_into_args(&mut args, &file.openshell.gateway, &matches);
4099+
assert!(!args.service_auth_enabled);
4100+
let (args, _) =
4101+
parse_with_args(&["openshell-gateway", "--service-auth-enabled", "true"]);
4102+
assert!(args.service_auth_enabled);
4103+
});
4104+
}
4105+
40624106
#[test]
40634107
fn operator_auth_is_opt_in_and_file_flag_precedence_is_explicit() {
40644108
Environment::new()

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

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

166166
// ── Service routing ──────────────────────────────────────────────────
167+
/// Opt in to gateway authentication for sandbox application requests.
168+
#[serde(default)]
169+
pub service_auth_enabled: Option<bool>,
167170
/// Subject Alternative Names configured on the gateway server certificate.
168171
/// Wildcard DNS SANs also enable sandbox service URLs under that domain.
169172
#[serde(default)]

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3758,12 +3758,14 @@ mod tests {
37583758
let client_cert = CertificateDer::from(client.der().to_vec());
37593759
let key_pem = client_key.serialize_pem().unwrap();
37603760
let client_key = PrivateKeyDer::from_pem_slice(key_pem.as_bytes()).unwrap();
3761-
for enabled in [false, true] {
3761+
for (enabled, auth_enabled) in [(false, false), (false, true), (true, false), (true, true)]
3762+
{
37623763
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
37633764
let addr = listener.local_addr().unwrap();
37643765
let mut state = test_state(addr, false).await;
37653766
let inner = Arc::get_mut(&mut state).unwrap();
37663767
inner.config.auth.allow_unauthenticated_users = false;
3768+
inner.config.service_routing.auth_enabled = auth_enabled;
37673769
inner.config.mtls_auth.enabled = true;
37683770
inner.config.supervisor_app_listener.enabled = enabled;
37693771
inner.config.tls = Some(openshell_core::TlsConfig {
@@ -3827,12 +3829,12 @@ mod tests {
38273829
.unwrap();
38283830
let response = String::from_utf8_lossy(&response);
38293831
assert!(
3830-
response.starts_with(if authenticated {
3832+
response.starts_with(if authenticated || !auth_enabled {
38313833
"HTTP/1.1 404"
38323834
} else {
38333835
"HTTP/1.1 401"
38343836
}),
3835-
"listener={enabled}, authenticated={authenticated}: {response}"
3837+
"listener={enabled}, auth={auth_enabled}, authenticated={authenticated}: {response}"
38363838
);
38373839
}
38383840
stop_listener(shutdown, handle).await;

0 commit comments

Comments
 (0)