Skip to content

Commit e7c662c

Browse files
committed
feat(server)!: per-sandbox JWT identity over Bearer (wire break)
Switches every sandbox-to-gateway gRPC call from "path-based mTLS-only trust" to "Authorization: Bearer <gateway-minted-JWT>" presented by the sandbox supervisor. Closes the trust-boundary half of issue #1354; the per-handler sandbox_id equality check follows in PR 4. Sandbox side: - crates/openshell-sandbox/src/grpc_client.rs gains an AuthInterceptor that injects the Bearer header on every outbound RPC. The token is resolved at startup from one of three sources, in order: 1. OPENSHELL_SANDBOX_TOKEN (env, test harnesses) 2. OPENSHELL_SANDBOX_TOKEN_FILE (Docker/Podman/VM drivers) 3. OPENSHELL_K8S_SA_TOKEN_FILE (K8s driver — projected SA token exchanged for a gateway JWT via IssueSandboxToken) Gateway side: - handle_create_sandbox mints a gateway JWT and passes it through the compute layer to DriverSandboxSpec.sandbox_token. K8s sandboxes ignore the field; Docker and Podman drivers inject it as OPENSHELL_SANDBOX_TOKEN in the container env. - Removes the path-based SANDBOX_METHODS / DUAL_AUTH_METHODS branches and the x-openshell-auth-source metadata marker. The AuthGrpcRouter chain is now uniform: K8s SA -> SandboxJwt -> OIDC, all extension-based. - Removes LegacySandboxMarkerAuthenticator and the SandboxIdentitySource:: LegacyMarker variant. Handlers read Principal::Sandbox directly from request extensions. Kubernetes driver: - Sandbox pods gain a projected ServiceAccount token volume mounted at /var/run/secrets/openshell/token (audience openshell-gateway, 1h TTL, kubelet auto-rotates). - Each pod is annotated with openshell.io/sandbox-id; the gateway resolves the SA token claim's pod uid back to a sandbox id via this annotation. - Helm Role grants the gateway pods:get in the sandbox namespace. No ClusterRoleBinding to system:auth-delegator — the gateway validates SA tokens against the apiserver's anonymous JWKS endpoint instead of via TokenReview, so no cluster-scoped privilege is required. The full JWKS verifier + pod-annotation lookup lands in the follow-up that brings the K8s helm-dev demo end-to-end; PR 3 exercises the wire break with Docker/Podman as the working drivers. Signed-off-by: Taylor Mutch <[email protected]>
1 parent 5465846 commit e7c662c

16 files changed

Lines changed: 384 additions & 415 deletions

File tree

‎crates/openshell-driver-docker/src/lib.rs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,19 @@ fn build_environment(sandbox: &DriverSandbox, config: &DockerDriverRuntimeConfig
982982
);
983983
}
984984

985+
// Gateway-minted sandbox JWT (PR 3 of the per-sandbox identity series).
986+
// Passed via env var since Docker has no native secret mount that is
987+
// simpler than the existing bind-mount pattern; the trust boundary
988+
// (`docker inspect` access) is already equivalent to the TLS key mount.
989+
if let Some(spec) = sandbox.spec.as_ref()
990+
&& !spec.sandbox_token.is_empty()
991+
{
992+
environment.insert(
993+
openshell_core::sandbox_env::SANDBOX_TOKEN.to_string(),
994+
spec.sandbox_token.clone(),
995+
);
996+
}
997+
985998
let mut pairs = environment.into_iter().collect::<Vec<_>>();
986999
pairs.sort_by(|left, right| left.0.cmp(&right.0));
9871000
pairs

‎crates/openshell-driver-docker/src/tests.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ fn test_sandbox() -> DriverSandbox {
3333
}),
3434
gpu: false,
3535
gpu_device: String::new(),
36+
sandbox_token: String::new(),
3637
}),
3738
status: None,
3839
}

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

Lines changed: 73 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,8 +1147,28 @@ fn sandbox_template_to_k8s(
11471147
if !template.labels.is_empty() {
11481148
metadata.insert("labels".to_string(), serde_json::json!(template.labels));
11491149
}
1150-
if let Some(annotations) = platform_config_struct(template, "annotations") {
1151-
metadata.insert("annotations".to_string(), annotations);
1150+
// Carry the sandbox UUID as a pod annotation so the gateway can resolve
1151+
// a projected SA token claim (pod name + uid) back to a sandbox identity
1152+
// when the supervisor calls `IssueSandboxToken` at startup. The gateway's
1153+
// K8s Role does NOT grant `patch pods`, so this annotation is
1154+
// effectively immutable post-create (see plan §11.8).
1155+
let mut pod_annotations = platform_config_struct(template, "annotations")
1156+
.and_then(|v| match v {
1157+
serde_json::Value::Object(map) => Some(map),
1158+
_ => None,
1159+
})
1160+
.unwrap_or_default();
1161+
if !params.sandbox_id.is_empty() {
1162+
pod_annotations.insert(
1163+
"openshell.io/sandbox-id".to_string(),
1164+
serde_json::Value::String(params.sandbox_id.to_string()),
1165+
);
1166+
}
1167+
if !pod_annotations.is_empty() {
1168+
metadata.insert(
1169+
"annotations".to_string(),
1170+
serde_json::Value::Object(pod_annotations),
1171+
);
11521172
}
11531173

11541174
let mut spec = serde_json::Map::new();
@@ -1235,17 +1255,26 @@ fn sandbox_template_to_k8s(
12351255
}),
12361256
);
12371257

