1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
pub mod compression;
pub mod header_modifier;
mod inject;
#[cfg(feature = "cache")]
mod limit;
pub mod maintenance;
pub mod redirect;
pub mod retry;
pub mod rewrite;
pub mod status;
use async_trait::async_trait;

use core::fmt;
use serde_json::Value;
use std::collections::HashMap;

use tardis::basic::error::TardisError;
use tardis::basic::result::TardisResult;
use tardis::url::Url;
use tardis::{log, TardisFuns};

use crate::config::gateway_dto::{SgGateway, SgParameters};
use crate::config::http_route_dto::{SgBackendRef, SgHttpPathMatchType, SgHttpRoute, SgHttpRouteRule};
use crate::config::plugin_filter_dto::{SgHttpPathModifier, SgHttpPathModifierType, SgRouteFilter};
use crate::instance::SgHttpRouteMatchInst;

use super::context::SgRoutePluginContext;

static mut FILTERS: Option<HashMap<String, Box<dyn SgPluginFilterDef>>> = None;

fn init_filter_defs() {
    let mut filters: HashMap<String, Box<dyn SgPluginFilterDef>> = HashMap::new();
    filters.insert(header_modifier::CODE.to_string(), Box::new(header_modifier::SgFilterHeaderModifierDef));
    filters.insert(rewrite::CODE.to_string(), Box::new(rewrite::SgFilterRewriteDef));
    filters.insert(redirect::CODE.to_string(), Box::new(redirect::SgFilterRedirectDef));
    filters.insert(inject::CODE.to_string(), Box::new(inject::SgFilterInjectDef));
    #[cfg(feature = "cache")]
    filters.insert(limit::CODE.to_string(), Box::new(limit::SgFilterLimitDef));
    filters.insert(compression::CODE.to_string(), Box::new(compression::SgFilterCompressionDef));
    filters.insert(status::CODE.to_string(), Box::new(status::SgFilterStatusDef));
    filters.insert(maintenance::CODE.to_string(), Box::new(maintenance::SgFilterMaintenanceDef));
    filters.insert(retry::CODE.to_string(), Box::new(retry::SgFilterRetryDef));
    unsafe {
        FILTERS = Some(filters);
    }
}

pub fn register_filter_def(code: impl Into<String>, filter_def: Box<dyn SgPluginFilterDef>) {
    unsafe {
        if FILTERS.is_none() {
            init_filter_defs();
        }
        FILTERS.as_mut().expect("Unreachable code").insert(code.into(), filter_def);
    }
}

pub fn get_filter_def(code: &str) -> TardisResult<&dyn SgPluginFilterDef> {
    unsafe {
        if FILTERS.is_none() {
            init_filter_defs();
        }
        FILTERS
            .as_ref()
            .expect("Unreachable code")
            .get(code)
            .map(|f| f.as_ref())
            .ok_or_else(|| TardisError::format_error(&format!("[SG.FILTER] Filter code '{code}' not found"), ""))
    }
}

pub async fn init(filter_configs: Vec<SgRouteFilter>, init_dto: SgPluginFilterInitDto) -> TardisResult<Vec<(String, BoxSgPluginFilter)>> {
    let mut plugin_filters: Vec<(String, BoxSgPluginFilter)> = Vec::new();
    let mut elements_to_remove = vec![];
    for filter_conf in filter_configs {
        let name = filter_conf.name.unwrap_or(TardisFuns::field.nanoid());
        let filter_def = get_filter_def(&filter_conf.code)?;
        let filter_inst = filter_def.inst(filter_conf.spec)?;
        plugin_filters.push((format!("{}_{name}", filter_conf.code), filter_inst));
    }
    for (i, (id, plugin_filter)) in plugin_filters.iter_mut().enumerate() {
        log::trace!("[SG.Filter] init {id} from {} .....", init_dto.attached_level);
        if plugin_filter.init(&init_dto).await.is_err() {
            elements_to_remove.push(i);
        }
    }
    for &i in elements_to_remove.iter().rev() {
        log::info!("[SG.Filter] Remove filter: {}", plugin_filters.remove(i).0);
    }
    Ok(plugin_filters)
}

