Skip to content

Commit

Permalink
Merge pull request #5 from RWDai/test
Browse files Browse the repository at this point in the history
add OPRedisPublisher
  • Loading branch information
4t145 authored Apr 2, 2024
2 parents 9d896ba + 548b330 commit 8a9224d
Show file tree
Hide file tree
Showing 23 changed files with 382 additions and 65 deletions.
2 changes: 1 addition & 1 deletion basic/src/rbum/dto/rbum_cert_dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct RbumCertAddReq {
pub is_outside: bool,
}

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Default)]
#[cfg_attr(feature = "default", derive(poem_openapi::Object))]
pub struct RbumCertModifyReq {
#[cfg_attr(feature = "default", oai(validator(min_length = "2", max_length = "2000")))]
Expand Down
15 changes: 9 additions & 6 deletions basic/src/rbum/serv/rbum_cert_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,12 +443,15 @@ impl RbumCrudOperation<rbum_cert::ActiveModel, RbumCertAddReq, RbumCertModifyReq
}
}
// Fill Time
if let Some(start_time) = &add_req.start_time {
add_req.end_time = Some(*start_time + Duration::try_seconds(rbum_cert_conf.expire_sec).unwrap_or(TimeDelta::max_value()));
} else {
let now = Utc::now();
add_req.start_time = Some(now);
add_req.end_time = Some(now + Duration::try_seconds(rbum_cert_conf.expire_sec).unwrap_or(TimeDelta::max_value()));
if add_req.end_time.is_none() {
if let Some(start_time) = &add_req.start_time {
add_req.end_time = Some(*start_time + Duration::try_seconds(rbum_cert_conf.expire_sec).unwrap_or(TimeDelta::max_value()));
} else {
add_req.end_time = Some(Utc::now() + Duration::try_seconds(rbum_cert_conf.expire_sec).unwrap_or(TimeDelta::max_value()));
}
}
if add_req.start_time.is_none() {
add_req.start_time = Some(Utc::now());
}
if rbum_cert_conf.sk_dynamic {
add_req.end_time = None;
Expand Down
33 changes: 33 additions & 0 deletions gateway/spacegate-lib/src/extension.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,37 @@
use http::Extensions;
use spacegate_shell::{kernel::extension::ExtensionPack as _, BoxError};
use tardis::serde_json::{self, Value};

use self::audit_log_param::LogParamContent;

pub mod audit_log_param;
pub mod before_encrypt_body;
pub mod cert_info;
pub mod request_crypto_status;

pub enum ExtensionPackEnum {
LogParamContent(),
None,
}

impl From<String> for ExtensionPackEnum {
fn from(value: String) -> Self {
match value.as_str() {
"log_content" => ExtensionPackEnum::LogParamContent(),
_ => ExtensionPackEnum::None,
}
}
}
impl ExtensionPackEnum {
pub fn to_value(&self, ext: &Extensions) -> Result<Option<Value>, BoxError> {
match self {
ExtensionPackEnum::LogParamContent() => {
if let Some(ext) = LogParamContent::get(ext) {
return Ok(Some(serde_json::to_value(ext)?));
}
}
ExtensionPackEnum::None => (),
}
Ok(None)
}
}
26 changes: 25 additions & 1 deletion gateway/spacegate-lib/src/extension/audit_log_param.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use spacegate_shell::hyper::HeaderMap;
use std::time::Duration;

use serde::{Deserialize, Serialize};
use spacegate_shell::{hyper::HeaderMap, kernel::extension::ExtensionPack};

use super::cert_info::RoleInfo;

#[derive(Clone)]
pub struct AuditLogParam {
Expand All @@ -8,3 +13,22 @@ pub struct AuditLogParam {
pub request_scheme: String,
pub request_ip: String,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct LogParamContent {
pub op: String,
pub name: String,
pub user_id: Option<String>,
pub own_paths: Option<String>,
pub role: Vec<RoleInfo>,
pub ip: String,
pub path: String,
pub scheme: String,
pub token: Option<String>,
pub server_timing: Option<Duration>,
pub resp_status: String,
//Indicates whether the business operation was successful.
pub success: bool,
}

impl ExtensionPack for LogParamContent {}
1 change: 1 addition & 0 deletions gateway/spacegate-lib/src/extension/cert_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize};
#[derive(Clone)]
pub struct CertInfo {
pub id: String,
pub own_paths: Option<String>,
pub name: Option<String>,
pub roles: Vec<RoleInfo>,
}
Expand Down
2 changes: 2 additions & 0 deletions gateway/spacegate-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod marker;
mod plugin;

pub const PACKAGE_NAME: &str = "spacegate_lib";
use plugin::op_redis_publisher;
use spacegate_shell::plugin::SgPluginRepository;
pub fn register_lib_plugins(repo: &SgPluginRepository) {
repo.register::<ip_time::IpTimePlugin>();
Expand All @@ -16,4 +17,5 @@ pub fn register_lib_plugins(repo: &SgPluginRepository) {
repo.register::<rewrite_ns_b_ip::RewriteNsPlugin>();
repo.register::<audit_log::AuditLogPlugin>();
repo.register::<auth::AuthPlugin>();
repo.register::<op_redis_publisher::RedisPublisherPlugin>();
}
1 change: 1 addition & 0 deletions gateway/spacegate-lib/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pub mod anti_xss;
pub mod audit_log;
pub mod auth;
pub mod ip_time;
pub mod op_redis_publisher;
pub mod rewrite_ns_b_ip;
27 changes: 7 additions & 20 deletions gateway/spacegate-lib/src/plugin/audit_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ use tardis::{
TardisFuns, TardisFunsInst,
};

use crate::extension::audit_log_param::AuditLogParam;
use crate::extension::audit_log_param::{AuditLogParam, LogParamContent};
use crate::extension::before_encrypt_body::BeforeEncryptBody;
use crate::extension::cert_info::{CertInfo, RoleInfo};
use crate::extension::cert_info::CertInfo;

pub const CODE: &str = "audit_log";

Expand Down Expand Up @@ -134,9 +134,10 @@ impl AuditLogPlugin {
path: param.request_path,
scheme: param.request_scheme,
token: param.request_headers.get(&self.header_token_name).and_then(|v| v.to_str().ok().map(|v| v.to_string())),
server_timing: start_time.map(|st| (end_time - st).as_millis() as i64),
server_timing: start_time.map(|st| end_time - st),
resp_status: resp.status().as_u16().to_string(),
success,
own_paths: resp.extensions().get::<CertInfo>().and_then(|info| info.own_paths.clone()),
};
Ok((resp, Some(content)))
}
Expand Down Expand Up @@ -182,6 +183,7 @@ impl AuditLogPlugin {
} else {
reflect.insert(CertInfo {
id: ident,
own_paths: None,
name: None,
roles: vec![],
});
Expand Down Expand Up @@ -284,36 +286,21 @@ fn get_tardis_inst() -> TardisFunsInst {
TardisFuns::inst(CODE.to_string(), None)
}

#[derive(Serialize, Deserialize)]
pub struct LogParamContent {
pub op: String,
pub name: String,
pub user_id: Option<String>,
pub role: Vec<RoleInfo>,
pub ip: String,
pub path: String,
pub scheme: String,
pub token: Option<String>,
pub server_timing: Option<i64>,
pub resp_status: String,
//Indicates whether the business operation was successful.
pub success: bool,
}

impl LogParamContent {
fn to_value(&self) -> Value {
json!({
"name":self.name,
"id":self.user_id,
"own_paths":self.own_paths,
"ip":self.ip,
"op":self.op,
"path":self.path,
"resp_status": self.resp_status,
"server_timing":self.server_timing,
"success":self.success,
})
}
}

#[cfg(test)]
mod test {
use http::{HeaderName, Request, Response};
Expand Down
1 change: 1 addition & 0 deletions gateway/spacegate-lib/src/plugin/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ fn success_auth_result_to_req(auth_result: AuthResult, config: &AuthConfig, req:
let (mut parts, mut body) = req.into_parts();
let cert_info = CertInfo {
id: auth_result.ctx.as_ref().and_then(|ctx| ctx.account_id.clone().or(ctx.ak.clone())).unwrap_or_default(),
own_paths: auth_result.ctx.as_ref().and_then(|ctx| ctx.own_paths.clone()),
name: None,
roles: auth_result
.ctx
Expand Down
Loading

0 comments on commit 8a9224d

Please sign in to comment.