From 2edfe6999c86bef0992a60ba05e2b77e13d98912 Mon Sep 17 00:00:00 2001 From: 4t145 Date: Thu, 26 Oct 2023 10:32:56 +0800 Subject: [PATCH] Update to tardis 0.1.0 rc.2 (#502) --- Cargo.toml | 6 +- basic/Cargo.toml | 1 + basic/src/process/task_processor.rs | 14 ++-- basic/src/rbum/dto/rbum_safe_dto.rs | 2 +- basic/src/rbum/rbum_initializer.rs | 26 +++--- basic/src/rbum/serv/rbum_cert_serv.rs | 14 ++-- basic/src/rbum/serv/rbum_domain_serv.rs | 4 +- basic/src/rbum/serv/rbum_item_serv.rs | 8 +- basic/src/rbum/serv/rbum_kind_serv.rs | 8 +- basic/src/spi/serv/spi_bs_serv.rs | 4 +- basic/src/spi/spi_initializer.rs | 19 +++-- basic/src/test/init_rbum_test_container.rs | 4 +- basic/src/test/test_http_client.rs | 7 +- clients/hwsms/src/lib.rs | 4 +- gateway/spacegate-lib/Cargo.toml | 2 + .../spacegate-lib/src/plugin/anti_replay.rs | 4 +- gateway/spacegate-lib/src/plugin/auth.rs | 83 +++++++++++-------- gateway/test/Cargo.toml | 2 +- gateway/test/src/init_apisix.rs | 7 +- gateway/test/src/main.rs | 31 +++---- .../flow/src/serv/flow_external_serv.rs | 6 +- middleware/flow/tests/test_flow_api.rs | 4 +- .../schedule/src/serv/schedule_job_serv.rs | 44 +++++----- middleware/schedule/tests/test_common.rs | 6 +- middleware/schedule/tests/test_schedule.rs | 6 +- sdk/invoke/src/clients.rs | 8 +- sdk/invoke/src/clients/base_spi_client.rs | 6 +- services/bios-all/src/initializer.rs | 4 +- services/bios-all/src/main.rs | 2 +- services/spi-all/src/main.rs | 2 +- .../src/serv/redis/cache_redis_initializer.rs | 7 +- spi/spi-cache/tests/test_cache.rs | 2 +- spi/spi-conf/Cargo.toml | 1 + spi/spi-conf/src/api.rs | 4 +- spi/spi-conf/src/conf_config.rs | 2 +- spi/spi-conf/src/conf_initializer.rs | 2 +- .../src/serv/pg/conf_pg_config_serv.rs | 6 +- spi/spi-conf/tests/config/conf-default.toml | 6 +- spi/spi-conf/tests/spi_conf_api_test.rs | 2 +- spi/spi-conf/tests/spi_conf_listener_test.rs | 22 ++--- .../tests/spi_conf_nacos_compatible_test.rs | 8 +- spi/spi-conf/tests/spi_conf_test_common.rs | 9 +- spi/spi-graph/tests/test_graph.rs | 2 +- spi/spi-kv/tests/test_kv.rs | 2 +- spi/spi-log/tests/test_log.rs | 2 +- .../src/serv/s3/object_s3_initializer.rs | 31 ++++--- .../src/serv/s3/object_s3_obj_serv.rs | 6 +- spi/spi-object/tests/test_object.rs | 2 +- .../src/api/ci/plugin_ci_api_api.rs | 1 - spi/spi-plugin/src/serv/plugin_exec_serv.rs | 2 +- spi/spi-plugin/tests/test_plugin.rs | 2 +- spi/spi-reldb/src/reldb_initializer.rs | 18 ++-- spi/spi-reldb/tests/test_reldb.rs | 2 +- spi/spi-search/Cargo.toml | 1 + .../src/serv/es/search_es_initializer.rs | 5 +- .../src/serv/es/search_es_item_serv.rs | 2 +- spi/spi-search/tests/init_search_container.rs | 9 +- spi/spi-search/tests/test_search.rs | 2 +- spi/spi-stats/tests/test_stats.rs | 2 +- support/auth/Cargo.toml | 2 +- support/auth/src/api/auth_kernel_api.rs | 2 +- support/auth/src/auth_config.rs | 2 +- support/auth/src/serv/auth_crypto_serv.rs | 9 +- support/auth/src/serv/auth_kernel_serv.rs | 22 ++--- .../auth/src/serv/clients/spi_log_client.rs | 4 +- support/auth/tests/init_cache_container.rs | 2 +- support/auth/tests/test_auth.rs | 2 +- support/auth/tests/test_auth_encrypt.rs | 21 +++-- support/auth/tests/test_auth_req.rs | 4 +- .../enhance-wasm/src/mini_tardis/crypto.rs | 2 +- support/iam/src/basic/dto/iam_res_dto.rs | 6 +- .../iam/src/basic/serv/clients/mail_client.rs | 6 +- .../iam/src/basic/serv/clients/sms_client.rs | 20 +++-- support/iam/src/basic/serv/iam_attr_serv.rs | 2 +- support/iam/src/basic/serv/iam_cert_serv.rs | 10 +-- .../src/basic/serv/iam_cert_user_pwd_serv.rs | 6 +- .../iam/src/basic/serv/iam_key_cache_serv.rs | 2 +- support/iam/src/basic/serv/iam_tenant_serv.rs | 4 +- .../oauth2_spi/iam_cert_oauth2_spi_github.rs | 4 +- .../console_common/api/iam_cc_system_api.rs | 2 +- .../api/iam_ci_system_api.rs | 2 +- .../serv/iam_cp_cert_mail_vcode_serv.rs | 6 +- .../serv/iam_cp_cert_phone_vcode_serv.rs | 6 +- .../serv/iam_cp_cert_user_pwd_serv.rs | 22 ++--- support/iam/src/iam_test_helper.rs | 10 ++- .../iam/src/integration/ldap/ldap_server.rs | 5 +- support/iam/tests/test_basic.rs | 6 +- support/iam/tests/test_iam_cert_sync.rs | 2 +- support/reach/Cargo.toml | 2 + .../api/reach_api_cc/reach_api_cc_message.rs | 2 +- .../api/reach_api_ct/reach_api_ct_message.rs | 4 +- .../api/reach_api_ct/reach_api_ct_msg_log.rs | 1 + support/reach/src/domain/message.rs | 2 +- support/reach/src/reach_send_channel.rs | 22 ++--- support/reach/src/serv/message_log.rs | 14 +++- support/reach/tests/test_reach_common.rs | 7 +- support/reach/tests/test_send_client.rs | 2 +- 97 files changed, 395 insertions(+), 354 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index de3437224..7ffecc41a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,14 +47,14 @@ lazy_static = { version = "1" } itertools = { version = "0" } fancy-regex = { version = "0" } run_script = { version = "0.10" } +testcontainers-modules = { version = "0.1"} strum ={ version = "0.25", features = ["derive"] } # tardis -tardis = { version = "=0.1.0-beta.12" } +tardis = { version = "=0.1.0-rc.2" } # tardis = { path = "../tardis/tardis" } # tardis = { git = "https://github.com/ideal-world/tardis.git", rev = "9424e16" } - #spacegate -spacegate-kernel = { git = "https://github.com/ideal-world/spacegate.git", rev = "aac2989e48b6064c31db4dedcc30b3c4135b399d",features = [ +spacegate-kernel = { git = "https://github.com/ideal-world/spacegate.git", rev = "ed34d33",features = [ "ws", "cache", "k8s", diff --git a/basic/Cargo.toml b/basic/Cargo.toml index 6f789ec9a..05176f469 100644 --- a/basic/Cargo.toml +++ b/basic/Cargo.toml @@ -37,6 +37,7 @@ itertools.workspace = true fancy-regex.workspace = true tardis = { workspace = true, optional = true } strum = { workspace = true } +testcontainers-modules ={ workspace = true } [dev-dependencies] tardis = { workspace = true, features = ["test"] } diff --git a/basic/src/process/task_processor.rs b/basic/src/process/task_processor.rs index 3f7d2282f..0a0ce0097 100644 --- a/basic/src/process/task_processor.rs +++ b/basic/src/process/task_processor.rs @@ -47,7 +47,7 @@ impl TaskProcessor { pub async fn init_task(cache_key: &str, cache_client: &TardisCacheClient) -> TardisResult { //todo change to SnowFlake or other distributed ID generator - let task_id = Local::now().timestamp_nanos(); + let task_id = Local::now().timestamp_nanos_opt().expect("maybe in 23rd centery"); let max: i64 = u32::MAX.into(); let task_id_split1: usize = (task_id / max).try_into()?; let task_id_split2: usize = (task_id % max).try_into()?; @@ -97,13 +97,13 @@ impl TaskProcessor { P: FnOnce(i64) -> T + Send + Sync + 'static, T: Future> + Send + 'static, { - let task_id = TaskProcessor::init_task(cache_key, funs.cache()).await?; + let task_id = TaskProcessor::init_task(cache_key, &funs.cache()).await?; let cache_client = funs.cache(); let cache_key = cache_key.to_string(); let handle = tardis::tokio::spawn(async move { let result = process(task_id).await; match result { - Ok(_) => match TaskProcessor::set_status(&cache_key, task_id, true, cache_client).await { + Ok(_) => match TaskProcessor::set_status(&cache_key, task_id, true, &cache_client).await { Ok(_) => {} Err(e) => log::error!("Asynchronous task [{}] process error:{:?}", task_id, e), }, @@ -117,7 +117,7 @@ impl TaskProcessor { } pub async fn execute_task_external(cache_key: &str, funs: &TardisFunsInst) -> TardisResult { - let task_id = TaskProcessor::init_task(cache_key, funs.cache()).await?; + let task_id = TaskProcessor::init_task(cache_key, &funs.cache()).await?; Ok(task_id) } @@ -136,7 +136,7 @@ impl TaskProcessor { } pub async fn stop_task(cache_key: &str, task_id: i64, funs: &TardisFunsInst) -> TardisResult<()> { - if TaskProcessor::check_status(cache_key, task_id, funs.cache()).await? { + if TaskProcessor::check_status(cache_key, task_id, &funs.cache()).await? { TASK_HANDLE.write().await.remove(&task_id); } else { funs.mq() @@ -155,7 +155,7 @@ impl TaskProcessor { } pub async fn stop_task_external(cache_key: &str, task_id: i64, funs: &TardisFunsInst) -> TardisResult<()> { - match TaskProcessor::set_status(cache_key, task_id, true, funs.cache()).await { + match TaskProcessor::set_status(cache_key, task_id, true, &funs.cache()).await { Ok(_) => {} Err(e) => log::error!("Asynchronous task [{}] stop error:{:?}", task_id, e), } @@ -166,7 +166,7 @@ impl TaskProcessor { match TASK_HANDLE.write().await.remove(&task_id) { Some(handle) => { handle.abort(); - match TaskProcessor::set_status(cache_key, task_id, true, funs.cache()).await { + match TaskProcessor::set_status(cache_key, task_id, true, &funs.cache()).await { Ok(_) => {} Err(e) => log::error!("Asynchronous task [{}] stop error:{:?}", task_id, e), } diff --git a/basic/src/rbum/dto/rbum_safe_dto.rs b/basic/src/rbum/dto/rbum_safe_dto.rs index dfbea8ddd..ed16eca53 100644 --- a/basic/src/rbum/dto/rbum_safe_dto.rs +++ b/basic/src/rbum/dto/rbum_safe_dto.rs @@ -1,4 +1,4 @@ -use serde::{Deserialize, Serialize}; +use serde::{Serialize}; #[cfg(feature = "default")] use tardis::db::sea_orm; use tardis::{ diff --git a/basic/src/rbum/rbum_initializer.rs b/basic/src/rbum/rbum_initializer.rs index d79c903df..2076fcac3 100644 --- a/basic/src/rbum/rbum_initializer.rs +++ b/basic/src/rbum/rbum_initializer.rs @@ -23,19 +23,19 @@ pub async fn init(code: &str, config: RbumConfig) -> TardisResult<()> { } tx.begin().await?; TardisFuns::dict.add("__RBUM_INIT__", "", "", &tx).await?; - tx.init(rbum_domain::ActiveModel::init(db_kind, Some("update_time"), compatible_type.clone())).await?; - tx.init(rbum_kind::ActiveModel::init(db_kind, Some("update_time"), compatible_type.clone())).await?; - tx.init(rbum_item::ActiveModel::init(db_kind, Some("update_time"), compatible_type.clone())).await?; - tx.init(rbum_kind_attr::ActiveModel::init(db_kind, Some("update_time"), compatible_type.clone())).await?; - tx.init(rbum_item_attr::ActiveModel::init(db_kind, Some("update_time"), compatible_type.clone())).await?; - tx.init(rbum_rel::ActiveModel::init(db_kind, Some("update_time"), compatible_type.clone())).await?; - tx.init(rbum_rel_attr::ActiveModel::init(db_kind, Some("update_time"), compatible_type.clone())).await?; - tx.init(rbum_rel_env::ActiveModel::init(db_kind, Some("update_time"), compatible_type.clone())).await?; - tx.init(rbum_cert_conf::ActiveModel::init(db_kind, Some("update_time"), compatible_type.clone())).await?; - tx.init(rbum_cert::ActiveModel::init(db_kind, Some("update_time"), compatible_type.clone())).await?; - tx.init(rbum_set::ActiveModel::init(db_kind, Some("update_time"), compatible_type.clone())).await?; - tx.init(rbum_set_cate::ActiveModel::init(db_kind, Some("update_time"), compatible_type.clone())).await?; - tx.init(rbum_set_item::ActiveModel::init(db_kind, Some("update_time"), compatible_type.clone())).await?; + tx.init(rbum_domain::ActiveModel::init(db_kind, Some("update_time"), compatible_type)).await?; + tx.init(rbum_kind::ActiveModel::init(db_kind, Some("update_time"), compatible_type)).await?; + tx.init(rbum_item::ActiveModel::init(db_kind, Some("update_time"), compatible_type)).await?; + tx.init(rbum_kind_attr::ActiveModel::init(db_kind, Some("update_time"), compatible_type)).await?; + tx.init(rbum_item_attr::ActiveModel::init(db_kind, Some("update_time"), compatible_type)).await?; + tx.init(rbum_rel::ActiveModel::init(db_kind, Some("update_time"), compatible_type)).await?; + tx.init(rbum_rel_attr::ActiveModel::init(db_kind, Some("update_time"), compatible_type)).await?; + tx.init(rbum_rel_env::ActiveModel::init(db_kind, Some("update_time"), compatible_type)).await?; + tx.init(rbum_cert_conf::ActiveModel::init(db_kind, Some("update_time"), compatible_type)).await?; + tx.init(rbum_cert::ActiveModel::init(db_kind, Some("update_time"), compatible_type)).await?; + tx.init(rbum_set::ActiveModel::init(db_kind, Some("update_time"), compatible_type)).await?; + tx.init(rbum_set_cate::ActiveModel::init(db_kind, Some("update_time"), compatible_type)).await?; + tx.init(rbum_set_item::ActiveModel::init(db_kind, Some("update_time"), compatible_type)).await?; tx.commit().await?; Ok(()) } diff --git a/basic/src/rbum/serv/rbum_cert_serv.rs b/basic/src/rbum/serv/rbum_cert_serv.rs index 16d94358e..6552b5fd4 100644 --- a/basic/src/rbum/serv/rbum_cert_serv.rs +++ b/basic/src/rbum/serv/rbum_cert_serv.rs @@ -103,8 +103,8 @@ impl RbumCrudOperation TardisResult<()> { - if !R_URL_PART_CODE.is_match(add_req.code.0.as_str()) { + if !R_URL_PART_CODE.is_match(add_req.code.as_str()) { return Err(funs.err().bad_request(&Self::get_obj_name(), "add", &format!("code {} is invalid", add_req.code), "400-rbum-*-code-illegal")); } if funs .db() - .count(Query::select().column(rbum_domain::Column::Id).from(rbum_domain::Entity).and_where(Expr::col(rbum_domain::Column::Code).eq(add_req.code.0.as_str()))) + .count(Query::select().column(rbum_domain::Column::Id).from(rbum_domain::Entity).and_where(Expr::col(rbum_domain::Column::Code).eq(add_req.code.as_str()))) .await? > 0 { diff --git a/basic/src/rbum/serv/rbum_item_serv.rs b/basic/src/rbum/serv/rbum_item_serv.rs index 0435e28bf..db021881b 100644 --- a/basic/src/rbum/serv/rbum_item_serv.rs +++ b/basic/src/rbum/serv/rbum_item_serv.rs @@ -48,7 +48,7 @@ impl RbumCrudOperation TardisResult { - let id = if let Some(id) = &add_req.id { id.0.clone() } else { TardisFuns::field.nanoid() }; + let id = if let Some(id) = &add_req.id { id.to_string() } else { TardisFuns::field.nanoid() }; let code = if let Some(code) = &add_req.code { if funs .db() @@ -64,14 +64,14 @@ impl RbumCrudOperation 0 { return Err(funs.err().conflict(&Self::get_obj_name(), "add", &format!("code {code} already exists"), "409-rbum-*-code-exist")); } - code.0.clone() + code.to_string() } else { id.clone() }; @@ -113,7 +113,7 @@ impl RbumCrudOperation TardisResult<()> { - if !R_URL_PART_CODE.is_match(add_req.code.0.as_str()) { + if !R_URL_PART_CODE.is_match(add_req.code.as_str()) { return Err(funs.err().bad_request(&Self::get_obj_name(), "add", &format!("code {} is invalid", add_req.code), "400-rbum-*-code-illegal")); } - if funs.db().count(Query::select().column(rbum_kind::Column::Id).from(rbum_kind::Entity).and_where(Expr::col(rbum_kind::Column::Code).eq(add_req.code.0.as_str()))).await? + if funs.db().count(Query::select().column(rbum_kind::Column::Id).from(rbum_kind::Entity).and_where(Expr::col(rbum_kind::Column::Code).eq(add_req.code.as_str()))).await? > 0 { return Err(funs.err().conflict(&Self::get_obj_name(), "add", &format!("code {} already exists", add_req.code), "409-rbum-*-code-exist")); @@ -188,8 +188,8 @@ impl RbumCrudOperation TardisResult { let ext = TardisFuns::json.str_to_json(&bs_cert.ext)?; let compatible_type = TardisFuns::json.json_to_obj(ext.get("compatible_type").unwrap_or(&tardis::serde_json::Value::String("None".to_string())).clone())?; - let client = TardisRelDBClient::init( - &bs_cert.conn_uri, - ext.get("max_connections").unwrap().as_u64().unwrap() as u32, - ext.get("min_connections").unwrap().as_u64().unwrap() as u32, - None, - None, + let client = TardisRelDBClient::init(&DBModuleConfig { + url: bs_cert.conn_uri.parse().expect("invalid url"), + max_connections: ext.get("max_connections").unwrap().as_u64().unwrap() as u32, + min_connections: ext.get("min_connections").unwrap().as_u64().unwrap() as u32, + connect_timeout_sec: None, + idle_timeout_sec: None, compatible_type, - ) + }) .await?; let mut ext = HashMap::new(); let schema_name = if bs_cert.private { @@ -206,7 +207,7 @@ pub mod common_pg { primary_keys: Option>, update_time_field: Option<&str>, ) -> TardisResult<(TardisRelDBlConnection, String)> { - let tag = tag.map(|t| format!("_{t}")).unwrap_or_else(|| "".to_string()); + let tag = tag.map(|t| format!("_{t}")).unwrap_or_default(); let conn = bs_inst.0.conn(); let schema_name = get_schema_name_from_ext(bs_inst.1).unwrap(); if check_table_exit(&format!("{table_flag}{tag}"), &conn, ctx).await? { @@ -236,7 +237,7 @@ pub mod common_pg { update_time_field: Option<&str>, ctx: &TardisContext, ) -> TardisResult<()> { - let tag = tag.map(|t| format!("_{t}")).unwrap_or_else(|| "".to_string()); + let tag = tag.map(|t| format!("_{t}")).unwrap_or_default(); let schema_name = get_schema_name_from_context(ctx); do_init_table(&schema_name, conn, &tag, table_flag, table_create_content, indexes, primary_keys, update_time_field).await } diff --git a/basic/src/test/init_rbum_test_container.rs b/basic/src/test/init_rbum_test_container.rs index 6496de1c7..d10eb4590 100644 --- a/basic/src/test/init_rbum_test_container.rs +++ b/basic/src/test/init_rbum_test_container.rs @@ -3,8 +3,8 @@ use std::env; use tardis::basic::result::TardisResult; use tardis::test::test_container::TardisTestContainer; use tardis::testcontainers::clients::Cli; -use tardis::testcontainers::images::generic::GenericImage; -use tardis::testcontainers::images::redis::Redis; +use tardis::testcontainers::GenericImage; +use testcontainers_modules::redis::Redis; use tardis::testcontainers::Container; use tardis::TardisFuns; diff --git a/basic/src/test/test_http_client.rs b/basic/src/test/test_http_client.rs index e0fde4aa6..8b15a35bd 100644 --- a/basic/src/test/test_http_client.rs +++ b/basic/src/test/test_http_client.rs @@ -4,6 +4,7 @@ use serde::de::DeserializeOwned; use serde::Serialize; use tardis::basic::dto::TardisContext; use tardis::basic::result::TardisResult; +use tardis::config::config_dto::WebClientModuleConfig; use tardis::log::{info, warn}; use tardis::web::poem_openapi::types::{ParseFromJSON, ToJSON}; use tardis::web::web_client::TardisWebClient; @@ -19,7 +20,7 @@ pub struct TestHttpClient { impl TestHttpClient { pub fn new(base_url: String) -> TestHttpClient { TestHttpClient { - client: TardisWebClient::init(600).unwrap(), + client: TardisWebClient::init(&WebClientModuleConfig { connect_timeout_sec: 600, ..Default::default() }).unwrap(), context: Default::default(), base_url, } @@ -27,7 +28,9 @@ impl TestHttpClient { pub fn set_auth(&mut self, ctx: &TardisContext) -> TardisResult<()> { let ctx_base64 = &TardisFuns::crypto.base64.encode(TardisFuns::json.obj_to_string(&ctx)?); - self.set_default_header(&TardisFuns::fw_config().web_server.context_conf.context_header_name, ctx_base64); + let fw_config = TardisFuns::fw_config(); + let web_server_config = fw_config.web_server(); + self.set_default_header(&web_server_config.context_conf.context_header_name, ctx_base64); self.context = ctx.clone(); Ok(()) } diff --git a/clients/hwsms/src/lib.rs b/clients/hwsms/src/lib.rs index de7dcb305..ee9c80eee 100644 --- a/clients/hwsms/src/lib.rs +++ b/clients/hwsms/src/lib.rs @@ -4,7 +4,7 @@ use tardis::{ basic::result::TardisResult, chrono::{SecondsFormat, Utc}, - crypto::rust_crypto::sha2::Sha256, + crypto::crypto_digest::algorithm::Sha256, rand::random, url::Url, web::reqwest::{ @@ -39,7 +39,7 @@ impl SmsClient { // and random 1~128bit number let nonce = format!("{:X}", random::()); let digest_raw = format!("{}{}{}", nonce, created, &self.app_secret); - let password_digest = BASE64.encode_raw(DIGEST.digest_raw(digest_raw.as_bytes(), Sha256::new())?); + let password_digest = BASE64.encode_raw(DIGEST.digest_bytes::(digest_raw)?); let wsse_header = format!(r#"UsernameToken Username="{username}",PasswordDigest="{password_digest}",Nonce="{nonce}",Created="{created}""#); let wsse_header = HeaderValue::from_str(&wsse_header).expect("Fail to build sms header, maybe there are unexpected char in app_key."); headers.insert(AUTHORIZATION, HeaderValue::from_static(Self::AUTH_WSSE_HEADER_VALUE)); diff --git a/gateway/spacegate-lib/Cargo.toml b/gateway/spacegate-lib/Cargo.toml index c689e74d5..769a6f048 100644 --- a/gateway/spacegate-lib/Cargo.toml +++ b/gateway/spacegate-lib/Cargo.toml @@ -23,8 +23,10 @@ jsonpath-rust = "0.3.1" bios-auth = { path = "../../support/auth", default-features = false } tardis = { workspace = true, features = ["web-client"] } ipnet = "2.8.0" + [dev-dependencies] pprof = { version = "0.12.1", features = ["flamegraph"] } tardis = { workspace = true, features = ["test", "web-client"] } tracing-subscriber = { version = "0.3", features = ["env-filter"] } +testcontainers-modules = { workspace = true } \ No newline at end of file diff --git a/gateway/spacegate-lib/src/plugin/anti_replay.rs b/gateway/spacegate-lib/src/plugin/anti_replay.rs index af940e815..3dd2c5a0e 100644 --- a/gateway/spacegate-lib/src/plugin/anti_replay.rs +++ b/gateway/spacegate-lib/src/plugin/anti_replay.rs @@ -137,9 +137,9 @@ mod tests { }; use tardis::{ test::test_container::TardisTestContainer, - testcontainers::{self, clients::Cli, images::redis::Redis, Container}, + testcontainers::{self, clients::Cli, Container}, }; - + use testcontainers_modules::redis::Redis; use super::*; #[tokio::test] diff --git a/gateway/spacegate-lib/src/plugin/auth.rs b/gateway/spacegate-lib/src/plugin/auth.rs index ccb5fa992..05baeb343 100644 --- a/gateway/spacegate-lib/src/plugin/auth.rs +++ b/gateway/spacegate-lib/src/plugin/auth.rs @@ -31,11 +31,10 @@ use std::{ str::FromStr, sync::{Arc, OnceLock}, }; -use tardis::web::poem_openapi::types::Type; use tardis::{ async_trait, - basic::{error::TardisError, result::TardisResult, tracing::TardisTracing}, - config::config_dto::{AppConfig, CacheConfig, DBConfig, FrameworkConfig, LogConfig, TardisConfig, WebServerConfig}, + basic::{error::TardisError, result::TardisResult}, + config::config_dto::{AppConfig, CacheModuleConfig, FrameworkConfig, LogConfig, TardisConfig}, log, serde_json::{self, json, Value}, tokio::{sync::RwLock, task::JoinHandle}, @@ -43,6 +42,7 @@ use tardis::{ web::web_resp::TardisResp, TardisFuns, }; +use tardis::{tracing, web::poem_openapi::types::Type, basic::tracing::Directive}; use super::plugin_constants; #[allow(clippy::type_complexity)] @@ -134,8 +134,17 @@ impl SgPluginFilter for SgFilterAuth { async fn init(&mut self, init_dto: &SgPluginFilterInitDto) -> TardisResult<()> { if let Some(log_level) = &init_dto.gateway_parameters.log_level { - let _ = TardisTracing::update_log_level_by_domain_code(crate::PACKAGE_NAME, log_level); - let _ = TardisTracing::update_log_level_by_domain_code(bios_auth::auth_constants::PACKAGE_NAME, log_level); + let mut log_config = TardisFuns::fw_config().log().clone(); + fn directive(path: &str, lvl: &str) -> Directive { + let s = format!("{path}={lvl}"); + format!("{path}={lvl}").parse().unwrap_or_else(|e| { + tracing::error!("[SG.Filter.Auth] failed to parse directive {:?}: {}", s, e); + Default::default() + }) + } + log_config.directives.push(directive(crate::PACKAGE_NAME, log_level)); + log_config.directives.push(directive(bios_auth::auth_constants::PACKAGE_NAME, log_level)); + TardisFuns::tracing().update_config(&log_config); } let config_md5 = TardisFuns::crypto.digest.md5(TardisFuns::json.obj_to_string(self)?)?; @@ -163,28 +172,28 @@ impl SgPluginFilter for SgFilterAuth { desc: "This is a spacegate plugin-auth".to_string(), ..Default::default() }, - web_server: WebServerConfig { - enabled: false, - ..Default::default() - }, - db: DBConfig { - enabled: false, - ..Default::default() - }, - cache: CacheConfig { - enabled: true, - url: if self.cache_url.is_empty() { - if let Some(redis_url) = init_dto.gateway_parameters.redis_url.clone() { - redis_url - } else { - "redis://127.0.0.1:6379".to_string() - } - } else { - self.cache_url.clone() - }, + cache: Some( + CacheModuleConfig::builder() + .url( + if self.cache_url.is_empty() { + if let Some(redis_url) = init_dto.gateway_parameters.redis_url.clone() { + redis_url.as_str() + } else { + "redis://127.0.0.1:6379" + } + } else { + self.cache_url.as_str() + } + .parse() + .map_err(|e| TardisError::internal_error(&format!("[Plugin.Auth]invalid redis url: {e:?}"), "-1"))?, + ) + .build() + .into(), + ), + log: init_dto.gateway_parameters.log_level.as_ref().map(|l| LogConfig { + level: l.parse().unwrap_or_default(), ..Default::default() - }, - log: init_dto.gateway_parameters.log_level.as_ref().map(|l| LogConfig { level: l.clone() }), + }), ..Default::default() }, }) @@ -465,6 +474,7 @@ mod tests { use bios_auth::auth_constants; + use super::*; use spacegate_kernel::config::gateway_dto::SgParameters; use spacegate_kernel::http::{Method, Uri, Version}; use spacegate_kernel::hyper::{self, Body, StatusCode}; @@ -472,11 +482,10 @@ mod tests { use tardis::crypto::crypto_sm2_4::{TardisCryptoSm2, TardisCryptoSm2PrivateKey}; use tardis::{ test::test_container::TardisTestContainer, - testcontainers::{self, clients::Cli, images::redis::Redis, Container}, + testcontainers::{self, clients::Cli, Container}, tokio, }; - - use super::*; + use testcontainers_modules::redis::Redis; #[tokio::test] async fn test() { @@ -811,7 +820,7 @@ mod tests { "".to_string(), None, ); - let (is_ok, mut before_filter_ctx) = filter_auth.req_filter("", ctx).await.unwrap(); + let (is_ok, before_filter_ctx) = filter_auth.req_filter("", ctx).await.unwrap(); assert!(!is_ok); assert_eq!(before_filter_ctx.get_action(), &SgRouteFilterRequestAction::Redirect); assert_eq!(before_filter_ctx.request.get_uri().path(), &format!("/{}", true_path)); @@ -832,30 +841,32 @@ mod tests { fn decode_context(headers: &HeaderMap) -> TardisContext { let config = TardisFuns::cs_config::(auth_constants::DOMAIN_CODE); let ctx = headers.get(&config.head_key_context).unwrap(); - let ctx = TardisFuns::crypto.base64.decode(ctx.to_str().unwrap()).unwrap(); + let ctx = TardisFuns::crypto.base64.decode_to_string(ctx.to_str().unwrap()).unwrap(); TardisFuns::json.str_to_obj(&ctx).unwrap() } fn crypto_req(body: &str, serv_pub_key: &str, front_pub_key: &str, need_crypto_resp: bool) -> (String, String) { let pub_key = TardisFuns::crypto.sm2.new_public_key_from_public_key(serv_pub_key).unwrap(); - let sm4_key = TardisFuns::crypto.key.rand_16_hex().unwrap(); - let sm4_iv = TardisFuns::crypto.key.rand_16_hex().unwrap(); + let sm4_key = TardisFuns::crypto.key.rand_16_bytes(); + let sm4_key_hex = TardisFuns::crypto.hex.encode(&sm4_key); + let sm4_iv = TardisFuns::crypto.key.rand_16_bytes(); + let sm4_iv_hex = TardisFuns::crypto.hex.encode(&sm4_key); let data = TardisFuns::crypto.sm4.encrypt_cbc(body, &sm4_key, &sm4_iv).unwrap(); let sign_data = TardisFuns::crypto.digest.sm3(&data).unwrap(); let sm4_encrypt = if need_crypto_resp { - pub_key.encrypt(&format!("{sign_data} {sm4_key} {sm4_iv} {front_pub_key}",)).unwrap() + pub_key.encrypt(&format!("{sign_data} {sm4_key_hex} {sm4_iv_hex} {front_pub_key}",)).unwrap() } else { - pub_key.encrypt(&format!("{sign_data} {sm4_key} {sm4_iv}",)).unwrap() + pub_key.encrypt(&format!("{sign_data} {sm4_key_hex} {sm4_iv_hex}",)).unwrap() }; let base64_encrypt = TardisFuns::crypto.base64.encode(sm4_encrypt); (data, base64_encrypt) } fn crypto_resp(body: &str, crypto_header: &str, front_pri_key: &TardisCryptoSm2PrivateKey) -> String { - let decode_base64 = TardisFuns::crypto.base64.decode(crypto_header).unwrap(); + let decode_base64 = TardisFuns::crypto.base64.decode_to_string(crypto_header).unwrap(); let decrypt_key = front_pri_key.decrypt(&decode_base64).unwrap(); let splits: Vec<_> = decrypt_key.split(' ').collect(); if splits.len() != 3 { diff --git a/gateway/test/Cargo.toml b/gateway/test/Cargo.toml index ffa05ee0d..98ffd7132 100644 --- a/gateway/test/Cargo.toml +++ b/gateway/test/Cargo.toml @@ -16,4 +16,4 @@ default = ["tardis", "tardis/web-server", "tardis/web-client", "tardis/crypto", [dependencies] serde.workspace = true run_script.workspace = true -tardis = { workspace = true, optional = true } +tardis = { workspace = true, optional = true } \ No newline at end of file diff --git a/gateway/test/src/init_apisix.rs b/gateway/test/src/init_apisix.rs index 98ef8c4cd..eef9d4d58 100644 --- a/gateway/test/src/init_apisix.rs +++ b/gateway/test/src/init_apisix.rs @@ -2,8 +2,7 @@ use tardis::basic::result::TardisResult; use tardis::serde_json::json; use tardis::testcontainers::clients::Cli; use tardis::testcontainers::core::WaitFor; -use tardis::testcontainers::images::generic::GenericImage; -use tardis::testcontainers::{images, Container, RunnableImage}; +use tardis::testcontainers::{GenericImage, Container, RunnableImage}; use tardis::TardisFuns; pub(crate) async fn init(docker: &Cli) -> TardisResult<(String, Container)> { @@ -20,7 +19,7 @@ pub(crate) async fn init(docker: &Cli) -> TardisResult<(String, Container = image.into(); runnable_image = runnable_image.with_network("host"); runnable_image = runnable_image.with_container_name("apisix_test"); @@ -45,7 +44,7 @@ pub(crate) async fn init(docker: &Cli) -> TardisResult<(String, Container TardisResult<()> { }) .unwrap(), ); - let resp: TardisResp = TardisFuns::web_client().post(&format!("{gateway_url}/test/echo/2"), &body, Some(header)).await?.body.unwrap(); + let resp: TardisResp = TardisFuns::web_client().post(&format!("{gateway_url}/test/echo/2"), &body, header).await?.body.unwrap(); let resp = resp.data.unwrap(); assert_eq!(&resp.code, "c001"); assert_eq!(&resp.description, "测试002"); assert!(!resp.done); let header: Vec<(String, String)> = vec![("Bios-Crypto".to_string(), "".to_string())]; - let resp: TardisResp = TardisFuns::web_client().get(&format!("{gateway_url}/test/echo/get/3"), Some(header)).await?.body.unwrap(); + let resp: TardisResp = TardisFuns::web_client().get(&format!("{gateway_url}/test/echo/get/3"), header).await?.body.unwrap(); let resp = resp.data.unwrap(); assert_eq!(resp, "3".to_string()); @@ -76,7 +76,7 @@ async fn main() -> TardisResult<()> { .unwrap(), ); let header: Vec<(String, String)> = vec![("Bios-Crypto".to_string(), "".to_string())]; - let resp: TardisResp = TardisFuns::web_client().post(&format!("{gateway_url}/apis"), &body, Some(header)).await?.body.unwrap(); + let resp: TardisResp = TardisFuns::web_client().post(&format!("{gateway_url}/apis"), &body, header).await?.body.unwrap(); let resp = resp.data.unwrap(); assert_eq!(&resp.code, "c001"); assert_eq!(&resp.description, "测试003"); @@ -102,7 +102,7 @@ impl AuthApi { let mut headers = req.headers; if req.path == "/test/echo/2" { assert!(req.body.is_some()); - let req_body = TardisFuns::crypto.base64.decode(req.body.clone().unwrap())?; + let req_body = TardisFuns::crypto.base64.decode_to_string(req.body.clone().unwrap())?; assert!(req_body.contains("测试002")); req.body = Some(req_body); headers.insert("Bios-Crypto".to_string(), "".to_string()); @@ -131,7 +131,7 @@ impl AuthApi { let mut headers = req.headers; if req.path.contains("/apis") { assert!(req.body.is_some()); - let req_body = TardisFuns::crypto.base64.decode(req.body.clone().unwrap())?; + let req_body = TardisFuns::crypto.base64.decode_to_string(req.body.clone().unwrap())?; assert!(req_body.contains("测试003")); req.body = Some(req_body); headers.insert("Bios-Crypto".to_string(), "".to_string()); @@ -156,15 +156,16 @@ async fn start_serv() -> TardisResult<()> { TardisFuns::init_conf(TardisConfig { cs: Default::default(), fw: FrameworkConfig { - web_server: WebServerConfig { - enabled: true, - port: 8080, - modules: HashMap::from([ - ("auth".to_string(), WebServerModuleConfig { ..Default::default() }), - ("test".to_string(), WebServerModuleConfig { ..Default::default() }), - ]), - ..Default::default() - }, + web_server: Some( + WebServerConfig::builder() + .common(WebServerCommonConfig::builder().port(8080).build()) + .default(WebServerModuleConfig::default()) + .modules([ + ("auth".to_string(), WebServerModuleConfig::default()), + ("test".to_string(), WebServerModuleConfig::default()), + ]) + .build(), + ), ..Default::default() }, }) diff --git a/middleware/flow/src/serv/flow_external_serv.rs b/middleware/flow/src/serv/flow_external_serv.rs index 7f308daeb..b43022fb6 100644 --- a/middleware/flow/src/serv/flow_external_serv.rs +++ b/middleware/flow/src/serv/flow_external_serv.rs @@ -214,13 +214,13 @@ impl FlowExternalServ { Ok(external_url.value.as_str().unwrap_or_default().to_string()) } - async fn headers(headers: Option>, _funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult>> { + async fn headers(headers: Option>, _funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult> { let base_ctx = (TARDIS_CONTEXT.to_string(), TardisFuns::crypto.base64.encode(TardisFuns::json.obj_to_string(ctx)?)); if let Some(mut headers) = headers { headers.push(base_ctx); - return Ok(Some(headers)); + return Ok(headers); } - let headers = Some(vec![base_ctx]); + let headers = vec![base_ctx]; Ok(headers) } } diff --git a/middleware/flow/tests/test_flow_api.rs b/middleware/flow/tests/test_flow_api.rs index 556fc21e9..a2b41aa9d 100644 --- a/middleware/flow/tests/test_flow_api.rs +++ b/middleware/flow/tests/test_flow_api.rs @@ -30,7 +30,7 @@ async fn test_flow_api() -> TardisResult<()> { flow_initializer::init_db(funs).await?; let web_server = TardisFuns::web_server(); - flow_initializer::init(web_server).await.unwrap(); + flow_initializer::init(&web_server).await.unwrap(); web_server.add_module("mock", mock_api::MockApi).await; init_spi_kv().await?; @@ -77,7 +77,7 @@ async fn init_spi_kv() -> TardisResult<()> { let web_server = TardisFuns::web_server(); // Initialize SPI KV - kv_initializer::init(web_server).await.unwrap(); + kv_initializer::init(&web_server).await.unwrap(); tokio::spawn(async move { web_server.start().await.unwrap(); diff --git a/middleware/schedule/src/serv/schedule_job_serv.rs b/middleware/schedule/src/serv/schedule_job_serv.rs index 550cdee24..64bdc68e8 100644 --- a/middleware/schedule/src/serv/schedule_job_serv.rs +++ b/middleware/schedule/src/serv/schedule_job_serv.rs @@ -37,15 +37,15 @@ fn service() -> Arc { pub(crate) async fn add_or_modify(add_or_modify: ScheduleJobAddOrModifyReq, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> { let log_url = &funs.conf::().log_url; let kv_url = &funs.conf::().kv_url; - let code = &add_or_modify.code.0; + let code = add_or_modify.code.as_str(); let spi_ctx = TardisContext { owner: funs.conf::().spi_app_id.clone(), ..ctx.clone() }; - let headers = Some(vec![( + let headers = vec![( "Tardis-Context".to_string(), TardisFuns::crypto.base64.encode(TardisFuns::json.obj_to_string(&spi_ctx)?), - )]); + )]; // if exist delete it first if service().code_uuid.write().await.get(code).is_some() { delete(code, funs, ctx).await?; @@ -78,7 +78,7 @@ pub(crate) async fn add_or_modify(add_or_modify: ScheduleJobAddOrModifyReq, funs let cache_key_job_changed_info = &config.cache_key_job_changed_info; conn.set_ex(&format!("{cache_key_job_changed_info}{code}"), "update", config.cache_key_job_changed_timer_sec as usize).await?; // 4. do add at local scheduler - ScheduleTaskServ::add(add_or_modify, config).await?; + ScheduleTaskServ::add(add_or_modify, &config).await?; Ok(()) } @@ -89,10 +89,10 @@ pub(crate) async fn delete(code: &str, funs: &TardisFunsInst, ctx: &TardisContex owner: funs.conf::().spi_app_id.clone(), ..ctx.clone() }; - let headers = Some(vec![( + let headers = vec![( "Tardis-Context".to_string(), TardisFuns::crypto.base64.encode(TardisFuns::json.obj_to_string(&spi_ctx)?), - )]); + )]; // 1. log this operation TardisFuns::web_client() .post_obj_to_str( @@ -133,10 +133,10 @@ pub(crate) async fn find_job(code: Option, page_number: u32, page_size: owner: funs.conf::().spi_app_id.clone(), ..ctx.clone() }; - let headers = Some(vec![( + let headers = [( "Tardis-Context".to_string(), TardisFuns::crypto.base64.encode(TardisFuns::json.obj_to_string(&spi_ctx)?), - )]); + )]; let resp = funs .web_client() .get::>>( @@ -196,10 +196,10 @@ pub(crate) async fn find_one_job(code: &str, funs: &TardisFunsInst, ctx: &Tardis owner: funs.conf::().spi_app_id.clone(), ..ctx.clone() }; - let headers = Some(vec![( + let headers = [( "Tardis-Context".to_string(), TardisFuns::crypto.base64.encode(TardisFuns::json.obj_to_string(&spi_ctx)?), - )]); + )]; let resp = funs.web_client().get::>>(&format!("{}/ci/item?key={}", kv_url, format_args!("{}{}", KV_KEY_CODE, code)), headers).await?; let Some(body) = resp.body else { @@ -225,10 +225,10 @@ pub(crate) async fn find_task( owner: funs.conf::().spi_app_id.clone(), ..ctx.clone() }; - let headers = Some(vec![( + let headers = [( "Tardis-Context".to_string(), TardisFuns::crypto.base64.encode(&TardisFuns::json.obj_to_string(&spi_ctx)?), - )]); + )]; let mut url = format!( "{}/ci/item?tag={}&key={}&page_number={}&page_size={}", log_url, @@ -322,7 +322,7 @@ impl OwnedScheduleTaskServ { let sync_db_ctx = ctx.clone(); // sync from db task tardis::tokio::spawn(async move { - let funs = TardisFuns::inst(DOMAIN_CODE.into(), None); + let funs = TardisFuns::inst(DOMAIN_CODE, None); // every 5 seconds, query if webserver is started let mut interval = time::interval(Duration::from_secs(5)); let config = funs.conf::().clone(); @@ -353,7 +353,7 @@ impl OwnedScheduleTaskServ { let serv = serv_raw.clone(); // sync from cache task tardis::tokio::spawn(async move { - let funs = TardisFuns::inst(DOMAIN_CODE.into(), None); + let funs = TardisFuns::inst(DOMAIN_CODE, None); let config = funs.conf::(); let mut interval = time::interval(Duration::from_secs(config.cache_key_job_changed_timer_sec as u64)); loop { @@ -379,11 +379,11 @@ impl OwnedScheduleTaskServ { while let Some(remote_job_code) = res_iter.next_item().await { { let code = remote_job_code.trim_start_matches(&config.cache_key_job_changed_info); - let funs = TardisFuns::inst(DOMAIN_CODE.into(), None); + let funs = TardisFuns::inst(DOMAIN_CODE, None); match self::find_one_job(code, &funs, &ctx).await { Ok(Some(resp)) => { // if we have this job code in local cache, update or add it - serv.add(resp.value, config).await.map_err(|e| error!("fail to delete schedule task: {e}")).unwrap_or_default(); + serv.add(resp.value, &config).await.map_err(|e| error!("fail to delete schedule task: {e}")).unwrap_or_default(); } Ok(None) => { // if we don't have this job code in local cache, remove it @@ -409,13 +409,13 @@ impl OwnedScheduleTaskServ { /// add schedule task pub async fn add(&self, job_config: ScheduleJobAddOrModifyReq, config: &ScheduleConfig) -> TardisResult<()> { - let has_job = { self.code_uuid.read().await.get(&job_config.code.0).is_some() }; + let has_job = { self.code_uuid.read().await.get(job_config.code.as_str()).is_some() }; if has_job { - self.delete(&job_config.code.0).await?; + self.delete(&job_config.code).await?; } let callback_url = job_config.callback_url.clone(); let log_url = config.log_url.clone(); - let code = job_config.code.0.clone(); + let code = job_config.code.to_string(); let lock_key = OwnedScheduleTaskServ::gen_distributed_lock_key(&code, config); let distributed_lock_expire_sec = config.distributed_lock_expire_sec; let ctx = TardisContext { @@ -426,10 +426,10 @@ impl OwnedScheduleTaskServ { groups: vec![], ..Default::default() }; - let headers = Some(vec![( + let headers = [( "Tardis-Context".to_string(), TardisFuns::crypto.base64.encode(TardisFuns::json.obj_to_string(&ctx)?), - )]); + )]; let enable_time = job_config.enable_time; let disable_time = job_config.disable_time; @@ -526,7 +526,7 @@ impl OwnedScheduleTaskServ { TardisError::internal_error(&msg, "500-middleware-schedual-create-task-failed") })?; { - self.code_uuid.write().await.insert(job_config.code.0.clone(), (uuid, job_config.cron.clone(), job_config.callback_url.clone())); + self.code_uuid.write().await.insert(job_config.code.to_string(), (uuid, job_config.cron.clone(), job_config.callback_url.clone())); } Ok(()) } diff --git a/middleware/schedule/tests/test_common.rs b/middleware/schedule/tests/test_common.rs index 690e5b5e2..1d2ce63ba 100644 --- a/middleware/schedule/tests/test_common.rs +++ b/middleware/schedule/tests/test_common.rs @@ -48,9 +48,9 @@ pub async fn init_tardis() -> TardisResult<()> { let web_server = TardisFuns::web_server(); // cache_initializer::init(web_server).await?; bios_basic::rbum::rbum_initializer::init("bios-spi", bios_basic::rbum::rbum_config::RbumConfig::default()).await?; - log_initializer::init(web_server).await?; - kv_initializer::init(web_server).await?; - schedule_initializer::init(web_server).await?; + log_initializer::init(&web_server).await?; + kv_initializer::init(&web_server).await?; + schedule_initializer::init(&web_server).await?; Ok(()) } #[allow(dead_code)] diff --git a/middleware/schedule/tests/test_schedule.rs b/middleware/schedule/tests/test_schedule.rs index 9354398b8..4a563d03e 100644 --- a/middleware/schedule/tests/test_schedule.rs +++ b/middleware/schedule/tests/test_schedule.rs @@ -35,9 +35,9 @@ async fn init_data() -> TardisResult<()> { bios_basic::rbum::rbum_initializer::init(DOMAIN_CODE, RbumConfig::default()).await?; let web_server = TardisFuns::web_server(); // Initialize SPI shedule - schedule_initializer::init(web_server).await?; - log_initializer::init(web_server).await?; - kv_initializer::init(web_server).await?; + schedule_initializer::init(&web_server).await?; + log_initializer::init(&web_server).await?; + kv_initializer::init(&web_server).await?; tokio::spawn(async move { web_server.start().await.unwrap(); diff --git a/sdk/invoke/src/clients.rs b/sdk/invoke/src/clients.rs index 0526631cc..eed499454 100644 --- a/sdk/invoke/src/clients.rs +++ b/sdk/invoke/src/clients.rs @@ -181,7 +181,7 @@ macro_rules! tardis_api { )* let url = self.get_url(&[$($path,)*], query.as_ref()); let header = self.get_tardis_context_header()?; - let resp = self.get_funs().web_client().get::>(&url, Some(vec![header])).await?; + let resp = self.get_funs().web_client().get::>(&url, vec![header]).await?; Self::extract_response(resp) } }; @@ -204,7 +204,7 @@ macro_rules! tardis_api { )* let url = self.get_url(&[$($path,)*], query.as_ref()); let header = self.get_tardis_context_header()?; - let resp = self.get_funs().web_client().post::<$Body, TardisResp<$Resp>>(&url, body, Some(vec![header])).await?; + let resp = self.get_funs().web_client().post::<$Body, TardisResp<$Resp>>(&url, body, vec![header]).await?; Self::extract_response(resp) } }; @@ -227,7 +227,7 @@ macro_rules! tardis_api { )* let url = self.get_url(&[$($path,)*], query.as_ref()); let header = self.get_tardis_context_header()?; - let resp = self.get_funs().web_client().put::<$Body, TardisResp<$Resp>>(&url, body, Some(vec![header])).await?; + let resp = self.get_funs().web_client().put::<$Body, TardisResp<$Resp>>(&url, body, vec![header]).await?; Self::extract_response(resp) } }; @@ -250,7 +250,7 @@ macro_rules! tardis_api { )* let url = self.get_url(&[$($path,)*], query.as_ref()); let header = self.get_tardis_context_header()?; - let resp = self.get_funs().web_client().delete::>(&url, Some(vec![header])).await?; + let resp = self.get_funs().web_client().delete::>(&url, vec![header]).await?; Self::extract_response(resp) } }; diff --git a/sdk/invoke/src/clients/base_spi_client.rs b/sdk/invoke/src/clients/base_spi_client.rs index 37f20e364..88de66969 100644 --- a/sdk/invoke/src/clients/base_spi_client.rs +++ b/sdk/invoke/src/clients/base_spi_client.rs @@ -21,7 +21,7 @@ impl BaseSpiClient { Err(funs.err().conflict("spi-module", "spi_module", "spi module uri Not configured yet.", "400-spi-module-not-exist")) } - pub async fn headers(headers: Option>, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult>> { + pub async fn headers(headers: Option>, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult> { let spi_ctx = TardisContext { owner: funs.invoke_conf_spi_app_id(), ..ctx.clone() @@ -29,9 +29,9 @@ impl BaseSpiClient { let base_ctx = (TARDIS_CONTEXT.to_string(), TardisFuns::crypto.base64.encode(TardisFuns::json.obj_to_string(&spi_ctx)?)); if let Some(mut headers) = headers { headers.push(base_ctx); - return Ok(Some(headers)); + return Ok(headers); } - let headers = Some(vec![base_ctx]); + let headers = vec![base_ctx]; Ok(headers) } diff --git a/services/bios-all/src/initializer.rs b/services/bios-all/src/initializer.rs index 88cf409a1..4bae87328 100644 --- a/services/bios-all/src/initializer.rs +++ b/services/bios-all/src/initializer.rs @@ -1,5 +1,3 @@ -use std::sync::Arc; - use bios_reach::reach_send_channel::SendChannelMap; use tardis::basic::result::TardisResult; use tardis::web::web_server::TardisWebServer; @@ -11,7 +9,7 @@ pub async fn init(web_server: &TardisWebServer) -> TardisResult<()> { web_server, SendChannelMap::new() .with_arc_channel(bios_client_hwsms::SmsClient::from_reach_config()) - .with_arc_channel(Arc::new(tardis::TardisFuns::mail_by_module_or_default(bios_reach::reach_consts::MODULE_CODE))), + .with_arc_channel(tardis::TardisFuns::mail_by_module_or_default(bios_reach::reach_consts::MODULE_CODE)), ) .await?; diff --git a/services/bios-all/src/main.rs b/services/bios-all/src/main.rs index 1d495fac1..a831f3d83 100644 --- a/services/bios-all/src/main.rs +++ b/services/bios-all/src/main.rs @@ -13,7 +13,7 @@ async fn main() -> TardisResult<()> { // env::set_var("RUST_LOG", "debug,tardis=trace,sqlx=off,bios=bios-serv-all,hyper::proto=off,sqlparser::parser=off"); TardisFuns::init(Some("config")).await?; let web_server = TardisFuns::web_server(); - initializer::init(web_server).await?; + initializer::init(&web_server).await?; web_server.start().await?; web_server.await; Ok(()) diff --git a/services/spi-all/src/main.rs b/services/spi-all/src/main.rs index 0b76f42fc..79bcd3a1b 100644 --- a/services/spi-all/src/main.rs +++ b/services/spi-all/src/main.rs @@ -13,7 +13,7 @@ async fn main() -> TardisResult<()> { // env::set_var("RUST_LOG", "debug,tardis=trace,sqlx=off,bios-spi-serv-all=trace,hyper::proto=off,sqlparser::parser=off"); TardisFuns::init(Some("config")).await?; let web_server = TardisFuns::web_server(); - initializer::init(web_server).await?; + initializer::init(&web_server).await?; web_server.start().await?; web_server.await; Ok(()) diff --git a/spi/spi-cache/src/serv/redis/cache_redis_initializer.rs b/spi/spi-cache/src/serv/redis/cache_redis_initializer.rs index 5f7bdb643..e100b018a 100644 --- a/spi/spi-cache/src/serv/redis/cache_redis_initializer.rs +++ b/spi/spi-cache/src/serv/redis/cache_redis_initializer.rs @@ -3,11 +3,14 @@ use std::collections::HashMap; use bios_basic::spi::{dto::spi_bs_dto::SpiBsCertResp, spi_funs::SpiBsInst, spi_initializer}; use tardis::{ basic::{dto::TardisContext, result::TardisResult}, - cache::cache_client::TardisCacheClient, + cache::cache_client::TardisCacheClient, config::config_dto::CacheModuleConfig, }; pub async fn init(bs_cert: &SpiBsCertResp, ctx: &TardisContext, _: bool) -> TardisResult { - let client = TardisCacheClient::init(&bs_cert.conn_uri).await?; + let config = CacheModuleConfig { + url: bs_cert.conn_uri.parse().expect("invalid url") + }; + let client = TardisCacheClient::init(&config).await?; let mut ext = HashMap::new(); if !bs_cert.private { let key_prefix = spi_initializer::common::get_isolation_flag_from_context(ctx); diff --git a/spi/spi-cache/tests/test_cache.rs b/spi/spi-cache/tests/test_cache.rs index 1b69868e3..63b007907 100644 --- a/spi/spi-cache/tests/test_cache.rs +++ b/spi/spi-cache/tests/test_cache.rs @@ -32,7 +32,7 @@ async fn init_data() -> TardisResult<()> { let web_server = TardisFuns::web_server(); // Initialize SPI cache - cache_initializer::init(web_server).await.unwrap(); + cache_initializer::init(&web_server).await.unwrap(); tokio::spawn(async move { web_server.start().await.unwrap(); diff --git a/spi/spi-conf/Cargo.toml b/spi/spi-conf/Cargo.toml index 1b2e16283..017279a27 100644 --- a/spi/spi-conf/Cargo.toml +++ b/spi/spi-conf/Cargo.toml @@ -40,5 +40,6 @@ tokio-stream = { version = "0.1.14", features = ["sync"] } tardis = { workspace = true, features = ["test", "conf-remote", "mq"] } bios-basic = { path = "../../basic", features = ["default", "test"] } reqwest = "*" +testcontainers-modules = { workspace = true } [build-dependencies] poem-grpc-build = "0.2.21" diff --git a/spi/spi-conf/src/api.rs b/spi/spi-conf/src/api.rs index 1c1c1307d..5b08f14de 100644 --- a/spi/spi-conf/src/api.rs +++ b/spi/spi-conf/src/api.rs @@ -25,11 +25,11 @@ pub async fn init_api(web_server: &TardisWebServer) { pub async fn init_nacos_servers(cfg: &ConfConfig) -> TardisResult<()> { log::info!("[Spi.Conf] init nacos server"); - let http_server = TardisWebServer::init_simple(&cfg.nacos_host.to_string(), cfg.nacos_port)?; + let http_server = TardisWebServer::init_simple(cfg.nacos_host, cfg.nacos_port)?; let mut nacos_module = WebServerModule::new(ConfNacosApi::default()); nacos_module.options.set_uniform_error(false); - let grpc_server = TardisWebServer::init_simple(&cfg.nacos_host.to_string(), cfg.nacos_grpc_port)?; + let grpc_server = TardisWebServer::init_simple(cfg.nacos_host, cfg.nacos_grpc_port)?; grpc_server .add_grpc_route( diff --git a/spi/spi-conf/src/conf_config.rs b/spi/spi-conf/src/conf_config.rs index 64a817104..5db7c2ece 100644 --- a/spi/spi-conf/src/conf_config.rs +++ b/spi/spi-conf/src/conf_config.rs @@ -33,7 +33,7 @@ impl Default for ConfConfig { let auth_key = crypto_base64::TardisCryptoBase64.encode(random::<[u8; 32]>()); let password = format!("{:016x}", random::()); Self { - /// 18000 secs (5 hours) + // 18000 secs (5 hours) token_ttl: 18000, auth_key, auth_username: String::from("nacos"), diff --git a/spi/spi-conf/src/conf_initializer.rs b/spi/spi-conf/src/conf_initializer.rs index 28a4d6cfe..866e268fe 100644 --- a/spi/spi-conf/src/conf_initializer.rs +++ b/spi/spi-conf/src/conf_initializer.rs @@ -23,7 +23,7 @@ pub async fn init(web_server: &TardisWebServer) -> TardisResult<()> { init_api(web_server).await; let funs = crate::get_tardis_inst(); let cfg = funs.conf::(); - init_nacos_servers(cfg).await?; + init_nacos_servers(&cfg).await?; Ok(()) } diff --git a/spi/spi-conf/src/serv/pg/conf_pg_config_serv.rs b/spi/spi-conf/src/serv/pg/conf_pg_config_serv.rs index 49b3dcb9c..fbebf3618 100644 --- a/spi/spi-conf/src/serv/pg/conf_pg_config_serv.rs +++ b/spi/spi-conf/src/serv/pg/conf_pg_config_serv.rs @@ -36,10 +36,8 @@ macro_rules! get { use super::{add_history, conf_pg_initializer, gen_select_sql_stmt, HistoryInsertParams, OpType}; fn md5(content: &str) -> String { - use tardis::crypto::rust_crypto::{digest::Digest, md5::Md5}; - let mut md5 = Md5::new(); - md5.input_str(content); - md5.result_str() + use tardis::crypto::crypto_digest::TardisCryptoDigest; + TardisCryptoDigest.md5(content).expect("md5 digest shouldn't fail") } pub async fn get_config(descriptor: &mut ConfigDescriptor, _funs: &TardisFunsInst, ctx: &TardisContext, bs_inst: &SpiBsInst) -> TardisResult { diff --git a/spi/spi-conf/tests/config/conf-default.toml b/spi/spi-conf/tests/config/conf-default.toml index 83be73fed..a6dc7f961 100644 --- a/spi/spi-conf/tests/config/conf-default.toml +++ b/spi/spi-conf/tests/config/conf-default.toml @@ -6,7 +6,6 @@ nacos_port = 8848 nacos_grpc_port = 9848 nacos_host = "::0" [fw.web_server] -enabled = true port = 8080 tls_key = """ -----BEGIN PRIVATE KEY----- @@ -63,4 +62,7 @@ tCVeOl3bWPk2TUtKWlVKeiThSqCrgSB0msQ06niVFCKokC4= title = "配置服务" doc_urls = [["test env", "http://localhost:8080/"]] uniform_error = false -[fw.web_server.modules.spi-conf-nacos] \ No newline at end of file +[fw.web_server.modules.spi-conf-nacos] + +[fw.log] +level = "info" diff --git a/spi/spi-conf/tests/spi_conf_api_test.rs b/spi/spi-conf/tests/spi_conf_api_test.rs index 63ec8394d..79befcf0b 100644 --- a/spi/spi-conf/tests/spi_conf_api_test.rs +++ b/spi/spi-conf/tests/spi_conf_api_test.rs @@ -29,7 +29,7 @@ async fn spi_conf_namespace_test() -> TardisResult<()> { std::env::set_var("RUST_LOG", "info,sqlx=off,sea_orm=debug,spi_conf_namespace_test=DEBUG,bios_spi_conf=TRACE"); let docker = testcontainers::clients::Cli::default(); let container_hold = init_tardis(&docker).await?; - let _web_server_hanlde = start_web_server(); + let _web_server_hanlde = start_web_server().await?; let tardis_ctx = TardisContext::default(); let mut client = TestHttpClient::new("https://localhost:8080/spi-conf".to_string()); client.set_auth(&tardis_ctx)?; diff --git a/spi/spi-conf/tests/spi_conf_listener_test.rs b/spi/spi-conf/tests/spi_conf_listener_test.rs index 6a39b0066..47324ebd3 100644 --- a/spi/spi-conf/tests/spi_conf_listener_test.rs +++ b/spi/spi-conf/tests/spi_conf_listener_test.rs @@ -26,7 +26,7 @@ async fn spi_conf_namespace_test() -> TardisResult<()> { std::env::set_var("RUST_LOG", "error,spi_conf_listener_test=debug,sqlx=off,sea_orm=off,bios_spi_conf=DEBUG"); let docker = testcontainers::clients::Cli::default(); let container_hold = init_tardis(&docker).await?; - let _web_server_hanlde = start_web_server(); + start_web_server().await?; let tardis_ctx = TardisContext::default(); let mut client = TestHttpClient::new("https://localhost:8080/spi-conf".to_string()); client.set_auth(&tardis_ctx)?; @@ -47,7 +47,7 @@ async fn spi_conf_namespace_test() -> TardisResult<()> { }, ) .await; - let _: Void = client.put(&format!("/ci/manage/bs/{}/rel/app001", bs_id), &Void {}).await; + let _: tardis::serde_json::Value = client.put(&format!("/ci/manage/bs/{}/rel/app001", bs_id), &Void {}).await; client.set_auth(&TardisContext { own_paths: "t1/app001".to_string(), ak: "".to_string(), @@ -85,11 +85,8 @@ pub async fn test_single_listener(client: &mut TestHttpClient) -> TardisResult<( ) .await; let config = client.get::(&format!("/ci/cs/config?group=DEFAULT-GROUP&data_id={DATA_ID}")).await; - use tardis::crypto::rust_crypto::{digest::Digest, md5::Md5}; - let mut md5_encoder = Md5::new(); - md5_encoder.input_str(config.as_str()); - let md5 = md5_encoder.result_str(); - md5_encoder.reset(); + use tardis::crypto::crypto_digest::TardisCryptoDigest; + let md5 = TardisCryptoDigest.md5(config)?; let config = client.get_resp::(&format!("/ci/cs/configs/listener?data_id={DATA_ID}&group=DEFAULT-GROUP&md5={md5}")).await; // with a correct md5, no config should be returned assert!(config.data.is_none()); @@ -111,9 +108,7 @@ pub async fn test_single_listener(client: &mut TestHttpClient) -> TardisResult<( assert!(config.data.is_some()); // get new config let config = client.get::(&format!("/ci/cs/config?group=DEFAULT-GROUP&data_id={DATA_ID}")).await; - md5_encoder.input_str(config.as_str()); - let md5 = md5_encoder.result_str(); - md5_encoder.reset(); + let md5 = tardis::crypto::crypto_digest::TardisCryptoDigest.md5(config)?; // check update again let config = client.get_resp::(&format!("/ci/cs/configs/listener?data_id={DATA_ID}&group=DEFAULT-GROUP&md5={md5}")).await; // with a correct md5, no config should be returned @@ -170,16 +165,13 @@ pub async fn test_listener(client: &mut TestHttpClient) -> TardisResult<()> { let client = get_client("https://localhost:8080/spi-conf", &ctx); let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(1)); let mut md5 = String::new(); - use tardis::crypto::rust_crypto::{digest::Digest, md5::Md5}; - let mut md5_encoder = Md5::new(); + use tardis::crypto::crypto_digest::TardisCryptoDigest; loop { let config = client.get_resp::(&format!("/ci/cs/configs/listener?data_id=conf-default&group=DEFAULT-GROUP&md5={md5}")).await; if config.data.is_some() { update_counter.fetch_add(1, std::sync::atomic::Ordering::SeqCst); let config = client.get::("/ci/cs/config?group=DEFAULT-GROUP&data_id=conf-default").await; - md5_encoder.input_str(&config); - md5 = md5_encoder.result_str(); - md5_encoder.reset(); + md5 = TardisCryptoDigest.md5(config).expect("shall not fail"); } interval.tick().await; } diff --git a/spi/spi-conf/tests/spi_conf_nacos_compatible_test.rs b/spi/spi-conf/tests/spi_conf_nacos_compatible_test.rs index d4e6a439b..0138acece 100644 --- a/spi/spi-conf/tests/spi_conf_nacos_compatible_test.rs +++ b/spi/spi-conf/tests/spi_conf_nacos_compatible_test.rs @@ -9,6 +9,7 @@ use bios_spi_conf::{ conf_constants::DOMAIN_CODE, dto::conf_auth_dto::{RegisterRequest, RegisterResponse}, }; +use poem::http::HeaderName; use tardis::{ basic::{dto::TardisContext, field::TrimString, result::TardisResult}, log, testcontainers, tokio, @@ -28,7 +29,7 @@ async fn spi_conf_namespace_test() -> TardisResult<()> { std::env::set_var("PROFILE", "nacos"); let docker = testcontainers::clients::Cli::default(); let container_hold = init_tardis(&docker).await?; - let _web_server_hanlde = start_web_server(); + let _web_server_hanlde = start_web_server().await; let tardis_ctx = TardisContext::default(); let mut client = TestHttpClient::new(format!("{SCHEMA}://localhost:8080/spi-conf")); client.set_auth(&tardis_ctx)?; @@ -69,6 +70,7 @@ async fn spi_conf_namespace_test() -> TardisResult<()> { async fn test_tardis_compatibility(_test_client: &TestHttpClient) -> TardisResult<()> { use tardis::config::config_nacos::nacos_client::*; + let config = TardisFuns::fw_config(); let ctx = TardisContext { own_paths: "t1/app001".to_string(), ak: "".to_string(), @@ -79,7 +81,9 @@ async fn test_tardis_compatibility(_test_client: &TestHttpClient) -> TardisResul }; let ctx_base64 = &TardisFuns::crypto.base64.encode(TardisFuns::json.obj_to_string(&ctx)?); let mut headers = reqwest::header::HeaderMap::new(); - headers.append(TardisFuns::fw_config().web_server.context_conf.context_header_name.as_str(), ctx_base64.parse().unwrap()); + let web_server_config = config.web_server(); + let context_header_name = web_server_config.context_conf.context_header_name.clone(); + headers.append(HeaderName::from_bytes(context_header_name.as_bytes()).expect("should be ok") , ctx_base64.parse().unwrap()); let client = reqwest::ClientBuilder::default().danger_accept_invalid_certs(true).default_headers(headers).build().unwrap(); let mut nacos_client = NacosClient::new_with_client(format!("{SCHEMA}://localhost:8080/spi-conf-nacos/nacos"), client); // register diff --git a/spi/spi-conf/tests/spi_conf_test_common.rs b/spi/spi-conf/tests/spi_conf_test_common.rs index abd4aee4f..a5e71c293 100644 --- a/spi/spi-conf/tests/spi_conf_test_common.rs +++ b/spi/spi-conf/tests/spi_conf_test_common.rs @@ -3,14 +3,14 @@ use bios_basic::{ test::test_http_client::TestHttpClient, }; use bios_spi_conf::{conf_constants::DOMAIN_CODE, conf_initializer}; -use tardis::testcontainers::images::{generic::GenericImage, redis::Redis}; +use tardis::testcontainers::GenericImage; use tardis::{ basic::{dto::TardisContext, result::TardisResult}, test::test_container::TardisTestContainer, testcontainers::{clients::Cli, Container}, - tokio::{self, task::JoinHandle}, TardisFuns, }; +use testcontainers_modules::redis::Redis; pub struct Holder<'d> { pub pg: Container<'d, GenericImage>, pub redis: Container<'d, Redis>, @@ -45,9 +45,8 @@ pub async fn init_tardis(docker: &Cli) -> TardisResult { } #[allow(dead_code)] -pub fn start_web_server() -> JoinHandle> { - let task = TardisFuns::web_server().start(); - tokio::spawn(task) +pub async fn start_web_server() -> TardisResult<()> { + TardisFuns::web_server().start().await } #[allow(dead_code)] diff --git a/spi/spi-graph/tests/test_graph.rs b/spi/spi-graph/tests/test_graph.rs index a7469de29..373fc78b2 100644 --- a/spi/spi-graph/tests/test_graph.rs +++ b/spi/spi-graph/tests/test_graph.rs @@ -38,7 +38,7 @@ async fn init_data() -> TardisResult<()> { let web_server = TardisFuns::web_server(); // Initialize SPI Graph - graph_initializer::init(web_server).await.unwrap(); + graph_initializer::init(&web_server).await.unwrap(); tokio::spawn(async move { web_server.start().await.unwrap(); diff --git a/spi/spi-kv/tests/test_kv.rs b/spi/spi-kv/tests/test_kv.rs index a267e4e0c..55d95b9e4 100644 --- a/spi/spi-kv/tests/test_kv.rs +++ b/spi/spi-kv/tests/test_kv.rs @@ -35,7 +35,7 @@ async fn init_data() -> TardisResult<()> { let web_server = TardisFuns::web_server(); // Initialize SPI KV - kv_initializer::init(web_server).await.unwrap(); + kv_initializer::init(&web_server).await.unwrap(); tokio::spawn(async move { web_server.start().await.unwrap(); diff --git a/spi/spi-log/tests/test_log.rs b/spi/spi-log/tests/test_log.rs index 3fd6f33eb..6b42aa28a 100644 --- a/spi/spi-log/tests/test_log.rs +++ b/spi/spi-log/tests/test_log.rs @@ -35,7 +35,7 @@ async fn init_data() -> TardisResult<()> { let web_server = TardisFuns::web_server(); // Initialize SPI Log - log_initializer::init(web_server).await.unwrap(); + log_initializer::init(&web_server).await.unwrap(); tokio::spawn(async move { web_server.start().await.unwrap(); diff --git a/spi/spi-object/src/serv/s3/object_s3_initializer.rs b/spi/spi-object/src/serv/s3/object_s3_initializer.rs index 86a75382c..6aafe720a 100644 --- a/spi/spi-object/src/serv/s3/object_s3_initializer.rs +++ b/spi/spi-object/src/serv/s3/object_s3_initializer.rs @@ -3,6 +3,7 @@ use std::collections::HashMap; use bios_basic::spi::{dto::spi_bs_dto::SpiBsCertResp, spi_funs::SpiBsInst, spi_initializer}; use tardis::{ basic::{dto::TardisContext, error::TardisError, result::TardisResult}, + config::config_dto::OSModuleConfig, os::os_client::TardisOSClient, TardisFuns, }; @@ -11,24 +12,20 @@ use tardis::serde_json::Value as JsonValue; pub async fn init(bs_cert: &SpiBsCertResp, ctx: &TardisContext, _: bool) -> TardisResult { let ext = TardisFuns::json.str_to_json(&bs_cert.ext)?; - let client = TardisOSClient::init( - "s3", - &bs_cert.conn_uri, - &bs_cert.ak, - &bs_cert.sk, - ext.get("region").and_then(JsonValue::as_str).ok_or(TardisError::bad_request( - "Tardis context ext should have a `region` field with type string", + let region = ext.get("region").and_then(JsonValue::as_str).ok_or(TardisError::bad_request( + "Tardis context ext should have a `region` field with type string", + "400-spi-invalid-tardis-ctx", + ))?; + let default_bucket = if let Some(default_bucket) = ext.get("default_bucket") { + default_bucket.as_str().ok_or(TardisError::bad_request( + "Tardis context ext should have a `default_bucket` field with type string", "400-spi-invalid-tardis-ctx", - ))?, - if let Some(default_bucket) = ext.get("default_bucket") { - default_bucket.as_str().ok_or(TardisError::bad_request( - "Tardis context ext should have a `default_bucket` field with type string", - "400-spi-invalid-tardis-ctx", - ))? - } else { - "" - }, - )?; + ))? + } else { + "" + }; + let tardis_os_config = OSModuleConfig::builder().kind("s3").endpoint(&bs_cert.conn_uri).ak(&bs_cert.ak).sk(&bs_cert.sk).region(region).default_bucket(default_bucket).build(); + let client = TardisOSClient::init(&tardis_os_config)?; let mut ext = HashMap::new(); if !bs_cert.private { let bucket_name_prefix = spi_initializer::common::get_isolation_flag_from_context(ctx); diff --git a/spi/spi-object/src/serv/s3/object_s3_obj_serv.rs b/spi/spi-object/src/serv/s3/object_s3_obj_serv.rs index b708ca056..43b97f3bc 100644 --- a/spi/spi-object/src/serv/s3/object_s3_obj_serv.rs +++ b/spi/spi-object/src/serv/s3/object_s3_obj_serv.rs @@ -23,11 +23,11 @@ pub async fn presign_obj_url( let client = bs_inst.0; let bucket_name = common::get_isolation_flag_from_ext(bs_inst.1).map(|bucket_name_prefix| format!("{}-{}", bucket_name_prefix, if private { "pri" } else { "pub" })); match presign_kind { - ObjectObjPresignKind::Upload => client.object_create_url(object_path, exp_secs, bucket_name), - ObjectObjPresignKind::Delete => client.object_delete_url(object_path, exp_secs, bucket_name), + ObjectObjPresignKind::Upload => client.object_create_url(object_path, exp_secs, bucket_name.as_deref()), + ObjectObjPresignKind::Delete => client.object_delete_url(object_path, exp_secs, bucket_name.as_deref()), ObjectObjPresignKind::View => { if private { - client.object_get_url(object_path, exp_secs, bucket_name) + client.object_get_url(object_path, exp_secs, bucket_name.as_deref()) } else { let Some(bucket_name) = bucket_name else { return Err(TardisError::internal_error( diff --git a/spi/spi-object/tests/test_object.rs b/spi/spi-object/tests/test_object.rs index 14917f3f2..bfb29d303 100644 --- a/spi/spi-object/tests/test_object.rs +++ b/spi/spi-object/tests/test_object.rs @@ -34,7 +34,7 @@ async fn init_data(minio_url: &str) -> TardisResult<()> { let web_server = TardisFuns::web_server(); // Initialize SPI object - object_initializer::init(web_server).await.unwrap(); + object_initializer::init(&web_server).await.unwrap(); tokio::spawn(async move { web_server.start().await.unwrap(); diff --git a/spi/spi-plugin/src/api/ci/plugin_ci_api_api.rs b/spi/spi-plugin/src/api/ci/plugin_ci_api_api.rs index 1683c030a..55127427a 100644 --- a/spi/spi-plugin/src/api/ci/plugin_ci_api_api.rs +++ b/spi/spi-plugin/src/api/ci/plugin_ci_api_api.rs @@ -2,7 +2,6 @@ use bios_basic::rbum::dto::rbum_filer_dto::RbumBasicFilterReq; use bios_basic::rbum::serv::rbum_item_serv::RbumItemCrudOperation; use bios_basic::spi::serv::spi_bs_serv::SpiBsServ; -use tardis::basic::result; use tardis::chrono::{self, Utc}; use tardis::web::context_extractor::TardisContextExtractor; diff --git a/spi/spi-plugin/src/serv/plugin_exec_serv.rs b/spi/spi-plugin/src/serv/plugin_exec_serv.rs index c59dc6dd5..a9eceefcd 100644 --- a/spi/spi-plugin/src/serv/plugin_exec_serv.rs +++ b/spi/spi-plugin/src/serv/plugin_exec_serv.rs @@ -39,7 +39,7 @@ impl PluginExecServ { } headers.push(("Content-Type".to_string(), spi_api.content_type.to_string())); headers.push(("Callback-Url".to_string(), spi_api.callback.to_string())); - let headers = Some(headers); + let headers = headers; info!("url: {}", url); match spi_api.http_method { crate::plugin_enumeration::PluginApiMethodKind::GET => { diff --git a/spi/spi-plugin/tests/test_plugin.rs b/spi/spi-plugin/tests/test_plugin.rs index 5f100d8bf..07ad0d414 100644 --- a/spi/spi-plugin/tests/test_plugin.rs +++ b/spi/spi-plugin/tests/test_plugin.rs @@ -39,7 +39,7 @@ async fn init_data() -> TardisResult<()> { let web_server = TardisFuns::web_server(); // Initialize SPI plugin - plugin_initializer::init(web_server).await.unwrap(); + plugin_initializer::init(&web_server).await.unwrap(); tokio::spawn(async move { web_server.start().await.unwrap(); diff --git a/spi/spi-reldb/src/reldb_initializer.rs b/spi/spi-reldb/src/reldb_initializer.rs index 0d90784de..aeb9c386a 100644 --- a/spi/spi-reldb/src/reldb_initializer.rs +++ b/spi/spi-reldb/src/reldb_initializer.rs @@ -7,6 +7,7 @@ use bios_basic::spi::{ }; use tardis::{ basic::{dto::TardisContext, error::TardisError, result::TardisResult}, + config::config_dto::DBModuleConfig, db::reldb_client::{TardisRelDBClient, TardisRelDBlConnection}, serde_json::Value as JsonValue, web::web_server::TardisWebServer, @@ -47,21 +48,22 @@ async fn init_api(web_server: &TardisWebServer) -> TardisResult<()> { pub async fn init_fun(bs_cert: SpiBsCertResp, ctx: &TardisContext, _: bool) -> TardisResult { let ext = TardisFuns::json.str_to_json(&bs_cert.ext)?; let compatible_type = TardisFuns::json.json_to_obj(ext.get("compatible_type").unwrap_or(&tardis::serde_json::Value::String("None".to_string())).clone())?; - let client = TardisRelDBClient::init( - &bs_cert.conn_uri, - ext.get("max_connections").and_then(JsonValue::as_u64).ok_or(TardisError::bad_request( + + let config = DBModuleConfig { + url: bs_cert.conn_uri.clone(), + max_connections: ext.get("max_connections").and_then(JsonValue::as_u64).ok_or(TardisError::bad_request( "Tardis context ext expect `max_connections` as an unsigned interger number", "400-spi-invalid-tardis-ctx", ))? as u32, - ext.get("min_connections").and_then(JsonValue::as_u64).ok_or(TardisError::bad_request( + min_connections: ext.get("min_connections").and_then(JsonValue::as_u64).ok_or(TardisError::bad_request( "Tardis context ext expect `min_connections` as an unsigned interger number", "400-spi-invalid-tardis-ctx", ))? as u32, - None, - None, + connect_timeout_sec: None, + idle_timeout_sec: None, compatible_type, - ) - .await?; + }; + let client = TardisRelDBClient::init(&config).await?; let ext = match bs_cert.kind_code.as_str() { #[cfg(feature = "spi-pg")] spi_constants::SPI_PG_KIND_CODE => serv::pg::reldb_pg_initializer::init(&bs_cert, &client, ctx).await?, diff --git a/spi/spi-reldb/tests/test_reldb.rs b/spi/spi-reldb/tests/test_reldb.rs index ddbe15528..37f729cc2 100644 --- a/spi/spi-reldb/tests/test_reldb.rs +++ b/spi/spi-reldb/tests/test_reldb.rs @@ -35,7 +35,7 @@ async fn init_data() -> TardisResult<()> { let web_server = TardisFuns::web_server(); // Initialize SPI reldb - reldb_initializer::init(web_server).await.unwrap(); + reldb_initializer::init(&web_server).await.unwrap(); tokio::spawn(async move { web_server.start().await.unwrap(); diff --git a/spi/spi-search/Cargo.toml b/spi/spi-search/Cargo.toml index 0ed10ad1c..6dba1ddc4 100644 --- a/spi/spi-search/Cargo.toml +++ b/spi/spi-search/Cargo.toml @@ -28,3 +28,4 @@ strum = { workerspace = true, features = ["derive"] } [dev-dependencies] tardis = { workspace = true, features = ["test"] } bios-basic = { path = "../../basic", features = ["default", "test"] } +testcontainers-modules ={ workspace = true } \ No newline at end of file diff --git a/spi/spi-search/src/serv/es/search_es_initializer.rs b/spi/spi-search/src/serv/es/search_es_initializer.rs index 16ec70058..7af54b915 100644 --- a/spi/spi-search/src/serv/es/search_es_initializer.rs +++ b/spi/spi-search/src/serv/es/search_es_initializer.rs @@ -3,11 +3,12 @@ use std::collections::HashMap; use bios_basic::spi::{dto::spi_bs_dto::SpiBsCertResp, spi_funs::SpiBsInst, spi_initializer}; use tardis::{ basic::{dto::TardisContext, result::TardisResult}, - search::search_client::TardisSearchClient, + search::search_client::TardisSearchClient, config::config_dto::SearchModuleConfig, }; pub async fn init(bs_cert: &SpiBsCertResp, ctx: &TardisContext, _mgr: bool) -> TardisResult { - let client = TardisSearchClient::init(&bs_cert.conn_uri, 60)?; + let config = SearchModuleConfig { url: bs_cert.conn_uri.parse().expect("invalid url"), timeout_sec: 60 }; + let client = TardisSearchClient::init(&config)?; let mut ext = HashMap::new(); if !bs_cert.private { let key_prefix = spi_initializer::common::get_isolation_flag_from_context(ctx); diff --git a/spi/spi-search/src/serv/es/search_es_item_serv.rs b/spi/spi-search/src/serv/es/search_es_item_serv.rs index 8348c8536..a44934512 100644 --- a/spi/spi-search/src/serv/es/search_es_item_serv.rs +++ b/spi/spi-search/src/serv/es/search_es_item_serv.rs @@ -639,6 +639,6 @@ fn merge(a: &mut serde_json::Value, b: serde_json::Value) { } } -pub async fn query_metrics(query_req: &SearchQueryMetricsReq, funs: &TardisFunsInst, ctx: &TardisContext, inst: &SpiBsInst) -> TardisResult { +pub async fn query_metrics(_query_req: &SearchQueryMetricsReq, funs: &TardisFunsInst, _ctx: &TardisContext, _inst: &SpiBsInst) -> TardisResult { Err(funs.err().format_error("search_es_item_serv", "query_metrics", "not support", "500-not-support")) } diff --git a/spi/spi-search/tests/init_search_container.rs b/spi/spi-search/tests/init_search_container.rs index e6ea5dac4..4afab0cfc 100644 --- a/spi/spi-search/tests/init_search_container.rs +++ b/spi/spi-search/tests/init_search_container.rs @@ -4,9 +4,8 @@ use tardis::basic::result::TardisResult; use tardis::test::test_container::TardisTestContainer; use tardis::testcontainers::clients::Cli; use tardis::testcontainers::core::WaitFor; -use tardis::testcontainers::images::generic::GenericImage; -use tardis::testcontainers::images::redis::Redis; -use tardis::testcontainers::{images, Container}; +use tardis::testcontainers::{GenericImage, Container}; +use testcontainers_modules::redis::Redis; use tardis::TardisFuns; pub struct LifeHold<'a> { @@ -58,7 +57,7 @@ pub fn postgres_custom<'a>(init_script_path: Option<&str>, docker: &'a Cli) -> C .unwrap_or_else(|| panic!("[Tardis.Test_Container] Script Path [{}] get error", init_script_path)) .to_string(); docker.run( - images::generic::GenericImage::new("abcfy2/zhparser", "15") + GenericImage::new("abcfy2/zhparser", "15") .with_env_var("POSTGRES_PASSWORD", "123456") .with_env_var("POSTGRES_DB", "test") .with_volume(path, "/docker-entrypoint-initdb.d/") @@ -66,7 +65,7 @@ pub fn postgres_custom<'a>(init_script_path: Option<&str>, docker: &'a Cli) -> C ) } else { docker.run( - images::generic::GenericImage::new("abcfy2/zhparser", "15") + GenericImage::new("abcfy2/zhparser", "15") .with_env_var("POSTGRES_PASSWORD", "123456") .with_env_var("POSTGRES_DB", "test") .with_wait_for(WaitFor::message_on_stderr("database system is ready to accept connections")), diff --git a/spi/spi-search/tests/test_search.rs b/spi/spi-search/tests/test_search.rs index 9665537e3..a6e1d99b2 100644 --- a/spi/spi-search/tests/test_search.rs +++ b/spi/spi-search/tests/test_search.rs @@ -32,7 +32,7 @@ async fn init_data() -> TardisResult<()> { let web_server = TardisFuns::web_server(); // Initialize SPI search - search_initializer::init(web_server).await.unwrap(); + search_initializer::init(&web_server).await.unwrap(); tokio::spawn(async move { web_server.start().await.unwrap(); diff --git a/spi/spi-stats/tests/test_stats.rs b/spi/spi-stats/tests/test_stats.rs index 2673fcfdb..1d715a8c4 100644 --- a/spi/spi-stats/tests/test_stats.rs +++ b/spi/spi-stats/tests/test_stats.rs @@ -37,7 +37,7 @@ async fn init_data() -> TardisResult<()> { let web_server = TardisFuns::web_server(); // Initialize SPI Stats - stats_initializer::init(web_server).await.unwrap(); + stats_initializer::init(&web_server).await.unwrap(); tokio::spawn(async move { web_server.start().await.unwrap(); diff --git a/support/auth/Cargo.toml b/support/auth/Cargo.toml index e1e16cac0..9aa81aabc 100644 --- a/support/auth/Cargo.toml +++ b/support/auth/Cargo.toml @@ -28,6 +28,6 @@ tardis = { workspace = true, features = [ "crypto-with-sm", "future", ] } - [dev-dependencies] +testcontainers-modules = { workspace = true } tardis = { workspace = true, features = ["test", "web-client"] } diff --git a/support/auth/src/api/auth_kernel_api.rs b/support/auth/src/api/auth_kernel_api.rs index d460148e5..11d6bdcf6 100644 --- a/support/auth/src/api/auth_kernel_api.rs +++ b/support/auth/src/api/auth_kernel_api.rs @@ -86,7 +86,7 @@ impl AuthApi { async fn get_token_context(&self, token: Query, app_id: Query>) -> TardisApiResult { let config = TardisFuns::cs_config::(DOMAIN_CODE); let cache_client = TardisFuns::cache_by_module_or_default(DOMAIN_CODE); - let result = auth_kernel_serv::get_token_context(&token.0, &app_id.0.unwrap_or("".to_string()), config, cache_client).await?; + let result = auth_kernel_serv::get_token_context(&token.0, &app_id.0.unwrap_or("".to_string()), &config, &cache_client).await?; TardisResp::ok(TardisFuns::crypto.base64.encode(TardisFuns::json.obj_to_string(&result)?)) } diff --git a/support/auth/src/auth_config.rs b/support/auth/src/auth_config.rs index 7b6698e0c..0c1ff8647 100644 --- a/support/auth/src/auth_config.rs +++ b/support/auth/src/auth_config.rs @@ -55,7 +55,7 @@ impl Default for AuthConfig { head_key_token: "Bios-Token".to_string(), head_key_ak_authorization: "Bios-Authorization".to_string(), head_key_bios_ctx: "Bios-Ctx".to_string(), - /// Special: need use UTC Time + // Special: need use UTC Time head_key_date_flag: "Bios-Date".to_string(), head_key_app: "Bios-App".to_string(), head_key_protocol: "Bios-Proto".to_string(), diff --git a/support/auth/src/serv/auth_crypto_serv.rs b/support/auth/src/serv/auth_crypto_serv.rs index 457434fdf..bc46a4306 100644 --- a/support/auth/src/serv/auth_crypto_serv.rs +++ b/support/auth/src/serv/auth_crypto_serv.rs @@ -59,7 +59,7 @@ pub async fn decrypt_req( )); }; - let input_keys = TardisFuns::crypto.base64.decode(input_keys).map_err(|_| { + let input_keys = TardisFuns::crypto.base64.decode_to_string(input_keys).map_err(|_| { TardisError::bad_request( &format!("[Auth] Encrypted request: {} field in header is not base64 format.", config.head_key_crypto), "401-auth-req-crypto-error", @@ -152,7 +152,7 @@ pub async fn encrypt_body(req: &AuthEncryptReq) -> TardisResult )); }; - let pub_key = TardisFuns::crypto.base64.decode(pub_key).map_err(|_| { + let pub_key = TardisFuns::crypto.base64.decode_to_string(pub_key).map_err(|_| { TardisError::bad_request( &format!("[Auth] Encrypt response: {} field in header is not base64 format.", config.head_key_crypto), "401-auth-req-crypto-error", @@ -165,8 +165,9 @@ pub async fn encrypt_body(req: &AuthEncryptReq) -> TardisResult .new_public_key_from_public_key(&pub_key) .map_err(|e| TardisError::bad_request(&format!("[Auth] Encrypt response: generate public key error:{e}"), "401-auth-req-crypto-error"))?; - let sm4_key = TardisFuns::crypto.key.rand_16_hex()?; - let sm4_iv = TardisFuns::crypto.key.rand_16_hex()?; + let sm4_key = TardisFuns::crypto.key.rand_16_hex(); + + let sm4_iv = TardisFuns::crypto.key.rand_16_hex(); let data = TardisFuns::crypto .sm4 diff --git a/support/auth/src/serv/auth_kernel_serv.rs b/support/auth/src/serv/auth_kernel_serv.rs index 2ce008ca0..e02e5a455 100644 --- a/support/auth/src/serv/auth_kernel_serv.rs +++ b/support/auth/src/serv/auth_kernel_serv.rs @@ -24,20 +24,20 @@ pub async fn auth(req: &mut AuthReq, is_mix_req: bool) -> TardisResult(DOMAIN_CODE); match check(req) { - Ok(true) => return Ok(AuthResult::ok(None, None, None, config)), - Err(e) => return Ok(AuthResult::err(e, config)), + Ok(true) => return Ok(AuthResult::ok(None, None, None, &config)), + Err(e) => return Ok(AuthResult::err(e, &config)), _ => {} } let cache_client = TardisFuns::cache_by_module_or_default(DOMAIN_CODE); - match ident(req, config, cache_client).await { + match ident(req, &config, &cache_client).await { Ok(ident) => match do_auth(&ident).await { - Ok(res_container_leaf_info) => match decrypt(req, config, &res_container_leaf_info, is_mix_req).await { - Ok((body, headers)) => Ok(AuthResult::ok(Some(&ident), body, headers, config)), - Err(e) => Ok(AuthResult::err(e, config)), + Ok(res_container_leaf_info) => match decrypt(req, &config, &res_container_leaf_info, is_mix_req).await { + Ok((body, headers)) => Ok(AuthResult::ok(Some(&ident), body, headers, &config)), + Err(e) => Ok(AuthResult::err(e, &config)), }, - Err(e) => Ok(AuthResult::err(e, config)), + Err(e) => Ok(AuthResult::err(e, &config)), }, - Err(e) => Ok(AuthResult::err(e, config)), + Err(e) => Ok(AuthResult::err(e, &config)), } } @@ -111,7 +111,7 @@ async fn ident(req: &AuthReq, config: &AuthConfig, cache_client: &TardisCacheCli let (cache_sk, cache_tenant_id, cache_appid) = self::get_cache_ak(&ak, config, cache_client).await?; self::check_ak_signature(&ak, &cache_sk, &signature, &req_date, req).await?; let bios_ctx = if let Some(bios_ctx) = req.headers.get(&config.head_key_bios_ctx).or_else(|| req.headers.get(&config.head_key_bios_ctx.to_lowercase())) { - TardisFuns::json.str_to_obj::(&TardisFuns::crypto.base64.decode(bios_ctx)?)? + TardisFuns::json.str_to_obj::(&TardisFuns::crypto.base64.decode_to_string(bios_ctx)?)? } else { return Err(TardisError::unauthorized( &format!("[Auth] Request is not legal, missing header [{}]", config.head_key_bios_ctx), @@ -391,7 +391,7 @@ fn get_webhook_ak_key(req: &AuthReq, config: &AuthConfig) -> Option { pub async fn sign_webhook_ak(sign_req: &SignWebHookReq) -> TardisResult { let config = TardisFuns::cs_config::(DOMAIN_CODE); let cache_client = TardisFuns::cache_by_module_or_default(DOMAIN_CODE); - let (cache_sk, cache_tenant_id, cache_appid) = self::get_cache_ak(&sign_req.ak, config, cache_client).await?; + let (cache_sk, cache_tenant_id, cache_appid) = self::get_cache_ak(&sign_req.ak, &config, &cache_client).await?; let mut cache_own_paths = cache_tenant_id.clone(); if !cache_appid.is_empty() { cache_own_paths = format!("{cache_tenant_id}/{cache_appid}") @@ -531,7 +531,7 @@ pub async fn decrypt( #[cfg(feature = "web-server")] pub(crate) async fn parse_mix_req(req: AuthReq) -> TardisResult { let config = TardisFuns::cs_config::(DOMAIN_CODE); - let (body, headers) = auth_crypto_serv::decrypt_req(&req.headers, &req.body, true, true, config).await?; + let (body, headers) = auth_crypto_serv::decrypt_req(&req.headers, &req.body, true, true, &config).await?; let body = body.ok_or_else(|| TardisError::bad_request("[MixReq] decrypt body can't be empty", "401-parse_mix_req-parse-error"))?; let mix_body = TardisFuns::json.str_to_obj::(&body)?; diff --git a/support/auth/src/serv/clients/spi_log_client.rs b/support/auth/src/serv/clients/spi_log_client.rs index 564dcade2..7462a88bf 100644 --- a/support/auth/src/serv/clients/spi_log_client.rs +++ b/support/auth/src/serv/clients/spi_log_client.rs @@ -29,10 +29,10 @@ impl SpiLogClient { return Ok(()); } let spi_ctx = TardisContext { owner: spi_owner, ..ctx.clone() }; - let headers = Some(vec![( + let headers = [( "Tardis-Context".to_string(), TardisFuns::crypto.base64.encode(TardisFuns::json.obj_to_string(&spi_ctx)?), - )]); + )]; //add log item let mut body = HashMap::from([ diff --git a/support/auth/tests/init_cache_container.rs b/support/auth/tests/init_cache_container.rs index 1ff38b860..688463ae4 100644 --- a/support/auth/tests/init_cache_container.rs +++ b/support/auth/tests/init_cache_container.rs @@ -3,7 +3,7 @@ use std::env; use tardis::basic::result::TardisResult; use tardis::test::test_container::TardisTestContainer; use tardis::testcontainers::clients::Cli; -use tardis::testcontainers::images::redis::Redis; +use testcontainers_modules::redis::Redis; use tardis::testcontainers::Container; use tardis::TardisFuns; diff --git a/support/auth/tests/test_auth.rs b/support/auth/tests/test_auth.rs index 427e9772b..4e389e8bc 100644 --- a/support/auth/tests/test_auth.rs +++ b/support/auth/tests/test_auth.rs @@ -26,7 +26,7 @@ async fn test_auth() -> TardisResult<()> { test_auth_init::test_init().await?; let web_server = TardisFuns::web_server(); - auth_initializer::init_api(web_server).await?; + auth_initializer::init_api(&web_server).await?; tokio::spawn(async move { web_server.start().await.unwrap(); }); diff --git a/support/auth/tests/test_auth_encrypt.rs b/support/auth/tests/test_auth_encrypt.rs index d94616c23..ad391f5ff 100644 --- a/support/auth/tests/test_auth_encrypt.rs +++ b/support/auth/tests/test_auth_encrypt.rs @@ -10,6 +10,7 @@ use bios_auth::{ auth_kernel_dto::{AuthReq, AuthResp}, }, }; +use tardis::config::config_dto::WebClientModuleConfig; use tardis::{ basic::result::TardisResult, crypto::crypto_sm2_4::TardisCryptoSm2, @@ -25,8 +26,8 @@ use tardis::{ fn crypto_req(body: &str, serv_pub_key: &str, front_pub_key: &str, need_crypto_resp: bool) -> (String, String) { let pub_key = TardisFuns::crypto.sm2.new_public_key_from_public_key(serv_pub_key).unwrap(); - let sm4_key = TardisFuns::crypto.key.rand_16_hex().unwrap(); - let sm4_iv = TardisFuns::crypto.key.rand_16_hex().unwrap(); + let sm4_key = TardisFuns::crypto.key.rand_16_hex(); + let sm4_iv = TardisFuns::crypto.key.rand_16_hex(); let data = TardisFuns::crypto.sm4.encrypt_cbc(body, &sm4_key, &sm4_iv).unwrap(); let sign_data = TardisFuns::crypto.digest.sm3(&data).unwrap(); @@ -40,6 +41,12 @@ fn crypto_req(body: &str, serv_pub_key: &str, front_pub_key: &str, need_crypto_r (data, base64_encrypt) } + +const WEB_CLIENT_CFG: WebClientModuleConfig = WebClientModuleConfig { + connect_timeout_sec: 1, + request_timeout_sec: 60, +}; + async fn mock_req( method: &str, path: &str, @@ -51,7 +58,7 @@ async fn mock_req( need_crypto_req: bool, need_crypto_resp: bool, ) -> AuthResp { - let web_client = TardisWebClient::init(1).unwrap(); + let web_client = TardisWebClient::init(&WEB_CLIENT_CFG).unwrap(); info!(">>>>[Request]| path:{}, query:{}, headers:{:#?}", path, query, headers); let config = TardisFuns::cs_config::(DOMAIN_CODE); let encrypt; @@ -98,7 +105,7 @@ async fn mock_req( } pub async fn mock_req_mix_apis(method: &str, uri: &str, body: &str, mut headers: Vec<(&str, &str)>, serv_pub_key: &str, front_pub_key: &str) -> MixAuthResp { - let web_client = TardisWebClient::init(1).unwrap(); + let web_client = TardisWebClient::init(&WEB_CLIENT_CFG).unwrap(); let config = TardisFuns::cs_config::(DOMAIN_CODE); let mix_body = json!({ "method": method, @@ -147,7 +154,7 @@ pub async fn mock_req_mix_apis(method: &str, uri: &str, body: &str, mut headers: } async fn mock_encrypt_resp(body: &str, headers: HashMap, front_pri_key: &TardisCryptoSm2PrivateKey) -> String { - let web_client = TardisWebClient::init(1).unwrap(); + let web_client = TardisWebClient::init(&WEB_CLIENT_CFG).unwrap(); info!(">>>>[Response]| headers:{:#?}", headers); let config = TardisFuns::cs_config::(DOMAIN_CODE); let result: TardisResp = web_client @@ -165,7 +172,7 @@ async fn mock_encrypt_resp(body: &str, headers: HashMap, front_p .unwrap(); info!("<<<<[Response]| headers:{:#?}, result:{:#?}", headers, result); let result = result.data.unwrap(); - let decode_base64 = TardisFuns::crypto.base64.decode(result.headers.get(&config.head_key_crypto).unwrap()).unwrap(); + let decode_base64 = TardisFuns::crypto.base64.decode_to_string(result.headers.get(&config.head_key_crypto).unwrap()).unwrap(); let decrypt_key = front_pri_key.decrypt(&decode_base64).unwrap(); let splits: Vec<_> = decrypt_key.split(' ').collect(); if splits.len() != 3 { @@ -185,7 +192,7 @@ async fn init_get_pub_key(sm2: &TardisCryptoSm2) -> TardisResult<(TardisCryptoSm let pri_key = TardisFuns::crypto.sm2.new_private_key().unwrap(); let pub_key = TardisFuns::crypto.sm2.new_public_key(&pri_key).unwrap(); - let web_client = TardisWebClient::init(1).unwrap(); + let web_client = TardisWebClient::init(&WEB_CLIENT_CFG).unwrap(); let result: TardisResp = web_client.get(&format!("https://localhost:8080/{DOMAIN_CODE}/auth/crypto/key"), None).await.unwrap().body.unwrap(); Ok((sm2.new_public_key_from_public_key(&result.data.unwrap()).unwrap(), pri_key, pub_key)) } diff --git a/support/auth/tests/test_auth_req.rs b/support/auth/tests/test_auth_req.rs index 0497082d1..28ec02f50 100644 --- a/support/auth/tests/test_auth_req.rs +++ b/support/auth/tests/test_auth_req.rs @@ -17,7 +17,7 @@ use tardis::{ }; async fn mock_req(method: &str, path: &str, query: &str, headers: Vec<(&str, &str)>) -> AuthResp { - let web_client = TardisWebClient::init(1).unwrap(); + let web_client = TardisWebClient::init(&Default::default()).unwrap(); info!(">>>>[Request]| path:{}, query:{}, headers:{:#?}", path, query, headers); let hashmap_query = if query.is_empty() { HashMap::new() @@ -56,7 +56,7 @@ async fn mock_req(method: &str, path: &str, query: &str, headers: Vec<(&str, &st fn decode_context(headers: &HashMap) -> TardisContext { let config = TardisFuns::cs_config::(DOMAIN_CODE); let ctx = headers.get(&config.head_key_context).unwrap(); - let ctx = TardisFuns::crypto.base64.decode(ctx).unwrap(); + let ctx = TardisFuns::crypto.base64.decode_to_string(ctx).unwrap(); TardisFuns::json.str_to_obj(&ctx).unwrap() } diff --git a/support/enhance-wasm/src/mini_tardis/crypto.rs b/support/enhance-wasm/src/mini_tardis/crypto.rs index 028132228..8bab33693 100644 --- a/support/enhance-wasm/src/mini_tardis/crypto.rs +++ b/support/enhance-wasm/src/mini_tardis/crypto.rs @@ -27,7 +27,7 @@ pub mod key { pub fn rand_16_hex() -> TardisResult { let mut key: [u8; 8] = [0; 8]; - rand::rngs::OsRng::default().fill_bytes(&mut key); + rand::rngs::OsRng.fill_bytes(&mut key); Ok(hex::encode(key)) } diff --git a/support/iam/src/basic/dto/iam_res_dto.rs b/support/iam/src/basic/dto/iam_res_dto.rs index c07eac1f9..d19daf293 100644 --- a/support/iam/src/basic/dto/iam_res_dto.rs +++ b/support/iam/src/basic/dto/iam_res_dto.rs @@ -42,14 +42,14 @@ pub struct IamResAddReq { impl IamResAddReq { pub fn encoding(&mut self) -> &mut Self { - if self.code.0.starts_with('/') { - self.code.0 = self.code.0[1..].to_string(); + if self.code.starts_with('/') { + self.code = TrimString::new(self.code[1..].to_string()); } self.code = TrimString(format!( "{}/{}/{}", self.kind.to_int(), self.method.as_ref().unwrap_or(&TrimString("*".to_string())), - self.code.0 + self.code )); self } diff --git a/support/iam/src/basic/serv/clients/mail_client.rs b/support/iam/src/basic/serv/clients/mail_client.rs index 033577fe0..abc9c4ec2 100644 --- a/support/iam/src/basic/serv/clients/mail_client.rs +++ b/support/iam/src/basic/serv/clients/mail_client.rs @@ -98,9 +98,9 @@ impl MailClient { txt_body: content, html_body: None, to: vec![mail.to_string()], - reply_to: None, - cc: None, - bcc: None, + reply_to: vec![], + cc: vec![], + bcc: vec![], from: None, }, ) diff --git a/support/iam/src/basic/serv/clients/sms_client.rs b/support/iam/src/basic/serv/clients/sms_client.rs index e8f41c86c..99ade565d 100644 --- a/support/iam/src/basic/serv/clients/sms_client.rs +++ b/support/iam/src/basic/serv/clients/sms_client.rs @@ -26,16 +26,16 @@ impl SmsClient { pub async fn send_vcode(phone: &str, vcode: &str, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> { let conf = funs.conf::(); - let ctx_base64 = &TardisFuns::crypto.base64.encode(TardisFuns::json.obj_to_string(&ctx)?); + let ctx_base64 = TardisFuns::crypto.base64.encode(TardisFuns::json.obj_to_string(&ctx)?); + let fw_config = TardisFuns::fw_config(); + let web_server_config = fw_config.web_server(); + let header_name = web_server_config.context_conf.context_header_name.to_string(); match funs .web_client() .put_str_to_str( &format!("{}/{}/{}/{}", conf.sms_base_url, conf.sms_path, phone, vcode), "", - Some(vec![( - TardisFuns::fw_config().web_server.context_conf.context_header_name.to_string(), - ctx_base64.to_string(), - )]), + vec![(header_name, ctx_base64,)], ) .await { @@ -64,16 +64,18 @@ impl SmsClient { pub async fn send_pwd(phone: &str, pwd: &str, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> { let conf = funs.conf::(); - let ctx_base64 = &TardisFuns::crypto.base64.encode(TardisFuns::json.obj_to_string(&ctx)?); + let ctx_base64 = TardisFuns::crypto.base64.encode(TardisFuns::json.obj_to_string(&ctx)?); + let fw_config = TardisFuns::fw_config(); + let web_server_config = fw_config.web_server(); match funs .web_client() .put_str_to_str( &format!("{}/{}/{}/{}", conf.sms_base_url, conf.sms_pwd_path, phone, pwd), "", - Some(vec![( - TardisFuns::fw_config().web_server.context_conf.context_header_name.to_string(), + vec![( + web_server_config.context_conf.context_header_name.clone(), ctx_base64.to_string(), - )]), + )], ) .await { diff --git a/support/iam/src/basic/serv/iam_attr_serv.rs b/support/iam/src/basic/serv/iam_attr_serv.rs index 5c30d0bf9..942ac2075 100644 --- a/support/iam/src/basic/serv/iam_attr_serv.rs +++ b/support/iam/src/basic/serv/iam_attr_serv.rs @@ -23,7 +23,7 @@ impl IamAttrServ { pub async fn add_account_attr(add_req: &IamKindAttrAddReq, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult { let idx = add_req.idx.unwrap_or(false); let main_column = add_req.main_column.unwrap_or(false); - let name = add_req.name.0.as_str(); + let name = add_req.name.as_str(); if idx && !main_column { return Err(funs.err().bad_request("iam_account_attr", "add", "only the main table columns support indexes", "400-iam-attr-idx-not-in-main")); } diff --git a/support/iam/src/basic/serv/iam_cert_serv.rs b/support/iam/src/basic/serv/iam_cert_serv.rs index e76589b0e..eb0820ef5 100644 --- a/support/iam/src/basic/serv/iam_cert_serv.rs +++ b/support/iam/src/basic/serv/iam_cert_serv.rs @@ -1216,10 +1216,10 @@ impl IamCertServ { } pub async fn add_or_modify_sync_third_integration_config(reqs: Vec, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> { - let headers = Some(vec![( + let headers = vec![( "Tardis-Context".to_string(), TardisFuns::crypto.base64.encode(TardisFuns::json.obj_to_string(&ctx)?), - )]); + )]; let schedule_url = funs.conf::().spi.schedule_url.clone(); if schedule_url.is_empty() { return Err(funs.err().not_implemented("third_integration_config", "add_or_modify", "schedule is not impl!", "501-iam-schedule_not_impl_error")); @@ -1290,14 +1290,14 @@ impl IamCertServ { pub async fn get_third_intg_sync_status(task_id: &str, funs: &TardisFunsInst) -> TardisResult> { let mut result = None; let task_id = task_id.parse().map_err(|_| funs.err().format_error("system", "task", "task id format error", "406-iam-task-id-format"))?; - let mut is_end = TaskProcessor::check_status(&funs.conf::().cache_key_async_task_status, task_id, funs.cache()).await?; + let mut is_end = TaskProcessor::check_status(&funs.conf::().cache_key_async_task_status, task_id, &funs.cache()).await?; for _i in 0..5 { result = funs .cache() .get(&funs.conf::().cache_key_sync_ldap_status) .await? .and_then(|s| TardisFuns::json.str_to_obj::(&s).ok()); - is_end = TaskProcessor::check_status(&funs.conf::().cache_key_async_task_status, task_id, funs.cache()).await?; + is_end = TaskProcessor::check_status(&funs.conf::().cache_key_async_task_status, task_id, &funs.cache()).await?; if is_end || (result.is_some() && result.as_ref().expect("").total != 0) { break; } @@ -1326,7 +1326,7 @@ impl IamCertServ { let task_ctx = ctx.clone(); let sync = SYNC_LOCK.try_lock().map_err(|_| funs.err().conflict("third_integration_config", "sync", "The last synchronization has not ended yet", "iam-sync-not-ended"))?; if let Some(task_id) = funs.cache().get(&funs.conf::().cache_key_sync_ldap_task_lock).await?.and_then(|task_id| task_id.parse().ok()) { - if !TaskProcessor::check_status(&funs.conf::().cache_key_async_task_status, task_id, funs.cache()).await? { + if !TaskProcessor::check_status(&funs.conf::().cache_key_async_task_status, task_id, &funs.cache()).await? { return Err(funs.err().conflict("third_integration_config", "sync", "The last synchronization has not ended yet", "iam-sync-not-ended")); }; } diff --git a/support/iam/src/basic/serv/iam_cert_user_pwd_serv.rs b/support/iam/src/basic/serv/iam_cert_user_pwd_serv.rs index b565f7f47..1e851eaf3 100644 --- a/support/iam/src/basic/serv/iam_cert_user_pwd_serv.rs +++ b/support/iam/src/basic/serv/iam_cert_user_pwd_serv.rs @@ -188,9 +188,9 @@ impl IamCertUserPwdServ { .await?; if let Some(cert) = cert { IamCertUserPwdServ::check_sk_contains_ak(&cert.ak, &modify_req.new_sk, funs)?; - RbumCertServ::change_sk(&cert.id, &modify_req.original_sk.0, &modify_req.new_sk.0, &RbumCertFilterReq::default(), funs, ctx).await?; - IamCertPhoneVCodeServ::send_pwd(rel_iam_item_id, &modify_req.new_sk.0, funs, ctx).await?; - IamCertMailVCodeServ::send_pwd(rel_iam_item_id, &modify_req.new_sk.0, funs, ctx).await?; + RbumCertServ::change_sk(&cert.id, &modify_req.original_sk, &modify_req.new_sk, &RbumCertFilterReq::default(), funs, ctx).await?; + IamCertPhoneVCodeServ::send_pwd(rel_iam_item_id, &modify_req.new_sk, funs, ctx).await?; + IamCertMailVCodeServ::send_pwd(rel_iam_item_id, &modify_req.new_sk, funs, ctx).await?; RbumCertServ::modify_rbum( &cert.id, &mut RbumCertModifyReq { diff --git a/support/iam/src/basic/serv/iam_key_cache_serv.rs b/support/iam/src/basic/serv/iam_key_cache_serv.rs index 0eb262a28..426025386 100644 --- a/support/iam/src/basic/serv/iam_key_cache_serv.rs +++ b/support/iam/src/basic/serv/iam_key_cache_serv.rs @@ -58,7 +58,7 @@ impl IamIdentCacheServ { .hset( format!("{}{}", funs.conf::().cache_key_account_rel_, rel_iam_item_id).as_str(), token, - &format!("{},{}", token_kind, Utc::now().timestamp_nanos()), + &format!("{},{}", token_kind, Utc::now().timestamp_nanos_opt().expect("maybe in 23rd centery")), ) .await?; // Remove old tokens diff --git a/support/iam/src/basic/serv/iam_tenant_serv.rs b/support/iam/src/basic/serv/iam_tenant_serv.rs index 2082fee5b..bc751dd8e 100644 --- a/support/iam/src/basic/serv/iam_tenant_serv.rs +++ b/support/iam/src/basic/serv/iam_tenant_serv.rs @@ -302,7 +302,7 @@ impl IamTenantServ { &IamAccountAggAddReq { id: Some(TrimString(tenant_admin_id.clone())), name: add_req.admin_name.clone(), - cert_user_name: TrimString(add_req.admin_username.0.to_string()), + cert_user_name: add_req.admin_username.clone(), cert_password: Some(TrimString(admin_pwd.to_string())), cert_phone: add_req.admin_phone.clone(), cert_mail: add_req.admin_mail.clone(), @@ -331,7 +331,7 @@ impl IamTenantServ { &IamAccountAggAddReq { id: Some(TrimString(tenant_audit_id.clone())), name: add_req.audit_name.clone(), - cert_user_name: TrimString(add_req.audit_username.0.to_string()), + cert_user_name: add_req.audit_username.clone(), cert_password: Some(TrimString(audit_pwd.to_string())), cert_phone: add_req.audit_phone.clone(), cert_mail: add_req.audit_mail.clone(), diff --git a/support/iam/src/basic/serv/oauth2_spi/iam_cert_oauth2_spi_github.rs b/support/iam/src/basic/serv/oauth2_spi/iam_cert_oauth2_spi_github.rs index f639ce8f4..8157fbfd3 100644 --- a/support/iam/src/basic/serv/oauth2_spi/iam_cert_oauth2_spi_github.rs +++ b/support/iam/src/basic/serv/oauth2_spi/iam_cert_oauth2_spi_github.rs @@ -17,7 +17,7 @@ impl IamCertOAuth2Spi for IamCertOAuth2SpiGithub { .post_to_obj::( &format!("https://github.com/login/oauth/access_token?client_id={ak}&client_secret={sk}&code={code}"), "", - Some(headers), + headers, ) .await?; if result.code != 200 { @@ -38,7 +38,7 @@ impl IamCertOAuth2Spi for IamCertOAuth2SpiGithub { ("User-Agent".to_string(), "BIOS".to_string()), ]; //get user info - let result = funs.web_client().get_to_str("https://api.github.com/user", Some(headers)).await?; + let result = funs.web_client().get_to_str("https://api.github.com/user", headers).await?; trace!("iam oauth2 spi [Github] get user info response: {:?}", result); if result.code != 200 { return Err(funs.err().not_found( diff --git a/support/iam/src/console_common/api/iam_cc_system_api.rs b/support/iam/src/console_common/api/iam_cc_system_api.rs index 79c3e4ac1..c60f1c207 100644 --- a/support/iam/src/console_common/api/iam_cc_system_api.rs +++ b/support/iam/src/console_common/api/iam_cc_system_api.rs @@ -21,7 +21,7 @@ impl IamCcSystemApi { let task_ids = task_ids.0.split(','); for task_id in task_ids { let task_id = task_id.parse().map_err(|_| funs.err().format_error("system", "task", "task id format error", "406-iam-task-id-format"))?; - let is_finished = TaskProcessor::check_status(&funs.conf::().cache_key_async_task_status, task_id, funs.cache()).await?; + let is_finished = TaskProcessor::check_status(&funs.conf::().cache_key_async_task_status, task_id, &funs.cache()).await?; if !is_finished { return TardisResp::ok(false); } diff --git a/support/iam/src/console_interface/api/iam_ci_system_api.rs b/support/iam/src/console_interface/api/iam_ci_system_api.rs index fb4d1de8f..8db8ac5ed 100644 --- a/support/iam/src/console_interface/api/iam_ci_system_api.rs +++ b/support/iam/src/console_interface/api/iam_ci_system_api.rs @@ -20,7 +20,7 @@ impl IamCiSystemApi { let task_ids = task_ids.0.split(','); for task_id in task_ids { let task_id = task_id.parse().map_err(|_| funs.err().format_error("system", "task", "task id format error", "406-iam-task-id-format"))?; - let is_finished = TaskProcessor::check_status(&cache_key.0, task_id, funs.cache()).await?; + let is_finished = TaskProcessor::check_status(&cache_key.0, task_id, &funs.cache()).await?; if !is_finished { return TardisResp::ok(false); } diff --git a/support/iam/src/console_passport/serv/iam_cp_cert_mail_vcode_serv.rs b/support/iam/src/console_passport/serv/iam_cp_cert_mail_vcode_serv.rs index 0e31a0b12..125c3d098 100644 --- a/support/iam/src/console_passport/serv/iam_cp_cert_mail_vcode_serv.rs +++ b/support/iam/src/console_passport/serv/iam_cp_cert_mail_vcode_serv.rs @@ -25,7 +25,7 @@ impl IamCpCertMailVCodeServ { let rbum_cert_conf_id = IamCertServ::get_cert_conf_id_by_kind(&IamCertKernelKind::MailVCode.to_string(), Some(tenant_id.clone()), funs).await?; let result = IamCertServ::validate_by_ak_and_sk( &login_req.mail, - &login_req.vcode.0, + &login_req.vcode, Some(&rbum_cert_conf_id), None, false, @@ -39,7 +39,7 @@ impl IamCpCertMailVCodeServ { if e.code == "401-iam-cert-valid" { IamCertServ::validate_by_ak_and_sk( &login_req.mail, - &login_req.vcode.0, + &login_req.vcode, Some(&global_rbum_cert_conf_id), None, false, @@ -60,7 +60,7 @@ impl IamCpCertMailVCodeServ { } else { let (_, _, rbum_item_id) = IamCertServ::validate_by_ak_and_sk( &login_req.mail, - &login_req.vcode.0, + &login_req.vcode, Some(&global_rbum_cert_conf_id), None, false, diff --git a/support/iam/src/console_passport/serv/iam_cp_cert_phone_vcode_serv.rs b/support/iam/src/console_passport/serv/iam_cp_cert_phone_vcode_serv.rs index 86c50f60c..1bd58f665 100644 --- a/support/iam/src/console_passport/serv/iam_cp_cert_phone_vcode_serv.rs +++ b/support/iam/src/console_passport/serv/iam_cp_cert_phone_vcode_serv.rs @@ -26,7 +26,7 @@ impl IamCpCertPhoneVCodeServ { let rbum_cert_conf_id = IamCertServ::get_cert_conf_id_by_kind(&IamCertKernelKind::PhoneVCode.to_string(), Some(tenant_id.clone()), funs).await?; let result = IamCertServ::validate_by_ak_and_sk( &login_req.phone, - &login_req.vcode.0, + &login_req.vcode, Some(&rbum_cert_conf_id), None, false, @@ -40,7 +40,7 @@ impl IamCpCertPhoneVCodeServ { if e.code == "401-iam-cert-valid" { IamCertServ::validate_by_ak_and_sk( &login_req.phone, - &login_req.vcode.0, + &login_req.vcode, Some(&global_rbum_cert_conf_id), None, false, @@ -61,7 +61,7 @@ impl IamCpCertPhoneVCodeServ { } else { let (_, _, rbum_item_id) = IamCertServ::validate_by_ak_and_sk( &login_req.phone, - &login_req.vcode.0, + &login_req.vcode, Some(&global_rbum_cert_conf_id), None, false, diff --git a/support/iam/src/console_passport/serv/iam_cp_cert_user_pwd_serv.rs b/support/iam/src/console_passport/serv/iam_cp_cert_user_pwd_serv.rs index 095c37010..a67cec0a2 100644 --- a/support/iam/src/console_passport/serv/iam_cp_cert_user_pwd_serv.rs +++ b/support/iam/src/console_passport/serv/iam_cp_cert_user_pwd_serv.rs @@ -29,8 +29,8 @@ impl IamCpCertUserPwdServ { let mut tenant_id = Self::get_tenant_id(pwd_new_req.tenant_id.clone(), funs).await?; let mut rbum_cert_conf_id = IamCertServ::get_cert_conf_id_by_kind(&IamCertKernelKind::UserPwd.to_string(), Some(tenant_id.clone()), funs).await?; let validate_resp = IamCertServ::validate_by_ak_and_sk( - &pwd_new_req.ak.0, - &pwd_new_req.original_sk.0, + &pwd_new_req.ak, + &pwd_new_req.original_sk, None, Some(&RbumCertRelKind::Item), true, @@ -56,8 +56,8 @@ impl IamCpCertUserPwdServ { tenant_id = "".to_string(); rbum_cert_conf_id = IamCertServ::get_cert_conf_id_by_kind(&IamCertKernelKind::UserPwd.to_string(), Some(tenant_id.clone()), funs).await?; IamCertServ::validate_by_ak_and_sk( - &pwd_new_req.ak.0, - &pwd_new_req.original_sk.0, + &pwd_new_req.ak, + &pwd_new_req.original_sk, None, Some(&RbumCertRelKind::Item), true, @@ -118,8 +118,8 @@ impl IamCpCertUserPwdServ { let ctx = IamAccountServ::new_context_if_account_is_global(ctx, funs).await?; let rbum_cert_conf_id = IamCertServ::get_cert_conf_id_by_kind(&IamCertKernelKind::UserPwd.to_string(), tenant_id.clone(), funs).await?; let _ = IamCertServ::validate_by_ak_and_sk( - &req.original_ak.0, - &req.sk.0, + &req.original_ak, + &req.sk, None, Some(&RbumCertRelKind::Item), false, @@ -136,7 +136,7 @@ impl IamCpCertUserPwdServ { IamCertUserPwdServ::modify_ak_cert(&ctx.owner, req, &rbum_cert_conf_id, funs, &ctx).await?; let id = ctx.owner.to_string(); - let op_describe = format!("修改用户名为{}", req.new_ak.as_ref()); + let op_describe = format!("修改用户名为{}", req.new_ak); let _ = IamLogClient::add_ctx_task(LogParamTag::IamAccount, Some(id), op_describe, Some("ModifyUserName".to_string()), &ctx).await; Ok(()) @@ -187,8 +187,8 @@ impl IamCpCertUserPwdServ { pub async fn login_by_user_pwd(login_req: &IamCpUserPwdLoginReq, ip: Option, funs: &TardisFunsInst) -> TardisResult { let tenant_id = Self::get_tenant_id(login_req.tenant_id.clone(), funs).await?; let validate_resp = IamCertServ::validate_by_ak_and_sk( - &login_req.ak.0, - &login_req.sk.0, + &login_req.ak, + &login_req.sk, None, Some(&RbumCertRelKind::Item), false, @@ -212,8 +212,8 @@ impl IamCpCertUserPwdServ { } }; IamCertServ::validate_by_ak_and_sk( - &login_req.ak.0, - &login_req.sk.0, + &login_req.ak, + &login_req.sk, None, Some(&RbumCertRelKind::Item), false, diff --git a/support/iam/src/iam_test_helper.rs b/support/iam/src/iam_test_helper.rs index 353c62fdb..646f91385 100644 --- a/support/iam/src/iam_test_helper.rs +++ b/support/iam/src/iam_test_helper.rs @@ -5,6 +5,7 @@ use serde::Serialize; use tardis::basic::dto::TardisContext; use tardis::basic::field::TrimString; use tardis::basic::result::TardisResult; +use tardis::config::config_dto::WebClientModuleConfig; use tardis::log::{info, warn}; use tardis::web::poem_openapi::types::{ParseFromJSON, ToJSON}; use tardis::web::web_client::TardisWebClient; @@ -24,7 +25,7 @@ pub struct BIOSWebTestClient { impl BIOSWebTestClient { pub fn new(base_url: String) -> BIOSWebTestClient { BIOSWebTestClient { - client: TardisWebClient::init(600).unwrap(), + client: TardisWebClient::init(&WebClientModuleConfig::builder().connect_timeout_sec(600u64).build()).unwrap(), context: Default::default(), base_url, } @@ -32,8 +33,11 @@ impl BIOSWebTestClient { pub async fn set_auth(&mut self, token: &str, app_id: Option) -> TardisResult<()> { let context: String = self.put("/cp/context", &IamContextFetchReq { token: token.to_string(), app_id }).await; - self.context = TardisFuns::json.str_to_obj(&TardisFuns::crypto.base64.decode(&context)?)?; - self.set_default_header(&TardisFuns::fw_config().web_server.context_conf.context_header_name, &context); + self.context = TardisFuns::json.str_to_obj(&TardisFuns::crypto.base64.decode_to_string(&context)?)?; + let fw_config = TardisFuns::fw_config(); + let web_server_config = fw_config.web_server(); + let context_header_name = web_server_config.context_conf.context_header_name.as_str(); + self.set_default_header(context_header_name, &context); Ok(()) } diff --git a/support/iam/src/integration/ldap/ldap_server.rs b/support/iam/src/integration/ldap/ldap_server.rs index 7cd914afb..6d8884cd8 100644 --- a/support/iam/src/integration/ldap/ldap_server.rs +++ b/support/iam/src/integration/ldap/ldap_server.rs @@ -52,6 +52,7 @@ use std::convert::TryFrom; use std::net; use std::str::FromStr; +use std::sync::Arc; use ldap3_proto::simple::*; use ldap3_proto::LdapCodec; @@ -221,7 +222,8 @@ fn extract_cn(dn: &str) -> Option { } } -async fn handle_client(socket: TcpStream, _addr: net::SocketAddr, config: &IamLdapConfig) { +async fn handle_client(socket: TcpStream, _addr: net::SocketAddr, config: Arc) { + let config = &config.ldap; let (r, w) = tokio::io::split(socket); let mut reqs = FramedRead::new(r, LdapCodec); let mut resp = FramedWrite::new(w, LdapCodec); @@ -276,6 +278,7 @@ pub async fn start() -> TardisResult<()> { loop { match listener.accept().await { Ok((socket, addr)) => { + let config = TardisFuns::cs_config::(iam_constants::COMPONENT_CODE); tokio::spawn(handle_client(socket, addr, config)); } Err(e) => { diff --git a/support/iam/tests/test_basic.rs b/support/iam/tests/test_basic.rs index 5e6cca56c..e0b4b477b 100644 --- a/support/iam/tests/test_basic.rs +++ b/support/iam/tests/test_basic.rs @@ -6,7 +6,7 @@ use bios_iam::basic::serv::iam_cert_ldap_serv::AccountFieldMap; use tardis::basic::result::TardisResult; use tardis::testcontainers::clients::Cli; use tardis::testcontainers::core::{ExecCommand, WaitFor}; -use tardis::testcontainers::images::generic::GenericImage; +use tardis::testcontainers::GenericImage; use tardis::testcontainers::Container; use tardis::TardisFuns; @@ -56,7 +56,7 @@ pub struct LifeHold<'a> { } pub async fn init(docker: &'_ Cli) -> TardisResult> { - let ldap_container = get_ldap_container(docker).await; + let ldap_container = get_ldap_container(docker); TardisFuns::init(Some("tests/config")).await?; // TardisFuns::init("core/iam/tests/config").await?; @@ -64,7 +64,7 @@ pub async fn init(docker: &'_ Cli) -> TardisResult> { Ok(LifeHold { ldap: ldap_container }) } -async fn get_ldap_container<'a>(docker: &'a Cli) -> Container<'a, GenericImage> { +fn get_ldap_container<'a>(docker: &'a Cli) -> Container<'a, GenericImage> { const ORGANISATION: &str = "test"; const ADMIN_PASSWORD: &str = "123456"; let domain: String = format!("{}.com", ORGANISATION); diff --git a/support/iam/tests/test_iam_cert_sync.rs b/support/iam/tests/test_iam_cert_sync.rs index f12c42897..2a8840633 100644 --- a/support/iam/tests/test_iam_cert_sync.rs +++ b/support/iam/tests/test_iam_cert_sync.rs @@ -163,7 +163,7 @@ pub async fn test(ldap_account_num: u64, conf_ldap_add_or_modify_req: IamCertCon let mut is_finish = false; while is_finish { sleep(Duration::from_millis(100)).await; - is_finish = TaskProcessor::check_status(&funs.conf::().cache_key_async_task_status, task_id.parse::().unwrap(), funs.cache()).await.unwrap(); + is_finish = TaskProcessor::check_status(&funs.conf::().cache_key_async_task_status, task_id.parse::().unwrap(), &funs.cache()).await.unwrap(); } } let account_page = IamAccountServ::paginate_account_summary_aggs( diff --git a/support/reach/Cargo.toml b/support/reach/Cargo.toml index f98972b42..45635df1e 100644 --- a/support/reach/Cargo.toml +++ b/support/reach/Cargo.toml @@ -42,3 +42,5 @@ bios-reach = { path = "./", features = ["simple-client"] } toml = "*" bios-client-hwsms = { path = "../../clients/hwsms", features = ["reach"] } + +testcontainers-modules = { workspace = true } diff --git a/support/reach/src/api/reach_api_cc/reach_api_cc_message.rs b/support/reach/src/api/reach_api_cc/reach_api_cc_message.rs index 3f7550165..9d8eae344 100644 --- a/support/reach/src/api/reach_api_cc/reach_api_cc_message.rs +++ b/support/reach/src/api/reach_api_cc/reach_api_cc_message.rs @@ -82,7 +82,7 @@ impl ReachMessageCcApi { pub async fn pwd_send(&self, to: Path, code: Path) -> TardisApiResult { let funs = get_tardis_inst(); let config = funs.conf::(); - self.channel.send(ReachChannelKind::Sms, GenericTemplate::pwd_template(config), &[("pwd", code.0)].into(), &[to.0].into()).await?; + self.channel.send(ReachChannelKind::Sms, GenericTemplate::pwd_template(&config), &[("pwd", code.0)].into(), &[to.0].into()).await?; TardisResp::ok(VOID) } diff --git a/support/reach/src/api/reach_api_ct/reach_api_ct_message.rs b/support/reach/src/api/reach_api_ct/reach_api_ct_message.rs index 00ed11897..9fb62987f 100644 --- a/support/reach/src/api/reach_api_ct/reach_api_ct_message.rs +++ b/support/reach/src/api/reach_api_ct/reach_api_ct_message.rs @@ -56,7 +56,7 @@ impl ReachMessageCtApi { let resp = ReachMessageServ::get_rbum(&id, &filter, &funs, &ctx).await?; TardisResp::ok(resp) } - + /// 重新发送消息(仅仅适用于状态为Fail的消息) #[oai(method = "put", path = "/resend/:id")] pub async fn resend(&self, id: Path, TardisContextExtractor(ctx): TardisContextExtractor) -> TardisApiResult { @@ -64,7 +64,7 @@ impl ReachMessageCtApi { let success = ReachMessageServ::resend(&id, &funs, &ctx).await?; TardisResp::ok(success) } - + /// 添加消息 #[oai(method = "post", path = "/")] pub async fn add_message(&self, mut add_req: Json, TardisContextExtractor(ctx): TardisContextExtractor) -> TardisApiResult { diff --git a/support/reach/src/api/reach_api_ct/reach_api_ct_msg_log.rs b/support/reach/src/api/reach_api_ct/reach_api_ct_msg_log.rs index a731694a8..0330b5217 100644 --- a/support/reach/src/api/reach_api_ct/reach_api_ct_msg_log.rs +++ b/support/reach/src/api/reach_api_ct/reach_api_ct_msg_log.rs @@ -4,6 +4,7 @@ use crate::reach_consts::get_tardis_inst; use crate::reach_invoke::Client; use crate::serv::*; use bios_basic::rbum::serv::rbum_crud_serv::RbumCrudOperation; +#[cfg(feature = "simple-client")] use bios_sdk_invoke::simple_invoke_client; use tardis::web::context_extractor::TardisContextExtractor; use tardis::web::poem_openapi; diff --git a/support/reach/src/domain/message.rs b/support/reach/src/domain/message.rs index 5f9ad9791..d7adc0017 100644 --- a/support/reach/src/domain/message.rs +++ b/support/reach/src/domain/message.rs @@ -107,7 +107,7 @@ impl TardisActiveModel for ActiveModel { .if_not_exists() .col(ColumnDef::new(Column::Id).not_null().string().primary_key()) .col(ColumnDef::new(Column::OwnPaths).not_null().string()) - .col(ColumnDef::new(Column::Owner).not_null().string_len(255)) + .col(ColumnDef::new(Column::Owner).not_null().string_len(255).default(String::default())) .col(ColumnDef::new(Column::FromRes).not_null().string()) .col(ColumnDef::new(Column::RelReachChannel).not_null().string()) .col(ColumnDef::new(Column::ReceiveKind).not_null().string()) diff --git a/support/reach/src/reach_send_channel.rs b/support/reach/src/reach_send_channel.rs index 603317085..2a4ebd0c5 100644 --- a/support/reach/src/reach_send_channel.rs +++ b/support/reach/src/reach_send_channel.rs @@ -91,20 +91,16 @@ impl SendChannel for UnimplementedChannel { } #[async_trait] -impl SendChannel for &'static tardis::mail::mail_client::TardisMailClient { +impl SendChannel for tardis::mail::mail_client::TardisMailClient { async fn send(&self, template: GenericTemplate<'_>, content: &ContentReplace, to: &HashSet<&str>) -> TardisResult<()> { - (*self) - .send(&TardisMailSendReq { - subject: template.name.ok_or_else(|| bad_template("template missing field sms_from"))?.to_owned(), - txt_body: content.render_final_content::<{ usize::MAX }>(template.content), - html_body: None, - to: to.iter().map(|x| x.to_string()).collect(), - reply_to: None, - cc: None, - bcc: None, - from: None, - }) - .await + self.send( + &TardisMailSendReq::builder() + .subject(template.name.ok_or_else(|| bad_template("template missing field sms_from"))?) + .txt_body(content.render_final_content::<{ usize::MAX }>(template.content)) + .to(to.iter().map(|x| x.to_string()).collect::>()) + .build(), + ) + .await } fn kind(&self) -> ReachChannelKind { ReachChannelKind::Email diff --git a/support/reach/src/serv/message_log.rs b/support/reach/src/serv/message_log.rs index 426db9e1d..67dcd2529 100644 --- a/support/reach/src/serv/message_log.rs +++ b/support/reach/src/serv/message_log.rs @@ -1,4 +1,4 @@ -use crate::domain::message_log; +use crate::domain::{message, message_log}; use crate::dto::*; use bios_basic::rbum::serv::rbum_crud_serv::{RbumCrudOperation, RbumCrudQueryPackage}; use tardis::async_trait::async_trait; @@ -6,7 +6,7 @@ use tardis::basic::dto::TardisContext; use tardis::basic::result::TardisResult; use tardis::db::reldb_client::TardisActiveModel; -use tardis::db::sea_orm::sea_query::{Query, SelectStatement}; +use tardis::db::sea_orm::sea_query::{Query, SelectStatement, Expr, Alias}; use tardis::db::sea_orm::{ColumnTrait, Set}; use tardis::db::sea_orm::{EntityName, Iterable}; use tardis::{TardisFuns, TardisFunsInst}; @@ -39,11 +39,21 @@ impl RbumCrudOperation TardisResult { let mut query = Query::select(); query.columns(message_log::Column::iter().map(|c| (message_log::Entity, c))); + query.expr_as(Expr::col((message::Entity, message::Column::ReachStatus)), Alias::new("reach_status")); + query.expr_as(Expr::col((message::Entity, message::Column::ReceiveKind)), Alias::new("receive_kind")); + query.expr_as(Expr::col((message::Entity, message::Column::ToResIds)), Alias::new("to_res_ids")); + + + query.left_join( + message::Entity, + Expr::col((message_log::Entity, message_log::Column::RelReachMessageId)).equals((message::Entity, message::Column::Id)), + ); query.from(message_log::Entity); if let Some(id) = &filter.rel_reach_message_id { query.and_where(message_log::Column::RelReachMessageId.eq(id)); } query.with_filter(Self::get_table_name(), &filter.base_filter.basic, is_detail, false, ctx); + Ok(query) } } diff --git a/support/reach/tests/test_reach_common.rs b/support/reach/tests/test_reach_common.rs index 79cbb3d66..69edfcf33 100644 --- a/support/reach/tests/test_reach_common.rs +++ b/support/reach/tests/test_reach_common.rs @@ -12,7 +12,8 @@ use bios_reach::reach_send_channel::SendChannelMap; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::sync::{Arc, OnceLock}; -use tardis::testcontainers::images::{generic::GenericImage, redis::Redis}; +use tardis::testcontainers::GenericImage; +use testcontainers_modules::redis::Redis; use tardis::tokio::sync::RwLock; use tardis::web::poem_openapi::param::Path; use tardis::web::poem_openapi::payload::{Form, Json}; @@ -63,8 +64,8 @@ pub async fn init_tardis(docker: &Cli) -> TardisResult { bios_basic::rbum::rbum_initializer::init("", RbumConfig::default()).await?; let web_server = TardisFuns::web_server(); bios_reach::init( - web_server, - SendChannelMap::new().with_arc_channel(bios_client_hwsms::SmsClient::from_reach_config()).with_arc_channel(Arc::new(get_tardis_inst().mail())), + &web_server, + SendChannelMap::new().with_arc_channel(bios_client_hwsms::SmsClient::from_reach_config()).with_arc_channel(get_tardis_inst().mail()), ) .await?; let sms_mocker = HwSmsMockerApi::default(); diff --git a/support/reach/tests/test_send_client.rs b/support/reach/tests/test_send_client.rs index 7e05960f3..64721248d 100644 --- a/support/reach/tests/test_send_client.rs +++ b/support/reach/tests/test_send_client.rs @@ -16,7 +16,7 @@ pub struct TestConfig { impl TestConfig { fn load() -> Self { - toml::from_slice(include_bytes!("config/test-send-client.toml")).expect("invalid config") + toml::from_str(include_str!("config/test-send-client.toml")).expect("invalid config") } }