diff --git a/.gitignore b/.gitignore index ea8c4bf..e4c19c9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +enclave* diff --git a/Cargo.lock b/Cargo.lock index 07e6dc1..6edf7d7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -285,6 +285,15 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" +[[package]] +name = "crc32fast" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" +dependencies = [ + "cfg-if", +] + [[package]] name = "detee-sgx-daemon" version = "0.1.0" @@ -292,9 +301,11 @@ dependencies = [ "chrono", "detee-shared", "env_logger", + "flate2", "prost", "prost-types", "reqwest", + "tar", "tokio", "tonic", "tonic-build", @@ -383,12 +394,34 @@ version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" +[[package]] +name = "filetime" +version = "0.2.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35c0522e981e68cbfa8c3f978441a5f34b30b96e146b33cd3359176b50fe8586" +dependencies = [ + "cfg-if", + "libc", + "libredox", + "windows-sys 0.59.0", +] + [[package]] name = "fixedbitset" version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" +[[package]] +name = "flate2" +version = "1.0.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c936bfdafb507ebbf50b8074c54fa31c5be9a1e7e5f467dd659697041407d07c" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + [[package]] name = "fnv" version = "1.0.7" @@ -875,6 +908,17 @@ version = "0.2.169" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" +[[package]] +name = "libredox" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" +dependencies = [ + "bitflags", + "libc", + "redox_syscall", +] + [[package]] name = "libyml" version = "0.0.5" @@ -1205,6 +1249,15 @@ dependencies = [ "getrandom", ] +[[package]] +name = "redox_syscall" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03a862b389f93e68874fbf580b9de08dd02facb9a788ebadaf4a3fd33cf58834" +dependencies = [ + "bitflags", +] + [[package]] name = "regex" version = "1.11.1" @@ -1555,6 +1608,17 @@ dependencies = [ "libc", ] +[[package]] +name = "tar" +version = "0.4.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c65998313f8e17d0d553d28f91a0df93e4dbbbf770279c7bc21ca0f09ea1a1f6" +dependencies = [ + "filetime", + "libc", + "xattr", +] + [[package]] name = "tempfile" version = "3.15.0" @@ -2060,6 +2124,17 @@ version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e9df38ee2d2c3c5948ea468a8406ff0db0b29ae1ffde1bcf20ef305bcc95c51" +[[package]] +name = "xattr" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e105d177a3871454f754b33bb0ee637ecaaac997446375fd3e5d43a2ed00c909" +dependencies = [ + "libc", + "linux-raw-sys", + "rustix", +] + [[package]] name = "yoke" version = "0.7.5" diff --git a/Cargo.toml b/Cargo.toml index 8dfff04..4c91180 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,13 +7,15 @@ edition = "2021" env_logger = "0.11.6" prost = "0.13.4" prost-types = "0.13.4" -tokio = { version = "1.43.0", features = ["macros", "rt-multi-thread"] } +tokio = { version = "1.43.0", features = ["macros", "rt-multi-thread", "fs"] } tonic = "0.12.3" # detee-shared = { git = "ssh://git@gitea.detee.cloud/noormohammedb/detee-shared" } detee-shared = { path = "../detee-shared" } chrono = "0.4.39" reqwest = "0.12.12" +flate2 = "1.0.35" +tar = "0.4.43" [build-dependencies] tonic-build = "0.12.3" diff --git a/src/grpc.rs b/src/grpc.rs index a86d7a8..8c10525 100644 --- a/src/grpc.rs +++ b/src/grpc.rs @@ -1,11 +1,14 @@ use chrono::Utc; +use flate2::read::GzDecoder; use reqwest::Client; -use std::fs; -use std::io::Write; +use std::io::BufReader; use std::path::Path; use std::sync::Arc; use std::{net::SocketAddr, str::FromStr}; +use tar::Archive; +use tokio::io::AsyncWriteExt; use tokio::sync::RwLock; +use tokio::{fs, fs::File}; use tonic::transport::Server; use detee_shared::pb::daemon::daemon_service_server::{ @@ -50,39 +53,53 @@ impl DaemonServicePB for DaemonServer { request: tonic::Request, ) -> Result, tonic::Status> { let req_data = request.into_inner(); - dbg!(&req_data); if let Some(package_url) = req_data.package_url { - let dir_path = Path::new("./enclave_packages"); - fs::create_dir_all(dir_path)?; - - let file_name = format!( - "{}-enclave_packager.tar.gz", - Utc::now().format("%Y%m%d%H%M%S") - ); - let file_path = dir_path.join(file_name); - match download_file(&package_url, &file_path).await { - Ok(_) => { - println!("Downloaded file to {:?}", file_path); - } - Err(e) => { - println!("Error downloading file: {:?}", e); - return Err(tonic::Status::internal("Error downloading file")); - } - } + handle_package(package_url) + .await + .map_err(|err| tonic::Status::internal(err.to_string()))?; } Ok(tonic::Response::new(NewContainerRes::default())) } } +async fn handle_package(package_url: String) -> Result<(), Box> { + let dir_path = Path::new("./enclave_archives"); + 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_path = dir_path.join(file_name); + if let Err(e) = download_file(&package_url, &file_path).await { + println!("Error downloading file: {:?}", e); + return Err("Error downloading file".into()); + } + + let downloaded_file = std::fs::File::open(file_path)?; + let mut reader = BufReader::new(downloaded_file); + let mut archive = Archive::new(GzDecoder::new(&mut reader)); + + if let Err(er) = archive.entries() { + dbg!(&er); + return Err(format!("Error: file not an archive: {er:?}").into()); + }; + + let unarchive_dir = format!("./enclaves/{}", utc_time); + fs::create_dir_all(Path::new(&unarchive_dir)).await?; + archive.unpack(unarchive_dir)?; + + Ok(()) +} + async fn download_file(url: &str, file_path: &Path) -> Result<(), Box> { let client = Client::new(); let response = client.get(url).send().await?; let data = response.bytes().await?; - let mut file = fs::File::create(file_path)?; - file.write_all(&data)?; + let mut file = File::create(file_path).await?; + file.write_all(&data).await?; Ok(()) }