diff --git a/.env b/.env index 5d66ce7..2ac5089 100644 --- a/.env +++ b/.env @@ -1 +1 @@ -DATABASE_URL=./bin/u_server/u_server.db +export DATABASE_URL=postgres://postgres:12348756@172.17.0.2/u_db diff --git a/bin/u_server/.env b/bin/u_server/.env deleted file mode 100644 index ad706ab..0000000 --- a/bin/u_server/.env +++ /dev/null @@ -1 +0,0 @@ -DATABASE_URL=./u_server.db diff --git a/bin/u_server/Cargo.toml b/bin/u_server/Cargo.toml index fbf2af4..6018558 100644 --- a/bin/u_server/Cargo.toml +++ b/bin/u_server/Cargo.toml @@ -10,9 +10,10 @@ env_logger = "0.7.1" log = "0.4.11" anyhow = "*" warp = "0.2.4" +uuid = { version = "0.6.5", features = ["serde", "v4"] } [dependencies.diesel] -features = ["sqlite"] +features = ["postgres", "uuid"] version = "1.4.5" [dependencies.serde] diff --git a/bin/u_server/src/db.rs b/bin/u_server/src/db.rs index aa07d0c..f511b68 100644 --- a/bin/u_server/src/db.rs +++ b/bin/u_server/src/db.rs @@ -1,5 +1,5 @@ use diesel::{ - sqlite::SqliteConnection, + pg::PgConnection, prelude::* }; use dotenv::dotenv; @@ -16,18 +16,14 @@ use u_lib::models::*; pub type Storage = Arc>; pub struct UDB { - conn: SqliteConnection + conn: PgConnection } impl UDB { - pub fn new(path: Option) -> USrvResult { + pub fn new() -> USrvResult { dotenv()?; - let db_path = match path { - Some(p) => p, - None => env::var("DATABASE_URL").unwrap_or(":memory:".to_string()) - }; - let conn = SqliteConnection::establish(&db_path)?; - conn.execute("PRAGMA foreign_keys = ON;")?; + let db_path = env::var("DATABASE_URL").unwrap(); + let conn = PgConnection::establish(&db_path)?; let instance = UDB { conn }; @@ -55,7 +51,7 @@ mod tests { use super::*; fn setup_db() -> Storage { - return UDB::new(Some(":memory:".to_string())).unwrap(); + return UDB::new().unwrap(); } #[tokio::test] @@ -63,7 +59,7 @@ mod tests { let db = setup_db(); let agent = IAgent { alias: None, - agent_id: "000-000".to_string(), + id: "000-000".to_string(), hostname: "test".to_string(), is_root: false, is_root_allowed: false, diff --git a/bin/u_server/src/main.rs b/bin/u_server/src/main.rs index cee28c9..31971f2 100644 --- a/bin/u_server/src/main.rs +++ b/bin/u_server/src/main.rs @@ -24,7 +24,6 @@ use serde::{ de::DeserializeOwned }; - fn get_content() -> impl Filter,), Error = Rejection> + Clone @@ -40,7 +39,7 @@ where async fn main() { env_logger::init(); - let base_db = UDB::new(None).unwrap(); + let base_db = UDB::new().unwrap(); let db = warp::any().map(move || base_db.clone()); let new_client = warp::post() @@ -93,9 +92,8 @@ async fn main() { ; let routes = auth_zone - .or(agent_zone) - .with(warp::log("warp")); - warp::serve(routes) + .or(agent_zone); + warp::serve(routes.with(warp::log("warp"))) .run(([0,0,0,0], MASTER_PORT)).await; } diff --git a/lib/u_lib/Cargo.toml b/lib/u_lib/Cargo.toml index 6bfe371..4d65188 100644 --- a/lib/u_lib/Cargo.toml +++ b/lib/u_lib/Cargo.toml @@ -8,7 +8,7 @@ edition = "2018" [dependencies] serde = { version = "1.0.114", features = ["derive"] } -uuid = { version = "^0.8.1", features = ["serde", "v4"] } +uuid = { version = "0.6.5", features = ["serde", "v4"] } nix = "0.17" libc = "^0.2" lazy_static = "1.4.0" @@ -18,4 +18,5 @@ futures = "0.3.5" guess_host_triple = "0.1.2" [dependencies.diesel] -version = "1.4.5" \ No newline at end of file +version = "1.4.5" +features = ["postgres", "uuid"] \ No newline at end of file diff --git a/lib/u_lib/src/config.rs b/lib/u_lib/src/config.rs index 23e78dd..7db464c 100644 --- a/lib/u_lib/src/config.rs +++ b/lib/u_lib/src/config.rs @@ -4,7 +4,6 @@ use uuid::Uuid; pub const MASTER_SERVER: Ipv4Addr = Ipv4Addr::LOCALHOST; //Ipv4Addr::new(3,9,16,40) pub const MASTER_PORT: u16 = 63714; -pub type Uid = String; lazy_static! { - pub static ref UID: Uid = Uuid::new_v4().to_string(); + pub static ref UID: Uuid = Uuid::new_v4(); } \ No newline at end of file diff --git a/lib/u_lib/src/contracts/agent.rs b/lib/u_lib/src/contracts/agent.rs index 3fd681c..91d3f85 100644 --- a/lib/u_lib/src/contracts/agent.rs +++ b/lib/u_lib/src/contracts/agent.rs @@ -30,7 +30,7 @@ pub async fn gather() -> IAgent { #[cfg(unix)] IAgent { alias: None, - agent_id: UID.clone().to_string(), + id: UID.clone(), hostname: run_cmd_fast("hostname".to_string()).await, is_root: &run_cmd_fast("id -u".to_string()).await == "0", is_root_allowed: false, //TODO diff --git a/lib/u_lib/src/contracts/messaging.rs b/lib/u_lib/src/contracts/messaging.rs index cd5b44b..c92b84b 100644 --- a/lib/u_lib/src/contracts/messaging.rs +++ b/lib/u_lib/src/contracts/messaging.rs @@ -6,7 +6,8 @@ use serde::{ use std::{ borrow::Cow, }; -use crate::{UID, Uid}; +use uuid::Uuid; +use crate::{UID}; pub trait ToMsg: Clone { //+ Serialize + DeserializeOwned { fn as_message<'m>(&'m self) -> Message<'m, Self> @@ -21,7 +22,7 @@ pub trait ToMsg: Clone { //+ Serialize + DeserializeOwned { #[derive(Serialize, Deserialize, Debug)] pub struct Message<'cow, I> where I: ToMsg { - pub id: Uid, + pub id: Uuid, pub item: Cow<'cow, I> } @@ -31,7 +32,7 @@ impl<'cow, I> Message<'cow, I> pub fn new(item: C) -> Self where C: Into> { Self { - id: UID.clone().to_string(), + id: UID.clone(), item: item.into() } } diff --git a/lib/u_lib/src/lib.rs b/lib/u_lib/src/lib.rs index dba6417..444a331 100644 --- a/lib/u_lib/src/lib.rs +++ b/lib/u_lib/src/lib.rs @@ -15,4 +15,7 @@ pub use { }; #[macro_use] -extern crate lazy_static; \ No newline at end of file +extern crate lazy_static; + +#[macro_use] +extern crate diesel; \ No newline at end of file diff --git a/lib/u_lib/src/models/agent.rs b/lib/u_lib/src/models/agent.rs index 2a8fe37..4a440f0 100644 --- a/lib/u_lib/src/models/agent.rs +++ b/lib/u_lib/src/models/agent.rs @@ -2,12 +2,14 @@ use serde::{ Serialize, Deserialize }; +use std::time::SystemTime; use diesel::{ Queryable, Identifiable, Insertable }; use crate::models::schema::*; +use uuid::Uuid; type Uid = String; @@ -16,14 +18,13 @@ type Uid = String; #[table_name = "agents"] pub struct Agent { pub alias: Option, - pub agent_id: Uid, pub hostname: String, - pub id: i32, + pub id: Uuid, pub is_root: bool, pub is_root_allowed: bool, - pub last_active: String, + pub last_active: SystemTime, pub platform: String, - pub regtime: String, + pub regtime: SystemTime, pub status: Option, pub token: Option, pub username: String @@ -33,7 +34,7 @@ pub struct Agent { #[table_name = "agents"] pub struct IAgent { pub alias: Option, - pub agent_id: Uid, + pub id: Uuid, pub hostname: String, pub is_root: bool, pub is_root_allowed: bool, diff --git a/lib/u_lib/src/models/mod.rs b/lib/u_lib/src/models/mod.rs index 9119f8a..6758fca 100644 --- a/lib/u_lib/src/models/mod.rs +++ b/lib/u_lib/src/models/mod.rs @@ -3,6 +3,3 @@ pub mod schema; pub use agent::*; - -#[macro_use] -extern crate diesel; diff --git a/migrations/2020-10-24-111622_create_all/up.sql b/migrations/2020-10-24-111622_create_all/up.sql index 991e28b..f88007b 100644 --- a/migrations/2020-10-24-111622_create_all/up.sql +++ b/migrations/2020-10-24-111622_create_all/up.sql @@ -1,10 +1,11 @@ +CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; + CREATE TABLE IF NOT EXISTS agents ( alias TEXT - , agent_id TEXT NOT NULL UNIQUE , hostname TEXT NOT NULL - , id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL - , is_root BOOLEAN NOT NULL DEFAULT 0 - , is_root_allowed BOOLEAN NOT NULL DEFAULT 0 + , id UUID NOT NULL DEFAULT uuid_generate_v4() + , is_root BOOLEAN NOT NULL DEFAULT false + , is_root_allowed BOOLEAN NOT NULL DEFAULT false , last_active TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP -- target triplet , platform TEXT NOT NULL @@ -13,47 +14,53 @@ CREATE TABLE IF NOT EXISTS agents ( -- is needed to processing requests , token TEXT , username TEXT NOT NULL + , PRIMARY KEY(id) ); CREATE TABLE IF NOT EXISTS ip_addrs ( - agent_id INTEGER NOT NULL - , check_ts DATETIME NOT NULL + agent_id UUID NOT NULL + , check_ts TIMESTAMP NOT NULL , gateway TEXT - , id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL + , id SERIAL , iface TEXT NOT NULL , ip_addr TEXT NOT NULL - , is_gray BOOLEAN NOT NULL DEFAULT 1 + , is_gray BOOLEAN NOT NULL DEFAULT true , netmask TEXT NOT NULL + , PRIMARY KEY(id) , FOREIGN KEY(agent_id) REFERENCES agents(id) ); CREATE TABLE IF NOT EXISTS jobs ( alias TEXT - , id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL - -- Shell, Binary (with program download), Python (with program and python download if not exist), Management + , id SERIAL + -- Shell, Binary (with program download), + -- Python (with program and python download if not exist), Management , job_type TEXT CHECK(job_type IN ('S','B','P','M')) NOT NULL DEFAULT 'S' -- Executable type: ALL - no matter, W - windows, L = linux , exec_type TEXT CHECK(exec_type IN ('ALL', 'W', 'L')) NOT NULL DEFAULT 'L' , platform TEXT CHECK(platform IN ('x86', 'x64', 'aarch32', 'aarch64')) - , data BLOB NOT NULL + , path TEXT NOT NULL + , PRIMARY KEY(id) ); CREATE TABLE IF NOT EXISTS results ( - agent_id INTEGER NOT NULL + agent_id UUID NOT NULL , created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP - , id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL + , id SERIAL , job_id INTEGER NOT NULL - , result BLOB + , result TEXT -- Queued, Pending, Running, Finished , status TEXT CHECK(status IN ('Q', 'P', 'R', 'F')) , ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP , FOREIGN KEY(agent_id) REFERENCES agents(id) , FOREIGN KEY(job_id) REFERENCES jobs(id) + , PRIMARY KEY(id) ); CREATE TABLE IF NOT EXISTS certificates ( - agent_id INTEGER NOT NULL - , id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL + agent_id UUID NOT NULL + , id SERIAL , is_revoked BOOLEAN NOT NULL DEFAULT FALSE + , PRIMARY KEY(id) , FOREIGN KEY(agent_id) REFERENCES agents(id) ); \ No newline at end of file diff --git a/run_db.sh b/run_db.sh new file mode 100755 index 0000000..c10d80d --- /dev/null +++ b/run_db.sh @@ -0,0 +1,8 @@ +#!/bin/bash +docker run \ + -d \ + --rm \ + --name u_db \ + -e POSTGRES_PASSWORD=12348756 \ + -v $(pwd)/data:/var/lib/postgresql/data \ + postgres