From e0b0042e41eb875366580337e568e4dd577ed628 Mon Sep 17 00:00:00 2001 From: RWDai <27391645+RWDai@users.noreply.github.com> Date: Wed, 18 Oct 2023 04:09:32 -0500 Subject: [PATCH] spacegate-auth-get-raw-url (#488) * spacegate-auth-get-raw-url * cargo fmt --- gateway/spacegate-lib/src/plugin/auth.rs | 16 ++++++++++++---- spi/spi-conf/src/api/nacos/grpc/mod.rs | 2 +- support/reach/src/serv/message_send.rs | 11 ++++++----- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/gateway/spacegate-lib/src/plugin/auth.rs b/gateway/spacegate-lib/src/plugin/auth.rs index 292c128b6..ccb5fa992 100644 --- a/gateway/spacegate-lib/src/plugin/auth.rs +++ b/gateway/spacegate-lib/src/plugin/auth.rs @@ -81,6 +81,10 @@ pub struct SgFilterAuth { /// | `/apis` | `apis` | `/{true_url}` | /// |`/prefix/apis` | `apis` |`/prefix/{true_url}` | mix_replace_url: String, + /// Remove prefix of AuthReq path. + /// use for [ctx_to_auth_req] + /// Used to remove a fixed prefix from the original URL. + auth_path_ignore_prefix: String, } impl Default for SgFilterAuth { @@ -94,6 +98,7 @@ impl Default for SgFilterAuth { header_is_mix_req: "IS_MIX_REQ".to_string(), fetch_server_config_path: "/starsysApi/apis".to_string(), mix_replace_url: "apis".to_string(), + auth_path_ignore_prefix: "/starsysApi".to_string(), } } } @@ -228,7 +233,7 @@ impl SgPluginFilter for SgFilterAuth { return Ok((false, ctx)); } ctx.request.set_header_str(&self.header_is_mix_req, "false")?; - let (mut auth_req, req_body) = ctx_to_auth_req(&mut ctx).await?; + let (mut auth_req, req_body) = ctx_to_auth_req(&self.auth_path_ignore_prefix, &mut ctx).await?; match auth_kernel_serv::auth(&mut auth_req, is_true_mix_req).await { Ok(auth_result) => { @@ -360,8 +365,11 @@ async fn mix_req_to_ctx(auth_config: &AuthConfig, mix_replace_url: &str, mut ctx Ok(ctx) } -async fn ctx_to_auth_req(ctx: &mut SgRoutePluginContext) -> TardisResult<(AuthReq, Bytes)> { - let url = ctx.request.get_uri().clone(); +/// # Convert SgRoutePluginContext to AuthReq +/// Prepare the AuthReq required for the authentication process. +/// The authentication process requires a URL that has not been rewritten. +async fn ctx_to_auth_req(ignore_prefix: &str, ctx: &mut SgRoutePluginContext) -> TardisResult<(AuthReq, Bytes)> { + let url = ctx.request.get_uri_raw().clone(); let scheme = url.scheme().map(|s| s.to_string()).unwrap_or("http".to_string()); let headers = headermap_header_to_hashmap(ctx.request.get_headers())?; let req_body = ctx.request.take_body_into_bytes().await?; @@ -369,7 +377,7 @@ async fn ctx_to_auth_req(ctx: &mut SgRoutePluginContext) -> TardisResult<(AuthRe Ok(( AuthReq { scheme: scheme.clone(), - path: url.path().to_string(), + path: url.path().replace(ignore_prefix, "").to_string(), query: url .query() .filter(|q| !q.is_empty()) diff --git a/spi/spi-conf/src/api/nacos/grpc/mod.rs b/spi/spi-conf/src/api/nacos/grpc/mod.rs index b111c0bca..ce09527e8 100644 --- a/spi/spi-conf/src/api/nacos/grpc/mod.rs +++ b/spi/spi-conf/src/api/nacos/grpc/mod.rs @@ -277,7 +277,7 @@ pub async fn dispatch_request(type_info: &str, value: &str, access_token: Option "HealthCheckRequest" => HealthCheckResponse::success().as_payload(), "ConfigQueryRequest" => { let Ok(ctx) = get_ctx.await else { - return Ok(NaocsGrpcResponse::unregister().as_payload()) + return Ok(NaocsGrpcResponse::unregister().as_payload()); }; let ConfigQueryRequest { data_id, group, tenant } = serde_json::from_str(value).map_err(|_e| TardisError::bad_request("expect a ConfigQueryRequest", ""))?; let mut descriptor = ConfigDescriptor { diff --git a/support/reach/src/serv/message_send.rs b/support/reach/src/serv/message_send.rs index 56692ca3e..07ba84c71 100644 --- a/support/reach/src/serv/message_send.rs +++ b/support/reach/src/serv/message_send.rs @@ -2,7 +2,7 @@ use std::collections::{HashMap, HashSet}; use bios_basic::rbum::{dto::rbum_item_dto::RbumItemAddReq, serv::rbum_crud_serv::RbumCrudOperation}; use tardis::{ - basic::{result::TardisResult, dto::TardisContext}, + basic::{dto::TardisContext, result::TardisResult}, db::sea_orm::{sea_query::Query, ColumnTrait, Iterable}, TardisFunsInst, }; @@ -55,10 +55,11 @@ pub async fn message_send(send_req: ReachMsgSendReq, funs: &TardisFunsInst, ctx: map }); - let mut instance_group_code = instances.into_iter().filter(|inst| receive_group_code.contains_key(&inst.receive_group_code.clone())).fold(HashMap::>::new(), |mut map, item| { - map.entry(item.receive_group_code.clone()).or_default().push(item); - map - }); + let mut instance_group_code = + instances.into_iter().filter(|inst| receive_group_code.contains_key(&inst.receive_group_code.clone())).fold(HashMap::>::new(), |mut map, item| { + map.entry(item.receive_group_code.clone()).or_default().push(item); + map + }); if instance_group_code.is_empty() { return Ok(());