Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/ideal-world/bios
Browse files Browse the repository at this point in the history
  • Loading branch information
ljl committed Aug 1, 2024
2 parents eb14328 + a696304 commit 0440df1
Show file tree
Hide file tree
Showing 13 changed files with 381 additions and 291 deletions.
81 changes: 43 additions & 38 deletions backend/gateways/spacegate-plugins/src/plugin/audit_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashMap;
use std::str::FromStr;
use std::time::Instant;

use bios_sdk_invoke::clients::spi_log_client;
use bios_sdk_invoke::clients::spi_log_client::{self, LogItemAddReq};
use bios_sdk_invoke::invoke_config::InvokeConfig;
use bios_sdk_invoke::invoke_enumeration::InvokeModuleKind;
use bios_sdk_invoke::invoke_initializer;
Expand Down Expand Up @@ -141,26 +141,26 @@ impl AuditLogPlugin {
}

fn init(&mut self) -> Result<(), TardisError> {
if !self.log_url.is_empty() && !self.spi_app_id.is_empty() {
if let Ok(jsonpath_inst) = JsonPathInst::from_str(&self.success_json_path).map_err(|e| log::error!("[Plugin.AuditLog] invalid json path:{e}")) {
self.jsonpath_inst = Some(jsonpath_inst);
} else {
self.enabled = false;
return Ok(());
};
self.enabled = true;
if let Ok(jsonpath_inst) = JsonPathInst::from_str(&self.success_json_path).map_err(|e| log::error!("[Plugin.AuditLog] invalid json path:{e}")) {
self.jsonpath_inst = Some(jsonpath_inst);
} else {
self.enabled = false;
return Err(TardisError::bad_request("[Plugin.AuditLog] plugin is not active, miss log_url or spi_app_id.", ""));
};
self.enabled = true;
if self.log_url.is_empty() || self.spi_app_id.is_empty() {
warn!("[Plugin.AuditLog] log_url or spi_app_id is empty!");
} else {
invoke_initializer::init(
CODE,
InvokeConfig {
spi_app_id: self.spi_app_id.clone(),
module_urls: HashMap::from([(InvokeModuleKind::Log.to_string(), self.log_url.clone())]),
},
)?;
Ok(())
} else {
self.enabled = false;
Err(TardisError::bad_request("[Plugin.AuditLog] plugin is not active, miss log_url or spi_app_id.", ""))
}

Ok(())
}

fn req(&self, mut req: Request<SgBody>) -> Result<Request<SgBody>, Response<SgBody>> {
Expand Down Expand Up @@ -206,31 +206,36 @@ impl AuditLogPlugin {
};

let tag = self.tag.clone();
tokio::task::spawn(async move {
match spi_log_client::SpiLogClient::add_with_many_params(
&tag,
&TardisFuns::json.obj_to_string(&content).unwrap_or_default(),
Some(content.to_value()),
None,
None,
Some(content.op),
None,
Some(tardis::chrono::Utc::now().to_rfc3339()),
content.user_id,
None,
&funs,
&spi_ctx,
)
.await
{
Ok(_) => {
log::trace!("[Plugin.AuditLog] add log success")
}
Err(e) => {
log::warn!("[Plugin.AuditLog] failed to add log:{e}")
}
};
});
if !self.log_url.is_empty() && !self.spi_app_id.is_empty() {
tokio::task::spawn(async move {
match spi_log_client::SpiLogClient::add(
&LogItemAddReq {
tag,
content: TardisFuns::json.obj_to_string(&content).unwrap_or_default(),
kind: None,
ext: Some(content.to_value()),
key: None,
op: Some(content.op),
rel_key: None,
id: None,
ts: Some(tardis::chrono::Utc::now()),
owner: content.user_id,
own_paths: None,
},
&funs,
&spi_ctx,
)
.await
{
Ok(_) => {
log::trace!("[Plugin.AuditLog] add log success")
}
Err(e) => {
log::warn!("[Plugin.AuditLog] failed to add log:{e}")
}
};
});
}
}

Ok(resp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub(crate) fn init() {
if !TardisFuns::web_server().is_running().await {
tardis::tokio::task::yield_now().await;
} else {
break
break;
}
}
// 五秒钟轮询一次
Expand Down
1 change: 1 addition & 0 deletions backend/spi/spi-object/src/object_constants.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub const DOMAIN_CODE: &str = "spi-object";
pub const SPI_S3_KIND_CODE: &str = "spi-bs-s3";
pub const SPI_OBS_KIND_CODE: &str = "spi-bs-obs";
2 changes: 2 additions & 0 deletions backend/spi/spi-object/src/object_initializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ pub async fn init_fun(bs_cert: SpiBsCertResp, ctx: &TardisContext, mgr: bool) ->
let inst = match bs_cert.kind_code.as_str() {
#[cfg(feature = "spi-s3")]
object_constants::SPI_S3_KIND_CODE => serv::s3::object_s3_initializer::init(&bs_cert, ctx, mgr).await,
#[cfg(feature = "spi-s3")]
object_constants::SPI_OBS_KIND_CODE => serv::obs::object_obs_initializer::init(&bs_cert, ctx, mgr).await,
_ => Err(bs_cert.bs_not_implemented())?,
}?;
info!("[BIOS.Object] Fun [{}]({}) initialized", bs_cert.kind_code, bs_cert.conn_uri);
Expand Down
1 change: 1 addition & 0 deletions backend/spi/spi-object/src/serv.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod object_obj_serv;
pub mod obs;
pub mod s3;
62 changes: 52 additions & 10 deletions backend/spi/spi-object/src/serv/object_obj_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use tardis::TardisFunsInst;
use crate::dto::object_dto::{ObjectBatchBuildCreatePresignUrlReq, ObjectCompleteMultipartUploadReq, ObjectInitiateMultipartUploadReq, ObjectObjPresignKind};
use crate::{object_constants, object_initializer};

use super::s3;
use super::s3::S3 as _;
use super::{obs, s3};

pub async fn presign_obj_url(
presign_kind: ObjectObjPresignKind,
Expand All @@ -26,7 +27,11 @@ pub async fn presign_obj_url(
match inst.kind_code() {
#[cfg(feature = "spi-s3")]
object_constants::SPI_S3_KIND_CODE => {
s3::object_s3_obj_serv::presign_obj_url(presign_kind, object_path, max_width, max_height, exp_secs, private, special, obj_exp, funs, ctx, &inst).await
s3::object_s3_obj_serv::S3Service::presign_obj_url(presign_kind, object_path, max_width, max_height, exp_secs, private, special, obj_exp, funs, ctx, &inst).await
}
#[cfg(feature = "spi-s3")]
object_constants::SPI_OBS_KIND_CODE => {
obs::object_obs_obj_serv::OBSService::presign_obj_url(presign_kind, object_path, max_width, max_height, exp_secs, private, special, obj_exp, funs, ctx, &inst).await
}
kind_code => Err(funs.bs_not_implemented(kind_code)),
}
Expand All @@ -44,7 +49,13 @@ pub async fn batch_get_presign_obj_url(
let inst = funs.init(ctx, true, object_initializer::init_fun).await?;
match inst.kind_code() {
#[cfg(feature = "spi-s3")]
object_constants::SPI_S3_KIND_CODE => s3::object_s3_obj_serv::batch_get_presign_obj_url(object_paths, exp_secs, private, special, obj_exp, funs, ctx, &inst).await,
object_constants::SPI_S3_KIND_CODE => {
s3::object_s3_obj_serv::S3Service::batch_get_presign_obj_url(object_paths, exp_secs, private, special, obj_exp, funs, ctx, &inst).await
}
#[cfg(feature = "spi-s3")]
object_constants::SPI_OBS_KIND_CODE => {
obs::object_obs_obj_serv::OBSService::batch_get_presign_obj_url(object_paths, exp_secs, private, special, obj_exp, funs, ctx, &inst).await
}
kind_code => Err(funs.bs_not_implemented(kind_code)),
}
}
Expand All @@ -54,7 +65,11 @@ pub async fn initiate_multipart_upload(req: ObjectInitiateMultipartUploadReq, fu
match inst.kind_code() {
#[cfg(feature = "spi-s3")]
object_constants::SPI_S3_KIND_CODE => {
s3::object_s3_obj_serv::initiate_multipart_upload(&req.object_path, req.content_type, req.private, req.special, funs, ctx, &inst).await
s3::object_s3_obj_serv::S3Service::initiate_multipart_upload(&req.object_path, req.content_type, req.private, req.special, funs, ctx, &inst).await
}
#[cfg(feature = "spi-s3")]
object_constants::SPI_OBS_KIND_CODE => {
obs::object_obs_obj_serv::OBSService::initiate_multipart_upload(&req.object_path, req.content_type, req.private, req.special, funs, ctx, &inst).await
}
kind_code => Err(funs.bs_not_implemented(kind_code)),
}
Expand All @@ -65,7 +80,22 @@ pub async fn batch_build_create_presign_url(req: ObjectBatchBuildCreatePresignUr
match inst.kind_code() {
#[cfg(feature = "spi-s3")]
object_constants::SPI_S3_KIND_CODE => {
s3::object_s3_obj_serv::batch_build_create_presign_url(
s3::object_s3_obj_serv::S3Service::batch_build_create_presign_url(
&req.object_path,
&req.upload_id,
req.part_number,
req.expire_sec,
req.private,
req.special,
funs,
ctx,
&inst,
)
.await
}
#[cfg(feature = "spi-s3")]
object_constants::SPI_OBS_KIND_CODE => {
obs::object_obs_obj_serv::OBSService::batch_build_create_presign_url(
&req.object_path,
&req.upload_id,
req.part_number,
Expand All @@ -87,7 +117,11 @@ pub async fn complete_multipart_upload(req: ObjectCompleteMultipartUploadReq, fu
match inst.kind_code() {
#[cfg(feature = "spi-s3")]
object_constants::SPI_S3_KIND_CODE => {
s3::object_s3_obj_serv::complete_multipart_upload(&req.object_path, &req.upload_id, req.parts, req.private, req.special, funs, ctx, &inst).await
s3::object_s3_obj_serv::S3Service::complete_multipart_upload(&req.object_path, &req.upload_id, req.parts, req.private, req.special, funs, ctx, &inst).await
}
#[cfg(feature = "spi-s3")]
object_constants::SPI_OBS_KIND_CODE => {
obs::object_obs_obj_serv::OBSService::complete_multipart_upload(&req.object_path, &req.upload_id, req.parts, req.private, req.special, funs, ctx, &inst).await
}
kind_code => Err(funs.bs_not_implemented(kind_code)),
}
Expand All @@ -104,7 +138,9 @@ pub async fn object_delete(
let inst = funs.init(ctx, true, object_initializer::init_fun).await?;
match inst.kind_code() {
#[cfg(feature = "spi-s3")]
object_constants::SPI_S3_KIND_CODE => s3::object_s3_obj_serv::object_delete(&object_path, private, special, obj_exp, funs, ctx, &inst).await,
object_constants::SPI_S3_KIND_CODE => s3::object_s3_obj_serv::S3Service::object_delete(&object_path, private, special, obj_exp, funs, ctx, &inst).await,
#[cfg(feature = "spi-s3")]
object_constants::SPI_OBS_KIND_CODE => obs::object_obs_obj_serv::OBSService::object_delete(&object_path, private, special, obj_exp, funs, ctx, &inst).await,
kind_code => Err(funs.bs_not_implemented(kind_code)),
}
}
Expand All @@ -120,7 +156,9 @@ pub async fn batch_object_delete(
let inst = funs.init(ctx, true, object_initializer::init_fun).await?;
match inst.kind_code() {
#[cfg(feature = "spi-s3")]
object_constants::SPI_S3_KIND_CODE => s3::object_s3_obj_serv::batch_object_delete(object_paths, private, special, obj_exp, funs, ctx, &inst).await,
object_constants::SPI_S3_KIND_CODE => s3::object_s3_obj_serv::S3Service::batch_object_delete(object_paths, private, special, obj_exp, funs, ctx, &inst).await,
#[cfg(feature = "spi-s3")]
object_constants::SPI_OBS_KIND_CODE => obs::object_obs_obj_serv::OBSService::batch_object_delete(object_paths, private, special, obj_exp, funs, ctx, &inst).await,
kind_code => Err(funs.bs_not_implemented(kind_code)),
}
}
Expand All @@ -129,7 +167,9 @@ pub async fn object_copy(from: String, to: String, private: Option<bool>, specia
let inst = funs.init(ctx, true, object_initializer::init_fun).await?;
match inst.kind_code() {
#[cfg(feature = "spi-s3")]
object_constants::SPI_S3_KIND_CODE => s3::object_s3_obj_serv::object_copy(&from, &to, private, special, funs, ctx, &inst).await,
object_constants::SPI_S3_KIND_CODE => s3::object_s3_obj_serv::S3Service::object_copy(&from, &to, private, special, funs, ctx, &inst).await,
#[cfg(feature = "spi-s3")]
object_constants::SPI_OBS_KIND_CODE => obs::object_obs_obj_serv::OBSService::object_copy(&from, &to, private, special, funs, ctx, &inst).await,
kind_code => Err(funs.bs_not_implemented(kind_code)),
}
}
Expand All @@ -145,7 +185,9 @@ pub async fn object_exist(
let inst = funs.init(ctx, true, object_initializer::init_fun).await?;
match inst.kind_code() {
#[cfg(feature = "spi-s3")]
object_constants::SPI_S3_KIND_CODE => s3::object_s3_obj_serv::object_exist(&object_paths, private, special, obj_exp, funs, ctx, &inst).await,
object_constants::SPI_S3_KIND_CODE => s3::object_s3_obj_serv::S3Service::object_exist(&object_paths, private, special, obj_exp, funs, ctx, &inst).await,
#[cfg(feature = "spi-s3")]
object_constants::SPI_OBS_KIND_CODE => obs::object_obs_obj_serv::OBSService::object_exist(&object_paths, private, special, obj_exp, funs, ctx, &inst).await,
kind_code => Err(funs.bs_not_implemented(kind_code)),
}
}
2 changes: 2 additions & 0 deletions backend/spi/spi-object/src/serv/obs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod object_obs_initializer;
pub mod object_obs_obj_serv;
8 changes: 8 additions & 0 deletions backend/spi/spi-object/src/serv/obs/object_obs_initializer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use bios_basic::spi::{dto::spi_bs_dto::SpiBsCertResp, spi_funs::SpiBsInst};
use tardis::basic::{dto::TardisContext, result::TardisResult};

use crate::serv::s3;

pub async fn init(bs_cert: &SpiBsCertResp, ctx: &TardisContext, mgr: bool) -> TardisResult<SpiBsInst> {
s3::object_s3_initializer::init(bs_cert, ctx, mgr).await
}
16 changes: 16 additions & 0 deletions backend/spi/spi-object/src/serv/obs/object_obs_obj_serv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use tardis::{
basic::{result::TardisResult},
os::{
os_client::TardisOSClient,
},
};

use crate::serv::s3::S3;

/// OBS need manually configure lifecycle rules
pub(crate) struct OBSService;
impl S3 for OBSService {
async fn rebuild_path(_bucket_name: Option<&str>, origin_path: &str, _obj_exp: Option<u32>, _client: &TardisOSClient) -> TardisResult<String> {
Ok(origin_path.to_string())
}
}
Loading

0 comments on commit 0440df1

Please sign in to comment.