Skip to content

Commit d0dbbe4

Browse files
committed
feat(kubernetes): bootstrap supervisors through pod registration stream
Signed-off-by: Gordon Sim <gsim@redhat.com>
1 parent a4ab699 commit d0dbbe4

30 files changed

Lines changed: 1729 additions & 1272 deletions

‎architecture/compute-runtimes.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,15 @@ identity is absent, malformed, unknown, ambiguous, or resolves to UID/GID 0.
247247
The supervisor itself remains root so it can establish isolation before
248248
starting unprivileged children.
249249

250+
Kubernetes supervisors authenticate to the gateway in two stages. They first
251+
call `RegisterSupervisorPod` with the projected ServiceAccount token; the
252+
gateway validates the pod-bound token and live Agent Sandbox owner state, then
253+
activates already-bound cold pods by streaming back a gateway-minted sandbox
254+
JWT. The supervisor installs that JWT in memory and starts the normal
255+
`ConnectSupervisor` session as the activated sandbox. The gateway does not dial
256+
pod IPs or require an inbound activation port; activation is supervisor
257+
initiated over the existing outbound gRPC connection.
258+
250259
Kubernetes can run the supervisor in the default combined topology or in a
251260
sidecar topology. Combined mode keeps network and process supervision in the
252261
agent container. Sidecar mode runs network enforcement, the proxy, and gateway

‎architecture/gateway.md‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,9 @@ token is reported as connected but unauthenticated.
194194
Sandbox supervisor RPCs authenticate with explicit sandbox credentials; mTLS
195195
does not grant sandbox identity. Kubernetes deployments use the
196196
gateway-minted JWT bootstrap path: the supervisor starts with a projected
197-
ServiceAccount token, exchanges it for a gateway-minted sandbox JWT, and uses
198-
that JWT on subsequent gateway RPCs.
197+
ServiceAccount token, registers the pod with the gateway, receives a
198+
gateway-minted sandbox JWT after activation, and uses that JWT on subsequent
199+
gateway RPCs.
199200
User-facing RPCs are authorized by descriptor-declared role and scope policy
200201
when OIDC or edge identity is enabled. The OIDC admin role grants platform-wide
201202
access and bypasses workspace membership checks. Workspace Admin and Workspace
@@ -209,16 +210,19 @@ identity inspection without client-side token decoding.
209210
Sandbox secrets are gateway-signed JWTs bound to a single sandbox ID. Docker,
210211
Podman, and VM drivers deliver the initial token through supervisor-only
211212
runtime material; Kubernetes supervisors exchange a projected ServiceAccount
212-
token through `IssueSandboxToken`. The gateway validates that projected token
213-
with Kubernetes `TokenReview`, requires the configured sandbox service account,
214-
checks the returned pod binding against the live pod UID, and verifies the pod's
215-
controlling `Sandbox` ownerReference against the live Sandbox CR UID and
216-
sandbox-id label before minting the gateway JWT. The bootstrap path accepts
217-
both `agents.x-k8s.io/v1beta1` ownerReferences from newer Agent Sandbox
218-
controllers and `agents.x-k8s.io/v1alpha1` ownerReferences from existing
219-
deployments. Supervisors renew gateway JWTs in memory before expiry only while
220-
the sandbox record still exists. Older tokens are not server-revoked; shared
221-
deployments bound replay exposure with short `gateway_jwt.ttl_secs` lifetimes.
213+
token through `RegisterSupervisorPod`. The gateway validates that projected
214+
token with Kubernetes `TokenReview`, requires the configured sandbox service
215+
account, checks the returned pod binding against the live pod UID, and verifies
216+
the pod's controlling `Sandbox` ownerReference against the live Sandbox CR UID
217+
and sandbox-id label before sending an activation message with the gateway JWT.
218+
The bootstrap path accepts both `agents.x-k8s.io/v1beta1` ownerReferences from
219+
newer Agent Sandbox controllers and `agents.x-k8s.io/v1alpha1` ownerReferences
220+
from existing deployments. `IssueSandboxToken` remains as a compatibility shim
221+
for older supervisor images and mints through the same already-bound pod
222+
activation logic. Supervisors renew gateway JWTs in memory before expiry only
223+
while the sandbox record still exists. Older tokens are not server-revoked;
224+
shared deployments bound replay exposure with short `gateway_jwt.ttl_secs`
225+
lifetimes.
222226
The config default is
223227
`gateway_jwt.ttl_secs = 0` for local single-player Docker, Podman, and VM
224228
gateways; those tokens carry `exp = 0` and do not expire. Kubernetes and other

