package extraction functionality to DaemonService

This commit is contained in:
Noor 2025-01-20 20:14:54 +05:30
parent a709bffb02
commit eb811bb2db
Signed by: noormohammedb
GPG Key ID: D83EFB8B3B967146
4 changed files with 118 additions and 23 deletions

1
.gitignore vendored

@ -1 +1,2 @@
/target /target
enclave*

75
Cargo.lock generated

@ -285,6 +285,15 @@ version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" 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]] [[package]]
name = "detee-sgx-daemon" name = "detee-sgx-daemon"
version = "0.1.0" version = "0.1.0"
@ -292,9 +301,11 @@ dependencies = [
"chrono", "chrono",
"detee-shared", "detee-shared",
"env_logger", "env_logger",
"flate2",
"prost", "prost",
"prost-types", "prost-types",
"reqwest", "reqwest",
"tar",
"tokio", "tokio",
"tonic", "tonic",
"tonic-build", "tonic-build",
@ -383,12 +394,34 @@ version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" 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]] [[package]]
name = "fixedbitset" name = "fixedbitset"
version = "0.4.2" version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" 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]] [[package]]
name = "fnv" name = "fnv"
version = "1.0.7" version = "1.0.7"
@ -875,6 +908,17 @@ version = "0.2.169"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" 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]] [[package]]
name = "libyml" name = "libyml"
version = "0.0.5" version = "0.0.5"
@ -1205,6 +1249,15 @@ dependencies = [
"getrandom", "getrandom",
] ]
[[package]]
name = "redox_syscall"
version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03a862b389f93e68874fbf580b9de08dd02facb9a788ebadaf4a3fd33cf58834"
dependencies = [
"bitflags",
]
[[package]] [[package]]
name = "regex" name = "regex"
version = "1.11.1" version = "1.11.1"
@ -1555,6 +1608,17 @@ dependencies = [
"libc", "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]] [[package]]
name = "tempfile" name = "tempfile"
version = "3.15.0" version = "3.15.0"
@ -2060,6 +2124,17 @@ version = "0.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e9df38ee2d2c3c5948ea468a8406ff0db0b29ae1ffde1bcf20ef305bcc95c51" 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]] [[package]]
name = "yoke" name = "yoke"
version = "0.7.5" version = "0.7.5"

@ -7,13 +7,15 @@ edition = "2021"
env_logger = "0.11.6" env_logger = "0.11.6"
prost = "0.13.4" prost = "0.13.4"
prost-types = "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" tonic = "0.12.3"
# detee-shared = { git = "ssh://git@gitea.detee.cloud/noormohammedb/detee-shared" } # detee-shared = { git = "ssh://git@gitea.detee.cloud/noormohammedb/detee-shared" }
detee-shared = { path = "../detee-shared" } detee-shared = { path = "../detee-shared" }
chrono = "0.4.39" chrono = "0.4.39"
reqwest = "0.12.12" reqwest = "0.12.12"
flate2 = "1.0.35"
tar = "0.4.43"
[build-dependencies] [build-dependencies]
tonic-build = "0.12.3" tonic-build = "0.12.3"

@ -1,11 +1,14 @@
use chrono::Utc; use chrono::Utc;
use flate2::read::GzDecoder;
use reqwest::Client; use reqwest::Client;
use std::fs; use std::io::BufReader;
use std::io::Write;
use std::path::Path; use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
use std::{net::SocketAddr, str::FromStr}; use std::{net::SocketAddr, str::FromStr};
use tar::Archive;
use tokio::io::AsyncWriteExt;
use tokio::sync::RwLock; use tokio::sync::RwLock;
use tokio::{fs, fs::File};
use tonic::transport::Server; use tonic::transport::Server;
use detee_shared::pb::daemon::daemon_service_server::{ use detee_shared::pb::daemon::daemon_service_server::{
@ -50,39 +53,53 @@ impl DaemonServicePB for DaemonServer {
request: tonic::Request<Container>, request: tonic::Request<Container>,
) -> Result<tonic::Response<NewContainerRes>, tonic::Status> { ) -> Result<tonic::Response<NewContainerRes>, tonic::Status> {
let req_data = request.into_inner(); let req_data = request.into_inner();
dbg!(&req_data);
if let Some(package_url) = req_data.package_url { if let Some(package_url) = req_data.package_url {
let dir_path = Path::new("./enclave_packages"); handle_package(package_url)
fs::create_dir_all(dir_path)?; .await
.map_err(|err| tonic::Status::internal(err.to_string()))?;
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"));
}
}
} }
Ok(tonic::Response::new(NewContainerRes::default())) Ok(tonic::Response::new(NewContainerRes::default()))
} }
} }
async fn handle_package(package_url: String) -> Result<(), Box<dyn std::error::Error>> {
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<dyn std::error::Error>> { async fn download_file(url: &str, file_path: &Path) -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(); let client = Client::new();
let response = client.get(url).send().await?; let response = client.get(url).send().await?;
let data = response.bytes().await?; let data = response.bytes().await?;
let mut file = fs::File::create(file_path)?; let mut file = File::create(file_path).await?;
file.write_all(&data)?; file.write_all(&data).await?;
Ok(()) Ok(())
} }