Skip to content

Commit 097e5ba

Browse files
letv1nnnjohntmyers
authored andcommitted
fix(docker): reclaim sandbox token files on out-of-band removal
Signed-off-by: Artem Lytvyn <[email protected]>
1 parent c6c8573 commit 097e5ba

4 files changed

Lines changed: 104 additions & 1 deletion

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/openshell-driver-docker/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ opentelemetry = { workspace = true }
4545
opentelemetry_sdk = { workspace = true, features = ["testing"] }
4646
prost-types = { workspace = true }
4747
tar = "0.4"
48-
temp-env = "0.3"
48+
temp-env = { version = "0.3", features = ["async_closure"] }
4949
tempfile = "3"
5050
tracing-subscriber = { workspace = true }
5151

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,11 @@ impl DockerComputeDriver {
11041104
}
11051105
}
11061106
}
1107+
// Container gone and no in-memory record survived (gateway
1108+
// restarted after an out-of-band `docker rm`). DeleteSandbox is
1109+
// the only thing that ever reclaims the token file, so reclaim it
1110+
// here too.
1111+
cleanup_sandbox_token_file_for_delete(sandbox_id, None, &self.config);
11071112
return Ok(false);
11081113
};
11091114
let Some(target) = summary_container_target(&container) else {

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

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,29 @@ fn request_with_traceparent<T>(message: T) -> Request<T> {
209209
request
210210
}
211211

212+
async fn fake_docker_with_no_containers() -> (String, JoinHandle<()>) {
213+
use tokio::io::{AsyncReadExt, AsyncWriteExt};
214+
215+
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
216+
let address = listener.local_addr().unwrap();
217+
let server = tokio::spawn(async move {
218+
while let Ok((mut stream, _)) = listener.accept().await {
219+
openshell_core::net::set_tcp_nodelay_best_effort(&stream);
220+
let mut scratch = [0_u8; 4096_usize];
221+
let _ = stream.read(&mut scratch).await;
222+
let _ = stream
223+
.write_all(
224+
b"HTTP/1.1 200 OK\r\n\
225+
Content-Type: application/json\r\n\
226+
Content-Length: 2\r\n\r\n[]",
227+
)
228+
.await;
229+
let _ = stream.flush().await;
230+
}
231+
});
232+
(format!("http://{address}"), server)
233+
}
234+
212235
async fn standalone_traced_client() -> (
213236
TestDriverClient,
214237
tokio::sync::oneshot::Sender<()>,
@@ -3718,3 +3741,77 @@ fn managed_container_identity_requires_the_configured_namespace() {
37183741
"demo"
37193742
));
37203743
}
3744+
3745+
#[tokio::test]
3746+
async fn delete_sandbox_reclaims_token_file_when_container_and_pending_are_gone() {
3747+
let state_dir = tempfile::tempdir().unwrap();
3748+
let (endpoint, server) = fake_docker_with_no_containers().await;
3749+
3750+
temp_env::async_with_vars([("XDG_STATE_HOME", Some(state_dir.path()))], async {
3751+
let config = runtime_config();
3752+
let mut driver = test_driver_with_config(config.clone());
3753+
driver.docker = Arc::new(
3754+
Docker::connect_with_http(&endpoint, 5, bollard::API_DEFAULT_VERSION).unwrap(),
3755+
);
3756+
3757+
// Arrange the leak: token on disk, container gone, `pending` empty.
3758+
let token = openshell_core::driver_utils::sandbox_token_path(
3759+
"docker-sandbox-tokens",
3760+
Some(&config.sandbox_namespace),
3761+
"sandbox-1",
3762+
)
3763+
.unwrap();
3764+
3765+
fs::create_dir_all(token.parent().unwrap()).unwrap();
3766+
fs::write(&token, "jwt\n").unwrap();
3767+
3768+
let deleted = driver.delete_sandbox_inner("sandbox-1", "").await.unwrap();
3769+
assert!(!deleted, "nothing was removed, must not claim a deletion");
3770+
assert!(!token.exists(), "token file must be reclaimed");
3771+
})
3772+
.await;
3773+
3774+
server.abort();
3775+
}
3776+
3777+
#[tokio::test]
3778+
async fn delete_sandbox_by_name_only_leaves_the_namespace_directory_alone() {
3779+
// `DeleteSandbox` accepts a name without an id. With no id there is no
3780+
// token path to derive, so the cleanup must be a no-op: deriving a path
3781+
// from an empty id yields `<namespace>/sandbox.jwt`, whose parent is the
3782+
// shared namespace directory.
3783+
let state_dir = tempfile::tempdir().unwrap();
3784+
let (endpoint, server) = fake_docker_with_no_containers().await;
3785+
3786+
temp_env::async_with_vars([("XDG_STATE_HOME", Some(state_dir.path()))], async {
3787+
let config = runtime_config();
3788+
let mut driver = test_driver_with_config(config.clone());
3789+
driver.docker = Arc::new(
3790+
Docker::connect_with_http(&endpoint, 5, bollard::API_DEFAULT_VERSION).unwrap(),
3791+
);
3792+
3793+
let namespace_dir = openshell_core::driver_utils::sandbox_token_path(
3794+
"docker-sandbox-tokens",
3795+
Some(&config.sandbox_namespace),
3796+
"sandbox-1",
3797+
)
3798+
.unwrap()
3799+
.parent()
3800+
.and_then(Path::parent)
3801+
.unwrap()
3802+
.to_path_buf();
3803+
fs::create_dir_all(&namespace_dir).unwrap();
3804+
3805+
let deleted = driver.delete_sandbox_inner("", "sandbox-1").await.unwrap();
3806+
3807+
assert!(!deleted, "nothing was removed, must not claim a deletion");
3808+
assert!(
3809+
namespace_dir.is_dir(),
3810+
"namespace directory must survive a name-only delete: {}",
3811+
namespace_dir.display()
3812+
);
3813+
})
3814+
.await;
3815+
3816+
server.abort();
3817+
}

0 commit comments

Comments
 (0)