Resolve "TLS" Closes #9 See merge request root/unki!54-update-check
commit
18343cc829
36 changed files with 344 additions and 228 deletions
@ -1,3 +1,4 @@ |
||||
ADMIN_AUTH_TOKEN=464af63dbd241969baa1e94b2461d94d |
||||
POSTGRES_PASSWORD=12348756 |
||||
DATABASE_URL=postgres://postgres:${POSTGRES_PASSWORD}@u_db/u_db |
||||
DB_HOST=u_db |
||||
DB_NAME=u_db |
||||
DB_USER=postgres |
||||
RUST_BACKTRACE=1 |
@ -0,0 +1,4 @@ |
||||
# remove '.sample' to activate |
||||
ADMIN_AUTH_TOKEN= |
||||
DB_PASSWORD= |
||||
POSTGRES_PASSWORD=${DB_PASSWORD} |
@ -0,0 +1,8 @@ |
||||
use std::path::PathBuf; |
||||
|
||||
fn main() { |
||||
let server_cert = PathBuf::from("../../certs/ca.crt"); |
||||
if !server_cert.exists() { |
||||
panic!("CA certificate doesn't exist. Create it first with certs/gen_certs.sh"); |
||||
} |
||||
} |
@ -0,0 +1,17 @@ |
||||
set -ex |
||||
DIR=. |
||||
V3_CFG=v3.ext |
||||
|
||||
cat > $DIR/$V3_CFG << EOF |
||||
authorityKeyIdentifier=keyid,issuer |
||||
basicConstraints=CA:FALSE |
||||
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment, keyAgreement, keyCertSign, cRLSign |
||||
EOF |
||||
|
||||
|
||||
openssl req -x509 -newkey rsa:4096 -keyout $DIR/ca.key -out $DIR/ca.crt -nodes -days 365 -subj "/CN=root" |
||||
openssl req -newkey rsa:4096 -keyout $DIR/alice.key -out $DIR/alice.csr -nodes -days 365 -subj "/CN=alice" |
||||
openssl req -newkey rsa:4096 -keyout $DIR/server.key -out $DIR/server.csr -nodes -days 365 -subj "/CN=u_server" |
||||
openssl x509 -req -in $DIR/alice.csr -CA $DIR/ca.crt -CAkey $DIR/ca.key -out $DIR/alice.crt -set_serial 01 -days 365 -extfile $DIR/$V3_CFG |
||||
openssl x509 -req -in $DIR/server.csr -CA $DIR/ca.crt -CAkey $DIR/ca.key -out $DIR/server.crt -set_serial 01 -days 365 -extfile $DIR/$V3_CFG |
||||
openssl pkcs12 -export -out $DIR/alice.p12 -inkey $DIR/alice.key -in $DIR/alice.crt -passin pass: -passout pass: |
@ -1,5 +1,3 @@ |
||||
pub mod client; |
||||
pub mod panel; |
||||
|
||||
pub use client::AgentClient; |
||||
pub use panel::Panel; |
||||
|
@ -1,80 +1,67 @@ |
||||
mod helpers; |
||||
|
||||
use helpers::{AgentClient, Panel}; |
||||
use helpers::Panel; |
||||
|
||||
use serde_json::json; |
||||
use std::error::Error; |
||||
use std::thread::sleep; |
||||
use std::time::Duration; |
||||
use u_lib::{api::ClientHandler, models::*}; |
||||
use uuid::Uuid; |
||||
|
||||
type TestResult<R = ()> = Result<R, Box<dyn std::error::Error>>; |
||||
type TestResult<R = ()> = Result<R, Box<dyn Error>>; |
||||
|
||||
async fn register_agent() -> Uuid { |
||||
let cli = AgentClient::new(); |
||||
let cli = ClientHandler::new(None); |
||||
let agent_uid = Uuid::new_v4(); |
||||
let resp = cli.get(format!("get_agent_jobs/{}", agent_uid)).await; |
||||
let job_id = &resp["job_id"]; |
||||
let resp = cli.get(format!("get_jobs/{}", job_id)).await; |
||||
assert_eq!(&resp["alias"], "agent_hello"); |
||||
let agent_data = json! { |
||||
{"id": &agent_uid,"inner":[ |
||||
{"Agent": |
||||
{"alias":null, |
||||
"hostname":"3b1030fa6324", |
||||
"id":&agent_uid, |
||||
"is_root":false, |
||||
"is_root_allowed":false, |
||||
"last_active":{"secs_since_epoch":1625271265,"nanos_since_epoch":92814921}, |
||||
"platform":"x86_64-unknown-linux-gnu", |
||||
"regtime":{"secs_since_epoch":1625271265,"nanos_since_epoch":92814945}, |
||||
"state":"New", |
||||
"token":null, |
||||
"username":"root"} |
||||
} |
||||
]} |
||||
let resp = cli |
||||
.get_personal_jobs(Some(agent_uid)) |
||||
.await |
||||
.unwrap() |
||||
.pop() |
||||
.unwrap(); |
||||
let job_id = resp.job_id; |
||||
let resp = cli.get_jobs(Some(job_id)).await.unwrap().pop().unwrap(); |
||||
assert_eq!(resp.alias, Some("agent_hello".to_string())); |
||||
let agent_data = Agent { |
||||
id: agent_uid, |
||||
..Default::default() |
||||
}; |
||||
cli.post("report", &agent_data).await; |
||||
cli.report(&vec![ExecResult::Agent(agent_data)]) |
||||
.await |
||||
.unwrap(); |
||||
agent_uid |
||||
} |
||||
|
||||
#[tokio::test] |
||||
async fn test_registration() -> TestResult { |
||||
let agent_uid = register_agent().await; |
||||
let agents = Panel::check_output("agents list"); |
||||
let found = agents |
||||
.iter() |
||||
.find(|v| v["id"].as_str().unwrap() == agent_uid.to_string()); |
||||
let agents: Vec<Agent> = Panel::check_output("agents list"); |
||||
let found = agents.iter().find(|v| v.id == agent_uid); |
||||
assert!(found.is_some()); |
||||
//teardown
|
||||
Panel::check_status::<i32>(&format!("agents delete {}", agent_uid)); |
||||
Ok(()) |
||||
} |
||||
|
||||
#[tokio::test] |
||||
async fn test_setup_tasks() -> TestResult { |
||||
let agent_uid = Panel::check_output("agents list")[0]["id"].clone(); |
||||
dbg!(&agent_uid); |
||||
//some independent agents should present
|
||||
let agents: Vec<Agent> = Panel::check_output("agents list"); |
||||
let agent_uid = agents[0].id; |
||||
let job_alias = "passwd_contents"; |
||||
let cmd = format!("jobs add --alias {} 'cat /etc/passwd'", job_alias); |
||||
Panel::check_status(cmd); |
||||
Panel::check_status::<Empty>(&cmd); |
||||
let cmd = format!("jobmap add {} {}", agent_uid, job_alias); |
||||
let assigned_uids = Panel::check_output(cmd); |
||||
dbg!(&assigned_uids); |
||||
loop { |
||||
let result = Panel::check_output(format!("jobmap list {}", assigned_uids[0])); |
||||
dbg!(&result); |
||||
match result.get(0) { |
||||
Some(entry) if entry["state"] == "Finished" => { |
||||
println!("{}", result[0]); |
||||
break; |
||||
} |
||||
None => { |
||||
eprintln!("jobmap list is empty (bad bad bad)"); |
||||
continue; |
||||
} |
||||
_ => { |
||||
sleep(Duration::from_secs(1)); |
||||
eprintln!("waiting for task"); |
||||
} |
||||
let assigned_uids: Vec<Uuid> = Panel::check_output(cmd); |
||||
for _ in 0..3 { |
||||
let result: Vec<AssignedJob> = |
||||
Panel::check_output(format!("jobmap list {}", assigned_uids[0])); |
||||
if result[0].state == JobState::Finished { |
||||
return Ok(()); |
||||
} else { |
||||
sleep(Duration::from_secs(5)); |
||||
eprintln!("waiting for task"); |
||||
} |
||||
} |
||||
Ok(()) |
||||
panic!() |
||||
} |
||||
|
@ -0,0 +1,10 @@ |
||||
use crate::UError; |
||||
use serde::{Deserialize, Serialize}; |
||||
|
||||
#[derive(Serialize, Deserialize)] |
||||
#[serde(rename_all = "lowercase")] |
||||
#[serde(tag = "status", content = "data")] |
||||
pub enum DataResult<M> { |
||||
Ok(M), |
||||
Err(UError), |
||||
} |
@ -1,8 +1,9 @@ |
||||
use crate::models::{Agent, AssignedJob}; |
||||
use serde::{Deserialize, Serialize}; |
||||
|
||||
#[derive(Serialize, Deserialize, Clone)] |
||||
#[derive(Serialize, Deserialize, Clone, PartialEq)] |
||||
pub enum ExecResult { |
||||
Assigned(AssignedJob), |
||||
Agent(Agent), |
||||
Dummy, |
||||
} |
||||
|
@ -1,11 +1,12 @@ |
||||
#!/bin/bash |
||||
set -e |
||||
set -ex |
||||
source $(dirname $0)/rootdir.sh #set ROOTDIR |
||||
umask 002 |
||||
ARGS=$@ |
||||
docker run \ |
||||
--env-file $ROOTDIR/.env \ |
||||
-v $ROOTDIR:/volume \ |
||||
-v cargo-cache:/root/.cargo/registry \ |
||||
-w /volume \ |
||||
-it \ |
||||
unki/musllibs \ |
||||
cargo $@ |
||||
bash -c "umask 0000; cargo $ARGS" |
||||
|
Loading…
Reference in new issue