Skip to content

Commit 44edc78

Browse files
RWDaiZzIsGod1019
andauthored
Feat update gateway plugin to dev branch (#729)
* update-gateway-plugin-to-dev-branch * update --------- Co-authored-by: ZzIsGod1019 <1498852723@qq.com>
1 parent 1143c63 commit 44edc78

File tree

9 files changed

+66
-15
lines changed

9 files changed

+66
-15
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ tardis = { version = "0.1.0-rc.15" }
7979
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

8585
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/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::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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ 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> {
4652
let layer_config = serde_json::from_value::<RedisPublisherConfig>(config.spec.clone())?;
4753

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

0 commit comments

Comments
 (0)