Skip to content

Commit 5f66489

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

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
@@ -368,6 +368,10 @@ pub struct Config {
368368
/// Part of the programmatically-built [`Config`]; never deserialized directly.
369369
#[derive(Debug, Clone)]
370370
pub struct ServiceRoutingConfig {
371+
/// Opt in to gateway authentication for proxied application requests.
372+
/// Independent of the supervisor application listener.
373+
pub auth_enabled: bool,
374+
371375
/// Base domains accepted for `sandbox--service.<domain>` routes.
372376
/// The first domain is used when the gateway prints endpoint URLs.
373377
pub base_domains: Vec<String>,
@@ -1281,6 +1285,13 @@ impl Config {
12811285
.then_some(self.name.as_str())
12821286
}
12831287

1288+
/// Opt in to configured gateway authentication on application requests.
1289+
#[must_use]
1290+
pub const fn with_service_auth(mut self, enabled: bool) -> Self {
1291+
self.service_routing.auth_enabled = enabled;
1292+
self
1293+
}
1294+
12841295
/// Enable or disable plaintext HTTP routing for loopback sandbox service
12851296
/// hostnames on TLS-enabled gateway listeners.
12861297
#[must_use]
@@ -1300,6 +1311,7 @@ impl Config {
13001311
impl Default for ServiceRoutingConfig {
13011312
fn default() -> Self {
13021313
Self {
1314+
auth_enabled: false,
13031315
base_domains: default_service_routing_domains(),
13041316
enable_loopback_service_http: default_enable_loopback_service_http(),
13051317
service_hostname_format: ServiceHostnameFormat::default(),
@@ -1839,6 +1851,8 @@ mod tests {
18391851
vec![DEFAULT_SERVICE_ROUTING_DOMAIN.to_string()]
18401852
);
18411853
assert!(cfg.service_routing.enable_loopback_service_http);
1854+
assert!(!cfg.service_routing.auth_enabled);
1855+
assert!(cfg.with_service_auth(true).service_routing.auth_enabled);
18421856
}
18431857

18441858
#[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
@@ -1552,6 +1557,11 @@ fn merge_file_into_args(args: &mut RunArgs, file: &GatewayFileSection, matches:
15521557
{
15531558
args.service_hostname_format = Some(*format);
15541559
}
1560+
if let Some(enabled) = file.service_auth_enabled
1561+
&& arg_defaulted(matches, "service_auth_enabled")
1562+
{
1563+
args.service_auth_enabled = enabled;
1564+
}
15551565
if let Some(enabled) = file.enable_loopback_service_http
15561566
&& arg_defaulted(matches, "enable_loopback_service_http")
15571567
{
@@ -4127,6 +4137,40 @@ enabled = false
41274137
});
41284138
}
41294139

4140+
#[test]
4141+
fn service_auth_is_opt_in_with_explicit_cli_environment_and_file_precedence() {
4142+
Environment::new()
4143+
.remove("OPENSHELL_SERVICE_AUTH_ENABLED")
4144+
.run(|| {
4145+
let file = toml::from_str::<ConfigFile>(
4146+
"[openshell.gateway]\nservice_auth_enabled = true\n",
4147+
)
4148+
.unwrap();
4149+
let (mut args, matches) = parse_with_args(&["openshell-gateway"]);
4150+
assert!(!args.service_auth_enabled);
4151+
merge_file_into_args(&mut args, &file.openshell.gateway, &matches);
4152+
assert!(args.service_auth_enabled);
4153+
let (mut args, matches) =
4154+
parse_with_args(&["openshell-gateway", "--service-auth-enabled", "false"]);
4155+
merge_file_into_args(&mut args, &file.openshell.gateway, &matches);
4156+
assert!(!args.service_auth_enabled);
4157+
});
4158+
Environment::new()
4159+
.set("OPENSHELL_SERVICE_AUTH_ENABLED", "false")
4160+
.run(|| {
4161+
let file = toml::from_str::<ConfigFile>(
4162+
"[openshell.gateway]\nservice_auth_enabled = true\n",
4163+
)
4164+
.unwrap();
4165+
let (mut args, matches) = parse_with_args(&["openshell-gateway"]);
4166+
merge_file_into_args(&mut args, &file.openshell.gateway, &matches);
4167+
assert!(!args.service_auth_enabled);
4168+
let (args, _) =
4169+
parse_with_args(&["openshell-gateway", "--service-auth-enabled", "true"]);
4170+
assert!(args.service_auth_enabled);
4171+
});
4172+
}
4173+
41304174
#[test]
41314175
fn operator_auth_is_opt_in_and_file_flag_precedence_is_explicit() {
41324176
Environment::new()

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

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

176176
// ── Service routing ──────────────────────────────────────────────────
177+
/// Opt in to gateway authentication for sandbox application requests.
178+
#[serde(default)]
179+
pub service_auth_enabled: Option<bool>,
177180
/// Subject Alternative Names configured on the gateway server certificate.
178181
/// Wildcard DNS SANs also enable sandbox service URLs under that domain.
179182
#[serde(default)]

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3752,12 +3752,14 @@ mod tests {
37523752
let client_cert = CertificateDer::from(client.der().to_vec());
37533753
let key_pem = client_key.serialize_pem().unwrap();
37543754
let client_key = PrivateKeyDer::from_pem_slice(key_pem.as_bytes()).unwrap();
3755-
for enabled in [false, true] {
3755+
for (enabled, auth_enabled) in [(false, false), (false, true), (true, false), (true, true)]
3756+
{
37563757
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
37573758
let addr = listener.local_addr().unwrap();
37583759
let mut state = test_state(addr, false).await;
37593760
let inner = Arc::get_mut(&mut state).unwrap();
37603761
inner.config.auth.allow_unauthenticated_users = false;
3762+
inner.config.service_routing.auth_enabled = auth_enabled;
37613763
inner.config.mtls_auth.enabled = true;
37623764
inner.config.supervisor_app_listener.enabled = enabled;
37633765
inner.config.tls = Some(openshell_core::TlsConfig {
@@ -3821,12 +3823,12 @@ mod tests {
38213823
.unwrap();
38223824
let response = String::from_utf8_lossy(&response);
38233825
assert!(
3824-
response.starts_with(if authenticated {
3826+
response.starts_with(if authenticated || !auth_enabled {
38253827
"HTTP/1.1 404"
38263828
} else {
38273829
"HTTP/1.1 401"
38283830
}),
3829-
"listener={enabled}, authenticated={authenticated}: {response}"
3831+
"listener={enabled}, auth={auth_enabled}, authenticated={authenticated}: {response}"
38303832
);
38313833
}
38323834
stop_listener(shutdown, handle).await;

0 commit comments

Comments
 (0)