Skip to content

Commit cd310e1

Browse files
author
ljl
committed
Merge branch 'main' of https://github.com/ideal-world/bios
2 parents 3310541 + 44edc78 commit cd310e1

File tree

19 files changed

+119
-67
lines changed

19 files changed

+119
-67
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ tardis = { version = "0.1.0-rc.15" }
7676
# "ext-redis",
7777
# "ext-axum",
7878
# ] }
79-
spacegate-shell = { git = "https://github.com/ideal-world/spacegate.git", branch = "master", features = [
79+
spacegate-shell = { git = "https://github.com/ideal-world/spacegate.git", branch = "dev", features = [
8080
"cache",
8181
"k8s",
82-
# "ext-redis",
82+
"ext-axum",
8383
] }
8484

85-
spacegate-plugin = { git = "https://github.com/ideal-world/spacegate.git", branch = "master" }
85+
spacegate-plugin = { git = "https://github.com/ideal-world/spacegate.git", branch = "dev" }

backend/gateways/spacegate-plugins/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ schema = ["spacegate-plugin/schema"]
1616
[dependencies]
1717
serde.workspace = true
1818
lazy_static.workspace = true
19-
spacegate-shell = { workspace = true, features = ["cache", "k8s", "ext-redis"] }
19+
spacegate-shell = { workspace = true, features = [
20+
"cache",
21+
"k8s",
22+
"ext-redis",
23+
"ext-axum",
24+
] }
2025
spacegate-plugin = { workspace = true, features = ["schema"], optional = true }
2126

2227
bios-sdk-invoke = { path = "../../../frontend/sdks/invoke", features = [

backend/gateways/spacegate-plugins/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ mod plugin;
99

1010
pub const PACKAGE_NAME: &str = "spacegate_lib";
1111
use plugin::op_redis_publisher;
12-
use spacegate_shell::plugin::SgPluginRepository;
13-
pub fn register_lib_plugins(repo: &SgPluginRepository) {
12+
use spacegate_shell::plugin::PluginRepository;
13+
pub fn register_lib_plugins(repo: &PluginRepository) {
1414
repo.register::<ip_time::IpTimePlugin>();
1515
repo.register::<anti_replay::AntiReplayPlugin>();
1616
repo.register::<anti_xss::AntiXssPlugin>();

backend/gateways/spacegate-plugins/src/plugin/anti_replay.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ use std::sync::Arc;
22
use std::time::Duration;
33

44
use serde::{Deserialize, Serialize};
5+
use spacegate_shell::ext_redis::{redis::AsyncCommands, RedisClient};
56
use spacegate_shell::hyper::{Request, Response, StatusCode};
67
use spacegate_shell::kernel::extension::PeerAddr;
78
use spacegate_shell::kernel::helper_layers::function::Inner;
89
use spacegate_shell::plugin::{Plugin, PluginError};
9-
use spacegate_shell::spacegate_ext_redis::{redis::AsyncCommands, RedisClient};
1010
use spacegate_shell::{BoxError, SgBody, SgRequestExt, SgResponseExt};
1111

1212
use tardis::serde_json;
@@ -15,7 +15,7 @@ use tardis::{basic::result::TardisResult, tokio};
1515
#[cfg(feature = "schema")]
1616
use spacegate_plugin::schemars;
1717
#[cfg(feature = "schema")]
18-
spacegate_plugin::schema!(AntiReplayPlugin, SgFilterAntiReplay);
18+
spacegate_plugin::schema!(AntiReplayPlugin, AntiReplayPlugin);
1919
#[derive(Serialize, Deserialize, Clone)]
2020
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2121
#[serde(default)]
@@ -81,6 +81,13 @@ async fn get_status(md5: &str, cache_key: &str, cache_client: &RedisClient) -> T
8181

8282
impl Plugin for AntiReplayPlugin {
8383
const CODE: &'static str = "anti-replay";
84+
85+
fn meta() -> spacegate_plugin::PluginMetaData {
86+
spacegate_plugin::plugin_meta!(
87+
description: "Anti-replay plugin for Spacegate. It can prevent replay attacks by checking the MD5 hash of the request."
88+
)
89+
}
90+
8491
fn create(plugin_config: spacegate_shell::plugin::PluginConfig) -> Result<Self, BoxError> {
8592
let config: AntiReplayPlugin = serde_json::from_value(plugin_config.spec)?;
8693
Ok(config)

backend/gateways/spacegate-plugins/src/plugin/anti_xss.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ macro_rules! append_value {
2020
use spacegate_plugin::schemars;
2121
use tardis::serde_json;
2222
#[cfg(feature = "schema")]
23-
spacegate_plugin::schema!(AntiXssPlugin, SgFilterAntiXSS);
23+
spacegate_plugin::schema!(AntiXssPlugin, CSPConfig);
24+
2425
#[derive(Default, Serialize, Deserialize)]
2526
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
2627
#[serde(default)]
@@ -157,6 +158,13 @@ pub struct AntiXssPlugin {
157158

158159
impl Plugin for AntiXssPlugin {
159160
const CODE: &'static str = "anti-xss";
161+
162+
fn meta() -> spacegate_plugin::PluginMetaData {
163+
spacegate_plugin::plugin_meta!(
164+
description: "Anti XSS plugin"
165+
)
166+
}
167+
160168
fn create(plugin_config: spacegate_shell::plugin::PluginConfig) -> Result<Self, BoxError> {
161169
let config: AntiXssConfig = serde_json::from_value(plugin_config.spec)?;
162170
let header = header::HeaderValue::from_str(&config.csp_config.to_string_header_value())?;

backend/gateways/spacegate-plugins/src/plugin/audit_log.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub const CODE: &str = "audit_log";
3939
#[cfg(feature = "schema")]
4040
use spacegate_plugin::schemars;
4141
#[cfg(feature = "schema")]
42-
spacegate_plugin::schema!(AuditLogPlugin, SgFilterAuditLog);
42+
spacegate_plugin::schema!(AuditLogPlugin, AuditLogPlugin);
4343

4444
#[derive(Serialize, Deserialize, Clone)]
4545
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
@@ -260,9 +260,14 @@ impl Default for AuditLogPlugin {
260260
}
261261

262262
impl Plugin for AuditLogPlugin {
263-
// type MakeLayer = SgFilterAuditLog;
264263
const CODE: &'static str = CODE;
265264

265+
fn meta() -> spacegate_plugin::PluginMetaData {
266+
spacegate_plugin::plugin_meta!(
267+
description: "Audit log for spacegate, it's base on spi-log"
268+
)
269+
}
270+
266271
fn create(config: spacegate_shell::plugin::PluginConfig) -> Result<Self, BoxError> {
267272
let mut plugin: AuditLogPlugin = serde_json::from_value(config.spec.clone()).map_err(|e| -> BoxError { format!("[Plugin.AuditLog] deserialize error:{e}").into() })?;
268273
plugin.init()?;

backend/gateways/spacegate-plugins/src/plugin/auth.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,13 @@ fn headermap_to_hashmap(old_headers: &HeaderMap<HeaderValue>) -> TardisResult<Ha
536536

537537
impl Plugin for AuthPlugin {
538538
const CODE: &'static str = CODE;
539-
// type MakeLayer = SgPluginAuth;
539+
540+
fn meta() -> spacegate_plugin::PluginMetaData {
541+
spacegate_plugin::plugin_meta!(
542+
description: "Auth plugin for spacegate, it is used to authenticate the request"
543+
)
544+
}
545+
540546
fn create(plugin_config: PluginConfig) -> Result<Self, BoxError> {
541547
let config: SgPluginAuthConfig = serde_json::from_value(plugin_config.spec.clone())?;
542548
let plugin: AuthPlugin = config.clone().into();

backend/gateways/spacegate-plugins/src/plugin/ip_time.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ impl IpTimePlugin {
9999
}
100100
impl Plugin for IpTimePlugin {
101101
const CODE: &'static str = CODE;
102+
103+
fn meta() -> spacegate_plugin::PluginMetaData {
104+
spacegate_plugin::plugin_meta!(
105+
description: "Block/Allow IP by time rule"
106+
)
107+
}
108+
102109
fn create(config: spacegate_shell::plugin::PluginConfig) -> Result<Self, BoxError> {
103110
let ip_time_config: SgFilterIpTimeConfig = serde_json::from_value(config.spec.clone())?;
104111
let plugin: IpTimePlugin = ip_time_config.into();
@@ -116,9 +123,4 @@ impl Plugin for IpTimePlugin {
116123
}
117124
Ok(inner.call(req).await)
118125
}
119-
// fn create(_: Option<String>, value: JsonValue) -> Result<Self::MakeLayer, BoxError> {
120-
// let config: SgFilterIpTimeConfig = serde_json::from_value(value)?;
121-
// let filter: SgFilterIpTime = config.into();
122-
// Ok(filter)
123-
// }
124126
}

backend/gateways/spacegate-plugins/src/plugin/op_redis_publisher.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,17 @@ pub struct RedisPublisherPlugin {
4242
impl Plugin for RedisPublisherPlugin {
4343
const CODE: &'static str = "op_redis_publisher";
4444

45+
fn meta() -> spacegate_plugin::PluginMetaData {
46+
spacegate_plugin::plugin_meta!(
47+
description: "Build for open platform, and it depend on plugin audit-log"
48+
)
49+
}
50+
4551
fn create(config: PluginConfig) -> Result<Self, BoxError> {
46-
let id = config.none_mono_id();
4752
let layer_config = serde_json::from_value::<RedisPublisherConfig>(config.spec.clone())?;
4853

4954
Ok(Self {
50-
key: id.redis_prefix(),
55+
key: config.id.redis_prefix(),
5156
jsonpath_inst: if let Ok(jsonpath_inst) = JsonPathInst::from_str(&layer_config.success_json_path).map_err(|e| log::error!("[Plugin.AuditLog] invalid json path:{e}")) {
5257
Some(jsonpath_inst)
5358
} else {

backend/gateways/spacegate-plugins/src/plugin/rewrite_ns_b_ip.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use std::net::IpAddr;
1212
use std::str::FromStr;
1313
use std::sync::Arc;
1414

15+
#[cfg(feature = "schema")]
16+
use spacegate_plugin::schemars;
1517
use tardis::{log, serde_json};
1618

1719
/// Kube available only!
@@ -66,6 +68,13 @@ impl Default for RewriteNsConfig {
6668

6769
impl Plugin for RewriteNsPlugin {
6870
const CODE: &'static str = "rewrite-ns";
71+
72+
fn meta() -> spacegate_plugin::PluginMetaData {
73+
spacegate_plugin::plugin_meta!(
74+
description: "Rewrite namespace for request.Kubernetes available only!"
75+
)
76+
}
77+
6978
fn create(plugin_config: PluginConfig) -> Result<Self, spacegate_shell::BoxError> {
7079
let config: RewriteNsConfig = serde_json::from_value(plugin_config.spec)?;
7180
let ip_list: Vec<IpNet> = config
@@ -119,6 +128,7 @@ impl RewriteNsPlugin {
119128
mod test {
120129

121130
use http::{Method, Request, Uri, Version};
131+
use spacegate_plugin::{PluginInstanceId, PluginInstanceName};
122132
use spacegate_shell::{
123133
config::K8sServiceData,
124134
extension::k8s_service::K8sService,
@@ -133,9 +143,11 @@ mod test {
133143
#[tokio::test]
134144
async fn test() {
135145
let plugin = RewriteNsPlugin::create(PluginConfig {
136-
code: "rewrite-ns".into(),
146+
id: PluginInstanceId {
147+
code: "rewrite-ns".into(),
148+
name: PluginInstanceName::mono(),
149+
},
137150
spec: json!({"ip_list":["198.168.1.0/24"],"target_ns":"target"}),
138-
name: None,
139151
})
140152
.unwrap();
141153

backend/services/spacegate/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ spacegate-shell = { workspace = true, features = [
1919
"k8s",
2020
"plugin-all",
2121
"ext-redis",
22-
# "ext-axum",
22+
"ext-axum",
2323
"cache",
2424
] }
2525
tardis = { workspace = true }

backend/services/spacegate/src/main.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use serde::Deserialize;
22
use spacegate_plugins::register_lib_plugins;
3-
use spacegate_shell::plugin::SgPluginRepository;
3+
use spacegate_shell::plugin::PluginRepository;
44
use spacegate_shell::BoxError;
55
use tardis::basic::tracing::TardisTracing;
66
use tardis::tokio;
@@ -25,14 +25,9 @@ fn main() -> Result<(), BoxError> {
2525
}
2626
let rt = builder.build().expect("fail to build runtime");
2727
let namespaces = std::env::args().nth(1).or(config.spacegate_ns);
28-
register_lib_plugins(SgPluginRepository::global());
28+
register_lib_plugins(PluginRepository::global());
2929
rt.block_on(async move {
3030
let local_set = tokio::task::LocalSet::new();
31-
local_set
32-
.run_until(async move {
33-
let join_handle = spacegate_shell::startup_k8s(namespaces.as_deref()).await.expect("fail to start spacegate");
34-
join_handle.await.expect("join handle error")
35-
})
36-
.await
31+
local_set.run_until(async move { spacegate_shell::startup_k8s(namespaces.as_deref()).await }).await
3732
})
3833
}

backend/spi/spi-conf/src/serv.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ pub async fn register(req: RegisterRequest, funs: &TardisFunsInst, ctx: &TardisC
166166
// add a cert
167167
let ext = json!({
168168
"owner": ctx.owner,
169-
"own_paths": ctx.own_paths
169+
"own_paths": ctx.own_paths,
170+
"ak": ctx.ak
170171
})
171172
.to_string();
172173
let mut add_cert_req = RbumCertAddReq {
@@ -228,8 +229,10 @@ pub async fn auth(ak: &str, sk: &str, funs: &TardisFunsInst) -> TardisResult<Tar
228229
let ext: serde_json::Value = serde_json::from_str(&cert.ext).map_err(|_| funs.err().internal_error("spi-conf", "auth", "invalid ext", "500-conf-invalid-cert-ext"))?;
229230
let owner = ext.get("owner").and_then(serde_json::Value::as_str).unwrap_or_default();
230231
let own_paths = ext.get("own_paths").and_then(serde_json::Value::as_str).unwrap_or_default();
232+
let ak = ext.get("ak").and_then(serde_json::Value::as_str).unwrap_or_default();
231233
ctx.owner = owner.to_owned();
232234
ctx.own_paths = own_paths.to_owned();
235+
ctx.ak = ak.to_owned();
233236
Ok(ctx)
234237
}
235238

backend/spi/spi-conf/src/serv/pg/conf_pg_config_serv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub async fn get_config(descriptor: &mut ConfigDescriptor, _funs: &TardisFunsIns
7474
let qry_result = conn
7575
.query_one(
7676
&format!(
77-
r#"SELECT (content, md5) FROM {table_name} cc
77+
r#"SELECT "content", "md5" FROM {table_name} cc
7878
WHERE cc.namespace_id=$1 AND cc.grp=$2 AND cc.data_id=$3
7979
"#,
8080
),

backend/spi/spi-conf/tests/spi_conf_api_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async fn spi_conf_namespace_test() -> TardisResult<()> {
3131
let mut client = TestHttpClient::new("https://127.0.0.1:8080/spi-conf".to_string());
3232
client.set_auth(&TardisContext {
3333
own_paths: "t1/app001".to_string(),
34-
ak: "".to_string(),
34+
ak: "app001".to_string(),
3535
roles: vec![],
3636
groups: vec![],
3737
owner: "app001".to_string(),

backend/spi/spi-conf/tests/spi_conf_listener_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ async fn spi_conf_namespace_test() -> TardisResult<()> {
4949
log::info!("username: {username}, password: {password}");
5050
client.set_auth(&TardisContext {
5151
own_paths: "t1/app001".to_string(),
52-
ak: "".to_string(),
52+
ak: "app001".to_string(),
5353
roles: vec![],
5454
groups: vec![],
5555
owner: "app001".to_string(),
@@ -119,7 +119,7 @@ pub async fn test_listener(client: &mut TestHttpClient) -> TardisResult<()> {
119119
let update_counter = Arc::new(AtomicUsize::new(0));
120120
let ctx_raw = Arc::new(TardisContext {
121121
own_paths: "t1/app001".to_string(),
122-
ak: "".to_string(),
122+
ak: "app001".to_string(),
123123
roles: vec![],
124124
groups: vec![],
125125
owner: "app001".to_string(),

backend/spi/spi-conf/tests/spi_conf_nacos_compatible_test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const SCHEMA: &str = "https";
2222
async fn spi_conf_namespace_test() -> TardisResult<()> {
2323
std::env::set_var(
2424
"RUST_LOG",
25-
"info,tardis=debug,spi_conf_listener_test=debug,sqlx=off,sea_orm=off,bios_spi_conf=DEBUG,poem_grpc=TRACE,tonic=TRACE",
25+
"info,tardis=debug,spi_conf_listener_test=debug,sqlx=off,sea_orm=debug,bios_spi_conf=DEBUG,poem_grpc=TRACE,tonic=TRACE",
2626
);
2727
std::env::set_var("PROFILE", "nacos");
2828
let docker = testcontainers::clients::Cli::default();
@@ -51,7 +51,7 @@ async fn spi_conf_namespace_test() -> TardisResult<()> {
5151
log::info!("username: {username}, password: {password}");
5252
client.set_auth(&TardisContext {
5353
own_paths: "t1/app001".to_string(),
54-
ak: "".to_string(),
54+
ak: "app001".to_string(),
5555
roles: vec![],
5656
groups: vec![],
5757
owner: "app001".to_string(),
@@ -71,7 +71,7 @@ async fn test_tardis_compatibility(_test_client: &TestHttpClient) -> TardisResul
7171
let config = TardisFuns::fw_config();
7272
let ctx = TardisContext {
7373
own_paths: "t1/app001".to_string(),
74-
ak: "".to_string(),
74+
ak: "app001".to_string(),
7575
roles: vec![],
7676
groups: vec![],
7777
owner: "app001".to_string(),
@@ -196,7 +196,7 @@ async fn test_tardis_compatibility(_test_client: &TestHttpClient) -> TardisResul
196196
let _resp = nacos_client
197197
.publish_config(
198198
&NacosConfigDescriptor::new("hc-db.yaml", "hc", &(Default::default())),
199-
&mut std::fs::File::open("tests/config/test-prod.yaml").expect("fail to open"),
199+
&mut std::fs::File::open("tests/config/conf-nacos.toml").expect("fail to open"),
200200
)
201201
.await
202202
.expect("publish failed");

backend/spi/spi-conf/tests/spi_conf_test_common.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use testcontainers_modules::redis::Redis;
1414
pub struct Holder<'d> {
1515
pub pg: Container<'d, GenericImage>,
1616
pub redis: Container<'d, Redis>,
17-
pub mq: Container<'d, GenericImage>,
1817
}
1918

2019
#[allow(dead_code)]
@@ -27,14 +26,9 @@ pub async fn init_tardis(docker: &Cli) -> TardisResult<Holder> {
2726
let port = redis_container.get_host_port_ipv4(6379);
2827
let url = format!("redis://127.0.0.1:{port}/0");
2928
std::env::set_var("TARDIS_FW.CACHE.URL", url);
30-
let mq_container = TardisTestContainer::rabbit_custom(docker);
31-
let port = mq_container.get_host_port_ipv4(5672);
32-
let url = format!("amqp://guest:guest@127.0.0.1:{port}/%2f");
33-
std::env::set_var("TARDIS_FW.MQ.URL", url);
3429
let holder = Holder {
3530
pg: reldb_container,
3631
redis: redis_container,
37-
mq: mq_container,
3832
};
3933
TardisFuns::init(Some("tests/config")).await?;
4034
bios_basic::rbum::rbum_initializer::init(DOMAIN_CODE, RbumConfig::default()).await?;

0 commit comments

Comments
 (0)