From 4c8146273bf1d573466da8a8ba7428f2c15fcac3 Mon Sep 17 00:00:00 2001 From: Eugene Pankov Date: Thu, 1 Sep 2022 20:57:05 +0200 Subject: [PATCH] lint --- warpgate-admin/src/api/roles.rs | 4 +-- warpgate-admin/src/api/targets.rs | 6 ++-- warpgate-admin/src/api/users.rs | 4 +-- warpgate-common/src/config/mod.rs | 2 +- warpgate-common/src/error.rs | 2 ++ warpgate-protocol-http/src/api/auth.rs | 12 ++++---- .../src/api/sso_provider_list.rs | 11 +++---- .../src/api/targets_list.rs | 14 ++++----- warpgate-protocol-http/src/catchall.rs | 7 ++--- warpgate-protocol-http/src/common.rs | 29 +++++++++---------- warpgate-protocol-http/src/error.rs | 4 +-- warpgate-protocol-http/src/lib.rs | 2 +- warpgate-protocol-http/src/proxy.rs | 26 +++++++++-------- warpgate-protocol-http/src/session.rs | 11 ++++--- warpgate-protocol-http/src/session_handle.rs | 4 +-- warpgate-protocol-ssh/src/server/mod.rs | 2 +- .../src/server/service_output.rs | 8 ++--- warpgate-protocol-ssh/src/server/session.rs | 2 +- warpgate-web/package.json | 1 + .../src/admin/AuthPolicyEditor.svelte | 4 +-- warpgate-web/src/admin/CreateTicket.svelte | 6 ++-- warpgate-web/src/admin/User.svelte | 4 +-- .../src/admin/UserCredentialModal.svelte | 5 ++-- warpgate-web/src/gateway/App.svelte | 4 +-- warpgate-web/yarn.lock | 7 +++++ warpgate/src/commands/setup.rs | 2 +- 26 files changed, 93 insertions(+), 90 deletions(-) diff --git a/warpgate-admin/src/api/roles.rs b/warpgate-admin/src/api/roles.rs index 7c67b0f..ca943d5 100644 --- a/warpgate-admin/src/api/roles.rs +++ b/warpgate-admin/src/api/roles.rs @@ -146,7 +146,7 @@ impl DetailApi { return Ok(UpdateRoleResponse::NotFound); }; - if &role.name == BUILTIN_ADMIN_ROLE_NAME { + if role.name == BUILTIN_ADMIN_ROLE_NAME { return Ok(UpdateRoleResponse::Forbidden); } @@ -175,7 +175,7 @@ impl DetailApi { return Ok(DeleteRoleResponse::NotFound); }; - if &role.name == BUILTIN_ADMIN_ROLE_NAME { + if role.name == BUILTIN_ADMIN_ROLE_NAME { return Ok(DeleteRoleResponse::Forbidden); } diff --git a/warpgate-admin/src/api/targets.rs b/warpgate-admin/src/api/targets.rs index f6dc975..15fa18a 100644 --- a/warpgate-admin/src/api/targets.rs +++ b/warpgate-admin/src/api/targets.rs @@ -281,8 +281,8 @@ impl RolesApi { let db = db.lock().await; if !TargetRoleAssignment::Entity::find() - .filter(TargetRoleAssignment::Column::TargetId.eq(id.0.clone())) - .filter(TargetRoleAssignment::Column::RoleId.eq(role_id.0.clone())) + .filter(TargetRoleAssignment::Column::TargetId.eq(id.0)) + .filter(TargetRoleAssignment::Column::RoleId.eq(role_id.0)) .all(&*db) .await .map_err(WarpgateError::from)? @@ -329,7 +329,7 @@ impl RolesApi { return Ok(DeleteTargetRoleResponse::NotFound); }; - if &role.name == BUILTIN_ADMIN_ROLE_NAME && target.kind == TargetKind::WebAdmin { + if role.name == BUILTIN_ADMIN_ROLE_NAME && target.kind == TargetKind::WebAdmin { return Ok(DeleteTargetRoleResponse::Forbidden); } diff --git a/warpgate-admin/src/api/users.rs b/warpgate-admin/src/api/users.rs index 7d3e342..0b813f0 100644 --- a/warpgate-admin/src/api/users.rs +++ b/warpgate-admin/src/api/users.rs @@ -262,8 +262,8 @@ impl RolesApi { let db = db.lock().await; if !UserRoleAssignment::Entity::find() - .filter(UserRoleAssignment::Column::UserId.eq(id.0.clone())) - .filter(UserRoleAssignment::Column::RoleId.eq(role_id.0.clone())) + .filter(UserRoleAssignment::Column::UserId.eq(id.0)) + .filter(UserRoleAssignment::Column::RoleId.eq(role_id.0)) .all(&*db) .await .map_err(WarpgateError::from)? diff --git a/warpgate-common/src/config/mod.rs b/warpgate-common/src/config/mod.rs index db790da..82940d2 100644 --- a/warpgate-common/src/config/mod.rs +++ b/warpgate-common/src/config/mod.rs @@ -76,7 +76,7 @@ pub struct User { pub id: Uuid, pub username: String, pub credentials: Vec, - #[serde(skip_serializing_if = "Option::is_none", rename="require")] + #[serde(skip_serializing_if = "Option::is_none", rename = "require")] pub credential_policy: Option, pub roles: Vec, } diff --git a/warpgate-common/src/error.rs b/warpgate-common/src/error.rs index 26d1994..5ea452f 100644 --- a/warpgate-common/src/error.rs +++ b/warpgate-common/src/error.rs @@ -23,6 +23,8 @@ pub enum WarpgateError { ExternalHostNotSet, #[error("URL contains no host")] NoHostInUrl, + #[error("Inconsistent state error")] + InconsistentState, } impl ResponseError for WarpgateError { diff --git a/warpgate-protocol-http/src/api/auth.rs b/warpgate-protocol-http/src/api/auth.rs index fd1d318..cfce6b0 100644 --- a/warpgate-protocol-http/src/api/auth.rs +++ b/warpgate-protocol-http/src/api/auth.rs @@ -229,7 +229,7 @@ impl Api { auth: Option>, id: Path, ) -> poem::Result { - let Some(state_arc) = get_auth_state(&*id, *services, auth.map(|x|x.0)).await else { + let Some(state_arc) = get_auth_state(&id, &services, auth.map(|x|x.0)).await else { return Ok(AuthStateResponse::NotFound); }; serialize_auth_state_inner(state_arc).await @@ -247,7 +247,7 @@ impl Api { auth: Option>, id: Path, ) -> poem::Result { - let Some(state_arc) = get_auth_state(&*id, *services, auth.map(|x|x.0)).await else { + let Some(state_arc) = get_auth_state(&id, &services, auth.map(|x|x.0)).await else { return Ok(AuthStateResponse::NotFound); }; @@ -258,7 +258,7 @@ impl Api { }; if let AuthResult::Accepted { .. } = auth_result { - services.auth_state_store.lock().await.complete(&*id).await; + services.auth_state_store.lock().await.complete(&id).await; } serialize_auth_state_inner(state_arc).await } @@ -275,11 +275,11 @@ impl Api { auth: Option>, id: Path, ) -> poem::Result { - let Some(state_arc) = get_auth_state(&*id, *services, auth.map(|x|x.0)).await else { + let Some(state_arc) = get_auth_state(&id, &services, auth.map(|x|x.0)).await else { return Ok(AuthStateResponse::NotFound); }; state_arc.lock().await.reject(); - services.auth_state_store.lock().await.complete(&*id).await; + services.auth_state_store.lock().await.complete(&id).await; serialize_auth_state_inner(state_arc).await } } @@ -299,7 +299,7 @@ async fn get_auth_state( return None; }; - let Some(state_arc) = store.get(&*id) else { + let Some(state_arc) = store.get(&id) else { return None; }; diff --git a/warpgate-protocol-http/src/api/sso_provider_list.rs b/warpgate-protocol-http/src/api/sso_provider_list.rs index 9672177..5f947a9 100644 --- a/warpgate-protocol-http/src/api/sso_provider_list.rs +++ b/warpgate-protocol-http/src/api/sso_provider_list.rs @@ -54,7 +54,7 @@ impl Api { services: Data<&Services>, ) -> poem::Result { let mut providers = services.config.lock().await.store.sso_providers.clone(); - providers.sort_by(|a, b| a.label().cmp(&b.label())); + providers.sort_by(|a, b| a.label().cmp(b.label())); Ok(GetSsoProvidersResponse::Ok(Json( providers .into_iter() @@ -130,12 +130,9 @@ impl Api { state.add_valid_credential(cred); } - match state.verify() { - AuthResult::Accepted { username } => { - auth_state_store.complete(state.id()).await; - authorize_session(req, username).await?; - } - _ => (), + if let AuthResult::Accepted { username } = state.verify() { + auth_state_store.complete(state.id()).await; + authorize_session(req, username).await?; } Ok(Response::new(ReturnToSsoResponse::Ok).header( diff --git a/warpgate-protocol-http/src/api/targets_list.rs b/warpgate-protocol-http/src/api/targets_list.rs index a483127..d58c405 100644 --- a/warpgate-protocol-http/src/api/targets_list.rs +++ b/warpgate-protocol-http/src/api/targets_list.rs @@ -51,13 +51,13 @@ impl Api { SessionAuthorization::Ticket { target_name, .. } => target_name == name, SessionAuthorization::User(_) => { let mut config_provider = services.config_provider.lock().await; - match config_provider - .authorize_target(auth.username(), &name) - .await - { - Ok(true) => true, - _ => false, - } + + matches!( + config_provider + .authorize_target(auth.username(), &name) + .await, + Ok(true) + ) } } } diff --git a/warpgate-protocol-http/src/catchall.rs b/warpgate-protocol-http/src/catchall.rs index 554ffed..56f8d6b 100644 --- a/warpgate-protocol-http/src/catchall.rs +++ b/warpgate-protocol-http/src/catchall.rs @@ -79,8 +79,7 @@ async fn get_target_for_request( TargetOptions::Http(ref options) => Some((t, options)), _ => None, }) - .filter(|(_, o)| o.external_host.as_deref() == Some(host)) - .next() + .find(|(_, o)| o.external_host.as_deref() == Some(host)) .map(|(t, _)| t.name.clone()) } else { None @@ -127,7 +126,7 @@ async fn get_target_for_request( .config_provider .lock() .await - .authorize_target(&auth.username(), &target.0.name) + .authorize_target(auth.username(), &target.0.name) .await? { return Ok(None); @@ -137,5 +136,5 @@ async fn get_target_for_request( } } - return Ok(None); + Ok(None) } diff --git a/warpgate-protocol-http/src/common.rs b/warpgate-protocol-http/src/common.rs index 592d3f2..3b7b266 100644 --- a/warpgate-protocol-http/src/common.rs +++ b/warpgate-protocol-http/src/common.rs @@ -53,7 +53,7 @@ impl SessionExt for Session { } fn get_username(&self) -> Option { - return self.get_auth().map(|x| x.username().to_owned()); + self.get_auth().map(|x| x.username().to_owned()) } fn get_auth(&self) -> Option { @@ -91,7 +91,7 @@ impl SessionAuthorization { } async fn is_user_admin(req: &Request, auth: &SessionAuthorization) -> poem::Result { - let services: Data<&Services> = <_>::from_request_without_body(&req).await?; + let services: Data<&Services> = <_>::from_request_without_body(req).await?; let SessionAuthorization::User(username) = auth else { return Ok(false) @@ -102,7 +102,7 @@ async fn is_user_admin(req: &Request, auth: &SessionAuthorization) -> poem::Resu for target in targets { if matches!(target.options, TargetOptions::WebAdmin(_)) && config_provider - .authorize_target(&username, &target.name) + .authorize_target(username, &target.name) .await? { drop(config_provider); @@ -169,7 +169,7 @@ pub fn gateway_redirect(req: &Request) -> Response { .original_uri() .path_and_query() .map(|p| p.to_string()) - .unwrap_or("".into()); + .unwrap_or_else(|| "".into()); let path = format!( "/@warpgate#/login?next={}", @@ -184,20 +184,17 @@ pub async fn get_auth_state_for_request( session: &Session, store: &mut AuthStateStore, ) -> Result>, WarpgateError> { - match session.get_auth_state_id() { - Some(id) => { - if !store.contains_key(&id.0) { - session.remove(AUTH_STATE_ID_SESSION_KEY) - } + if let Some(id) = session.get_auth_state_id() { + if !store.contains_key(&id.0) { + session.remove(AUTH_STATE_ID_SESSION_KEY) } - None => (), - }; + } match session.get_auth_state_id() { - Some(id) => Ok(store.get(&id.0).unwrap()), + Some(id) => Ok(store.get(&id.0).ok_or(WarpgateError::InconsistentState)?), None => { let (id, state) = store - .create(&username, crate::common::PROTOCOL_NAME) + .create(username, crate::common::PROTOCOL_NAME) .await?; session.set(AUTH_STATE_ID_SESSION_KEY, AuthStateId(id)); Ok(state) @@ -207,13 +204,13 @@ pub async fn get_auth_state_for_request( pub async fn authorize_session(req: &Request, username: String) -> poem::Result<()> { let session_middleware: Data<&Arc>> = - <_>::from_request_without_body(&req).await?; - let session: &Session = <_>::from_request_without_body(&req).await?; + <_>::from_request_without_body(req).await?; + let session: &Session = <_>::from_request_without_body(req).await?; let server_handle = session_middleware .lock() .await - .create_handle_for(&req) + .create_handle_for(req) .await?; server_handle .lock() diff --git a/warpgate-protocol-http/src/error.rs b/warpgate-protocol-http/src/error.rs index 7f709d9..bd9fd7f 100644 --- a/warpgate-protocol-http/src/error.rs +++ b/warpgate-protocol-http/src/error.rs @@ -2,7 +2,7 @@ use http::StatusCode; use poem::IntoResponse; pub fn error_page(e: poem::Error) -> impl IntoResponse { - return poem::web::Html(format!( + poem::web::Html(format!( r#"