pub trait SgPluginFilterDef {
    fn get_code(&self) -> &str;
    fn inst(&self, spec: Value) -> TardisResult<BoxSgPluginFilter>;
}

pub type BoxSgPluginFilter = Box<dyn SgPluginFilter>;

#[async_trait]
pub trait SgPluginFilter: Send + Sync + 'static {
    /// Enable the filter to have a state that determines
    /// whether to execute the filter at runtime
    fn accept(&self) -> SgPluginFilterAccept {
        SgPluginFilterAccept::default()
    }

    /// Whether to filter the response
    fn before_resp_filter_check(&self, ctx: &SgRoutePluginContext) -> bool {
        let accept_error_response = if ctx.is_resp_error() { self.accept().accept_error_response } else { true };
        if accept_error_response {
            self.accept().kind.contains(ctx.get_request_kind())
        } else {
            false
        }
    }

    async fn init(&mut self, init_dto: &SgPluginFilterInitDto) -> TardisResult<()>;

    async fn destroy(&self) -> TardisResult<()>;

    /// Request Filtering:
    ///
    /// This method is used for request filtering. It takes two parameters:
    ///
    /// - `id`: The plugin instance ID, which identifies the specific plugin
    /// instance.
    /// - `ctx`: A mutable context object that holds information about the
    /// request and allows for modifications.
    async fn req_filter(&self, id: &str, mut ctx: SgRoutePluginContext) -> TardisResult<(bool, SgRoutePluginContext)>;

    /// Response Filtering:
    ///
    /// This method is used for response filtering. It takes two parameters:
    ///
    /// - `id`: The plugin instance ID, which identifies the specific plugin
    /// instance.
    /// - `ctx`: A mutable context object that holds information about the
    /// request and allows for modifications.
    async fn resp_filter(&self, id: &str, mut ctx: SgRoutePluginContext) -> TardisResult<(bool, SgRoutePluginContext)>;

    fn boxed(self) -> BoxSgPluginFilter
    where
        Self: Sized,
    {
        Box::new(self)
    }
}

pub fn http_common_modify_path(uri: &http::Uri, modify_path: &Option<SgHttpPathModifier>, matched_match_inst: Option<&SgHttpRouteMatchInst>) -> TardisResult<Option<http::Uri>> {
    if let Some(modify_path) = &modify_path {
        let mut uri = Url::parse(&uri.to_string())?;
        match modify_path.kind {
            SgHttpPathModifierType::ReplaceFullPath => {
                log::debug!(
                    "[SG.Plugin.Filter.Common] Modify path with modify kind [ReplaceFullPath], form {} to  {}",
                    uri.path(),
                    modify_path.value
                );
                uri.set_path(&modify_path.value);
            }
            SgHttpPathModifierType::ReplacePrefixMatch => {
                if let Some(Some(matched_path)) = matched_match_inst.map(|m| m.path.as_ref()) {
                    match matched_path.kind {
                        SgHttpPathMatchType::Exact => {
                            // equivalent to ` SgHttpPathModifierType::ReplaceFullPath`
                            // https://cloud.yandex.com/en/docs/application-load-balancer/k8s-ref/http-route
                            log::debug!(
                                "[SG.Plugin.Filter.Common] Modify path with modify kind [ReplacePrefixMatch] and match kind [Exact], form {} to {}",
                                uri.path(),
                                modify_path.value
                            );
                            uri.set_path(&modify_path.value);
                        }
                        _ => {
                            let origin_path = uri.path();
                            let match_path = if matched_path.kind == SgHttpPathMatchType::Prefix {
                                &matched_path.value
                            } else {
                                // Support only one capture group
                                matched_path.regular.as_ref().expect("").captures(origin_path).map(|cap| cap.get(1).map_or("", |m| m.as_str())).unwrap_or("")
                            };
                            let match_path_reduce = origin_path.strip_prefix(match_path).ok_or_else(|| {
                                TardisError::format_error(
                                    "[SG.Plugin.Filter.Common] Modify path with modify kind [ReplacePrefixMatch] and match kind [Exact] failed",
                                    "",
                                )
                            })?;
                            let new_path = if match_path_reduce.is_empty() {
                                modify_path.value.to_string()
                            } else if match_path_reduce.starts_with('/') && modify_path.value.ends_with('/') {
                                format!("{}{}", modify_path.value, &match_path_reduce.to_string()[1..])
                            } else if match_path_reduce.starts_with('/') || modify_path.value.ends_with('/') {
                                format!("{}{}", modify_path.value, &match_path_reduce.to_string())
                            } else {
                                format!("{}/{}", modify_path.value, &match_path_reduce.to_string())
                            };
                            log::debug!(
                                "[SG.Plugin.Filter.Common] Modify path with modify kind [ReplacePrefixMatch] and match kind [Prefix/Regular], form {} to {}",
                                origin_path,
                                new_path,
                            );
                            uri.set_path(&new_path);
                        }
                    }
                } else {
                    // TODO
                    // equivalent to ` SgHttpPathModifierType::ReplaceFullPath`
                    log::debug!(
                        "[SG.Plugin.Filter.Common] Modify path with modify kind [None], form {} to {}",
                        uri.path(),
                        modify_path.value,
                    );
                    uri.set_path(&modify_path.value);
                }
            }
        }
        return Ok(Some(
            uri.as_str().parse().map_err(|e| TardisError::internal_error(&format!("[SG.Plugin.Filter.Common] uri parse error: {}", e), ""))?,
        ));
    }
    Ok(None)
}

