From 949801fc4d7431b5fa6ae933f1e7ef8b8750b281 Mon Sep 17 00:00:00 2001 From: Johannes Heuel Date: Sun, 9 Oct 2022 20:21:37 +0200 Subject: [PATCH] clean up --- zoidberg_client/src/main.rs | 6 ++--- zoidberg_server/src/auth.rs | 24 ++++++++++++++++++ zoidberg_server/src/main.rs | 49 +++++++++++-------------------------- 3 files changed, 41 insertions(+), 38 deletions(-) create mode 100644 zoidberg_server/src/auth.rs diff --git a/zoidberg_client/src/main.rs b/zoidberg_client/src/main.rs index faac711..f814be8 100644 --- a/zoidberg_client/src/main.rs +++ b/zoidberg_client/src/main.rs @@ -16,7 +16,7 @@ use zoidberg_lib::types::{ const VERSION: &str = env!("CARGO_PKG_VERSION"); fn build_client(secret: &str) -> Client { - let cookie = format!("{}", secret); + let cookie = secret.to_string(); let mut headers = header::HeaderMap::new(); headers.insert( @@ -50,7 +50,7 @@ impl Worker { let r: RegisterResponse = serde_json::from_str(&body)?; log::info!("registered worker with id: {}", &r.id); Ok(Worker { - id: r.id.to_string(), + id: r.id, secret: secret.to_string(), server: server.to_string(), }) @@ -129,7 +129,7 @@ async fn main() -> Result<(), Box> { let matches = App::new("Zoidberg client") .version(VERSION) - .author("Johannes Heuel") + .author("by Johannes Heuel") .arg( Arg::with_name("server") .takes_value(true) diff --git a/zoidberg_server/src/auth.rs b/zoidberg_server/src/auth.rs new file mode 100644 index 0000000..3fccb6b --- /dev/null +++ b/zoidberg_server/src/auth.rs @@ -0,0 +1,24 @@ +use actix_web::{dev, error::ErrorBadRequest, Error, FromRequest, HttpRequest, Result}; +use futures::future::{err, ok, Ready}; + +pub struct Authorization {} + +impl FromRequest for Authorization { + type Error = Error; + type Future = Ready>; + + fn from_request(req: &HttpRequest, _payload: &mut dev::Payload) -> Self::Future { + if let Some(head) = req.headers().get("cookie") { + if let Ok(cookie) = head.to_str() { + if let Some(secret) = req.app_data::() { + if secret == cookie { + return ok(Authorization {}); + } else { + return err(ErrorBadRequest("no auth")); + } + } + } + } + err(ErrorBadRequest("no auth")) + } +} diff --git a/zoidberg_server/src/main.rs b/zoidberg_server/src/main.rs index da0a38c..f4b6561 100644 --- a/zoidberg_server/src/main.rs +++ b/zoidberg_server/src/main.rs @@ -1,13 +1,9 @@ -use actix_web::error::ErrorBadRequest; use actix_web::{ - dev, get, middleware::Logger, post, web, App, Error, FromRequest, HttpRequest, HttpResponse, - HttpServer, Responder, Result, + get, middleware::Logger, post, web, App, HttpResponse, HttpServer, Responder, Result, }; use chrono::Utc; -use clap; use env_logger::Env; -use futures::future::{err, ok, Ready}; -use log; + use std::sync::Mutex; use std::time::Duration; use uuid::Uuid; @@ -16,8 +12,11 @@ use zoidberg_lib::types::{ Worker, }; +mod auth; mod webpage; +use auth::Authorization; + const VERSION: &str = env!("CARGO_PKG_VERSION"); struct State { @@ -38,28 +37,6 @@ impl State { } } -struct Authorization {} - -impl FromRequest for Authorization { - type Error = Error; - type Future = Ready>; - - fn from_request(req: &HttpRequest, _payload: &mut dev::Payload) -> Self::Future { - if let Some(head) = req.headers().get("cookie") { - if let Ok(cookie) = head.to_str() { - if let Some(secret) = req.app_data::() { - if secret == cookie { - return ok(Authorization {}); - } else { - return err(ErrorBadRequest("no auth")); - } - } - } - } - err(ErrorBadRequest("no auth")) - } -} - #[get("/")] async fn index(data: web::Data) -> impl Responder { let workers = data.workers.lock().unwrap(); @@ -204,7 +181,7 @@ async fn main() -> std::io::Result<()> { let _matches = clap::App::new("Zoidberg server") .version(VERSION) - .author("Johannes Heuel") + .author("by Johannes Heuel") .get_matches(); let state = web::Data::new(State::new()); @@ -272,12 +249,13 @@ mod tests { .uri("/register") .to_request(); let resp: RegisterResponse = test::call_and_read_body_json(&app, req).await; - assert!(resp.id.len() > 0); + assert!(!resp.id.is_empty()); } #[actix_web::test] async fn test_fetch() { let cmd = String::from("hi"); + let jobid = 11; let app = test::init_service( App::new() .app_data(String::from("secret")) @@ -288,7 +266,7 @@ mod tests { last_heartbeat: None, }]), new_jobs: Mutex::new(vec![Job { - id: 0, + id: jobid, cmd: cmd.clone(), status: Status::Submitted, }]), @@ -313,7 +291,7 @@ mod tests { panic!("did not expect FetchResponse::Terminate from worker {}", w) } FetchResponse::Jobs(new_jobs) => { - assert_eq!(new_jobs[0].id, 0); + assert_eq!(new_jobs[0].id, jobid); assert_eq!(new_jobs[0].cmd, cmd); } } @@ -322,6 +300,7 @@ mod tests { #[actix_web::test] async fn test_status() { let cmd = String::from("hi"); + let jobid = 1; let app = test::init_service( App::new() .app_data(String::from("secret")) @@ -330,7 +309,7 @@ mod tests { workers: Mutex::new(Vec::new()), new_jobs: Mutex::new(Vec::new()), jobs: Mutex::new(vec![Job { - id: 1, + id: jobid, cmd: cmd.clone(), status: Status::Submitted, }]), @@ -340,11 +319,11 @@ mod tests { .await; let req = test::TestRequest::post() .append_header(("cookie", "secret")) - .set_json(vec![StatusRequest { id: 1 }]) + .set_json(vec![StatusRequest { id: jobid }]) .uri("/status") .to_request(); let resp: Vec = test::call_and_read_body_json(&app, req).await; - assert_eq!(resp[0].id, 1); + assert_eq!(resp[0].id, jobid); } #[actix_web::test]