1238-
// Mount client TLS secret for mTLS to the server.
1258+
// Mount client TLS secret for mTLS to the server, plus the projected
1259+
// ServiceAccount token used to bootstrap the sandbox's gateway JWT
1260+
// via `IssueSandboxToken`.
1261+
let mut volume_mounts: Vec<serde_json::Value> = Vec::new();
12391262
if !params.client_tls_secret_name.is_empty() {
1240-
container.insert(
1241-
"volumeMounts".to_string(),
1242-
serde_json::json!([{
1243-
"name": "openshell-client-tls",
1244-
"mountPath": "/etc/openshell-tls/client",
1245-
"readOnly": true
1246-
}]),
1247-
);
1248-
}
1263+
volume_mounts.push(serde_json::json!({
1264+
"name": "openshell-client-tls",
1265+
"mountPath": "/etc/openshell-tls/client",
1266+
"readOnly": true
1267+
}));
1268+
}
1269+
volume_mounts.push(serde_json::json!({
1270+
"name": "openshell-sa-token",
1271+
"mountPath": "/var/run/secrets/openshell",
1272+
"readOnly": true,
1273+
}));
1274+
container.insert(
1275+
"volumeMounts".to_string(),
1276+
serde_json::Value::Array(volume_mounts),
1277+
);
12491278

12501279
if let Some(resources) = container_resources(template, gpu) {
12511280
container.insert("resources".to_string(), resources);
@@ -1257,15 +1286,31 @@ fn sandbox_template_to_k8s(
12571286

12581287
// Add TLS secret volume. Mode 0400 (owner-read) prevents the
12591288
// unprivileged sandbox user from reading the mTLS private key.
1289+
let mut volumes: Vec<serde_json::Value> = Vec::new();
12601290
if !params.client_tls_secret_name.is_empty() {
1261-
spec.insert(
1262-
"volumes".to_string(),
1263-
serde_json::json!([{
1264-
"name": "openshell-client-tls",
1265-
"secret": { "secretName": params.client_tls_secret_name, "defaultMode": 256 }
1266-
}]),
1267-
);
1268-
}
1291+
volumes.push(serde_json::json!({
1292+
"name": "openshell-client-tls",
1293+
"secret": { "secretName": params.client_tls_secret_name, "defaultMode": 256 }
1294+
}));
1295+
}
1296+
// Projected ServiceAccountToken volume — kubelet writes a short-lived
1297+
// audience-bound JWT into /var/run/secrets/openshell/token and rotates
1298+
// it automatically. The supervisor exchanges this for a gateway-minted
1299+
// JWT via `IssueSandboxToken` once at startup.
1300+
volumes.push(serde_json::json!({
1301+
"name": "openshell-sa-token",
1302+
"projected": {
1303+
"sources": [{
1304+
"serviceAccountToken": {
1305+
"audience": "openshell-gateway",
1306+
"expirationSeconds": 3600_i64,
1307+
"path": "token"
1308+
}
1309+
}],
1310+
"defaultMode": 256
1311+
}
1312+
}));
1313+
spec.insert("volumes".to_string(), serde_json::Value::Array(volumes));
12691314

12701315
// Add hostAliases so sandbox pods can reach the Docker host.
12711316
if !params.host_gateway_ip.is_empty() {
@@ -1444,6 +1489,14 @@ fn apply_required_env(
14441489
"/etc/openshell-tls/client/tls.key",
14451490
);
14461491
}
1492+
// Projected ServiceAccount token written by kubelet (see the volume
1493+
// definition in `sandbox_template_to_k8s`). The supervisor reads this
1494+
// and exchanges it for a gateway-minted JWT via `IssueSandboxToken`.
1495+
upsert_env(
1496+
env,
1497+
openshell_core::sandbox_env::K8S_SA_TOKEN_FILE,
1498+
"/var/run/secrets/openshell/token",
1499+
);
14471500
}
14481501

14491502
fn upsert_env(env: &mut Vec<serde_json::Value>, name: &str, value: &str) {

‎crates/openshell-driver-podman/src/container.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,17 @@ fn build_env(
299299
);
300300
}
301301

302+
// 4. Gateway-minted sandbox JWT (PR 3 of the per-sandbox identity
303+
// series). Passed via env var; the supervisor reads it directly.
304+
if let Some(s) = spec
305+
&& !s.sandbox_token.is_empty()
306+
{
307+
env.insert(
308+
openshell_core::sandbox_env::SANDBOX_TOKEN.into(),
309+
s.sandbox_token.clone(),
310+
);
311+
}
312+
302313
env
303314
}
304315

0 commit comments

Comments
 (0)