‎crates/openshell-cli/tests/ensure_providers_integration.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,17 @@ impl OpenShell for TestOpenShell {
619619
Err(Status::unimplemented("not implemented in test"))
620620
}
621621

622+
type RegisterSupervisorPodStream = tokio_stream::wrappers::ReceiverStream<
623+
Result<openshell_core::proto::PodActivationMessage, Status>,
624+
>;
625+
626+
async fn register_supervisor_pod(
627+
&self,
628+
_request: tonic::Request<openshell_core::proto::RegisterSupervisorPodRequest>,
629+
) -> Result<Response<Self::RegisterSupervisorPodStream>, Status> {
630+
Err(Status::unimplemented("not implemented in test"))
631+
}
632+
622633
async fn refresh_sandbox_token(
623634
&self,
624635
_request: tonic::Request<openshell_core::proto::RefreshSandboxTokenRequest>,

‎crates/openshell-cli/tests/mtls_integration.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,17 @@ impl OpenShell for TestOpenShell {
498498
Err(Status::unimplemented("not implemented in test"))
499499
}
500500

501+
type RegisterSupervisorPodStream = tokio_stream::wrappers::ReceiverStream<
502+
Result<openshell_core::proto::PodActivationMessage, Status>,
503+
>;
504+
505+
async fn register_supervisor_pod(
506+
&self,
507+
_request: tonic::Request<openshell_core::proto::RegisterSupervisorPodRequest>,
508+
) -> Result<Response<Self::RegisterSupervisorPodStream>, Status> {
509+
Err(Status::unimplemented("not implemented in test"))
510+
}
511+
501512
async fn refresh_sandbox_token(
502513
&self,
503514
_request: tonic::Request<openshell_core::proto::RefreshSandboxTokenRequest>,

‎crates/openshell-cli/tests/provider_commands_integration.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,17 @@ impl OpenShell for TestOpenShell {
10221022
Err(Status::unimplemented("not implemented in test"))
10231023
}
10241024

1025+
type RegisterSupervisorPodStream = tokio_stream::wrappers::ReceiverStream<
1026+
Result<openshell_core::proto::PodActivationMessage, Status>,
1027+
>;
1028+
1029+
async fn register_supervisor_pod(
1030+
&self,
1031+
_request: tonic::Request<openshell_core::proto::RegisterSupervisorPodRequest>,
1032+
) -> Result<Response<Self::RegisterSupervisorPodStream>, Status> {
1033+
Err(Status::unimplemented("not implemented in test"))
1034+
}
1035+
10251036
async fn refresh_sandbox_token(
10261037
&self,
10271038
_request: tonic::Request<openshell_core::proto::RefreshSandboxTokenRequest>,

‎crates/openshell-cli/tests/sandbox_create_lifecycle_integration.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,17 @@ impl OpenShell for TestOpenShell {
792792
Err(Status::unimplemented("not implemented in test"))
793793
}
794794

795+
type RegisterSupervisorPodStream = tokio_stream::wrappers::ReceiverStream<
796+
Result<openshell_core::proto::PodActivationMessage, Status>,
797+
>;
798+
799+
async fn register_supervisor_pod(
800+
&self,
801+
_request: tonic::Request<openshell_core::proto::RegisterSupervisorPodRequest>,
802+
) -> Result<Response<Self::RegisterSupervisorPodStream>, Status> {
803+
Err(Status::unimplemented("not implemented in test"))
804+
}
805+
795806
async fn refresh_sandbox_token(
796807
&self,
797808
_request: tonic::Request<openshell_core::proto::RefreshSandboxTokenRequest>,

‎crates/openshell-cli/tests/sandbox_name_fallback_integration.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,17 @@ impl OpenShell for TestOpenShell {
586586
Err(Status::unimplemented("not implemented in test"))
587587
}
588588

589+
type RegisterSupervisorPodStream = tokio_stream::wrappers::ReceiverStream<
590+
Result<openshell_core::proto::PodActivationMessage, Status>,
591+
>;
592+
593+
async fn register_supervisor_pod(
594+
&self,
595+
_request: tonic::Request<openshell_core::proto::RegisterSupervisorPodRequest>,
596+
) -> Result<Response<Self::RegisterSupervisorPodStream>, Status> {
597+
Err(Status::unimplemented("not implemented in test"))
598+
}
599+
589600
async fn refresh_sandbox_token(
590601
&self,
591602
_request: tonic::Request<openshell_core::proto::RefreshSandboxTokenRequest>,

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

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
//! Podman / VM drivers write this to a bundle file at sandbox-create
1313
//! time).
1414
//! 3. `OPENSHELL_K8S_SA_TOKEN_FILE` — projected `ServiceAccount` JWT; the
15-
//! supervisor exchanges it for a gateway JWT via `IssueSandboxToken`
16-
//! once at startup.
15+
//! supervisor registers its pod and receives a gateway JWT via
16+
//! `RegisterSupervisorPod` once at startup.
1717
//!
1818
//! The resolved bearer credential is held in process memory thereafter and
1919
//! injected on every outbound call by [`AuthInterceptor`].
@@ -24,11 +24,11 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};
2424

2525
use crate::proto::{
2626
DenialSummary, GetDraftPolicyRequest, GetInferenceBundleRequest, GetInferenceBundleResponse,
27-
GetSandboxConfigRequest, GetSandboxProviderEnvironmentRequest, IssueSandboxTokenRequest,
28-
NetworkActivitySummary, PolicyChunk, PolicySource, PolicyStatus, RefreshSandboxTokenRequest,
29-
ReportPolicyStatusRequest, SandboxPolicy as ProtoSandboxPolicy, SubmitPolicyAnalysisRequest,
30-
SubmitPolicyAnalysisResponse, UpdateConfigRequest, inference_client::InferenceClient,
31-
open_shell_client::OpenShellClient,
27+
GetSandboxConfigRequest, GetSandboxProviderEnvironmentRequest, NetworkActivitySummary,
28+
PolicyChunk, PolicySource, PolicyStatus, RefreshSandboxTokenRequest,
29+
RegisterSupervisorPodRequest, ReportPolicyStatusRequest, SandboxPolicy as ProtoSandboxPolicy,
30+
SubmitPolicyAnalysisRequest, SubmitPolicyAnalysisResponse, UpdateConfigRequest,
31+
inference_client::InferenceClient, open_shell_client::OpenShellClient,
3232
};
3333
use crate::sandbox_env;
3434
use miette::{IntoDiagnostic, Result, WrapErr};
@@ -258,7 +258,7 @@ async fn token_slot(endpoint: &str, plain_channel: &Channel) -> Result<(TokenSlo
258258
///
259259
/// `endpoint` is logged on errors but never used for transport here; the
260260
/// actual network call lives inside this function only on the K8s
261-
/// bootstrap path, which uses `plain_channel` to call `IssueSandboxToken`
261+
/// bootstrap path, which uses `plain_channel` to call `RegisterSupervisorPod`
262262
/// once before the steady-state Bearer-authenticated channel is built.
263263
async fn acquire_sandbox_token(endpoint: &str, plain_channel: &Channel) -> Result<AcquiredToken> {
264264
if let Ok(t) = std::env::var(sandbox_env::SANDBOX_TOKEN)
@@ -311,7 +311,7 @@ async fn acquire_k8s_sandbox_token(
311311
.wrap_err_with(|| format!("failed to read K8s SA token from {sa_path}"))?
312312
.trim()
313313
.to_string();
314-
info!(endpoint = %endpoint, "exchanging K8s ServiceAccount token for sandbox JWT");
314+
info!(endpoint = %endpoint, "registering K8s supervisor pod for sandbox activation");
315315
// The bootstrap exchange uses a one-off interceptor pinned to the
316316
// SA token; the resulting gateway JWT becomes the value in the
317317
// shared `TOKEN_SLOT` once `connect_channel` returns.
@@ -324,11 +324,23 @@ async fn acquire_k8s_sandbox_token(
324324
let bootstrap = InterceptedService::new(plain_channel.clone(), interceptor);
325325
let mut client = OpenShellClient::new(bootstrap);
326326
let resp = client
327-
.issue_sandbox_token(IssueSandboxTokenRequest {})
327+
.register_supervisor_pod(RegisterSupervisorPodRequest {})
328328
.await
329329
.into_diagnostic()
330-
.wrap_err("IssueSandboxToken bootstrap exchange failed")?;
331-
Ok(resp.into_inner().token)
330+
.wrap_err("RegisterSupervisorPod bootstrap stream failed")?;
331+
let mut stream = resp.into_inner();
332+
let activation = stream
333+
.message()
334+
.await
335+
.into_diagnostic()
336+
.wrap_err("RegisterSupervisorPod activation stream failed")?
337+
.ok_or_else(|| miette::miette!("RegisterSupervisorPod stream closed before activation"))?;
338+
info!(
339+
sandbox_id = %activation.sandbox_id,
340+
sandbox_name = %activation.sandbox_name,
341+
"received supervisor pod activation"
342+
);
343+
Ok(activation.token)
332344
}
333345

334346
/// Build an authenticated channel for direct external use (e.g. the

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ pub const USER_ENVIRONMENT: &str = "OPENSHELL_USER_ENVIRONMENT";
8787

8888
/// Path to the projected `ServiceAccount` JWT (Kubernetes driver).
8989
///
90-
/// Used to bootstrap a gateway-minted JWT via `IssueSandboxToken`. Kubelet
91-
/// writes and rotates this file; the supervisor exchanges its contents
92-
/// for a gateway JWT at startup and on refresh.
90+
/// Used to register the supervisor pod and receive a gateway-minted JWT via
91+
/// `RegisterSupervisorPod`. Kubelet writes and rotates this file; the
92+
/// supervisor presents its contents at startup and when rebootstrap is needed
93+
/// after refresh authentication failure.
9394
pub const K8S_SA_TOKEN_FILE: &str = "OPENSHELL_K8S_SA_TOKEN_FILE";
9495

9596
/// Filesystem path to the SPIFFE Workload API UNIX socket used for provider

‎crates/openshell-driver-kubernetes/README.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ values must override image-provided environment variables.
8787
Sandbox pods run as `service_account_name` and keep
8888
`automountServiceAccountToken: false`. The only Kubernetes token exposed to the
8989
supervisor is an explicit, audience-bound projected token mounted at
90-
`/var/run/secrets/openshell/token` for the one-shot `IssueSandboxToken`
91-
bootstrap exchange.
90+
`/var/run/secrets/openshell/token` for the `RegisterSupervisorPod` bootstrap
91+
stream. The gateway validates the pod-bound token and activates already-bound
92+
cold pods by returning a gateway-minted sandbox JWT on that stream.
9293

9394
The gateway uses the supervisor relay for connect, exec, and file sync. Sandbox
9495
pods do not need direct external ingress for SSH.

0 commit comments

Comments
 (0)