From 2015a08d22b21ba58b474a163896a180868ea747 Mon Sep 17 00:00:00 2001 From: Samuel Gomez Date: Sun, 14 Apr 2024 08:41:05 +0100 Subject: [PATCH] Removing cloudsmith dependencies and improving server response --- .cargo/config | 3 - Cargo.lock | 92 +++++++++++++++++------------- Cargo.toml | 6 +- src/algebra/mod.rs | 12 ++-- src/algebra/refresh.rs | 13 +++-- src/algebra/trigger.rs | 11 ++-- src/service/http/admin/refresh.rs | 2 +- src/service/http/middleware.rs | 2 +- src/service/http/mod.rs | 43 +++----------- src/service/http/public/health.rs | 5 +- src/service/http/public/trigger.rs | 3 +- src/service/mod.rs | 30 +++++----- 12 files changed, 98 insertions(+), 124 deletions(-) delete mode 100644 .cargo/config diff --git a/.cargo/config b/.cargo/config deleted file mode 100644 index 11ab0ed..0000000 --- a/.cargo/config +++ /dev/null @@ -1,3 +0,0 @@ -[registries.CLOUDSMITH] -index = "sparse+https://cargo.cloudsmith.io/integrationos/repository/" -credential-provider = "cargo:token" diff --git a/Cargo.lock b/Cargo.lock index 6241047..68f09f7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -221,24 +221,6 @@ dependencies = [ "url", ] -[[package]] -name = "actix-web-actors" -version = "4.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf6e9ccc371cfddbed7aa842256a4abc7a6dcac9f3fce392fe1d0f68cfd136b2" -dependencies = [ - "actix", - "actix-codec", - "actix-http", - "actix-web", - "bytes", - "bytestring", - "futures-core", - "pin-project-lite", - "tokio", - "tokio-util", -] - [[package]] name = "actix-web-codegen" version = "4.2.2" @@ -651,6 +633,20 @@ dependencies = [ "inout", ] +[[package]] +name = "combine" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba5a308b75df32fe02788e748662718f03fde005016435c444eea572398219fd" +dependencies = [ + "bytes", + "futures-core", + "memchr", + "pin-project-lite", + "tokio", + "tokio-util", +] + [[package]] name = "convert_case" version = "0.4.0" @@ -942,12 +938,6 @@ dependencies = [ "syn 1.0.109", ] -[[package]] -name = "derive" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72e3ae26c830a573f2e231fc2475f71fce4705609097cb9523abfc4007caed0b" - [[package]] name = "derive_more" version = "0.99.17" @@ -1103,15 +1093,6 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" -[[package]] -name = "features" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83072b3c84e55f9d0c0ff36a4575d0fd2e543ae4a56e04e7f5a9222188d574e3" -dependencies = [ - "bitflags 1.3.2", -] - [[package]] name = "finl_unicode" version = "1.2.0" @@ -1611,9 +1592,9 @@ dependencies = [ [[package]] name = "integrationos-domain" -version = "0.1.3" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d59e3478f25e9aefea50eb59f3252988be8c9969bdbd1809c86c8ca7e365ab4" +checksum = "dcc08bc095761357574216a564449339a1af1f234188781f2fd41b917e8de288" dependencies = [ "actix-web", "aes", @@ -1641,6 +1622,7 @@ dependencies = [ "pin-project", "prost", "rand", + "redis", "reqwest", "semver 1.0.21", "serde", @@ -1651,6 +1633,7 @@ dependencies = [ "thiserror", "tokio", "tracing", + "tracing-subscriber", "uuid", ] @@ -2112,15 +2095,12 @@ dependencies = [ "actix-cors", "actix-governor", "actix-web", - "actix-web-actors", "actix-web-lab", "anyhow", "chrono", - "derive", "dotenvy", "envconfig", "fake", - "features", "futures", "handlebars 5.1.1", "integrationos-domain", @@ -2133,7 +2113,6 @@ dependencies = [ "reqwest", "serde", "serde_json", - "thiserror", "tokio", "tracing", "tracing-actix-web", @@ -2568,6 +2547,30 @@ dependencies = [ "bitflags 2.4.2", ] +[[package]] +name = "redis" +version = "0.23.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f49cdc0bb3f412bf8e7d1bd90fe1d9eb10bc5c399ba90973c14662a27b3f8ba" +dependencies = [ + "arc-swap", + "async-trait", + "bytes", + "combine", + "futures", + "futures-util", + "itoa", + "percent-encoding", + "pin-project-lite", + "ryu", + "sha1_smol", + "socket2 0.4.10", + "tokio", + "tokio-retry", + "tokio-util", + "url", +] + [[package]] name = "redox_syscall" version = "0.4.1" @@ -3359,6 +3362,17 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-retry" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f57eb36ecbe0fc510036adff84824dd3c24bb781e21bfa67b69d556aa85214f" +dependencies = [ + "pin-project", + "rand", + "tokio", +] + [[package]] name = "tokio-rustls" version = "0.24.1" diff --git a/Cargo.toml b/Cargo.toml index 585bcfc..ecd07fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,6 @@ actix = "0.13.1" actix-cors = "0.7.0" actix-governor = "0.5.0" actix-web = "4.5.1" -actix-web-actors = "4.2.0" actix-web-lab = "0.20.2" anyhow = "1.0.79" chrono = { version = "0.4.33", features = ["serde"] } @@ -19,14 +18,13 @@ dotenvy = "0.15.7" envconfig = "0.10.0" futures = "0.3.30" handlebars = "5.1.1" -integrationos-domain = { version = "0.1.3", features = ["dummy", "actix-error"] } +integrationos-domain = { version = "1.3.0", features = ["dummy", "actix-error"] } jsonwebtoken = "9.2.0" moka = { version = "0.12.5", features = ["future"] } mongodb = "2.8.0" reqwest = "0.11.24" serde = { version = "1.0.196", features = ["derive"] } serde_json = "1.0.113" -thiserror = "1.0.56" tokio = { version = "1.35.1", features = ["macros", "rt-multi-thread"] } tracing = { version = "0.1.40", features = ["log"] } tracing-actix-web = "0.7.9" @@ -42,9 +40,7 @@ name = "oauth-api" path = "src/main.rs" [dev-dependencies] -derive = "1.0.0" fake = { version = "=2.9.1", features = ["dummy"] } -features = "0.10.0" mark-flaky-tests = { version = "1.0.2", features = ["tokio"] } once_cell = "1.19.0" rand = "0.8.5" diff --git a/src/algebra/mod.rs b/src/algebra/mod.rs index e3eec19..2494b45 100644 --- a/src/algebra/mod.rs +++ b/src/algebra/mod.rs @@ -10,16 +10,16 @@ pub use trigger::*; use chrono::{DateTime, Utc}; use integrationos_domain::{ - algebra::adapter::StoreAdapter, error::IntegrationOSError as Error, mongo::MongoDbStore, - Connection, Id, + algebra::{MongoStore, StoreExt}, + Connection, Id, IntegrationOSError, }; use mongodb::bson::doc; pub async fn get_connections_to_refresh( - collection: &MongoDbStore, + collection: &MongoStore, refresh_before: &DateTime, refresh_after: &DateTime, -) -> Result, Error> { +) -> Result, IntegrationOSError> { collection .get_many( Some(doc! { @@ -37,9 +37,9 @@ pub async fn get_connections_to_refresh( } pub async fn get_connection_to_trigger( - collection: &MongoDbStore, + collection: &MongoStore, id: Id, -) -> Result, Error> { +) -> Result, IntegrationOSError> { collection .get_one(doc! { "_id": id.to_string(), diff --git a/src/algebra/refresh.rs b/src/algebra/refresh.rs index c839da4..7936c72 100644 --- a/src/algebra/refresh.rs +++ b/src/algebra/refresh.rs @@ -5,15 +5,16 @@ use actix::prelude::*; use chrono::{Duration, Utc}; use futures::lock::Mutex; use integrationos_domain::{ - connection_oauth_definition::ConnectionOAuthDefinition, error::IntegrationOSError as Error, - mongo::MongoDbStore, service::secrets_client::SecretsClient, Connection, InternalError, + algebra::MongoStore, connection_oauth_definition::ConnectionOAuthDefinition, + error::IntegrationOSError as Error, service::secrets_client::SecretsClient, Connection, + InternalError, }; use reqwest::Client; use std::sync::Arc; pub struct RefreshActor { - connections: Arc>, - oauths: Arc>, + connections: Arc>, + oauths: Arc>, secrets: Arc, client: Client, state: Arc>, @@ -21,8 +22,8 @@ pub struct RefreshActor { impl RefreshActor { pub fn new( - oauths: Arc>, - connections: Arc>, + oauths: Arc>, + connections: Arc>, secrets: Arc, client: Client, ) -> Self { diff --git a/src/algebra/trigger.rs b/src/algebra/trigger.rs index 5113f5f..907a440 100644 --- a/src/algebra/trigger.rs +++ b/src/algebra/trigger.rs @@ -3,12 +3,11 @@ use crate::prelude::{Outcome, Trigger}; use actix::prelude::*; use chrono::Duration; use integrationos_domain::{ - algebra::adapter::StoreAdapter, + algebra::{MongoStore, StoreExt}, api_model_config::ContentType, connection_oauth_definition::{Computation, ConnectionOAuthDefinition, OAuthResponse}, error::IntegrationOSError as Error, get_secret_request::GetSecretRequest, - mongo::MongoDbStore, oauth_secret::OAuthSecret, service::secrets_client::SecretsClient, ApplicationError, Connection, Id, InternalError, OAuth, @@ -21,8 +20,8 @@ use tracing::warn; use tracing_actix_web::RequestId; pub struct TriggerActor { - connections: Arc>, - oauths: Arc>, + connections: Arc>, + oauths: Arc>, secrets_client: Arc, request_id: Option, client: Client, @@ -30,8 +29,8 @@ pub struct TriggerActor { impl TriggerActor { pub fn new( - connections: Arc>, - oauths: Arc>, + connections: Arc>, + oauths: Arc>, secrets_client: Arc, client: Client, request_id: Option, diff --git a/src/service/http/admin/refresh.rs b/src/service/http/admin/refresh.rs index e1e6511..3dc05f3 100644 --- a/src/service/http/admin/refresh.rs +++ b/src/service/http/admin/refresh.rs @@ -13,5 +13,5 @@ pub async fn get_state(state: Data) -> Result { .await .map_err(|e| InternalError::io_err(e.to_string().as_str(), None))?; - Ok(HttpResponse::Ok().json(ServerResponse::new(ResponseType::Query, response))) + Ok(ServerResponse::from(ResponseType::Query, response, 200)) } diff --git a/src/service/http/middleware.rs b/src/service/http/middleware.rs index fed5109..3ba1465 100644 --- a/src/service/http/middleware.rs +++ b/src/service/http/middleware.rs @@ -7,7 +7,7 @@ use actix_web::{ Error as ActixWebError, HttpMessage, }; use actix_web_lab::middleware::Next; -use integrationos_domain::{algebra::adapter::StoreAdapter, event_access::EventAccess, Claims}; +use integrationos_domain::{algebra::StoreExt, event_access::EventAccess, Claims}; use jsonwebtoken::{decode, DecodingKey, Validation}; use mongodb::bson::doc; use std::sync::Arc; diff --git a/src/service/http/mod.rs b/src/service/http/mod.rs index 1d7d3e7..b691fed 100644 --- a/src/service/http/mod.rs +++ b/src/service/http/mod.rs @@ -3,10 +3,12 @@ mod application; mod middleware; mod public; +use actix_web::{HttpResponse, HttpResponseBuilder}; pub use admin::*; pub use application::*; pub use middleware::*; pub use public::*; +use reqwest::StatusCode; #[derive(serde::Serialize, serde::Deserialize, Debug, Eq, PartialEq)] #[serde(rename_all = "camelCase")] @@ -24,48 +26,17 @@ where T: serde::Serialize, { #[serde(rename = "type")] - pub response_type: ResponseType, + pub r#type: ResponseType, pub args: T, + pub code: u16, } impl ServerResponse where T: serde::Serialize, { - pub fn new(response_type: ResponseType, args: T) -> Self { - Self { - response_type, - args, - } - } -} - -#[derive(serde::Serialize, serde::Deserialize, Debug)] -#[serde(rename_all = "camelCase")] -pub struct ServerError { - pub message: Vec, -} - -impl ServerError { - pub fn new(message: Vec) -> Self { - Self { message } - } -} - -impl From> for ServerResponse { - fn from(message: Vec) -> Self { - Self { - response_type: ResponseType::Error, - args: ServerError::new(message.iter().map(|s| s.to_string()).collect()), - } - } -} - -impl<'a> From> for ServerResponse { - fn from(message: Vec<&'a str>) -> Self { - Self { - response_type: ResponseType::Error, - args: ServerError::new(message.iter().map(|s| s.to_string()).collect()), - } + pub fn from(r#type: ResponseType, args: T, code: u16) -> HttpResponse { + HttpResponseBuilder::new(StatusCode::from_u16(code).unwrap_or(StatusCode::OK)) + .json(ServerResponse { r#type, args, code }) } } diff --git a/src/service/http/public/health.rs b/src/service/http/public/health.rs index ddf0de8..ea85250 100644 --- a/src/service/http/public/health.rs +++ b/src/service/http/public/health.rs @@ -3,8 +3,5 @@ use actix_web::{get, HttpResponse}; #[get("/health_check")] pub async fn health_check() -> HttpResponse { - HttpResponse::Ok().json(ServerResponse::new( - ResponseType::Health, - "I'm alive!".to_string(), - )) + ServerResponse::from(ResponseType::Health, "I'm alive!".to_string(), 200) } diff --git a/src/service/http/public/trigger.rs b/src/service/http/public/trigger.rs index 6192226..732b734 100644 --- a/src/service/http/public/trigger.rs +++ b/src/service/http/public/trigger.rs @@ -50,5 +50,6 @@ pub async fn trigger_refresh( "outcome": outcome, }); - Ok(HttpResponse::Ok().json(ServerResponse::new(ResponseType::Trigger, json))) + // Ok(HttpResponse::Ok().json(ServerResponse::new(ResponseType::Trigger, json))) + Ok(ServerResponse::from(ResponseType::Trigger, json, 200)) } diff --git a/src/service/mod.rs b/src/service/mod.rs index 11f2443..f5da017 100644 --- a/src/service/mod.rs +++ b/src/service/mod.rs @@ -7,9 +7,9 @@ pub use http::*; use crate::prelude::RefreshActor; use actix::{Addr, Supervisor}; use integrationos_domain::{ - connection_oauth_definition::ConnectionOAuthDefinition, error::IntegrationOSError as Error, - event_access::EventAccess, mongo::MongoDbStore, service::secrets_client::SecretsClient, - Connection, InternalError, Store, + algebra::MongoStore, connection_oauth_definition::ConnectionOAuthDefinition, + error::IntegrationOSError as Error, event_access::EventAccess, + service::secrets_client::SecretsClient, Connection, InternalError, Store, }; use moka::future::Cache; use mongodb::options::FindOptions; @@ -23,9 +23,9 @@ pub struct AppState { cache: Cache>, client: Client, secrets: Arc, - connections: Arc>, - oauths: Arc>, - event_access: Arc>, + connections: Arc>, + oauths: Arc>, + event_access: Arc>, refresh_actor: Addr, } @@ -61,16 +61,14 @@ impl AppState { let database = mongo_client.database(config.oauth().database().control_db_name.as_ref()); let secrets = SecretsClient::new(config.oauth().secrets_config())?; - let oauths = MongoDbStore::::new_with_db( - database.clone(), - Store::ConnectionOAuthDefinitions, + let oauths = MongoStore::::new( + &database, + &Store::ConnectionOAuthDefinitions, ) .await?; - let connections = - MongoDbStore::::new_with_db(database.clone(), Store::Connections).await?; + let connections = MongoStore::::new(&database, &Store::Connections).await?; let cache = Cache::new(config.server().cache_size()); - let event_access = - MongoDbStore::::new_with_db(database.clone(), Store::EventAccess).await?; + let event_access = MongoStore::::new(&database, &Store::EventAccess).await?; let oauths = Arc::new(oauths); let connections = Arc::new(connections); @@ -105,7 +103,7 @@ impl AppState { &self.client } - pub fn connections(&self) -> &Arc> { + pub fn connections(&self) -> &Arc> { &self.connections } @@ -113,11 +111,11 @@ impl AppState { &self.cache } - pub fn oauths(&self) -> &Arc> { + pub fn oauths(&self) -> &Arc> { &self.oauths } - pub fn event_access(&self) -> &Arc> { + pub fn event_access(&self) -> &Arc> { &self.event_access }