You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
3.2 KiB
121 lines
3.2 KiB
#[macro_use] |
|
extern crate log; |
|
|
|
#[cfg_attr(test, macro_use)] |
|
extern crate mockall; |
|
#[cfg_attr(test, macro_use)] |
|
extern crate mockall_double; |
|
|
|
// due to linking errors |
|
extern crate openssl; |
|
// don't touch anything |
|
extern crate diesel; |
|
// in this block |
|
|
|
mod db; |
|
mod filters; |
|
mod handlers; |
|
|
|
use db::UDB; |
|
use filters::make_filters; |
|
use std::path::PathBuf; |
|
use u_lib::{config::MASTER_PORT, models::*, utils::init_env}; |
|
use warp::Filter; |
|
|
|
const LOGFILE: &str = "u_server.log"; |
|
|
|
fn prefill_jobs() { |
|
let agent_hello = JobMeta::builder() |
|
.with_type(misc::JobType::Manage) |
|
.with_alias("agent_hello") |
|
.build() |
|
.unwrap(); |
|
UDB::lock_db().insert_jobs(&[agent_hello]).unwrap(); |
|
} |
|
|
|
fn init_logger() { |
|
use simplelog::*; |
|
use std::fs::OpenOptions; |
|
let log_cfg = ConfigBuilder::new() |
|
.set_time_format_str("%x %X") |
|
.set_time_to_local(true) |
|
.build(); |
|
let logfile = OpenOptions::new() |
|
.append(true) |
|
.create(true) |
|
.open(PathBuf::from("logs").join(LOGFILE)) |
|
.unwrap(); |
|
let level = LevelFilter::Info; |
|
let loggers = vec![ |
|
WriteLogger::new(level, log_cfg.clone(), logfile) as Box<dyn SharedLogger>, |
|
TermLogger::new(level, log_cfg, TerminalMode::Stderr, ColorChoice::Auto), |
|
]; |
|
CombinedLogger::init(loggers).unwrap(); |
|
} |
|
|
|
fn init_all() { |
|
init_logger(); |
|
init_env(); |
|
prefill_jobs(); |
|
} |
|
|
|
//TODO: tracing-subscriber |
|
pub async fn serve() { |
|
init_all(); |
|
let routes = make_filters(); |
|
let certs_dir = PathBuf::from("certs"); |
|
warp::serve(routes.with(warp::log("warp"))) |
|
.tls() |
|
.cert_path(certs_dir.join("server.crt")) |
|
.key_path(certs_dir.join("server.key")) |
|
.client_auth_required_path(certs_dir.join("ca.crt")) |
|
.run(([0, 0, 0, 0], MASTER_PORT)) |
|
.await; |
|
} |
|
|
|
#[cfg(test)] |
|
mod tests { |
|
use super::*; |
|
#[double] |
|
use crate::handlers::Endpoints; |
|
use handlers::build_ok; |
|
use mockall::predicate::*; |
|
use test_case::test_case; |
|
use u_lib::messaging::{AsMsg, BaseMessage, Reportable}; |
|
use uuid::Uuid; |
|
use warp::test::request; |
|
|
|
#[test_case(Some(Uuid::new_v4()))] |
|
#[test_case(None => panics)] |
|
#[tokio::test] |
|
async fn test_get_agent_jobs_unauthorized(uid: Option<Uuid>) { |
|
let mock = Endpoints::get_agent_jobs_context(); |
|
mock.expect().with(eq(uid)).returning(|_| Ok(build_ok(""))); |
|
request() |
|
.path(&format!( |
|
"/get_agent_jobs/{}", |
|
uid.map(|u| u.simple().to_string()).unwrap_or(String::new()) |
|
)) |
|
.method("GET") |
|
.filter(&make_filters()) |
|
.await |
|
.unwrap(); |
|
mock.checkpoint(); |
|
} |
|
|
|
#[tokio::test] |
|
async fn test_report_unauth_successful() { |
|
let mock = Endpoints::report_context(); |
|
mock.expect() |
|
.withf(|msg: &BaseMessage<'_, Vec<Reportable>>| msg.inner_ref()[0] == Reportable::Dummy) |
|
.returning(|_| Ok(build_ok(""))); |
|
request() |
|
.path("/report/") |
|
.method("POST") |
|
.json(&vec![Reportable::Dummy].as_message()) |
|
.filter(&make_filters()) |
|
.await |
|
.unwrap(); |
|
mock.checkpoint(); |
|
} |
|
}
|
|
|