// TODO
/// The SgPluginFilterKind enum is used to represent the types of plugins
/// supported by Spacegate or to identify the type of the current request.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SgPluginFilterKind {
    Http,
    Grpc,
    Ws,
}

/// The SgAttachedLevel enum is used to represent the levels at which a plugin
/// can be attached within
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SgAttachedLevel {
    Gateway,
    HttpRoute,
    Rule,
    Backend,
}

impl fmt::Display for SgAttachedLevel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            SgAttachedLevel::Gateway => write!(f, "GateWay"),
            SgAttachedLevel::HttpRoute => write!(f, "HttpRoute"),
            SgAttachedLevel::Rule => write!(f, "Rule"),
            SgAttachedLevel::Backend => write!(f, "Backend"),
        }
    }
}

/// Encapsulation filter initialization parameters.
#[derive(Debug, Clone)]
pub struct SgPluginFilterInitDto {
    pub gateway_name: String,
    /// Provide gateway-level public configuration
    pub gateway_parameters: SgParameters,
    pub http_route_rules: Vec<SgHttpRouteRule>,
    /// Identifies the level to which the filter is attached
    pub attached_level: SgAttachedLevel,
}

impl SgPluginFilterInitDto {
    pub fn from_global(gateway_conf: &SgGateway, routes: &[SgHttpRoute]) -> Self {
        Self {
            gateway_name: gateway_conf.name.clone(),
            gateway_parameters: gateway_conf.parameters.clone(),
            http_route_rules: routes.iter().flat_map(|route| route.rules.clone().unwrap_or_default()).collect::<Vec<_>>(),
            attached_level: SgAttachedLevel::Gateway,
        }
    }
    pub fn from_route(gateway_conf: &SgGateway, route: &SgHttpRoute) -> Self {
        Self {
            gateway_name: gateway_conf.name.clone(),
            gateway_parameters: gateway_conf.parameters.clone(),
            http_route_rules: route.rules.clone().unwrap_or_default(),
            attached_level: SgAttachedLevel::HttpRoute,
        }
    }
    pub fn from_rule(gateway_conf: &SgGateway, rule: &SgHttpRouteRule) -> Self {
        Self {
            gateway_name: gateway_conf.name.clone(),
            gateway_parameters: gateway_conf.parameters.clone(),
            http_route_rules: vec![rule.clone()],
            attached_level: SgAttachedLevel::Rule,
        }
    }

