Compare commits

...

2 Commits

Author SHA1 Message Date
Ramil_Algayev
5aa562fb01 I tried getting rid of tokio, it didn't work out 2024-12-28 07:07:30 +04:00
Ramil_Algayev
b1049c4dcc improved /install 2024-12-27 23:04:17 +04:00
4 changed files with 78 additions and 34 deletions

43
dtrfs_api/Cargo.lock generated

@ -1,6 +1,6 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 4
[[package]] [[package]]
name = "actix-codec" name = "actix-codec"
@ -417,9 +417,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]] [[package]]
name = "bytes" name = "bytes"
version = "1.8.0" version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ac0150caa2ae65ca5bd83f25c7de183dea78d4d366469f148435e2acfbad0da" checksum = "325918d6fe32f23b19878fe4b34794ae41fc19ddbe53b10571a4874d44ffd39b"
[[package]] [[package]]
name = "bytestring" name = "bytestring"
@ -670,6 +670,7 @@ dependencies = [
"anyhow", "anyhow",
"base64", "base64",
"bincode", "bincode",
"bytes",
"ed25519-dalek", "ed25519-dalek",
"lazy_static", "lazy_static",
"regex", "regex",
@ -678,6 +679,9 @@ dependencies = [
"serde", "serde",
"sev", "sev",
"sha3", "sha3",
"tokio",
"tokio-stream",
"tokio-util",
] ]
[[package]] [[package]]
@ -2104,9 +2108,9 @@ dependencies = [
[[package]] [[package]]
name = "tokio" name = "tokio"
version = "1.41.1" version = "1.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22cfb5bee7a6a52939ca9224d6ac897bb669134078daa8735560897f69de4d33" checksum = "5cec9b21b0450273377fc97bd4c33a8acffc8c996c987a7c5b319a0083707551"
dependencies = [ dependencies = [
"backtrace", "backtrace",
"bytes", "bytes",
@ -2116,9 +2120,21 @@ dependencies = [
"pin-project-lite", "pin-project-lite",
"signal-hook-registry", "signal-hook-registry",
"socket2", "socket2",
"tokio-macros",
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
[[package]]
name = "tokio-macros"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "tokio-rustls" name = "tokio-rustls"
version = "0.26.0" version = "0.26.0"
@ -2131,10 +2147,21 @@ dependencies = [
] ]
[[package]] [[package]]
name = "tokio-util" name = "tokio-stream"
version = "0.7.12" version = "0.1.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61e7c3654c13bcd040d4a03abee2c75b1d14a37b423cf5a813ceae1cc903ec6a" checksum = "eca58d7bba4a75707817a2c44174253f9236b2d5fbd055602e9d5c07c139a047"
dependencies = [
"futures-core",
"pin-project-lite",
"tokio",
]
[[package]]
name = "tokio-util"
version = "0.7.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7fcaa8d55a2bdd6b83ace262b016eca0d79ee02818c5c1bcdf0305114081078"
dependencies = [ dependencies = [
"bytes", "bytes",
"futures-core", "futures-core",

@ -11,8 +11,12 @@ regex = "1.11.1"
sev = { version = "4.0", default-features = false, features = ['crypto_nossl','snp'] } sev = { version = "4.0", default-features = false, features = ['crypto_nossl','snp'] }
ed25519-dalek = { version = "2.1.1", features = ["pem", "pkcs8"] } ed25519-dalek = { version = "2.1.1", features = ["pem", "pkcs8"] }
lazy_static = "1.5.0" lazy_static = "1.5.0"
tokio = { version = "1.42.0", features = ["full"] }
tokio-util = "0.7.13"
tokio-stream = { version = "0.1.17", features = ["io-util"] }
actix-web = { version = "4.9.0", features = ["rustls-0_23"] } actix-web = { version = "4.9.0", features = ["rustls-0_23"] }
sha3 = "0.10.8" sha3 = "0.10.8"
rustls = "0.23.18" rustls = "0.23.18"
rustls-pemfile = "2.2.0" rustls-pemfile = "2.2.0"
serde = { version = "1.0.215", features = ["derive"] } serde = { version = "1.0.215", features = ["derive"] }
bytes = "1.9.0"

@ -93,8 +93,8 @@ async fn post_install_form(req: HttpRequest, form: web::Form<InstallForm>) -> Ht
if let Err(e) = verify(&req) { if let Err(e) = verify(&req) {
return HttpResponse::BadRequest().body(format!("Signature verification failed: {}", e)); return HttpResponse::BadRequest().body(format!("Signature verification failed: {}", e));
}; };
match os::encrypt_and_install_os(&form.url, &form.sha, &form.keyfile) { match os::encrypt_and_install_os(&form.url, &form.sha, &form.keyfile).await {
Ok(s) => HttpResponse::Ok().body(s), Ok(s) => HttpResponse::Ok().streaming(s),
Err(e) => HttpResponse::InternalServerError().body(format!("{e:?}")), Err(e) => HttpResponse::InternalServerError().body(format!("{e:?}")),
} }
} }

@ -1,47 +1,60 @@
use crate::snp::get_derived_key; use crate::snp::get_derived_key;
use bytes::Bytes;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use base64::prelude::{Engine, BASE64_URL_SAFE}; use base64::prelude::{Engine, BASE64_URL_SAFE};
use std::process::Command;
use std::{ use std::{
fs::File, fs::File,
io::{BufRead, BufReader, Write}, io::{BufRead, BufReader, Write},
path::Path, path::Path,
process::Command, process::Stdio,
}; };
const SNP_KEYFILE_PATH: &str = "/tmp/detee_snp_keyfile"; const SNP_KEYFILE_PATH: &str = "/tmp/detee_snp_keyfile";
const BACKUP_KEYFILE_PATH: &str = "/tmp/detee_backup_keyfile"; const BACKUP_KEYFILE_PATH: &str = "/tmp/detee_backup_keyfile";
pub fn encrypt_and_install_os( pub async fn encrypt_and_install_os(
install_url: &str, install_url: &str,
install_sha: &str, install_sha: &str,
keyfile: &str, keyfile: &str,
) -> Result<String> { ) -> Result<impl tokio_stream::Stream<Item = Result<Bytes>>> {
let binary_keyfile = BASE64_URL_SAFE.decode(keyfile)?; // I swear I tried my best to do without tokio
std::fs::write(BACKUP_KEYFILE_PATH, binary_keyfile)?; // It is just such a pain to do to anything without tokio
// this path is hardcoded also in the initrd creation script // I am sorry for the mess but I gave up after countless tries
let install_result = Command::new("/usr/lib/dtrfs/install_os.sh") use tokio::process::Command;
use tokio::io::{BufReader, AsyncBufReadExt};
use tokio_stream::{StreamExt, wrappers::LinesStream};
let binary_keyfile = base64::engine::general_purpose::URL_SAFE.decode(keyfile)?;
// Write the decoded keyfile to the backup path asynchronously
tokio::fs::write(BACKUP_KEYFILE_PATH, &binary_keyfile).await?;
// Spawn the installation script as a child process
let mut child = Command::new("/usr/lib/dtrfs/install_os.sh")
.env("INSTALL_URL", install_url) .env("INSTALL_URL", install_url)
.env("INSTALL_SHA", install_sha) .env("INSTALL_SHA", install_sha)
.env("SNP_KEY_FILE", SNP_KEYFILE_PATH) .env("SNP_KEY_FILE", SNP_KEYFILE_PATH)
.env("ROOT_KEYFILE", BACKUP_KEYFILE_PATH) .env("ROOT_KEYFILE", BACKUP_KEYFILE_PATH)
.output()?; .stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;
if !install_result.status.success() { // Take stdout and stderr from the child process
return Err(anyhow!( let stdout = child.stdout.take().unwrap();
"OS installation script failed.\nScript stdout:\n{}\nScript stderr:\n{}", let stderr = child.stderr.take().unwrap();
String::from_utf8(install_result.stdout)
.unwrap_or("Could not grab stdout from installation script.".to_string()), // Merge stdout and stderr into a single stream
String::from_utf8(install_result.stderr) let stdout_lines = LinesStream::new(BufReader::new(stdout).lines());
.unwrap_or("Could not grab stderr from installation script.".to_string()), let stderr_lines = LinesStream::new(BufReader::new(stderr).lines());
)); let merged_stream = stdout_lines.merge(stderr_lines).map(|line_result| {
} line_result
Ok(format!( .map(|line| Bytes::from(line + "\n"))
"Successfully installed OS. Script stdout:\n{}\nScript stderr:\n{}", .map_err(|e| anyhow::Error::new(e))
String::from_utf8(install_result.stdout) });
.unwrap_or("Could not grab stdout from installation script.".to_string()),
String::from_utf8(install_result.stderr) // Return the merged stream as the HTTP response
.unwrap_or("Could not grab stderr from installation script.".to_string()), Ok(merged_stream)
))
} }
pub fn try_hot_keyfile() -> Result<()> { pub fn try_hot_keyfile() -> Result<()> {