Skip to content

Commit

Permalink
spacegate-auth-get-raw-url (#488)
Browse files Browse the repository at this point in the history
* spacegate-auth-get-raw-url

* cargo fmt
  • Loading branch information
RWDai authored Oct 18, 2023
1 parent 75fc0a9 commit e0b0042
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
16 changes: 12 additions & 4 deletions gateway/spacegate-lib/src/plugin/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(),
}
}
}
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -360,16 +365,19 @@ 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?;
let body = String::from_utf8_lossy(&req_body).trim_matches('"').to_string();
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())
Expand Down
2 changes: 1 addition & 1 deletion spi/spi-conf/src/api/nacos/grpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 6 additions & 5 deletions support/reach/src/serv/message_send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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::<String, Vec<_>>::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::<String, Vec<_>>::new(), |mut map, item| {
map.entry(item.receive_group_code.clone()).or_default().push(item);
map
});

if instance_group_code.is_empty() {
return Ok(());
Expand Down

0 comments on commit e0b0042

Please sign in to comment.