container deletion from brain
delete package archive deleted all enclave files refactor constant directory name and some stuffs
This commit is contained in:
parent
2b67cff7c1
commit
23777d8a54
73
Cargo.lock
generated
73
Cargo.lock
generated
@ -26,21 +26,6 @@ dependencies = [
|
|||||||
"memchr",
|
"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]]
|
[[package]]
|
||||||
name = "anstream"
|
name = "anstream"
|
||||||
version = "0.6.18"
|
version = "0.6.18"
|
||||||
@ -249,20 +234,6 @@ version = "1.0.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
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]]
|
[[package]]
|
||||||
name = "colorchoice"
|
name = "colorchoice"
|
||||||
version = "1.0.3"
|
version = "1.0.3"
|
||||||
@ -299,7 +270,6 @@ name = "detee-sgx-daemon"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"chrono",
|
|
||||||
"detee-shared",
|
"detee-shared",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
"flate2",
|
"flate2",
|
||||||
@ -320,7 +290,7 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "detee-shared"
|
name = "detee-shared"
|
||||||
version = "0.1.0"
|
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 = [
|
dependencies = [
|
||||||
"base64",
|
"base64",
|
||||||
"prost",
|
"prost",
|
||||||
@ -701,29 +671,6 @@ dependencies = [
|
|||||||
"tracing",
|
"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]]
|
[[package]]
|
||||||
name = "icu_collections"
|
name = "icu_collections"
|
||||||
version = "1.5.0"
|
version = "1.5.0"
|
||||||
@ -1026,15 +973,6 @@ dependencies = [
|
|||||||
"tempfile",
|
"tempfile",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "num-traits"
|
|
||||||
version = "0.2.19"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
|
||||||
dependencies = [
|
|
||||||
"autocfg",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "object"
|
name = "object"
|
||||||
version = "0.36.7"
|
version = "0.36.7"
|
||||||
@ -2010,15 +1948,6 @@ dependencies = [
|
|||||||
"wasm-bindgen",
|
"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]]
|
[[package]]
|
||||||
name = "windows-registry"
|
name = "windows-registry"
|
||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
|
@ -9,7 +9,6 @@ prost = "0.13.4"
|
|||||||
prost-types = "0.13.4"
|
prost-types = "0.13.4"
|
||||||
tokio = { version = "1.43.0", features = ["macros", "rt-multi-thread", "fs"] }
|
tokio = { version = "1.43.0", features = ["macros", "rt-multi-thread", "fs"] }
|
||||||
tonic = "0.12.3"
|
tonic = "0.12.3"
|
||||||
chrono = "0.4.39"
|
|
||||||
reqwest = { version = "0.12.12", features = ["blocking"] }
|
reqwest = { version = "0.12.12", features = ["blocking"] }
|
||||||
flate2 = "1.0.35"
|
flate2 = "1.0.35"
|
||||||
tar = "0.4.43"
|
tar = "0.4.43"
|
||||||
|
10
src/data.rs
10
src/data.rs
@ -48,11 +48,7 @@ impl DaemonState {
|
|||||||
Ok(mapped_ports)
|
Ok(mapped_ports)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete_container(
|
pub async fn delete_container(&mut self, container_uuid: String) -> Result<()> {
|
||||||
&mut self,
|
|
||||||
admin_pubkey: String,
|
|
||||||
container_uuid: String,
|
|
||||||
) -> Result<()> {
|
|
||||||
let Some(container_position) = self
|
let Some(container_position) = self
|
||||||
.containers
|
.containers
|
||||||
.iter()
|
.iter()
|
||||||
@ -64,10 +60,6 @@ impl DaemonState {
|
|||||||
|
|
||||||
let container = &self.containers[container_position];
|
let container = &self.containers[container_position];
|
||||||
|
|
||||||
if container.admin != admin_pubkey {
|
|
||||||
return Err(anyhow!("Unauthorized"));
|
|
||||||
}
|
|
||||||
|
|
||||||
let container_name = format!("dtpm-{}", container.uuid);
|
let container_name = format!("dtpm-{}", container.uuid);
|
||||||
delete_enclave(container_name)?;
|
delete_enclave(container_name)?;
|
||||||
|
|
||||||
|
26
src/main.rs
26
src/main.rs
@ -19,11 +19,16 @@ use detee_shared::types::shared::Container as ContainerConfig;
|
|||||||
use tokio::sync::mpsc::Receiver;
|
use tokio::sync::mpsc::Receiver;
|
||||||
use tokio::sync::mpsc::Sender;
|
use tokio::sync::mpsc::Sender;
|
||||||
use tokio::time::sleep;
|
use tokio::time::sleep;
|
||||||
|
use utils::cleanup_enclave_disk_and_package;
|
||||||
use utils::handle_package;
|
use utils::handle_package;
|
||||||
|
|
||||||
const NODE_PUBKEY: &str = "0xd0837609aedd53854651210327db90f5c2626188a00e940bbc9eea2c7e6838b7";
|
const NODE_PUBKEY: &str = "0xd0837609aedd53854651210327db90f5c2626188a00e940bbc9eea2c7e6838b7";
|
||||||
const ADMIN_PUBKEY: &str = "0x28a3a71197250b0fa4dd0f86288e07ec9cc78ce3338e21e2ebef84dd7780e3eb";
|
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)]
|
#[derive(Debug)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub brain_url: String,
|
pub brain_url: String,
|
||||||
@ -67,7 +72,8 @@ impl ContainerHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Some(brain_message::Msg::DeleteContainer(msg)) => {
|
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)) => {
|
Some(brain_message::Msg::ListContainer(msg)) => {
|
||||||
@ -82,11 +88,14 @@ impl ContainerHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_new_container_req(&mut self, new_container_req: ContainerConfig) {
|
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 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,
|
Ok(unarchive_dir) => unarchive_dir,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let res = DaemonMessage {
|
let res = DaemonMessage {
|
||||||
@ -139,6 +148,15 @@ impl ContainerHandler {
|
|||||||
println!("sending response {:?}", res);
|
println!("sending response {:?}", res);
|
||||||
let _ = self.sender.send(res).await;
|
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]
|
#[tokio::main]
|
||||||
|
30
src/utils.rs
30
src/utils.rs
@ -1,5 +1,4 @@
|
|||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use chrono::Utc;
|
|
||||||
use flate2::read::GzDecoder;
|
use flate2::read::GzDecoder;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
@ -11,15 +10,15 @@ use tokio::io::AsyncWriteExt;
|
|||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
use tokio::{fs, fs::File};
|
use tokio::{fs, fs::File};
|
||||||
|
|
||||||
|
use crate::{PACKAGE_ARCHIVE_DIR_PATH, PACKAGE_ARCHIVE_POSTFIX, PACKAGE_DIR_PATH};
|
||||||
|
|
||||||
pub static IP_INFO: LazyLock<IPInfo> = LazyLock::new(|| get_ip_info().unwrap());
|
pub static IP_INFO: LazyLock<IPInfo> = LazyLock::new(|| get_ip_info().unwrap());
|
||||||
|
|
||||||
pub async fn handle_package(package_url: String) -> Result<String> {
|
pub async fn handle_package(package_url: String, container_uuid: String) -> Result<String> {
|
||||||
let dir_path = Path::new("./enclave_archives");
|
let dir_path = Path::new(PACKAGE_ARCHIVE_DIR_PATH);
|
||||||
fs::create_dir_all(dir_path).await?;
|
fs::create_dir_all(dir_path).await?;
|
||||||
|
|
||||||
let utc_time = Utc::now().format("%Y%m%d%H%M%S");
|
let file_name = format!("{container_uuid}{PACKAGE_ARCHIVE_POSTFIX}",);
|
||||||
|
|
||||||
let file_name = format!("{}-enclave_packager.tar.gz", utc_time);
|
|
||||||
let file_path = dir_path.join(file_name);
|
let file_path = dir_path.join(file_name);
|
||||||
if let Err(e) = download_file(&package_url, &file_path).await {
|
if let Err(e) = download_file(&package_url, &file_path).await {
|
||||||
println!("Error downloading file: {:?}", e);
|
println!("Error downloading file: {:?}", e);
|
||||||
@ -35,7 +34,7 @@ pub async fn handle_package(package_url: String) -> Result<String> {
|
|||||||
return Err(anyhow!("Error: file not an archive: {er:?}"));
|
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?;
|
fs::create_dir_all(Path::new(&unarchive_dir)).await?;
|
||||||
archive.unpack(&unarchive_dir)?;
|
archive.unpack(&unarchive_dir)?;
|
||||||
|
|
||||||
@ -96,3 +95,20 @@ fn get_ip_info() -> anyhow::Result<IPInfo> {
|
|||||||
log::info!("Got the following data from ipinfo.io: {body}");
|
log::info!("Got the following data from ipinfo.io: {body}");
|
||||||
Ok(serde_json::de::from_str(&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(())
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user