fips_configuration/modes/
fips.rs1use fips_plugin_registry::plugin::ExternalFunctions;
2
3use crate::rule_collection::{
4 CommonFunctions, ProxyFunctions, RuleCollectionError,
5 RuleTransformingFunctions,
6 apply_plugins,
7 default_as_true,
8};
9use crate::rule::Rule;
10
11use serde::{Deserialize, Serialize};
12use std::{ collections::HashMap };
13use hyper::Uri;
14use std::str::{FromStr};
15use schemars::JsonSchema;
16
17#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
18pub struct FIPS {
19 pub name: Option<String>,
20 pub path: String,
21 #[serde(rename = "matchProbability")]
22 pub match_with_prob: Option<f32>,
23 #[serde(rename = "matchBodyContains")]
24 pub match_body_contains: Option<String>,
25 #[serde(rename = "matchMethods")]
26 pub match_methods: Option<Vec<String>>,
27 #[serde(rename = "serveStatic")]
28 pub serve_static: Option<String>,
29 #[serde(skip)]
30 pub selected: bool,
31 pub sleep: Option<u64>,
32 #[serde(default = "default_as_true")]
33 pub active: bool,
34 #[serde(rename = "responseStatus")]
35 pub response_status: Option<u16>,
36 #[serde(rename = "forwardUri")]
37 pub forward_uri: String,
38 #[serde(rename = "forwardHeaders")]
39 pub forward_headers: Option<Vec<String>>,
40 #[serde(rename = "backwardHeaders")]
41 pub backward_headers: Option<Vec<String>>,
42 pub headers: Option<HashMap<String, String>>,
43 pub rules: Option<Vec<Rule>>,
44}
45
46impl RuleTransformingFunctions for FIPS {
47 fn apply_plugins(&mut self, template: &ExternalFunctions) {
48 if let Some(rules) = &mut self.rules {
49 for rule in rules {
50 if let Some(item) = &mut rule.item {
51 apply_plugins(item, template);
52 }
53 }
54 }
55 }
56}
57
58impl ProxyFunctions for FIPS {
59 fn get_forward_uri(&self) -> String {
60 self.forward_uri.clone()
61 }
62
63 fn get_forward_headers(&self) -> Option<Vec<String>> {
64 self.forward_headers.clone()
65 }
66
67 fn get_backward_headers(&self) -> Option<Vec<String>> {
68 self.backward_headers.clone()
69 }
70
71 fn form_forward_path(&self, uri: &Uri) -> Result<Uri, RuleCollectionError> {
72 Ok(Uri::from_str(&format!("{}{uri}", self.get_forward_uri()))?)
73 }
74}
75
76impl CommonFunctions for FIPS {
77 fn get_name(&self) -> Option<String> {
78 self.name.clone()
79 }
80
81 fn get_path(&self) -> String {
82 self.path.clone()
83 }
84
85 fn get_match_with_prob(&self) -> Option<f32> {
86 self.match_with_prob
87 }
88
89 fn get_match_body_contains(&self) -> Option<String> {
90 self.match_body_contains.clone()
91 }
92
93 fn get_match_methods(&self) -> Option<Vec<String>> {
94 self.match_methods.clone()
95 }
96
97 fn get_selected(&self) -> bool {
98 self.selected
99 }
100
101 fn get_sleep(&self) -> Option<u64> {
102 self.sleep
103 }
104
105 fn get_active(&self) -> bool {
106 self.active
107 }
108
109 fn get_headers(&self) -> Option<HashMap<String, String>> {
110 self.headers.clone()
111 }
112
113 fn set_selected(&mut self) {
114 self.selected = true;
115 }
116
117 fn set_unselected(&mut self) {
118 self.selected = false;
119 }
120
121 fn set_active(&mut self) {
122 self.active = true;
123 }
124
125 fn set_inactive(&mut self) {
126 self.active = false;
127 }
128}