    pub fn from_backend(gateway_conf: &SgGateway, rule: &SgHttpRouteRule, backend: &SgBackendRef) -> Self {
        let mut rule = rule.clone();
        rule.backends = Some(vec![backend.clone()]);
        Self {
            gateway_name: gateway_conf.name.clone(),
            gateway_parameters: gateway_conf.parameters.clone(),
            http_route_rules: vec![rule],
            attached_level: SgAttachedLevel::Backend,
        }
    }
}

#[derive(Debug, Clone)]
pub struct SgPluginFilterAccept {
    pub kind: Vec<SgPluginFilterKind>,
    /// Whether to accept the error response, default is false .
    ///
    /// if filter can accept the error response, it should return true
    pub accept_error_response: bool,
}

impl Default for SgPluginFilterAccept {
    fn default() -> Self {
        Self {
            kind: vec![SgPluginFilterKind::Http],
            accept_error_response: false,
        }
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use tardis::{basic::result::TardisResult, regex::Regex};

    use crate::{
        config::{
            http_route_dto::SgHttpPathMatchType,
            plugin_filter_dto::{SgHttpPathModifier, SgHttpPathModifierType},
        },
        instance::{SgHttpPathMatchInst, SgHttpRouteMatchInst},
        plugins::filters::http_common_modify_path,
    };

    #[test]
    fn test_http_common_modify_path() -> TardisResult<()> {
        let url = "http://sg.idealworld.group/iam/ct/001?name=sg".parse().unwrap();

        let path_prefix_modifier = SgHttpPathModifier {
            kind: SgHttpPathModifierType::ReplacePrefixMatch,
            value: "/new_iam".to_string(),
        };

        let path_full_modifier = SgHttpPathModifier {
            kind: SgHttpPathModifierType::ReplaceFullPath,
            value: "/other_iam".to_string(),
        };

        // with nothing
        assert!(http_common_modify_path(&url, &None, None)?.is_none());

        // without match inst
        assert_eq!(
            http_common_modify_path(&url, &Some(path_prefix_modifier.clone()), None)?.unwrap().to_string(),
            "http://sg.idealworld.group/new_iam?name=sg".to_string()
        );
        assert_eq!(
            http_common_modify_path(&url, &Some(path_full_modifier), None)?.unwrap().to_string(),
            "http://sg.idealworld.group/other_iam?name=sg".to_string()
        );

        // with math inst
        let exact_match_inst = SgHttpRouteMatchInst {
            path: Some(SgHttpPathMatchInst {
                kind: SgHttpPathMatchType::Exact,
                value: "/iam".to_string(),
                regular: None,
            }),
            ..Default::default()
        };
        let prefix_match_inst = SgHttpRouteMatchInst {
            path: Some(SgHttpPathMatchInst {
                kind: SgHttpPathMatchType::Prefix,
                value: "/iam".to_string(),
                regular: None,
            }),
            ..Default::default()
        };
        let regular_match_inst = SgHttpRouteMatchInst {
            path: Some(SgHttpPathMatchInst {
                kind: SgHttpPathMatchType::Regular,
                value: "(/[a-z]+)".to_string(),
                regular: Some(Regex::new("(/[a-z]+)")?),
            }),
            ..Default::default()
        };
        assert_eq!(
            http_common_modify_path(&url, &Some(path_prefix_modifier.clone()), Some(&exact_match_inst))?.unwrap().to_string(),
            "http://sg.idealworld.group/new_iam?name=sg".to_string()
        );
        assert_eq!(
            http_common_modify_path(&url, &Some(path_prefix_modifier.clone()), Some(&prefix_match_inst))?.unwrap().to_string(),
            "http://sg.idealworld.group/new_iam/ct/001?name=sg".to_string()
        );
        assert_eq!(
            http_common_modify_path(&url, &Some(path_prefix_modifier), Some(&regular_match_inst))?.unwrap().to_string(),
            "http://sg.idealworld.group/new_iam/ct/001?name=sg".to_string()
        );

        Ok(())
    }
}