use std::collections::BTreeMap;
use std::sync::{Arc, Mutex};
#[path = "kubernetes_egress.rs"]
mod egress;
use async_trait::async_trait;
use futures::stream::BoxStream;
use serde::{Deserialize, Serialize};
use crate::error::{ErrorData, Result};
use crate::providers::sandbox::agent_protocol::{self, AgentTransport};
use crate::traits::{
Binding, CommandOutput, CreateSandboxRequest, InteractiveCommand, JobPoll, JobStart,
PreviewCapability, ResolvedSandbox, RunCommandRequest, Sandbox, SandboxInstance, SandboxState,
};
use alien_core::bindings::KubernetesSandboxBinding;
use alien_core::{Platform, SandboxCapabilities, SandboxCapability};
use alien_error::{AlienError, Context, IntoAlienError};
const CAPABILITY_REFRESH_MARGIN_SECONDS: i64 = 60;
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct SessionList {
session_ids: Vec<String>,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ClaimResponse {
#[serde(default)]
agent_session_id: Option<String>,
session_id: String,
endpoint: String,
capability: String,
expires_at: i64,
#[serde(default = "first_generation")]
generation: u64,
#[serde(default = "yes")]
created: bool,
#[serde(default)]
tenant_key: Option<String>,
#[serde(default)]
deadline: Option<i64>,
#[serde(default)]
setup_pending: bool,
}
const GATEWAY_IDENTITY_FALLBACK_SECONDS: i64 = 86_400;
fn yes() -> bool {
true
}
fn first_generation() -> u64 {
1
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct ClaimRequest<'a> {
sandbox_id: &'a str,
session_id: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
tenant_key: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
timeout_ms: Option<u64>,
}
enum Lookup {
Found(ClaimResponse),
NotFound,
Unsupported,
}
async fn lookup_claim(client: &reqwest::Client, url: &str, token: &str) -> Result<Lookup> {
let response = client
.get(url)
.bearer_auth(token)
.send()
.await
.into_alien_error()
.context(ErrorData::OperationNotSupported {
operation: "sandbox.get".to_string(),
reason: "the sandbox broker is unreachable".to_string(),
})?;
if response.status() == reqwest::StatusCode::NOT_FOUND {
return Ok(Lookup::NotFound);
}
if response.status() == reqwest::StatusCode::METHOD_NOT_ALLOWED {
return Ok(Lookup::Unsupported);
}
if !response.status().is_success() {
let status = response.status();
let body = response.text().await.unwrap_or_default();
return Err(AlienError::new(ErrorData::OperationNotSupported {
operation: "sandbox.get".to_string(),
reason: broker_refusal(status, &body),
}));
}
response
.json()
.await
.into_alien_error()
.context(ErrorData::UnexpectedResponseFormat {
provider: "kubernetes-sandbox".to_string(),
binding_name: "sandbox.get".to_string(),
field: "body".to_string(),
response_json: "the broker returned a body this provider cannot parse".to_string(),
})
.map(Lookup::Found)
}
async fn fetch_claim(
client: &reqwest::Client,
url: &str,
token: &str,
) -> Result<Option<ClaimResponse>> {
match lookup_claim(client, url, token).await? {
Lookup::Found(claim) => Ok(Some(claim)),
Lookup::NotFound => Ok(None),
Lookup::Unsupported => Err(AlienError::new(ErrorData::OperationNotSupported {
operation: "sandbox.get".to_string(),
reason: "the sandbox broker predates lookups by id; upgrade the Operator".to_string(),
})),
}
}
const SETUP_WAIT: std::time::Duration = std::time::Duration::from_secs(150);
#[cfg(not(test))]
const SETUP_POLL: std::time::Duration = std::time::Duration::from_millis(500);
#[cfg(test)]
const SETUP_POLL: std::time::Duration = std::time::Duration::from_millis(5);
#[derive(Debug)]
struct SessionAgent {
client: reqwest::Client,
session_url: String,
token_path: String,
binding_name: String,
sandbox_id: String,
claim: tokio::sync::Mutex<ClaimResponse>,
}
#[async_trait]
impl super::interactive::BoundAgent for SessionAgent {
async fn request(
&self,
method: reqwest::Method,
path: &str,
) -> Result<reqwest::RequestBuilder> {
let mut claim = self.claim.lock().await;
if claim.expires_at - chrono::Utc::now().timestamp() <= CAPABILITY_REFRESH_MARGIN_SECONDS {
let token = read_identity_token(&self.token_path, &self.binding_name).await?;
*claim = fetch_claim(&self.client, &self.session_url, &token)
.await?
.ok_or_else(|| {
AlienError::new(ErrorData::OperationNotSupported {
operation: "sandbox.agent".to_string(),
reason: format!(
"sandbox '{}' no longer exists; it ended while a command was running",
self.sandbox_id
),
})
})?;
}
Ok(self
.client
.request(method, format!("{}{path}", claim.endpoint))
.bearer_auth(&claim.capability))
}
fn provider(&self) -> &'static str {
"kubernetes-sandbox"
}
}
fn broker_refusal(status: reqwest::StatusCode, body: &str) -> String {
if status == reqwest::StatusCode::UNAUTHORIZED {
return format!(
"the sandbox broker refused this workload's token ({status}). It accepts the \
ServiceAccount token projected for the audience `alien-sandbox-broker` (mounted at \
the sandbox binding's token path in every workload that links the sandbox) and, \
until the workload is rolled, the default ServiceAccount token of a workload of \
this deployment"
);
}
format!("the sandbox broker returned {status}: {body}")
}
const DEFAULT_TOKEN_PATH: &str = "/var/run/secrets/kubernetes.io/serviceaccount/token";
async fn read_identity_token(token_path: &str, binding_name: &str) -> Result<String> {
match tokio::fs::read_to_string(token_path).await {
Ok(token) => Ok(token),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
tokio::fs::read_to_string(DEFAULT_TOKEN_PATH)
.await
.into_alien_error()
.context(ErrorData::BindingConfigInvalid {
binding_name: binding_name.to_string(),
env_var: alien_core::bindings::binding_env_var_name(binding_name),
reason: format!(
"could not read the ServiceAccount token at '{token_path}' or at \
'{DEFAULT_TOKEN_PATH}'"
),
})
}
Err(error) => Err(error)
.into_alien_error()
.context(ErrorData::BindingConfigInvalid {
binding_name: binding_name.to_string(),
env_var: alien_core::bindings::binding_env_var_name(binding_name),
reason: format!("could not read the ServiceAccount token at '{token_path}'"),
}),
}
}
#[derive(Debug)]
pub struct KubernetesSandbox {
egress_manager: Option<Arc<crate::sandbox_egress::SandboxEgressManager>>,
default_policy: Option<alien_core::EgressPolicy>,
policy_write: tokio::sync::Mutex<()>,
egress: Option<alien_core::SandboxEgress>,
resource_id: String,
broker_url: String,
token_path: String,
binding_name: String,
client: reqwest::Client,
claims: Mutex<BTreeMap<String, ClaimResponse>>,
}
impl KubernetesSandbox {
pub fn new(
binding_name: &str,
binding: &KubernetesSandboxBinding,
resource_id: &str,
) -> Result<Self> {
Self::new_with_egress_manager(binding_name, binding, resource_id, None)
}
pub(crate) fn new_with_egress_manager(
binding_name: &str,
binding: &KubernetesSandboxBinding,
resource_id: &str,
egress_manager: Option<Arc<crate::sandbox_egress::SandboxEgressManager>>,
) -> Result<Self> {
let gateway = binding.egress.as_ref().and_then(|value| value.gateway());
if gateway.is_some() != egress_manager.is_some() {
return Err(super::egress::unsupported("gateway-backed sandbox"));
}
let default_policy = binding.default_policy.clone().or_else(|| {
binding
.egress
.as_ref()
.and_then(|value| value.default_policy().cloned())
});
if egress_manager.is_none() {
super::egress::require_default(
default_policy.as_ref(),
binding.egress.as_ref().map(|value| value.declaration()),
)?;
}
let value = |field: &'static str, value: alien_core::bindings::BindingValue<String>| {
value.into_value(binding_name, field).map_err(|error| {
AlienError::new(ErrorData::BindingConfigInvalid {
binding_name: binding_name.to_string(),
env_var: alien_core::bindings::binding_env_var_name(binding_name),
reason: error.to_string(),
})
})
};
Ok(Self {
egress_manager,
default_policy,
policy_write: tokio::sync::Mutex::new(()),
egress: binding
.egress
.as_ref()
.map(|egress| egress.declaration().clone()),
resource_id: resource_id.to_string(),
broker_url: value("brokerUrl", binding.broker_url.clone())?
.trim_end_matches('/')
.to_string(),
token_path: value("tokenPath", binding.token_path.clone())?,
binding_name: binding_name.to_string(),
client: reqwest::Client::new(),
claims: Mutex::new(BTreeMap::new()),
})
}
async fn identity_token(&self) -> Result<String> {
read_identity_token(&self.token_path, &self.binding_name).await
}
fn cached(&self, sandbox_id: &str) -> Option<ClaimResponse> {
self.claims
.lock()
.expect("no panic holds this lock")
.get(sandbox_id)
.cloned()
}
fn session_url(&self, session_id: &str) -> String {
format!(
"{}/v1/sandbox/{}/sessions/{session_id}",
self.broker_url, self.resource_id
)
}
async fn claimed(&self, sandbox_id: &str) -> Result<Option<ClaimResponse>> {
if let Some(claim) = self.cached(sandbox_id) {
let now = chrono::Utc::now().timestamp();
let capability_current = claim.expires_at - now > CAPABILITY_REFRESH_MARGIN_SECONDS;
let session_running = claim.deadline.is_none_or(|deadline| deadline > now);
if capability_current && session_running {
return Ok(Some(claim));
}
}
self.claimed_by_broker(sandbox_id).await
}
async fn claimed_by_broker(&self, sandbox_id: &str) -> Result<Option<ClaimResponse>> {
let token = self.identity_token().await?;
match lookup_claim(&self.client, &self.session_url(sandbox_id), &token).await? {
Lookup::Found(claim) => Ok(Some(self.remember(claim))),
Lookup::NotFound => {
self.forget(sandbox_id);
Ok(None)
}
Lookup::Unsupported => Err(AlienError::new(ErrorData::OperationNotSupported {
operation: "sandbox.get".to_string(),
reason: "the sandbox broker predates lookups by id; upgrade the Operator"
.to_string(),
})),
}
}
fn remember(&self, claim: ClaimResponse) -> ClaimResponse {
self.claims
.lock()
.expect("no panic holds this lock")
.insert(claim.session_id.clone(), claim.clone());
claim
}
fn forget(&self, sandbox_id: &str) {
self.claims
.lock()
.expect("no panic holds this lock")
.remove(sandbox_id);
}
async fn after_setup(&self, mut claim: ClaimResponse) -> Result<ClaimResponse> {
let started = std::time::Instant::now();
while claim.setup_pending {
if started.elapsed() >= SETUP_WAIT {
return Err(self.failed(
"sandbox.getOrCreate",
&format!(
"sandbox '{}' is still being set up by the process that created it",
claim.session_id
),
));
}
tokio::time::sleep(SETUP_POLL).await;
claim = self
.claimed_by_broker(&claim.session_id)
.await?
.ok_or_else(|| {
self.failed(
"sandbox.getOrCreate",
&format!(
"sandbox '{}' ended while the process that created it was setting it up",
claim.session_id
),
)
})?;
}
Ok(claim)
}
async fn setup_done(&self, session_id: &str) {
let outcome = async {
let response = self
.client
.post(format!("{}/ready", self.session_url(session_id)))
.bearer_auth(self.identity_token().await?)
.send()
.await
.into_alien_error()
.context(ErrorData::OperationNotSupported {
operation: "sandbox.create".to_string(),
reason: "the sandbox broker is unreachable".to_string(),
})?;
Ok::<_, AlienError<ErrorData>>(response.status())
}
.await;
match outcome {
Ok(status)
if status.is_success()
|| status == reqwest::StatusCode::NOT_FOUND
|| status == reqwest::StatusCode::METHOD_NOT_ALLOWED => {}
Ok(status) => {
tracing::warn!(%status, session_id, "the broker did not record the end of setup")
}
Err(error) => {
tracing::warn!(%error, session_id, "the broker did not record the end of setup")
}
}
}
async fn create_resolved(&self, request: CreateSandboxRequest) -> Result<ResolvedSandbox> {
self.validate_egress(request.egress.as_ref())?;
let _policy_write = self.policy_write.lock().await;
let policy = request
.egress
.clone()
.or_else(|| self.default_policy.clone())
.unwrap_or(alien_core::EgressPolicy::Internet);
let sandbox_id = request
.sandbox_id
.unwrap_or_else(|| uuid::Uuid::new_v4().simple().to_string());
let setup = if self.egress_manager.is_some() {
"?setup=pending"
} else {
""
};
let response = self
.client
.post(format!("{}/v1/sandbox/sessions{setup}", self.broker_url))
.bearer_auth(self.identity_token().await?)
.json(&ClaimRequest {
sandbox_id: &self.resource_id,
session_id: &sandbox_id,
tenant_key: request.tenant_key.as_deref(),
timeout_ms: request.timeout_ms,
})
.send()
.await
.into_alien_error()
.context(ErrorData::OperationNotSupported {
operation: "sandbox.create".to_string(),
reason: "the sandbox broker is unreachable".to_string(),
})?;
if !response.status().is_success() {
let status = response.status();
let body = response.text().await.unwrap_or_default();
return Err(self.failed("sandbox.create", &broker_refusal(status, &body)));
}
let claim: ClaimResponse = response.json().await.into_alien_error().context(
ErrorData::UnexpectedResponseFormat {
provider: "kubernetes-sandbox".to_string(),
binding_name: "sandbox.create".to_string(),
field: "body".to_string(),
response_json: "the broker returned a body this provider cannot parse".to_string(),
},
)?;
let claim = self.remember(claim);
if !claim.created {
let claim = self.after_setup(claim).await?;
self.require_same_tenant(&claim, request.tenant_key.as_deref())?;
return Ok(ResolvedSandbox::found(SandboxInstance {
sandbox_id: claim.session_id,
state: SandboxState::Running,
generation: claim.generation,
}));
}
if let Some(manager) = &self.egress_manager {
let installation = async {
let identity = Self::egress_identity(&claim)?;
let session_ends_at = match claim.deadline {
Some(deadline) => deadline,
None => crate::sandbox_egress::now_unix()? + GATEWAY_IDENTITY_FALLBACK_SECONDS,
};
let delivery = manager.create(identity, &policy, session_ends_at).await?;
self.deliver_egress(&claim.session_id, Some(&delivery.token), &delivery.policy)
.await
}
.await;
if let Err(error) = installation {
if let Err(cleanup) = self.terminate(&claim.session_id).await {
tracing::warn!(
sandbox_id = %claim.session_id,
error = %cleanup,
"the claimed pod could not be released after its initial egress policy failed"
);
}
return Err(self.explain_denied_signing_key(&claim.session_id, error));
}
}
if self.egress_manager.is_some() {
self.setup_done(&claim.session_id).await;
}
Ok(ResolvedSandbox::created(SandboxInstance {
sandbox_id: claim.session_id,
state: SandboxState::Running,
generation: claim.generation,
}))
}
fn require_same_tenant(&self, claim: &ClaimResponse, requested: Option<&str>) -> Result<()> {
if claim.tenant_key.as_deref() == requested {
return Ok(());
}
Err(self.failed(
"sandbox.getOrCreate",
&format!(
"sandbox '{}' exists with a different tenant key than the request names",
claim.session_id
),
))
}
fn explain_denied_signing_key(
&self,
session_id: &str,
error: AlienError<ErrorData>,
) -> AlienError<ErrorData> {
fn denied(code: &str, source: Option<&AlienError>) -> bool {
code == "REMOTE_ACCESS_DENIED"
|| source.is_some_and(|s| denied(&s.code, s.source.as_deref()))
}
if denied(&error.code, error.source.as_deref()) {
return self.failed(
"sandbox.create",
&format!(
"creating a session for sandbox {session_id} needs sandbox/management in the permission profile of this workload, from the first release: a permission profile cannot be changed by a later release once the stack is deployed. It was denied reading the deployment's signing key ({})",
error
),
);
}
error
}
fn failed(&self, operation: &str, reason: &str) -> AlienError<ErrorData> {
AlienError::new(ErrorData::OperationNotSupported {
operation: operation.to_string(),
reason: reason.to_string(),
})
}
}
#[async_trait]
impl AgentTransport for KubernetesSandbox {
async fn request(
&self,
sandbox_id: &str,
method: reqwest::Method,
path: &str,
) -> Result<reqwest::RequestBuilder> {
let claim = self.claimed(sandbox_id).await?.ok_or_else(|| {
self.failed(
"sandbox.agent",
&format!("sandbox '{sandbox_id}' does not exist; it may have been terminated"),
)
})?;
Ok(self
.client
.request(method, format!("{}{path}", claim.endpoint))
.bearer_auth(claim.capability))
}
fn provider(&self) -> &'static str {
"kubernetes-sandbox"
}
}
impl Binding for KubernetesSandbox {}
#[async_trait]
impl Sandbox for KubernetesSandbox {
fn egress_declaration(&self) -> Option<&alien_core::SandboxEgress> {
self.egress.as_ref()
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn capabilities(&self) -> SandboxCapabilities {
SandboxCapabilities::for_platform(Platform::Kubernetes)
.expect("Kubernetes has a sandbox backend")
}
async fn create(&self, request: CreateSandboxRequest) -> Result<SandboxInstance> {
self.create_resolved(request)
.await
.map(|resolved| resolved.sandbox)
}
async fn get(&self, sandbox_id: &str) -> Result<Option<SandboxInstance>> {
let Some(claim) = self.claimed_by_broker(sandbox_id).await? else {
return Ok(None);
};
let claim = self.after_setup(claim).await?;
Ok(Some(SandboxInstance {
sandbox_id: claim.session_id,
state: SandboxState::Running,
generation: claim.generation,
}))
}
async fn get_or_create(&self, request: CreateSandboxRequest) -> Result<ResolvedSandbox> {
self.validate_egress(request.egress.as_ref())?;
if let Some(id) = request.sandbox_id.as_deref() {
let token = self.identity_token().await?;
match lookup_claim(&self.client, &self.session_url(id), &token).await? {
Lookup::Found(claim) => {
let claim = self.remember(claim);
let claim = self.after_setup(claim).await?;
self.require_same_tenant(&claim, request.tenant_key.as_deref())?;
if let Some(policy) = request.egress.clone() {
self.update_egress(id, policy).await?;
}
return Ok(ResolvedSandbox::found(SandboxInstance {
sandbox_id: claim.session_id,
state: SandboxState::Running,
generation: claim.generation,
}));
}
Lookup::NotFound => self.forget(id),
Lookup::Unsupported => {
if request.tenant_key.is_some() {
return Err(self.failed(
"sandbox.getOrCreate",
"the sandbox broker predates lookups by id and cannot honour a tenantKey; upgrade the Operator",
));
}
}
}
}
self.create_resolved(request).await
}
async fn update_egress(
&self,
sandbox_id: &str,
policy: alien_core::EgressPolicy,
) -> Result<()> {
self.validate_egress(Some(&policy))?;
let _policy_write = self.policy_write.lock().await;
let manager = self
.egress_manager
.as_ref()
.ok_or_else(|| super::egress::unsupported("runtime policy"))?;
let claim = self
.claimed(sandbox_id)
.await?
.ok_or_else(|| self.failed("sandbox.updateEgress", "the sandbox does not exist"))?;
let identity = Self::egress_identity(&claim)?;
manager.update(identity, &policy).await?;
let record = manager.policy_record(identity).await?;
self.deliver_egress(sandbox_id, None, &record).await
}
async fn list(&self) -> Result<Vec<SandboxInstance>> {
let response = self
.client
.get(format!(
"{}/v1/sandbox/{}/sessions",
self.broker_url, self.resource_id
))
.bearer_auth(self.identity_token().await?)
.send()
.await
.into_alien_error()
.context(ErrorData::OperationNotSupported {
operation: "sandbox.list".to_string(),
reason: "the sandbox broker is unreachable".to_string(),
})?;
if !response.status().is_success() {
let status = response.status();
let body = response.text().await.unwrap_or_default();
return Err(self.failed("sandbox.list", &broker_refusal(status, &body)));
}
let list: SessionList = response.json().await.into_alien_error().context(
ErrorData::UnexpectedResponseFormat {
provider: "kubernetes-sandbox".to_string(),
binding_name: "sandbox.list".to_string(),
field: "body".to_string(),
response_json: "the broker returned a body this provider cannot parse".to_string(),
},
)?;
Ok(list
.session_ids
.into_iter()
.map(|sandbox_id| SandboxInstance {
sandbox_id,
state: SandboxState::Running,
generation: 1,
})
.collect())
}
async fn run_command(
&self,
sandbox_id: &str,
request: RunCommandRequest,
) -> Result<BoxStream<'static, Result<CommandOutput>>> {
agent_protocol::run_command(self, sandbox_id, request).await
}
async fn run_interactive(
&self,
sandbox_id: &str,
request: RunCommandRequest,
) -> Result<InteractiveCommand> {
let claim = self.claimed(sandbox_id).await?.ok_or_else(|| {
self.failed(
"sandbox.agent",
&format!("sandbox '{sandbox_id}' does not exist; it may have been terminated"),
)
})?;
let agent = Arc::new(SessionAgent {
client: self.client.clone(),
session_url: self.session_url(&claim.session_id),
token_path: self.token_path.clone(),
binding_name: self.binding_name.clone(),
sandbox_id: claim.session_id.clone(),
claim: tokio::sync::Mutex::new(claim),
});
super::interactive::run_interactive(agent, request).await
}
async fn start_job(&self, sandbox_id: &str, request: RunCommandRequest) -> Result<JobStart> {
agent_protocol::start_job(self, sandbox_id, request).await
}
async fn poll_job(
&self,
sandbox_id: &str,
job_id: &str,
since_seq: Option<u64>,
) -> Result<JobPoll> {
agent_protocol::poll_job(self, sandbox_id, job_id, since_seq).await
}
async fn cancel_job(&self, sandbox_id: &str, job_id: &str) -> Result<()> {
agent_protocol::cancel_job(self, sandbox_id, job_id).await
}
async fn read_file(&self, sandbox_id: &str, path: &str) -> Result<Vec<u8>> {
agent_protocol::read_file(self, sandbox_id, path).await
}
async fn write_files(&self, sandbox_id: &str, files: BTreeMap<String, Vec<u8>>) -> Result<()> {
agent_protocol::write_files(self, sandbox_id, files).await
}
async fn preview(&self, sandbox_id: &str, port: u16) -> Result<PreviewCapability> {
let claim = self.claimed(sandbox_id).await?.ok_or_else(|| {
self.failed(
"sandbox.preview",
&format!("sandbox '{sandbox_id}' does not exist; it may have been terminated"),
)
})?;
let response = self
.client
.post(format!("{}/preview", self.session_url(&claim.session_id)))
.bearer_auth(self.identity_token().await?)
.json(&serde_json::json!({ "port": port }))
.send()
.await
.into_alien_error()
.context(ErrorData::OperationNotSupported {
operation: "sandbox.preview".to_string(),
reason: "the sandbox broker is unreachable".to_string(),
})?;
if !response.status().is_success() {
let status = response.status();
let reason = if status == reqwest::StatusCode::FORBIDDEN {
format!("port {port} is not one of this sandbox's declared preview ports")
} else {
broker_refusal(status, "")
};
return Err(self.failed("sandbox.preview", &reason));
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct Grant {
endpoint: String,
capability: String,
expires_at: i64,
}
let grant: Grant =
response
.json()
.await
.into_alien_error()
.context(ErrorData::OperationNotSupported {
operation: "sandbox.preview".to_string(),
reason: "the sandbox broker's answer was unreadable".to_string(),
})?;
Ok(PreviewCapability {
endpoint: grant.endpoint,
headers: preview_headers(grant.capability, port),
allowed_ports: vec![port],
expires_in_seconds: u64::try_from(grant.expires_at - chrono::Utc::now().timestamp())
.unwrap_or(0),
caller_authorization_header: "Authorization".to_string(),
subprotocols: Vec::new(),
})
}
async fn pause(&self, _sandbox_id: &str) -> Result<()> {
Err(self.failed("pauseResume", "a pod cannot be paused and resumed"))
}
async fn resume(&self, sandbox_id: &str) -> Result<()> {
match self.claimed(sandbox_id).await? {
Some(_) => Ok(()),
None => Err(AlienError::new(ErrorData::SandboxCommandFailed {
failure: "sandboxTerminated".to_string(),
reason: format!("sandbox.resume: no sandbox '{sandbox_id}' is running"),
})),
}
}
async fn snapshot(&self, _sandbox_id: &str) -> Result<String> {
Err(self.failed("snapshot", "a pod has no snapshot primitive"))
}
async fn terminate(&self, sandbox_id: &str) -> Result<()> {
let Some(claim) = self.claimed(sandbox_id).await? else {
return Ok(());
};
let response = self
.client
.delete(self.session_url(&claim.session_id))
.bearer_auth(self.identity_token().await?)
.send()
.await
.into_alien_error()
.context(ErrorData::OperationNotSupported {
operation: "sandbox.terminate".to_string(),
reason: "the sandbox broker is unreachable".to_string(),
})?;
if !response.status().is_success() {
let status = response.status();
let body = response.text().await.unwrap_or_default();
return Err(self.failed("sandbox.terminate", &broker_refusal(status, &body)));
}
if let Some(manager) = &self.egress_manager {
if let Some(identity) = claim.agent_session_id.as_deref() {
manager.delete(identity).await?;
}
}
self.claims
.lock()
.expect("no panic holds this lock")
.remove(sandbox_id);
Ok(())
}
}
fn preview_headers(capability: String, port: u16) -> BTreeMap<String, String> {
BTreeMap::from([
(
alien_core::sandbox_image::PREVIEW_CAPABILITY_HEADER.to_string(),
capability,
),
(
alien_core::sandbox_image::PREVIEW_PORT_HEADER.to_string(),
port.to_string(),
),
])
}
#[cfg(test)]
mod tests {
#[test]
fn preview_headers_never_use_authorization() {
let headers = super::preview_headers("cap".to_string(), 8081);
assert!(!headers
.keys()
.any(|name| name.eq_ignore_ascii_case("authorization")));
assert_eq!(headers["x-alien-preview-capability"], "cap");
assert_eq!(headers["x-alien-preview-port"], "8081");
}
#[test]
fn a_claim_omits_the_fields_the_caller_did_not_set() {
let body = serde_json::to_value(ClaimRequest {
sandbox_id: "sbx",
session_id: "s1",
tenant_key: None,
timeout_ms: None,
})
.unwrap();
assert_eq!(
body,
serde_json::json!({"sandboxId": "sbx", "sessionId": "s1"})
);
let body = serde_json::to_value(ClaimRequest {
sandbox_id: "sbx",
session_id: "s1",
tenant_key: Some("t"),
timeout_ms: Some(5),
})
.unwrap();
assert_eq!(
body["tenantKey"], "t",
"a tenant the caller set is never dropped"
);
assert_eq!(body["timeoutMs"], 5);
}
#[tokio::test]
async fn the_token_falls_back_to_the_default_mount_when_the_projected_file_is_missing() {
let error = read_identity_token("/nonexistent/alien/token", "sbx")
.await
.expect_err("neither file exists in the test environment");
let message = format!("{error:?}");
assert!(message.contains(DEFAULT_TOKEN_PATH), "{message}");
}
use super::*;
#[test]
fn a_refused_token_names_the_audience_and_the_accepted_tokens() {
let message = broker_refusal(reqwest::StatusCode::UNAUTHORIZED, "");
assert!(message.contains("alien-sandbox-broker"), "{message}");
assert!(
message.contains("default ServiceAccount token"),
"{message}"
);
assert_eq!(
broker_refusal(reqwest::StatusCode::NOT_FOUND, "no such pod"),
"the sandbox broker returned 404 Not Found: no such pod"
);
}
use alien_core::bindings::BindingValue;
use axum::extract::{Path, Request};
use axum::http::StatusCode;
use axum::routing::{delete, post};
use axum::{Json, Router};
use std::net::SocketAddr;
use std::sync::Arc;
const BROKER_RELEASE_ROUTE: &str = "/v1/sandbox/{sandbox}/sessions/{session}";
async fn serve(router: Router) -> String {
let listener = tokio::net::TcpListener::bind::<SocketAddr>("127.0.0.1:0".parse().unwrap())
.await
.expect("bind");
let address = listener.local_addr().expect("address");
tokio::spawn(async move {
axum::serve(listener, router).await.expect("serve");
});
format!("http://{address}")
}
fn sandbox_over(broker: String, token: &tempfile::NamedTempFile) -> KubernetesSandbox {
std::fs::write(token.path(), "service-account-token").expect("the token is written");
KubernetesSandbox::new(
"sbx",
&KubernetesSandboxBinding {
default_policy: None,
egress: None,
namespace: BindingValue::Value("alien".to_string()),
runtime_class: BindingValue::Value("gvisor".to_string()),
selector: BindingValue::Value("alien/sandbox=sbx-pool".to_string()),
broker_url: BindingValue::Value(broker),
key_name: BindingValue::Value("sandbox-key".to_string()),
token_path: BindingValue::Value(token.path().display().to_string()),
},
"sbx-pool",
)
.expect("the binding is complete")
}
#[derive(Debug)]
struct DeniedVault;
impl crate::traits::Binding for DeniedVault {}
#[async_trait]
impl crate::traits::Vault for DeniedVault {
async fn get_secret(&self, _name: &str) -> Result<String> {
use alien_error::ContextError;
Err(
AlienError::new(alien_client_core::ErrorData::RemoteAccessDenied {
resource_type: "Secret".to_string(),
resource_name: "signing-key".to_string(),
})
.context(ErrorData::CloudPlatformError {
message: "Failed to get secret 'signing-key'".to_string(),
resource_id: None,
}),
)
}
async fn secret_presence(&self, _: &str) -> Result<crate::traits::SecretPresence> {
unimplemented!()
}
async fn set_secret(&self, _: &str, _: &str) -> Result<()> {
unimplemented!()
}
async fn delete_secret(&self, _: &str) -> Result<()> {
unimplemented!()
}
async fn list_secrets(&self) -> Result<Vec<String>> {
unimplemented!()
}
}
#[tokio::test]
async fn a_denied_signing_key_read_names_the_missing_permission() {
let released = Arc::new(std::sync::atomic::AtomicUsize::new(0));
let expires = chrono::Utc::now().timestamp() + 900;
let broker = serve(
Router::new()
.route(
BROKER_RELEASE_ROUTE,
delete({
let released = released.clone();
move || async move {
released.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
StatusCode::INTERNAL_SERVER_ERROR
}
}),
)
.route(
"/v1/sandbox/sessions",
post(move |Json(body): Json<serde_json::Value>| async move {
Json(serde_json::json!({
"agentSessionId": "uid-2", "sessionId": body["sessionId"],
"endpoint": "http://10.0.0.2:8080", "capability": "c",
"expiresAt": expires, "generation": 1, "created": true,
}))
}),
),
)
.await;
let token = tempfile::NamedTempFile::new().expect("a token file");
std::fs::write(token.path(), "service-account-token").unwrap();
let gateway = alien_core::SandboxEgressGateway {
container: "gateway".to_string(),
audience: "prefix/sbx".to_string(),
signing_key: alien_core::bindings::SandboxEgressSigningKey {
vault: "secrets".to_string(),
key: "signing-key".to_string(),
},
kid: "key-1".to_string(),
admin_ca_pem: None,
replicas: None,
};
let manager = crate::sandbox_egress::SandboxEgressManager::new(
gateway.clone(),
Arc::new(DeniedVault),
"http://127.0.0.1:3128",
"http://127.0.0.1:3129",
)
.expect("a manager");
let sandbox = KubernetesSandbox::new_with_egress_manager(
"sbx",
&KubernetesSandboxBinding {
default_policy: None,
egress: Some(alien_core::bindings::SandboxEgressBinding::Gateway(gateway)),
namespace: BindingValue::Value("alien".to_string()),
runtime_class: BindingValue::Value("gvisor".to_string()),
selector: BindingValue::Value("alien/sandbox=sbx-pool".to_string()),
broker_url: BindingValue::Value(broker),
key_name: BindingValue::Value("sandbox-key".to_string()),
token_path: BindingValue::Value(token.path().display().to_string()),
},
"sbx-pool",
Some(Arc::new(manager)),
)
.expect("the binding is complete");
let error = sandbox
.get_or_create(CreateSandboxRequest {
sandbox_id: Some("s1".to_string()),
..Default::default()
})
.await
.expect_err("the signing key cannot be read");
let message = format!("{error}");
assert!(
message.contains("creating a session for sandbox s1 needs sandbox/management in the permission profile"),
"{message}"
);
assert!(
message.contains("from the first release")
&& message.contains("cannot be changed by a later release"),
"the message says the permission must be there from the first release: {message}"
);
assert_eq!(
released.load(std::sync::atomic::Ordering::SeqCst),
1,
"the claimed pod's release route is called once, and its failure does not hide the denial"
);
}
#[tokio::test]
async fn resume_is_a_no_op_for_a_running_sandbox_and_refused_for_an_unknown_one() {
let broker = serve(Router::new().route(
BROKER_RELEASE_ROUTE,
axum::routing::get(|Path((_, session)): Path<(String, String)>| async move {
if session == "running" {
Ok(Json(serde_json::json!({
"sessionId": "running",
"endpoint": "http://10.0.0.1:8080",
"capability": "cap",
"expiresAt": chrono::Utc::now().timestamp() + 300,
})))
} else {
Err(StatusCode::NOT_FOUND)
}
}),
))
.await;
let token = tempfile::NamedTempFile::new().expect("a token file");
let sandbox = sandbox_over(broker, &token);
sandbox.resume("running").await.expect("a no-op");
let error = sandbox.resume("gone").await.expect_err("nothing to resume");
assert_eq!(error.code, "SANDBOX_COMMAND_FAILED", "{error}");
}
#[tokio::test]
async fn terminate_releases_at_the_route_the_broker_serves() {
let released = Arc::new(Mutex::new(Vec::new()));
let unrouted = Arc::new(Mutex::new(Vec::new()));
let released_seen = Arc::clone(&released);
let unrouted_seen = Arc::clone(&unrouted);
let broker = serve(
Router::new()
.route(
"/v1/sandbox/sessions",
post(|| async {
Json(serde_json::json!({
"sessionId": "s1",
"endpoint": "http://10.0.0.1:8080",
"capability": "cap",
"expiresAt": chrono::Utc::now().timestamp() + 300,
}))
}),
)
.route(
BROKER_RELEASE_ROUTE,
axum::routing::get(|| async { StatusCode::NOT_FOUND }).delete(
move |Path(addressed): Path<(String, String)>| {
let released = Arc::clone(&released);
async move {
released
.lock()
.expect("no panic holds this lock")
.push(addressed);
StatusCode::NO_CONTENT
}
},
),
)
.fallback(move |request: Request| {
let unrouted = Arc::clone(&unrouted);
async move {
unrouted
.lock()
.expect("no panic holds this lock")
.push(format!("{} {}", request.method(), request.uri().path()));
StatusCode::NOT_FOUND
}
}),
)
.await;
let token = tempfile::NamedTempFile::new().expect("a token file");
let sandbox = sandbox_over(broker, &token);
let claimed = sandbox
.create(CreateSandboxRequest {
sandbox_id: Some("s1".to_string()),
tenant_key: None,
env: BTreeMap::new(),
..Default::default()
})
.await
.expect("the broker claims a pod");
let released_claim = sandbox.terminate(&claimed.sandbox_id).await;
assert!(
unrouted_seen
.lock()
.expect("no panic holds this lock")
.is_empty(),
"terminate reached a path the broker does not route: {:?}",
unrouted_seen.lock().expect("no panic holds this lock")
);
released_claim.expect("the broker releases the claim");
assert_eq!(
*released_seen.lock().expect("no panic holds this lock"),
vec![("sbx-pool".to_string(), "s1".to_string())],
"the release names the resource and then the session"
);
assert!(
sandbox
.get(&claimed.sandbox_id)
.await
.expect("a released sandbox reads back")
.is_none(),
"a released sandbox is no longer claimed"
);
}
#[tokio::test]
async fn another_process_reattaches_by_id_and_capabilities_are_refreshed() {
use axum::routing::get;
let lookups = Arc::new(Mutex::new(0_u32));
let counted = Arc::clone(&lookups);
let broker = serve(Router::new().route(
BROKER_RELEASE_ROUTE,
get(move |Path((_, session)): Path<(String, String)>| {
let counted = Arc::clone(&counted);
async move {
*counted.lock().expect("no panic holds this lock") += 1;
if session == "known" {
Ok(Json(serde_json::json!({
"agentSessionId": "uid-1",
"sessionId": "known",
"endpoint": "http://10.0.0.1:8080",
"capability": "fresh",
"expiresAt": chrono::Utc::now().timestamp() + 900,
})))
} else {
Err(StatusCode::NOT_FOUND)
}
}
}),
))
.await;
let token = tempfile::NamedTempFile::new().expect("a token file");
let sandbox = sandbox_over(broker, &token);
let found = sandbox
.get("known")
.await
.expect("lookup works")
.expect("a process that never claimed it still finds it");
assert_eq!(found.sandbox_id, "known");
assert!(sandbox
.get("probe-id")
.await
.expect("an unknown id is not an error")
.is_none());
sandbox.get("known").await.unwrap().unwrap();
assert_eq!(*lookups.lock().unwrap(), 3, "one lookup per call");
}
#[tokio::test]
async fn an_agent_request_refreshes_a_capability_near_its_expiry() {
use axum::routing::get;
let lookups = Arc::new(Mutex::new(0_u32));
let counted = Arc::clone(&lookups);
let broker = serve(Router::new().route(
BROKER_RELEASE_ROUTE,
get(move |Path((_, session)): Path<(String, String)>| {
let counted = Arc::clone(&counted);
async move {
*counted.lock().expect("no panic holds this lock") += 1;
Json(session_body(&session, false, None))
}
}),
))
.await;
let token = tempfile::NamedTempFile::new().expect("a token file");
let sandbox = sandbox_over(broker, &token);
sandbox.remember(serde_json::from_value(session_body("s1", false, None)).unwrap());
let _ = sandbox
.request("s1", reqwest::Method::GET, "/v1/health")
.await
.expect("a request is built");
assert_eq!(
*lookups.lock().unwrap(),
0,
"served from the held capability"
);
sandbox
.claims
.lock()
.unwrap()
.get_mut("s1")
.unwrap()
.expires_at = chrono::Utc::now().timestamp() + 10;
let _ = sandbox
.request("s1", reqwest::Method::GET, "/v1/health")
.await
.expect("a request is built");
assert_eq!(*lookups.lock().unwrap(), 1, "refreshed before it expired");
}
fn session_body(session: &str, pending: bool, deadline: Option<i64>) -> serde_json::Value {
let mut body = serde_json::json!({
"agentSessionId": "uid-1", "sessionId": session,
"endpoint": "http://10.0.0.1:8080", "capability": "c",
"expiresAt": chrono::Utc::now().timestamp() + 900, "created": false,
});
if pending {
body["setupPending"] = true.into();
}
if let Some(deadline) = deadline {
body["deadline"] = deadline.into();
}
body
}
#[tokio::test]
async fn a_session_still_being_set_up_by_its_creator_is_waited_for() {
use axum::routing::get;
let lookups = Arc::new(Mutex::new(0));
let seen = Arc::clone(&lookups);
let broker = serve(Router::new().route(
BROKER_RELEASE_ROUTE,
get(move |Path((_, session)): Path<(String, String)>| {
let seen = Arc::clone(&seen);
async move {
let mut lookups = seen.lock().unwrap();
*lookups += 1;
match (session.as_str(), *lookups) {
("ready-later", 1 | 2) => Ok(Json(session_body(&session, true, None))),
("ready-later", _) => Ok(Json(session_body(&session, false, None))),
(_, 1) => Ok(Json(session_body(&session, true, None))),
_ => Err(StatusCode::NOT_FOUND),
}
}
}),
))
.await;
let token = tempfile::NamedTempFile::new().unwrap();
let sandbox = sandbox_over(broker, &token);
let found = sandbox
.get_or_create(CreateSandboxRequest {
sandbox_id: Some("ready-later".to_string()),
..Default::default()
})
.await
.expect("found once the creator reports");
assert!(!found.created);
assert_eq!(
*lookups.lock().unwrap(),
3,
"it waited for two pending answers"
);
*lookups.lock().unwrap() = 0;
let error = sandbox
.get_or_create(CreateSandboxRequest {
sandbox_id: Some("failed".to_string()),
..Default::default()
})
.await
.expect_err("the creator's setup failed");
assert!(error.to_string().contains("ended while"), "{error}");
}
#[tokio::test]
async fn get_or_create_asks_the_broker_even_when_the_cached_capability_is_unexpired() {
use axum::routing::get;
let gone = Arc::new(Mutex::new(false));
let flag = Arc::clone(&gone);
let broker = serve(
Router::new()
.route(
BROKER_RELEASE_ROUTE,
get(move |Path((_, session)): Path<(String, String)>| {
let flag = Arc::clone(&flag);
async move {
if *flag.lock().unwrap() {
Err(StatusCode::NOT_FOUND)
} else {
Ok(Json(session_body(&session, false, None)))
}
}
}),
)
.route(
"/v1/sandbox/sessions",
axum::routing::post(|| async {
let mut body = session_body("s1", false, None);
body["created"] = true.into();
Json(body)
}),
),
)
.await;
let token = tempfile::NamedTempFile::new().unwrap();
let sandbox = sandbox_over(broker, &token);
let request = || CreateSandboxRequest {
sandbox_id: Some("s1".to_string()),
..Default::default()
};
assert!(!sandbox.get_or_create(request()).await.unwrap().created);
*gone.lock().unwrap() = true;
assert!(
sandbox.get_or_create(request()).await.unwrap().created,
"the session ended; a replacement is created"
);
sandbox.remember(serde_json::from_value(session_body("s1", false, None)).unwrap());
assert!(sandbox.get("s1").await.unwrap().is_none());
}
#[tokio::test]
async fn get_waits_for_a_session_that_is_still_being_set_up() {
use axum::routing::get;
let lookups = Arc::new(Mutex::new(0));
let seen = Arc::clone(&lookups);
let broker = serve(Router::new().route(
BROKER_RELEASE_ROUTE,
get(move |Path((_, session)): Path<(String, String)>| {
let seen = Arc::clone(&seen);
async move {
let mut lookups = seen.lock().unwrap();
*lookups += 1;
Json(session_body(&session, *lookups <= 2, None))
}
}),
))
.await;
let token = tempfile::NamedTempFile::new().unwrap();
let sandbox = sandbox_over(broker, &token);
sandbox.get("s1").await.unwrap().expect("found after setup");
assert_eq!(*lookups.lock().unwrap(), 3);
}
#[tokio::test]
async fn get_or_create_falls_back_to_create_on_an_operator_without_lookups() {
use axum::routing::post;
let posts = Arc::new(Mutex::new(Vec::new()));
let seen = Arc::clone(&posts);
let broker = serve(
Router::new()
.route(
BROKER_RELEASE_ROUTE,
axum::routing::delete(|| async { StatusCode::NO_CONTENT }),
)
.route(
"/v1/sandbox/sessions",
post(move |Json(body): Json<serde_json::Value>| {
let seen = Arc::clone(&seen);
async move {
seen.lock().unwrap().push(body.clone());
let mut reply =
session_body(body["sessionId"].as_str().unwrap(), false, None);
reply["created"] = true.into();
Json(reply)
}
}),
),
)
.await;
let token = tempfile::NamedTempFile::new().unwrap();
let sandbox = sandbox_over(broker, &token);
let created = sandbox
.get_or_create(CreateSandboxRequest {
sandbox_id: Some("s1".to_string()),
..Default::default()
})
.await
.expect("falls back to the old create");
assert!(created.created);
assert_eq!(posts.lock().unwrap().len(), 1);
let refused = sandbox
.get_or_create(CreateSandboxRequest {
sandbox_id: Some("s2".to_string()),
tenant_key: Some("tenant".to_string()),
..Default::default()
})
.await
.expect_err("a tenant key cannot be honoured");
assert!(refused.to_string().contains("tenantKey"), "{refused}");
assert_eq!(posts.lock().unwrap().len(), 1, "nothing was created for it");
}
#[tokio::test]
async fn get_or_create_reports_whether_this_call_created_the_session() {
use axum::routing::{get, post};
let expires = chrono::Utc::now().timestamp() + 900;
let broker = serve(
Router::new()
.route(
BROKER_RELEASE_ROUTE,
get(
move |Path((_, session)): Path<(String, String)>| async move {
if session == "known" {
Ok(Json(serde_json::json!({
"agentSessionId": "uid-1", "sessionId": "known",
"endpoint": "http://10.0.0.1:8080", "capability": "c",
"expiresAt": expires, "generation": 3, "created": false,
})))
} else {
Err(StatusCode::NOT_FOUND)
}
},
),
)
.route(
"/v1/sandbox/sessions",
post(move |Json(body): Json<serde_json::Value>| async move {
let id = body["sessionId"].as_str().unwrap().to_string();
let created = id != "raced";
Json(serde_json::json!({
"agentSessionId": "uid-2", "sessionId": id,
"endpoint": "http://10.0.0.2:8080", "capability": "c",
"expiresAt": expires, "generation": 1, "created": created,
}))
}),
),
)
.await;
let token = tempfile::NamedTempFile::new().expect("a token file");
std::fs::write(token.path(), "service-account-token").unwrap();
let sandbox = sandbox_over(broker, &token);
let request = |id: &str| CreateSandboxRequest {
sandbox_id: Some(id.to_string()),
..Default::default()
};
let known = sandbox
.get_or_create(request("known"))
.await
.expect("found");
assert!(!known.created);
assert_eq!(
known.sandbox.generation, 3,
"the pod's generation, not a constant"
);
let fresh = sandbox
.get_or_create(request("fresh"))
.await
.expect("created");
assert!(fresh.created);
let raced = sandbox
.get_or_create(request("raced"))
.await
.expect("found");
assert!(!raced.created, "losing the create race is a find");
}
#[tokio::test]
async fn get_or_create_refuses_an_existing_session_with_another_tenant_key() {
use axum::routing::{get, post};
let expires = chrono::Utc::now().timestamp() + 900;
let session = move |id: &str, key: Option<&str>, created: bool| {
let mut body = serde_json::json!({
"agentSessionId": "uid-1", "sessionId": id,
"endpoint": "http://10.0.0.1:8080", "capability": "c",
"expiresAt": expires, "generation": 1, "created": created,
});
if let Some(key) = key {
body["tenantKey"] = serde_json::json!(key);
}
body
};
let broker = serve(
Router::new()
.route(
BROKER_RELEASE_ROUTE,
get(move |Path((_, id)): Path<(String, String)>| async move {
match id.as_str() {
"keyed" => Ok(Json(session("keyed", Some("a"), false))),
"open" => Ok(Json(session("open", None, false))),
_ => Err(StatusCode::NOT_FOUND),
}
}),
)
.route(
"/v1/sandbox/sessions",
post(move |Json(body): Json<serde_json::Value>| async move {
let id = body["sessionId"].as_str().unwrap().to_string();
Json(session(&id, Some("a"), false))
}),
),
)
.await;
let token = tempfile::NamedTempFile::new().expect("a token file");
std::fs::write(token.path(), "service-account-token").unwrap();
let sandbox = sandbox_over(broker, &token);
let request = |id: &str, tenant: Option<&str>| CreateSandboxRequest {
sandbox_id: Some(id.to_string()),
tenant_key: tenant.map(str::to_string),
..Default::default()
};
for (id, tenant) in [
("keyed", Some("b")),
("keyed", None),
("open", Some("b")),
("raced", Some("b")),
("raced", None),
] {
let error = sandbox
.get_or_create(request(id, tenant))
.await
.expect_err(&format!("{id} with {tenant:?} must be refused"));
assert!(
error.to_string().contains("different tenant key"),
"{error}"
);
}
assert!(sandbox
.get_or_create(request("keyed", Some("a")))
.await
.is_ok());
assert!(sandbox.get_or_create(request("open", None)).await.is_ok());
assert!(sandbox
.get_or_create(request("raced", Some("a")))
.await
.is_ok());
}
#[tokio::test]
async fn an_interactive_command_refreshes_its_capability_before_it_expires() {
use super::super::interactive::BoundAgent;
let broker = serve(Router::new().route(
BROKER_RELEASE_ROUTE,
axum::routing::get(|| async {
Json(serde_json::json!({
"agentSessionId": "uid-1",
"sessionId": "s1",
"endpoint": "http://10.0.0.9:8080",
"capability": "second",
"expiresAt": chrono::Utc::now().timestamp() + 900,
}))
}),
))
.await;
let token = tempfile::NamedTempFile::new().expect("a token file");
std::fs::write(token.path(), "service-account-token").unwrap();
let first = ClaimResponse {
agent_session_id: None,
session_id: "s1".to_string(),
endpoint: "http://10.0.0.1:8080".to_string(),
capability: "first".to_string(),
expires_at: chrono::Utc::now().timestamp() + 20,
generation: 1,
created: false,
tenant_key: None,
deadline: None,
setup_pending: false,
};
let agent = SessionAgent {
client: reqwest::Client::new(),
session_url: format!("{broker}/v1/sandbox/sbx-pool/sessions/s1"),
token_path: token.path().display().to_string(),
binding_name: "sbx".to_string(),
sandbox_id: "s1".to_string(),
claim: tokio::sync::Mutex::new(first),
};
let request = agent
.request(reqwest::Method::POST, "/v1/interactive/input")
.await
.expect("a request is built")
.build()
.unwrap();
assert_eq!(
request.url().as_str(),
"http://10.0.0.9:8080/v1/interactive/input"
);
assert_eq!(
request.headers()["authorization"].to_str().unwrap(),
"Bearer second"
);
}
}