diff --git a/core/src/api/build.rs b/core/src/api/build.rs index 8d70357fe..a99176637 100644 --- a/core/src/api/build.rs +++ b/core/src/api/build.rs @@ -6,7 +6,7 @@ use axum::{routing::post, Extension, Json, Router}; use db::DbExtension; use helpers::handle_anyhow_error; use mungos::ObjectId; -use types::{Build, EntityType, Operation, PermissionLevel, Update}; +use types::{Build, Operation, PermissionLevel, Update, UpdateTarget}; use crate::{auth::RequestUserExtension, ws::update}; @@ -49,8 +49,7 @@ async fn create( let start_ts = unix_timestamp_ms() as i64; let build_id = db.builds.create_one(build).await?; let update = Update { - entity_type: EntityType::Build, - entity_id: Some(build_id), + target: UpdateTarget::Build(build_id), operation: Operation::CreateBuild, start_ts, end_ts: unix_timestamp_ms() as i64, diff --git a/core/src/api/deployment.rs b/core/src/api/deployment.rs index 33872e7cd..3d86925bf 100644 --- a/core/src/api/deployment.rs +++ b/core/src/api/deployment.rs @@ -3,7 +3,7 @@ use async_timing_util::unix_timestamp_ms; use axum::{routing::post, Extension, Json, Router}; use db::DbExtension; use helpers::handle_anyhow_error; -use types::{Deployment, EntityType, Operation, PermissionLevel, Update}; +use types::{Deployment, Operation, PermissionLevel, Update, UpdateTarget}; use crate::{auth::RequestUserExtension, ws::update}; @@ -36,8 +36,7 @@ async fn create( .await .context("failed to add server to db")?; let update = Update { - entity_type: EntityType::Deployment, - entity_id: Some(deployment_id), + target: UpdateTarget::Deployment(deployment_id), operation: Operation::CreateDeployment, start_ts, end_ts: unix_timestamp_ms() as i64, diff --git a/core/src/api/server.rs b/core/src/api/server.rs index eee1a03d1..a6005124a 100644 --- a/core/src/api/server.rs +++ b/core/src/api/server.rs @@ -6,7 +6,7 @@ use axum::{ }; use db::DbExtension; use helpers::handle_anyhow_error; -use types::{EntityType, Operation, PermissionLevel, Server, Update}; +use types::{Operation, PermissionLevel, Server, Update, UpdateTarget}; use crate::{auth::RequestUserExtension, ws::update}; @@ -70,8 +70,7 @@ async fn create( .await .context("failed to add server to db")?; let update = Update { - entity_type: EntityType::Server, - entity_id: Some(server_id), + target: UpdateTarget::Server(server_id), operation: Operation::CreateServer, start_ts, end_ts: unix_timestamp_ms() as i64, diff --git a/core/src/ws/update.rs b/core/src/ws/update.rs index eda6334ab..bb723b04a 100644 --- a/core/src/ws/update.rs +++ b/core/src/ws/update.rs @@ -21,7 +21,7 @@ use tokio::{ }, }; use tokio_util::sync::CancellationToken; -use types::{EntityType, PermissionLevel, Update, User}; +use types::{PermissionLevel, Update, User, UpdateTarget}; use crate::auth::{JwtClient, JwtExtension}; @@ -104,8 +104,7 @@ pub async fn ws_handler( match user_can_see_update( &user, &user_id, - update.entity_type, - &update.entity_id, + &update.target, &db_client, ) .await @@ -196,25 +195,21 @@ async fn login( async fn user_can_see_update( user: &User, user_id: &str, - entity_type: EntityType, - entity_id: &Option, + update_target: &UpdateTarget, db_client: &DbClient, ) -> anyhow::Result<()> { if user.admin { return Ok(()); } - match entity_type { - EntityType::System => { + match update_target { + UpdateTarget::System => { if user.admin { Ok(()) } else { Err(anyhow!("user not admin, can't recieve system updates")) } } - EntityType::Server => { - let server_id = entity_id - .as_ref() - .ok_or(anyhow!("must pass entity_id for {entity_type}"))?; + UpdateTarget::Server(server_id) => { let server = db_client .servers .find_one_by_id(server_id) @@ -227,10 +222,7 @@ async fn user_can_see_update( Err(anyhow!("user does not have permissions on server")) } } - EntityType::Deployment => { - let deployment_id = entity_id - .as_ref() - .ok_or(anyhow!("must pass entity_id for {entity_type}"))?; + UpdateTarget::Deployment(deployment_id) => { let deployment = db_client .deployments .find_one_by_id(deployment_id) @@ -245,10 +237,7 @@ async fn user_can_see_update( Err(anyhow!("user does not have permissions on deployment")) } } - EntityType::Build => { - let build_id = entity_id - .as_ref() - .ok_or(anyhow!("must pass entity_id for {entity_type}"))?; + UpdateTarget::Build(build_id) => { let build = db_client .builds .find_one_by_id(build_id) diff --git a/lib/types/src/lib.rs b/lib/types/src/lib.rs index d976401cf..2e68be2ab 100644 --- a/lib/types/src/lib.rs +++ b/lib/types/src/lib.rs @@ -7,8 +7,6 @@ use strum_macros::{Display, EnumString}; pub const PERIPHERY_BUILDER_BUSY: &str = "builder is busy"; -pub type PermissionsMap = HashMap; - pub type UserId = String; pub type ServerId = String; pub type DeploymentId = String; @@ -24,6 +22,8 @@ pub type DockerAccounts = HashMap; pub type SecretsMap = HashMap; // these are used for injection into deployments run commands +pub type PermissionsMap = HashMap; + #[derive(Serialize, Deserialize, Debug, Clone, Default)] pub struct User { #[serde(rename = "_id", skip_serializing_if = "Option::is_none")] @@ -152,9 +152,7 @@ pub struct BuildRecord { pub struct Update { #[serde(rename = "_id", skip_serializing_if = "Option::is_none")] pub id: Option, - pub entity_type: EntityType, - #[serde(skip_serializing_if = "Option::is_none")] - pub entity_id: Option, + pub target: UpdateTarget, pub operation: Operation, pub log: Vec, pub start_ts: i64, @@ -259,13 +257,6 @@ pub struct EnvironmentVar { pub value: String, } -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct Permission { - pub entity_type: EntityType, - pub id: String, - pub level: PermissionLevel, -} - #[derive(Deserialize, Debug, Clone)] pub struct OauthCredentials { pub id: String, @@ -389,19 +380,18 @@ pub enum AccountType { Docker, } -#[derive(Serialize, Deserialize, Debug, Display, EnumString, PartialEq, Hash, Eq, Clone, Copy)] -#[serde(rename_all = "snake_case")] -#[strum(serialize_all = "snake_case")] -pub enum EntityType { +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(tag = "type", content = "id")] +pub enum UpdateTarget { System, - Build, - Deployment, - Server, + Build(String), + Deployment(String), + Server(String) } -impl Default for EntityType { +impl Default for UpdateTarget { fn default() -> Self { - EntityType::System + UpdateTarget::System } }