postgressed

4-update-check
plazmoid 4 years ago
parent 3fc7da93ec
commit b0ca782fcb
  1. 2
      .env
  2. 1
      bin/u_server/.env
  3. 3
      bin/u_server/Cargo.toml
  4. 18
      bin/u_server/src/db.rs
  5. 8
      bin/u_server/src/main.rs
  6. 5
      lib/u_lib/Cargo.toml
  7. 3
      lib/u_lib/src/config.rs
  8. 2
      lib/u_lib/src/contracts/agent.rs
  9. 7
      lib/u_lib/src/contracts/messaging.rs
  10. 5
      lib/u_lib/src/lib.rs
  11. 11
      lib/u_lib/src/models/agent.rs
  12. 3
      lib/u_lib/src/models/mod.rs
  13. 39
      migrations/2020-10-24-111622_create_all/up.sql
  14. 8
      run_db.sh

@ -1 +1 @@
DATABASE_URL=./bin/u_server/u_server.db
export DATABASE_URL=postgres://postgres:12348756@172.17.0.2/u_db

@ -1 +0,0 @@
DATABASE_URL=./u_server.db

@ -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]

@ -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<Mutex<UDB>>;
pub struct UDB {
conn: SqliteConnection
conn: PgConnection
}
impl UDB {
pub fn new(path: Option<String>) -> USrvResult<Storage> {
pub fn new() -> USrvResult<Storage> {
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,

@ -24,7 +24,6 @@ use serde::{
de::DeserializeOwned
};
fn get_content<M>()
-> impl Filter<Extract = (Message<'static, M>,),
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;
}

@ -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"
version = "1.4.5"
features = ["postgres", "uuid"]

@ -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();
}

@ -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

@ -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<C>(item: C) -> Self
where C: Into<Cow<'cow, I>> {
Self {
id: UID.clone().to_string(),
id: UID.clone(),
item: item.into()
}
}

@ -15,4 +15,7 @@ pub use {
};
#[macro_use]
extern crate lazy_static;
extern crate lazy_static;
#[macro_use]
extern crate diesel;

@ -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<String>,
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<String>,
pub token: Option<String>,
pub username: String
@ -33,7 +34,7 @@ pub struct Agent {
#[table_name = "agents"]
pub struct IAgent {
pub alias: Option<String>,
pub agent_id: Uid,
pub id: Uuid,
pub hostname: String,
pub is_root: bool,
pub is_root_allowed: bool,

@ -3,6 +3,3 @@ pub mod schema;
pub use agent::*;
#[macro_use]
extern crate diesel;

@ -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)
);

@ -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
Loading…
Cancel
Save