From 9b189fc0454cbc9c4d37a3e7ba7150764e738255 Mon Sep 17 00:00:00 2001 From: Aumetra Weisman Date: Mon, 1 Apr 2024 21:00:21 +0200 Subject: [PATCH] finish --- Cargo.lock | 140 -------------------------- crates/kitsune-activitypub/Cargo.toml | 1 - crates/kitsune-db/Cargo.toml | 7 +- crates/kitsune-db/src/pool.rs | 15 ++- kitsune/src/http/extractor/auth.rs | 6 +- kitsune/src/main.rs | 8 +- kitsune/src/oauth2/authorizer.rs | 11 +- kitsune/src/oauth2/issuer.rs | 43 ++++---- kitsune/src/oauth2/registrar.rs | 14 ++- kitsune/src/oauth2/solicitor.rs | 9 +- 10 files changed, 72 insertions(+), 182 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e54d8bbae..7eb5a6187 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -572,32 +572,6 @@ dependencies = [ "syn 2.0.55", ] -[[package]] -name = "aws-lc-rs" -version = "1.6.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df33e4a55b03f8780ba55041bc7be91a2a8ec8c03517b0379d2d6c96d2c30d95" -dependencies = [ - "aws-lc-sys", - "mirai-annotations", - "paste", - "zeroize", -] - -[[package]] -name = "aws-lc-sys" -version = "0.13.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37ede3d6e360a48436fee127cb81710834407b1ec0c48a001cc29dec9005f73e" -dependencies = [ - "bindgen", - "cmake", - "dunce", - "fs_extra", - "libc", - "paste", -] - [[package]] name = "axum" version = "0.6.20" @@ -869,29 +843,6 @@ dependencies = [ "serde", ] -[[package]] -name = "bindgen" -version = "0.69.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a00dc851838a2120612785d195287475a3ac45514741da670b735818822129a0" -dependencies = [ - "bitflags 2.5.0", - "cexpr", - "clang-sys", - "itertools 0.12.1", - "lazy_static", - "lazycell", - "log", - "prettyplease", - "proc-macro2", - "quote", - "regex", - "rustc-hash", - "shlex", - "syn 2.0.55", - "which", -] - [[package]] name = "bit-set" version = "0.5.3" @@ -1248,15 +1199,6 @@ version = "1.0.90" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8cd6604a82acf3039f1144f54b8eb34e91ffba622051189e71b781822d5ee1f5" -[[package]] -name = "cexpr" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" -dependencies = [ - "nom", -] - [[package]] name = "cfg-if" version = "1.0.0" @@ -1321,17 +1263,6 @@ dependencies = [ "half", ] -[[package]] -name = "clang-sys" -version = "1.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67523a3b4be3ce1989d607a828d036249522dd9c1c8de7f4dd2dae43a37369d1" -dependencies = [ - "glob", - "libc", - "libloading", -] - [[package]] name = "clap" version = "4.5.4" @@ -1383,15 +1314,6 @@ dependencies = [ "serde", ] -[[package]] -name = "cmake" -version = "0.1.50" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a31c789563b815f77f4250caee12365734369f942439b7defd71e18a48197130" -dependencies = [ - "cc", -] - [[package]] name = "color-eyre" version = "0.6.3" @@ -2171,12 +2093,6 @@ dependencies = [ "dtoa", ] -[[package]] -name = "dunce" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56ce8c6da7551ec6c462cbaf3bfbc75131ebbfa1c944aeaa9dab51ca1c5f0c3b" - [[package]] name = "dyn-clone" version = "1.0.17" @@ -3062,15 +2978,6 @@ dependencies = [ "digest", ] -[[package]] -name = "home" -version = "0.5.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5" -dependencies = [ - "windows-sys 0.52.0", -] - [[package]] name = "hostname" version = "0.3.1" @@ -3757,7 +3664,6 @@ dependencies = [ "mime_guess", "pretty_assertions", "rsa", - "scoped-futures", "serde", "sha2", "simd-json", @@ -4485,16 +4391,6 @@ version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" -[[package]] -name = "libloading" -version = "0.8.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19" -dependencies = [ - "cfg-if", - "windows-targets 0.52.4", -] - [[package]] name = "libm" version = "0.2.8" @@ -4977,12 +4873,6 @@ dependencies = [ "windows-sys 0.48.0", ] -[[package]] -name = "mirai-annotations" -version = "1.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c9be0862c1b3f26a88803c4a49de6889c10e608b3ee9344e6ef5b45fb37ad3d1" - [[package]] name = "moka" version = "0.12.5" @@ -6008,16 +5898,6 @@ dependencies = [ "yansi", ] -[[package]] -name = "prettyplease" -version = "0.2.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d3928fb5db768cb86f891ff014f0144589297e3c6a1aba6ed7cecfdace270c7" -dependencies = [ - "proc-macro2", - "syn 2.0.55", -] - [[package]] name = "primeorder" version = "0.13.6" @@ -6757,7 +6637,6 @@ version = "0.23.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8c4d6d8ad9f2492485e13453acbb291dd08f64441b6609c491f1c2cd2c6b4fe1" dependencies = [ - "aws-lc-rs", "log", "once_cell", "ring", @@ -6821,7 +6700,6 @@ version = "0.102.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "faaa0a62740bedb9b2ef5afa303da42764c012f743917351dc9a237ea1663610" dependencies = [ - "aws-lc-rs", "ring", "rustls-pki-types", "untrusted", @@ -7265,12 +7143,6 @@ dependencies = [ "lazy_static", ] -[[package]] -name = "shlex" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" - [[package]] name = "signal-hook-registry" version = "1.4.1" @@ -9152,18 +9024,6 @@ dependencies = [ "once_cell", ] -[[package]] -name = "which" -version = "4.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7" -dependencies = [ - "either", - "home", - "once_cell", - "rustix", -] - [[package]] name = "whichlang" version = "0.1.0" diff --git a/crates/kitsune-activitypub/Cargo.toml b/crates/kitsune-activitypub/Cargo.toml index 8dd1c6c02..504734144 100644 --- a/crates/kitsune-activitypub/Cargo.toml +++ b/crates/kitsune-activitypub/Cargo.toml @@ -33,7 +33,6 @@ kitsune-wasm-mrf = { path = "../kitsune-wasm-mrf" } mime = "0.3.17" mime_guess = { version = "2.0.4", default-features = false } rsa = "0.9.6" -scoped-futures = "0.1.3" serde = "1.0.197" sha2 = "0.10.8" simd-json = "0.13.9" diff --git a/crates/kitsune-db/Cargo.toml b/crates/kitsune-db/Cargo.toml index ba7fe5965..687fc80f5 100644 --- a/crates/kitsune-db/Cargo.toml +++ b/crates/kitsune-db/Cargo.toml @@ -26,7 +26,12 @@ kitsune-language = { path = "../kitsune-language" } kitsune-type = { path = "../kitsune-type" } num-derive = "0.4.2" num-traits = "0.2.18" -rustls = "0.23.4" +rustls = { version = "0.23.4", default-features = false, features = [ + "logging", + "ring", + "std", + "tls12", +] } rustls-native-certs = "0.7.0" serde = { version = "1.0.197", features = ["derive"] } simd-json = "0.13.9" diff --git a/crates/kitsune-db/src/pool.rs b/crates/kitsune-db/src/pool.rs index 9eaff5c74..2201c9758 100644 --- a/crates/kitsune-db/src/pool.rs +++ b/crates/kitsune-db/src/pool.rs @@ -7,13 +7,20 @@ macro_rules! with_connection { }}; } +#[macro_export] +macro_rules! catch_error { + ($($tt:tt)*) => {{ + let result: ::std::result::Result<_, ::diesel_async::pooled_connection::bb8::RunError> = async { + Ok({ $($tt)* }) + }.await; + result + }}; +} + #[macro_export] macro_rules! with_connection_panicky { ($pool:expr, $($other:tt)*) => {{ - let result: ::std::result::Result<_, Box> = async { - Ok($crate::with_connection!($pool, $($other)*)) - }.await; - result.unwrap() + $crate::catch_error!($crate::with_connection!($pool, $($other)*)).unwrap() }}; } diff --git a/kitsune/src/http/extractor/auth.rs b/kitsune/src/http/extractor/auth.rs index 4f3e14e2f..7101c993c 100644 --- a/kitsune/src/http/extractor/auth.rs +++ b/kitsune/src/http/extractor/auth.rs @@ -11,6 +11,7 @@ use diesel_async::RunQueryDsl; use headers::{authorization::Bearer, Authorization}; use http::request::Parts; use kitsune_db::{ + catch_error, model::{account::Account, user::User}, schema::{accounts, oauth2_access_tokens, users}, with_connection, @@ -62,13 +63,14 @@ impl FromRequestParts .filter(oauth2_access_tokens::expires_at.gt(OffsetDateTime::now_utc())); } - let (user, account) = with_connection!(state.db_pool, |db_conn| { + let (user, account) = catch_error!(with_connection!(state.db_pool, |db_conn| { user_account_query .select(<(User, Account)>::as_select()) .get_result(db_conn) .await .map_err(Error::from) - })?; + })) + .map_err(Error::from)??; Ok(Self(UserData { account, user })) } diff --git a/kitsune/src/main.rs b/kitsune/src/main.rs index 6c0d19276..79f17be8a 100644 --- a/kitsune/src/main.rs +++ b/kitsune/src/main.rs @@ -1,9 +1,9 @@ use clap::Parser; +use color_eyre::eyre::{self, Context}; use kitsune::consts::STARTUP_FIGLET; use kitsune_config::Configuration; use kitsune_core::consts::VERSION; use kitsune_job_runner::JobDispatcherState; -use miette::{Context, IntoDiagnostic}; use std::{env, path::PathBuf}; #[global_allocator] @@ -18,7 +18,7 @@ struct Args { config: PathBuf, } -async fn boot() -> miette::Result<()> { +async fn boot() -> eyre::Result<()> { println!("{STARTUP_FIGLET}"); let args = Args::parse(); @@ -64,8 +64,8 @@ async fn boot() -> miette::Result<()> { Ok(()) } -fn main() -> miette::Result<()> { - miette::set_panic_hook(); +fn main() -> eyre::Result<()> { + color_eyre::install()?; let runtime = tokio::runtime::Builder::new_multi_thread() .enable_all() diff --git a/kitsune/src/oauth2/authorizer.rs b/kitsune/src/oauth2/authorizer.rs index f377760d5..9c4184413 100644 --- a/kitsune/src/oauth2/authorizer.rs +++ b/kitsune/src/oauth2/authorizer.rs @@ -3,6 +3,7 @@ use async_trait::async_trait; use diesel::{OptionalExtension, QueryDsl}; use diesel_async::RunQueryDsl; use kitsune_db::{ + catch_error, model::oauth2, schema::{oauth2_applications, oauth2_authorization_codes}, with_connection, PgPool, @@ -25,7 +26,7 @@ impl Authorizer for OAuthAuthorizer { let secret = generate_secret(); let expires_at = chrono_to_timestamp(grant.until); - with_connection!(self.db_pool, |db_conn| { + catch_error!(with_connection!(self.db_pool, |db_conn| { diesel::insert_into(oauth2_authorization_codes::table) .values(oauth2::NewAuthorizationCode { code: secret.as_str(), @@ -37,19 +38,21 @@ impl Authorizer for OAuthAuthorizer { .returning(oauth2_authorization_codes::code) .get_result(db_conn) .await - }) + })) + .map_err(|_| ())? .map_err(|_| ()) } async fn extract(&mut self, authorization_code: &str) -> Result, ()> { - let oauth_data = with_connection!(self.db_pool, |db_conn| { + let oauth_data = catch_error!(with_connection!(self.db_pool, |db_conn| { oauth2_authorization_codes::table .find(authorization_code) .inner_join(oauth2_applications::table) .first::<(oauth2::AuthorizationCode, oauth2::Application)>(db_conn) .await .optional() - }) + })) + .map_err(|_| ())? .map_err(|_| ())?; let oauth_data = oauth_data.map(|(code, app)| { diff --git a/kitsune/src/oauth2/issuer.rs b/kitsune/src/oauth2/issuer.rs index 02eeda4d8..4074a675b 100644 --- a/kitsune/src/oauth2/issuer.rs +++ b/kitsune/src/oauth2/issuer.rs @@ -4,6 +4,7 @@ use async_trait::async_trait; use diesel::{ExpressionMethods, OptionalExtension, QueryDsl, SelectableHelper}; use diesel_async::RunQueryDsl; use kitsune_db::{ + catch_error, model::oauth2, schema::{oauth2_access_tokens, oauth2_applications, oauth2_refresh_tokens}, with_connection, with_transaction, PgPool, @@ -29,7 +30,7 @@ impl Issuer for OAuthIssuer { let scopes = grant.scope.to_string(); let expires_at = chrono_to_timestamp(grant.until); - let (access_token, refresh_token) = with_transaction!(self.db_pool, |tx| { + let (access_token, refresh_token) = catch_error!(with_transaction!(self.db_pool, |tx| { let access_token = diesel::insert_into(oauth2_access_tokens::table) .values(oauth2::NewAccessToken { token: generate_secret().as_str(), @@ -53,7 +54,8 @@ impl Issuer for OAuthIssuer { .await?; Ok::<_, Error>((access_token, refresh_token)) - }) + })) + .map_err(|_| ())? .map_err(|_| ())?; Ok(IssuedToken { @@ -65,17 +67,19 @@ impl Issuer for OAuthIssuer { } async fn refresh(&mut self, refresh_token: &str, grant: Grant) -> Result { - let (refresh_token, access_token) = with_connection!(self.db_pool, |db_conn| { - oauth2_refresh_tokens::table - .find(refresh_token) - .inner_join(oauth2_access_tokens::table) - .select(<(oauth2::RefreshToken, oauth2::AccessToken)>::as_select()) - .get_result::<(oauth2::RefreshToken, oauth2::AccessToken)>(db_conn) - .await - }) - .map_err(|_| ())?; - - let (access_token, refresh_token) = with_transaction!(self.db_pool, |tx| { + let (refresh_token, access_token) = + catch_error!(with_connection!(self.db_pool, |db_conn| { + oauth2_refresh_tokens::table + .find(refresh_token) + .inner_join(oauth2_access_tokens::table) + .select(<(oauth2::RefreshToken, oauth2::AccessToken)>::as_select()) + .get_result::<(oauth2::RefreshToken, oauth2::AccessToken)>(db_conn) + .await + })) + .map_err(|_| ())? + .map_err(|_| ())?; + + let (access_token, refresh_token) = catch_error!(with_transaction!(self.db_pool, |tx| { let new_access_token = diesel::insert_into(oauth2_access_tokens::table) .values(oauth2::NewAccessToken { user_id: access_token.user_id, @@ -95,7 +99,8 @@ impl Issuer for OAuthIssuer { diesel::delete(&access_token).execute(tx).await?; Ok::<_, Error>((new_access_token, refresh_token)) - }) + })) + .map_err(|_| ())? .map_err(|_| ())?; Ok(RefreshedToken { @@ -107,7 +112,7 @@ impl Issuer for OAuthIssuer { } async fn recover_token(&mut self, access_token: &str) -> Result, ()> { - let oauth_data = with_connection!(self.db_pool, |db_conn| { + let oauth_data = catch_error!(with_connection!(self.db_pool, |db_conn| { oauth2_access_tokens::table .find(access_token) .inner_join(oauth2_applications::table) @@ -115,7 +120,8 @@ impl Issuer for OAuthIssuer { .get_result::<(oauth2::AccessToken, oauth2::Application)>(db_conn) .await .optional() - }) + })) + .map_err(|_| ())? .map_err(|_| ())?; let oauth_data = oauth_data.map(|(access_token, app)| { @@ -141,7 +147,7 @@ impl Issuer for OAuthIssuer { } async fn recover_refresh(&mut self, refresh_token: &str) -> Result, ()> { - let oauth_data = with_connection!(self.db_pool, |db_conn| { + let oauth_data = catch_error!(with_connection!(self.db_pool, |db_conn| { oauth2_refresh_tokens::table .find(refresh_token) .inner_join(oauth2_access_tokens::table) @@ -150,7 +156,8 @@ impl Issuer for OAuthIssuer { .get_result::<(oauth2::AccessToken, oauth2::Application)>(db_conn) .await .optional() - }) + })) + .map_err(|_| ())? .map_err(|_| ())?; let oauth_data = oauth_data.map(|(access_token, app)| { diff --git a/kitsune/src/oauth2/registrar.rs b/kitsune/src/oauth2/registrar.rs index 51dd26145..d6f567974 100644 --- a/kitsune/src/oauth2/registrar.rs +++ b/kitsune/src/oauth2/registrar.rs @@ -1,7 +1,9 @@ use async_trait::async_trait; use diesel::{ExpressionMethods, OptionalExtension, QueryDsl}; use diesel_async::RunQueryDsl; -use kitsune_db::{model::oauth2, schema::oauth2_applications, with_connection, PgPool}; +use kitsune_db::{ + catch_error, model::oauth2, schema::oauth2_applications, with_connection, PgPool, +}; use oxide_auth::{ endpoint::{PreGrant, Scope}, primitives::registrar::{BoundClient, ClientUrl, ExactUrl, RegisteredUrl, RegistrarError}, @@ -46,14 +48,15 @@ impl Registrar for OAuthRegistrar { .parse() .map_err(|_| RegistrarError::PrimitiveError)?; - let client = with_connection!(self.db_pool, |db_conn| { + let client = catch_error!(with_connection!(self.db_pool, |db_conn| { oauth2_applications::table .find(client_id) .filter(oauth2_applications::redirect_uri.eq(client.redirect_uri.as_str())) .get_result::(db_conn) .await .optional() - }) + })) + .map_err(|_| RegistrarError::PrimitiveError)? .map_err(|_| RegistrarError::PrimitiveError)? .ok_or(RegistrarError::Unspecified)?; @@ -104,13 +107,14 @@ impl Registrar for OAuthRegistrar { client_query = client_query.filter(oauth2_applications::secret.eq(passphrase)); } - with_connection!(self.db_pool, |db_conn| { + catch_error!(with_connection!(self.db_pool, |db_conn| { client_query .select(oauth2_applications::id) .execute(db_conn) .await .optional() - }) + })) + .map_err(|_| RegistrarError::PrimitiveError)? .map_err(|_| RegistrarError::PrimitiveError)? .map(|_| ()) .ok_or(RegistrarError::Unspecified) diff --git a/kitsune/src/oauth2/solicitor.rs b/kitsune/src/oauth2/solicitor.rs index f9747ad50..a2b0371ed 100644 --- a/kitsune/src/oauth2/solicitor.rs +++ b/kitsune/src/oauth2/solicitor.rs @@ -4,7 +4,9 @@ use async_trait::async_trait; use cursiv::CsrfHandle; use diesel::{OptionalExtension, QueryDsl}; use diesel_async::RunQueryDsl; -use kitsune_db::{model::user::User, schema::oauth2_applications, with_connection, PgPool}; +use kitsune_db::{ + catch_error, model::user::User, schema::oauth2_applications, with_connection, PgPool, +}; use oxide_auth::endpoint::{OAuthError, OwnerConsent, QueryParameter, Solicitation, WebRequest}; use oxide_auth_async::endpoint::OwnerSolicitor; use oxide_auth_axum::{OAuthRequest, OAuthResponse, WebError}; @@ -79,14 +81,15 @@ impl OAuthOwnerSolicitor { .parse() .map_err(|_| WebError::Endpoint(OAuthError::BadRequest))?; - let app_name = with_connection!(self.db_pool, |db_conn| { + let app_name = catch_error!(with_connection!(self.db_pool, |db_conn| { oauth2_applications::table .find(client_id) .select(oauth2_applications::name) .get_result::(db_conn) .await .optional() - }) + })) + .map_err(|_| WebError::InternalError(None))? .map_err(|_| WebError::InternalError(None))? .ok_or(WebError::Endpoint(OAuthError::DenySilently))?;