diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 8f0e83a01..11d80dcd4 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -28,14 +28,6 @@ "cwd": "${workspaceFolder}/core" }, }, - { - "type": "cargo", - "command": "run", - "label": "run periphery", - "options": { - "cwd": "${workspaceFolder}/periphery" - }, - }, { "type": "cargo", "command": "run", @@ -55,17 +47,17 @@ { "type": "cargo", "command": "run", - "label": "run builder", + "label": "run cli", "options": { - "cwd": "${workspaceFolder}/builder" + "cwd": "${workspaceFolder}/cli" }, }, { "type": "cargo", "command": "run", - "label": "run cli", + "label": "run tests", "options": { - "cwd": "${workspaceFolder}/cli" + "cwd": "${workspaceFolder}/tests" }, }, { diff --git a/Cargo.lock b/Cargo.lock index 6e0913de1..f7b990613 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -367,7 +367,7 @@ dependencies = [ "helpers", "hmac", "jwt", - "monitor_types 0.1.0", + "monitor_types", "mungos", "oauth2", "periphery_client", @@ -516,7 +516,7 @@ version = "0.1.0" dependencies = [ "anyhow", "axum", - "monitor_types 0.1.0", + "monitor_types", "mungos", ] @@ -794,7 +794,7 @@ dependencies = [ "async_timing_util", "axum", "bollard", - "monitor_types 0.1.0", + "monitor_types", "rand", "run_command", "serde", @@ -1245,7 +1245,7 @@ name = "monitor_client" version = "0.1.1" dependencies = [ "anyhow", - "monitor_types 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "monitor_types", "reqwest", "serde", "serde_derive", @@ -1264,20 +1264,6 @@ dependencies = [ "strum_macros", ] -[[package]] -name = "monitor_types" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d94342b29a6ae3fd7fd603f1a904fd233008f426b844cd1f18fc8bfe71a74d79" -dependencies = [ - "async_timing_util", - "mungos", - "serde", - "serde_derive", - "strum", - "strum_macros", -] - [[package]] name = "mungos" version = "0.2.24" @@ -1492,7 +1478,7 @@ dependencies = [ "dotenv", "envy", "helpers", - "monitor_types 0.1.0", + "monitor_types", "run_command", "serde", "serde_derive", @@ -1509,7 +1495,7 @@ version = "0.1.0" dependencies = [ "anyhow", "helpers", - "monitor_types 0.1.0", + "monitor_types", "reqwest", "serde", "serde_json", @@ -2149,6 +2135,20 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "tests" +version = "0.1.0" +dependencies = [ + "anyhow", + "dotenv", + "envy", + "monitor_client", + "serde", + "serde_derive", + "serde_json", + "tokio", +] + [[package]] name = "thiserror" version = "1.0.36" @@ -2215,9 +2215,9 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" [[package]] name = "tokio" -version = "1.21.1" +version = "1.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0020c875007ad96677dcc890298f4b942882c5d4eb7cc8f439fc3bf813dc9c95" +checksum = "d76ce4a75fb488c605c54bf610f221cea8b0dafb53333c1a67e8ee199dcd2ae3" dependencies = [ "autocfg", "bytes", @@ -2225,7 +2225,6 @@ dependencies = [ "memchr", "mio", "num_cpus", - "once_cell", "parking_lot", "pin-project-lite", "signal-hook-registry", diff --git a/Cargo.toml b/Cargo.toml index 5c4faa265..d8b33e26d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ members = [ "cli", "core", "periphery", + "tests", "lib/db_client", "lib/helpers", "lib/periphery_client", diff --git a/core/src/auth/secret.rs b/core/src/auth/secret.rs index c2a073e50..0f3f43a05 100644 --- a/core/src/auth/secret.rs +++ b/core/src/auth/secret.rs @@ -9,7 +9,7 @@ use super::JwtExtension; #[derive(Deserialize)] pub struct SecretLoginBody { - user_id: String, + username: String, secret: String, } @@ -23,14 +23,15 @@ pub fn router() -> Router { pub async fn login( Extension(db): DbExtension, Extension(jwt): JwtExtension, - Json(SecretLoginBody { user_id, secret }): Json, + Json(SecretLoginBody { username, secret }): Json, ) -> anyhow::Result { let user = db .users - .find_one_by_id(&user_id) + .find_one(doc! { "username": &username }, None) .await .context("failed at mongo query")? - .ok_or(anyhow!("did not find user with id {user_id}"))?; + .ok_or(anyhow!("did not find user with username {username}"))?; + let user_id = user.id.unwrap().to_string(); let ts = unix_timestamp_ms() as i64; for s in user.secrets { if let Some(expires) = s.expires { diff --git a/lib/monitor_client/Cargo.toml b/lib/monitor_client/Cargo.toml index e14163acd..3e0c825e8 100644 --- a/lib/monitor_client/Cargo.toml +++ b/lib/monitor_client/Cargo.toml @@ -9,7 +9,8 @@ license = "MIT" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -monitor_types = "0.1.0" +# monitor_types = "0.1.0" +monitor_types = { path = "../types" } reqwest = { version = "0.11", features = ["json"] } anyhow = "1.0" serde = "1.0" diff --git a/lib/monitor_client/src/build.rs b/lib/monitor_client/src/build.rs new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/lib/monitor_client/src/build.rs @@ -0,0 +1 @@ + diff --git a/lib/monitor_client/src/deployment.rs b/lib/monitor_client/src/deployment.rs new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/lib/monitor_client/src/deployment.rs @@ -0,0 +1 @@ + diff --git a/lib/monitor_client/src/lib.rs b/lib/monitor_client/src/lib.rs index 0642ff04c..04c2c09ca 100644 --- a/lib/monitor_client/src/lib.rs +++ b/lib/monitor_client/src/lib.rs @@ -1,3 +1,5 @@ +#![allow(unused)] + use anyhow::{anyhow, Context}; use reqwest::StatusCode; use serde::{de::DeserializeOwned, Serialize}; @@ -5,6 +7,12 @@ use serde::{de::DeserializeOwned, Serialize}; pub use monitor_types as types; use serde_json::json; +mod build; +mod deployment; +mod permissions; +mod secret; +mod server; + #[derive(Clone)] pub struct MonitorClient { http_client: reqwest::Client, @@ -21,30 +29,38 @@ impl MonitorClient { } } - pub async fn new_with_credentials( + pub async fn new_with_password( url: &str, username: impl Into, password: impl Into, ) -> anyhow::Result { let mut client = MonitorClient::new_with_token(url, ""); - client.login(username, password).await?; - Ok(client) - } - - async fn login( - &mut self, - username: impl Into, - password: impl Into, - ) -> anyhow::Result<()> { - let token = self + let token = client .post_string( "/auth/local/login", json!({ "username": username.into(), "password": password.into() }), ) .await - .context("failed to log in")?; - self.token = token; - Ok(()) + .context("failed to log in with password")?; + client.token = token; + Ok(client) + } + + pub async fn new_with_secret( + url: &str, + username: impl Into, + secret: impl Into, + ) -> anyhow::Result { + let mut client = MonitorClient::new_with_token(url, ""); + let token = client + .post_string( + "/auth/secret/login", + json!({ "username": username.into(), "secret": secret.into() }), + ) + .await + .context("failed to log in with secret")?; + client.token = token; + Ok(client) } async fn get(&self, endpoint: &str) -> anyhow::Result { diff --git a/lib/monitor_client/src/permissions.rs b/lib/monitor_client/src/permissions.rs new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/lib/monitor_client/src/permissions.rs @@ -0,0 +1 @@ + diff --git a/lib/monitor_client/src/secret.rs b/lib/monitor_client/src/secret.rs new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/lib/monitor_client/src/secret.rs @@ -0,0 +1 @@ + diff --git a/lib/monitor_client/src/server.rs b/lib/monitor_client/src/server.rs new file mode 100644 index 000000000..fac0c49cf --- /dev/null +++ b/lib/monitor_client/src/server.rs @@ -0,0 +1,27 @@ +use monitor_types::{Server, SystemStats}; +use serde_json::json; + +use crate::MonitorClient; + +impl MonitorClient { + pub async fn list_servers(&self) -> anyhow::Result> { + self.get("/api/server/list").await + } + + pub async fn create_server(&self, name: &str, address: &str) -> anyhow::Result<()> { + self.post( + "/api/server/create", + json!({ "name": name, "address": address }), + ) + .await + } + + pub async fn delete_server(&self, id: &str) -> anyhow::Result<()> { + self.delete::<(), _>(&format!("/api/server/delete/{id}"), None) + .await + } + + pub async fn get_server_stats(&self, id: &str) -> anyhow::Result { + self.get(&format!("/api/server/stats/{id}")).await + } +} diff --git a/tests/Cargo.toml b/tests/Cargo.toml new file mode 100644 index 000000000..6a405a4ae --- /dev/null +++ b/tests/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "tests" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +tokio = { version = "1.22", features = ["full"] } +monitor_client = { path = "../lib/monitor_client" } +serde = "1.0" +serde_json = "1.0" +serde_derive = "1.0" +envy = "0.4" +anyhow = "1.0" +dotenv = "0.15" \ No newline at end of file diff --git a/tests/src/config.rs b/tests/src/config.rs new file mode 100644 index 000000000..8938b6b6e --- /dev/null +++ b/tests/src/config.rs @@ -0,0 +1,34 @@ +use dotenv::dotenv; +use monitor_client::MonitorClient; +use serde::Deserialize; + +#[derive(Deserialize)] +pub struct Env { + pub url: String, + pub token: Option, + pub username: Option, + pub password: Option, + pub secret: Option, +} + +pub async fn load() -> MonitorClient { + dotenv().ok(); + let env: Env = envy::from_env().expect("failed to parse env"); + if let Some(token) = env.token { + MonitorClient::new_with_token(&env.url, token) + } else if let Some(username) = env.username { + if let Some(password) = env.password { + MonitorClient::new_with_password(&env.url, username, password) + .await + .expect("failed to log in with username / password") + } else if let Some(secret) = env.secret { + MonitorClient::new_with_secret(&env.url, username, secret) + .await + .expect("failed to log in with username / secret") + } else { + panic!("must provide either password or secret to login in along with username") + } + } else { + panic!("must provide either token, username / password, or username / secret") + } +} diff --git a/tests/src/main.rs b/tests/src/main.rs new file mode 100644 index 000000000..216039b60 --- /dev/null +++ b/tests/src/main.rs @@ -0,0 +1,16 @@ +#![allow(unused)] + +mod config; + +#[tokio::main] +async fn main() { + let monitor = config::load().await; + let servers = monitor + .list_servers() + .await + .expect("failed at list servers"); + let server = servers.get(0).unwrap(); + let server_id = server.id.unwrap().to_string(); + let stats = monitor.get_server_stats(&server_id).await.unwrap(); + println!("{stats:#?}") +}