diff --git a/src/configuration.rs b/src/configuration.rs index 6eaa94da..5a84d3e6 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -485,7 +485,7 @@ pub struct Rule { #[derive(Deserialize, Debug, Clone)] #[serde(rename_all = "camelCase")] -pub struct RateLimitPolicy { +pub struct Policy { pub name: String, pub domain: String, pub service: String, @@ -493,7 +493,7 @@ pub struct RateLimitPolicy { pub rules: Vec, } -impl RateLimitPolicy { +impl Policy { #[cfg(test)] pub fn new( name: String, @@ -502,7 +502,7 @@ impl RateLimitPolicy { hostnames: Vec, rules: Vec, ) -> Self { - RateLimitPolicy { + Policy { name, domain, service, @@ -533,7 +533,7 @@ impl TryFrom for FilterConfig { fn try_from(config: PluginConfiguration) -> Result { let mut index = PolicyIndex::new(); - for rlp in config.rate_limit_policies.iter() { + for rlp in config.policies.iter() { for rule in &rlp.rules { for datum in &rule.data { let result = datum.item.compile(); @@ -572,7 +572,8 @@ pub enum FailureMode { #[derive(Deserialize, Debug, Clone)] #[serde(rename_all = "camelCase")] pub struct PluginConfiguration { - pub rate_limit_policies: Vec, + #[serde(rename = "rateLimitPolicies")] + pub policies: Vec, // Deny/Allow request when faced with an irrecoverable failure. pub failure_mode: FailureMode, } @@ -635,9 +636,9 @@ mod test { assert!(res.is_ok()); let filter_config = res.unwrap(); - assert_eq!(filter_config.rate_limit_policies.len(), 1); + assert_eq!(filter_config.policies.len(), 1); - let rules = &filter_config.rate_limit_policies[0].rules; + let rules = &filter_config.policies[0].rules; assert_eq!(rules.len(), 1); let conditions = &rules[0].conditions; @@ -689,7 +690,7 @@ mod test { assert!(res.is_ok()); let filter_config = res.unwrap(); - assert_eq!(filter_config.rate_limit_policies.len(), 0); + assert_eq!(filter_config.policies.len(), 0); } #[test] @@ -722,9 +723,9 @@ mod test { assert!(res.is_ok()); let filter_config = res.unwrap(); - assert_eq!(filter_config.rate_limit_policies.len(), 1); + assert_eq!(filter_config.policies.len(), 1); - let rules = &filter_config.rate_limit_policies[0].rules; + let rules = &filter_config.policies[0].rules; assert_eq!(rules.len(), 1); let data_items = &rules[0].data; @@ -794,9 +795,9 @@ mod test { assert!(res.is_ok()); let filter_config = res.unwrap(); - assert_eq!(filter_config.rate_limit_policies.len(), 1); + assert_eq!(filter_config.policies.len(), 1); - let rules = &filter_config.rate_limit_policies[0].rules; + let rules = &filter_config.policies[0].rules; assert_eq!(rules.len(), 1); let conditions = &rules[0].conditions; @@ -855,9 +856,9 @@ mod test { assert!(res.is_ok()); let filter_config = res.unwrap(); - assert_eq!(filter_config.rate_limit_policies.len(), 1); + assert_eq!(filter_config.policies.len(), 1); - let rules = &filter_config.rate_limit_policies[0].rules; + let rules = &filter_config.policies[0].rules; assert_eq!(rules.len(), 1); let conditions = &rules[0].conditions; diff --git a/src/filter/http_context.rs b/src/filter/http_context.rs index 5e4f6a09..ad3df3cc 100644 --- a/src/filter/http_context.rs +++ b/src/filter/http_context.rs @@ -1,6 +1,5 @@ use crate::configuration::{ - Condition, DataItem, DataType, FailureMode, FilterConfig, PatternExpression, RateLimitPolicy, - Rule, + Condition, DataItem, DataType, FailureMode, FilterConfig, PatternExpression, Policy, Rule, }; use crate::envoy::{ RateLimitDescriptor, RateLimitDescriptor_Entry, RateLimitRequest, RateLimitResponse, @@ -59,7 +58,7 @@ impl Filter { } } - fn process_rate_limit_policy(&self, rlp: &RateLimitPolicy) -> Action { + fn process_rate_limit_policy(&self, rlp: &Policy) -> Action { let descriptors = self.build_descriptors(rlp); if descriptors.is_empty() { debug!( @@ -107,10 +106,7 @@ impl Filter { } } - fn build_descriptors( - &self, - rlp: &RateLimitPolicy, - ) -> protobuf::RepeatedField { + fn build_descriptors(&self, rlp: &Policy) -> protobuf::RepeatedField { rlp.rules .iter() .filter(|rule: &&Rule| self.filter_rule_by_conditions(&rule.conditions)) diff --git a/src/policy_index.rs b/src/policy_index.rs index a9d7319a..50a3e39f 100644 --- a/src/policy_index.rs +++ b/src/policy_index.rs @@ -1,9 +1,9 @@ use radix_trie::Trie; -use crate::configuration::RateLimitPolicy; +use crate::configuration::Policy; pub struct PolicyIndex { - raw_tree: Trie, + raw_tree: Trie, } impl PolicyIndex { @@ -13,12 +13,12 @@ impl PolicyIndex { } } - pub fn insert(&mut self, subdomain: &str, policy: RateLimitPolicy) { + pub fn insert(&mut self, subdomain: &str, policy: Policy) { let rev = Self::reverse_subdomain(subdomain); self.raw_tree.insert(rev, policy); } - pub fn get_longest_match_policy(&self, subdomain: &str) -> Option<&RateLimitPolicy> { + pub fn get_longest_match_policy(&self, subdomain: &str) -> Option<&Policy> { let rev = Self::reverse_subdomain(subdomain); self.raw_tree.get_ancestor_value(&rev) } @@ -37,11 +37,11 @@ impl PolicyIndex { #[cfg(test)] mod tests { - use crate::configuration::RateLimitPolicy; + use crate::configuration::Policy; use crate::policy_index::PolicyIndex; - fn build_ratelimit_policy(name: &str) -> RateLimitPolicy { - RateLimitPolicy::new( + fn build_ratelimit_policy(name: &str) -> Policy { + Policy::new( name.to_owned(), "".to_owned(), "".to_owned(),