diff --git a/Cargo.lock b/Cargo.lock index b1c76e9..aa39251 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,21 +26,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "android-tzdata" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" - -[[package]] -name = "android_system_properties" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" -dependencies = [ - "libc", -] - [[package]] name = "anstream" version = "0.6.18" @@ -249,20 +234,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "chrono" -version = "0.4.39" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e36cc9d416881d2e24f9a963be5fb1cd90966419ac844274161d10488b3e825" -dependencies = [ - "android-tzdata", - "iana-time-zone", - "js-sys", - "num-traits", - "wasm-bindgen", - "windows-targets", -] - [[package]] name = "colorchoice" version = "1.0.3" @@ -299,7 +270,6 @@ name = "detee-sgx-daemon" version = "0.1.0" dependencies = [ "anyhow", - "chrono", "detee-shared", "env_logger", "flate2", @@ -320,7 +290,7 @@ dependencies = [ [[package]] name = "detee-shared" version = "0.1.0" -source = "git+ssh://git@gitea.detee.cloud/noormohammedb/detee-shared#6e1b1853838905c44d535d984d1221dd5d0dc2bc" +source = "git+ssh://git@gitea.detee.cloud/noormohammedb/detee-shared#7c9f66a7394c06ad8af0934e34b113f9c965bc98" dependencies = [ "base64", "prost", @@ -701,29 +671,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "iana-time-zone" -version = "0.1.61" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220" -dependencies = [ - "android_system_properties", - "core-foundation-sys", - "iana-time-zone-haiku", - "js-sys", - "wasm-bindgen", - "windows-core", -] - -[[package]] -name = "iana-time-zone-haiku" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" -dependencies = [ - "cc", -] - [[package]] name = "icu_collections" version = "1.5.0" @@ -1026,15 +973,6 @@ dependencies = [ "tempfile", ] -[[package]] -name = "num-traits" -version = "0.2.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" -dependencies = [ - "autocfg", -] - [[package]] name = "object" version = "0.36.7" @@ -2010,15 +1948,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "windows-core" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" -dependencies = [ - "windows-targets", -] - [[package]] name = "windows-registry" version = "0.2.0" diff --git a/Cargo.toml b/Cargo.toml index 7855dd1..d8513ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,6 @@ prost = "0.13.4" prost-types = "0.13.4" tokio = { version = "1.43.0", features = ["macros", "rt-multi-thread", "fs"] } tonic = "0.12.3" -chrono = "0.4.39" reqwest = { version = "0.12.12", features = ["blocking"] } flate2 = "1.0.35" tar = "0.4.43" diff --git a/src/data.rs b/src/data.rs index 112753a..dea9ba7 100644 --- a/src/data.rs +++ b/src/data.rs @@ -48,11 +48,7 @@ impl DaemonState { Ok(mapped_ports) } - pub async fn delete_container( - &mut self, - admin_pubkey: String, - container_uuid: String, - ) -> Result<()> { + pub async fn delete_container(&mut self, container_uuid: String) -> Result<()> { let Some(container_position) = self .containers .iter() @@ -64,10 +60,6 @@ impl DaemonState { let container = &self.containers[container_position]; - if container.admin != admin_pubkey { - return Err(anyhow!("Unauthorized")); - } - let container_name = format!("dtpm-{}", container.uuid); delete_enclave(container_name)?; diff --git a/src/main.rs b/src/main.rs index 3b0c7ee..e63132c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,11 +19,16 @@ use detee_shared::types::shared::Container as ContainerConfig; use tokio::sync::mpsc::Receiver; use tokio::sync::mpsc::Sender; use tokio::time::sleep; +use utils::cleanup_enclave_disk_and_package; use utils::handle_package; const NODE_PUBKEY: &str = "0xd0837609aedd53854651210327db90f5c2626188a00e940bbc9eea2c7e6838b7"; const ADMIN_PUBKEY: &str = "0x28a3a71197250b0fa4dd0f86288e07ec9cc78ce3338e21e2ebef84dd7780e3eb"; +const PACKAGE_ARCHIVE_POSTFIX: &str = "-enclave_packager.tar.gz"; +const PACKAGE_ARCHIVE_DIR_PATH: &str = "./enclave_archives"; +const PACKAGE_DIR_PATH: &str = "./enclaves"; + #[derive(Debug)] pub struct Config { pub brain_url: String, @@ -67,7 +72,8 @@ impl ContainerHandler { } Some(brain_message::Msg::DeleteContainer(msg)) => { - dbg!(&msg); + let container_id = msg.uuid.unwrap_or_default(); + self.handle_del_container_req(container_id).await; } Some(brain_message::Msg::ListContainer(msg)) => { @@ -82,11 +88,14 @@ impl ContainerHandler { } async fn handle_new_container_req(&mut self, new_container_req: ContainerConfig) { - dbg!(&new_container_req); - let container_uuid = new_container_req.uuid.clone(); - let unarchive_dir = match handle_package(new_container_req.package_url.clone()).await { + let unarchive_dir = match handle_package( + new_container_req.package_url.clone(), + container_uuid.clone(), + ) + .await + { Ok(unarchive_dir) => unarchive_dir, Err(e) => { let res = DaemonMessage { @@ -139,6 +148,15 @@ impl ContainerHandler { println!("sending response {:?}", res); let _ = self.sender.send(res).await; } + + async fn handle_del_container_req(&mut self, container_uuid: String) { + if let Err(e) = self.data.delete_container(container_uuid.clone()).await { + log::error!("Failed to delete container:\n{e}"); + } + if let Err(er) = cleanup_enclave_disk_and_package(container_uuid).await { + log::error!("Failed to cleanup disk:\n{er}"); + }; + } } #[tokio::main] diff --git a/src/utils.rs b/src/utils.rs index a466b9d..d456e74 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,5 +1,4 @@ use anyhow::{anyhow, Result}; -use chrono::Utc; use flate2::read::GzDecoder; use rand::Rng; use reqwest::Client; @@ -11,15 +10,15 @@ use tokio::io::AsyncWriteExt; use tokio::net::TcpListener; use tokio::{fs, fs::File}; +use crate::{PACKAGE_ARCHIVE_DIR_PATH, PACKAGE_ARCHIVE_POSTFIX, PACKAGE_DIR_PATH}; + pub static IP_INFO: LazyLock = LazyLock::new(|| get_ip_info().unwrap()); -pub async fn handle_package(package_url: String) -> Result { - let dir_path = Path::new("./enclave_archives"); +pub async fn handle_package(package_url: String, container_uuid: String) -> Result { + let dir_path = Path::new(PACKAGE_ARCHIVE_DIR_PATH); fs::create_dir_all(dir_path).await?; - let utc_time = Utc::now().format("%Y%m%d%H%M%S"); - - let file_name = format!("{}-enclave_packager.tar.gz", utc_time); + let file_name = format!("{container_uuid}{PACKAGE_ARCHIVE_POSTFIX}",); let file_path = dir_path.join(file_name); if let Err(e) = download_file(&package_url, &file_path).await { println!("Error downloading file: {:?}", e); @@ -35,7 +34,7 @@ pub async fn handle_package(package_url: String) -> Result { return Err(anyhow!("Error: file not an archive: {er:?}")); }; - let unarchive_dir = format!("./enclaves/{}", utc_time); + let unarchive_dir = format!("{PACKAGE_DIR_PATH}/{}", container_uuid); fs::create_dir_all(Path::new(&unarchive_dir)).await?; archive.unpack(&unarchive_dir)?; @@ -96,3 +95,20 @@ fn get_ip_info() -> anyhow::Result { log::info!("Got the following data from ipinfo.io: {body}"); Ok(serde_json::de::from_str(&body)?) } + +pub async fn cleanup_enclave_disk_and_package(container_uuid: String) -> Result<()> { + let enclave_disk_dir_str = format!("{PACKAGE_DIR_PATH}/{container_uuid}"); + let enclave_disk_path = Path::new(&enclave_disk_dir_str); + if enclave_disk_path.exists() { + std::fs::remove_dir_all(enclave_disk_path)?; + } + + let enclave_archive_dir_str = + format!("{PACKAGE_ARCHIVE_DIR_PATH}/{container_uuid}{PACKAGE_ARCHIVE_POSTFIX}"); + let enclave_archive_path = Path::new(&enclave_archive_dir_str); + if enclave_archive_path.exists() { + std::fs::remove_file(enclave_archive_path)?; + } + + Ok(()) +}