Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert removal of allOf within conditions #89

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 65 additions & 50 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,10 +466,12 @@ impl TryFrom<PluginConfiguration> for FilterConfig {

for rlp in config.policies.iter() {
for rule in &rlp.rules {
for pe in &rule.conditions {
let result = pe.compile();
if result.is_err() {
return Err(result.err().unwrap());
for condition in &rule.conditions {
for pe in &condition.all_of {
let result = pe.compile();
if result.is_err() {
return Err(result.err().unwrap());
}
}
}
for action in &rule.actions {
Expand Down Expand Up @@ -636,19 +638,22 @@ mod test {
{
"conditions": [
{
"selector": "request.path",
"operator": "eq",
"value": "/admin/toy"
},
{
"selector": "request.method",
"operator": "eq",
"value": "POST"
},
{
"selector": "request.host",
"operator": "eq",
"value": "cars.toystore.com"
"allOf": [
{
"selector": "request.path",
"operator": "eq",
"value": "/admin/toy"
},
{
"selector": "request.method",
"operator": "eq",
"value": "POST"
},
{
"selector": "request.host",
"operator": "eq",
"value": "cars.toystore.com"
}]
}],
"actions": [
{
Expand Down Expand Up @@ -686,7 +691,10 @@ mod test {
assert_eq!(rules.len(), 1);

let conditions = &rules[0].conditions;
assert_eq!(conditions.len(), 3);
assert_eq!(conditions.len(), 1);

let all_of_conditions = &conditions[0].all_of;
assert_eq!(all_of_conditions.len(), 3);

let actions = &rules[0].actions;
assert_eq!(actions.len(), 1);
Expand Down Expand Up @@ -753,7 +761,6 @@ mod test {
"hostnames": ["*.toystore.com", "example.com"],
"rules": [
{
"conditions": [],
"actions": [
{
"extension": "limitador",
Expand Down Expand Up @@ -818,29 +825,32 @@ mod test {
{
"conditions": [
{
"selector": "request.path",
"operator": "eq",
"value": "/admin/toy"
},
{
"selector": "request.method",
"operator": "neq",
"value": "POST"
},
{
"selector": "request.host",
"operator": "startswith",
"value": "cars."
},
{
"selector": "request.host",
"operator": "endswith",
"value": ".com"
},
{
"selector": "request.host",
"operator": "matches",
"value": "*.com"
"allOf": [
{
"selector": "request.path",
"operator": "eq",
"value": "/admin/toy"
},
{
"selector": "request.method",
"operator": "neq",
"value": "POST"
},
{
"selector": "request.host",
"operator": "startswith",
"value": "cars."
},
{
"selector": "request.host",
"operator": "endswith",
"value": ".com"
},
{
"selector": "request.host",
"operator": "matches",
"value": "*.com"
}]
}],
"actions": [
{
Expand All @@ -864,7 +874,10 @@ mod test {
assert_eq!(rules.len(), 1);

let conditions = &rules[0].conditions;
assert_eq!(conditions.len(), 5);
assert_eq!(conditions.len(), 1);

let all_of_conditions = &conditions[0].all_of;
assert_eq!(all_of_conditions.len(), 5);

let expected_conditions = [
// selector, value, operator
Expand All @@ -876,9 +889,9 @@ mod test {
];

for i in 0..expected_conditions.len() {
assert_eq!(conditions[i].selector, expected_conditions[i].0);
assert_eq!(conditions[i].value, expected_conditions[i].1);
assert_eq!(conditions[i].operator, expected_conditions[i].2);
assert_eq!(all_of_conditions[i].selector, expected_conditions[i].0);
assert_eq!(all_of_conditions[i].value, expected_conditions[i].1);
assert_eq!(all_of_conditions[i].operator, expected_conditions[i].2);
}
}

Expand All @@ -898,7 +911,6 @@ mod test {
"hostnames": ["*.toystore.com", "example.com"],
"rules": [
{
"conditions": [],
"actions": [
{
"extension": "limitador",
Expand Down Expand Up @@ -1024,9 +1036,12 @@ mod test {
{
"conditions": [
{
"selector": "request.path",
"operator": "unknown",
"value": "/admin/toy"
"allOf": [
{
"selector": "request.path",
"operator": "unknown",
"value": "/admin/toy"
}]
}],
"actions": [
{
Expand Down
20 changes: 17 additions & 3 deletions src/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@ use log::debug;
use proxy_wasm::hostcalls;
use serde::Deserialize;

#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Condition {
pub all_of: Vec<PatternExpression>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct Rule {
pub conditions: Vec<PatternExpression>,
#[serde(default)]
pub conditions: Vec<Condition>,
pub actions: Vec<Action>,
}

Expand Down Expand Up @@ -33,15 +40,22 @@ impl Policy {
.find(|rule: &&Rule| self.filter_rule_by_conditions(&rule.conditions))
}

fn filter_rule_by_conditions(&self, conditions: &[PatternExpression]) -> bool {
fn filter_rule_by_conditions(&self, conditions: &[Condition]) -> bool {
if conditions.is_empty() {
// no conditions is equivalent to matching all the requests.
return true;
}

conditions
.iter()
.all(|condition| self.pattern_expression_applies(condition))
.any(|condition| self.condition_applies(condition))
}

fn condition_applies(&self, condition: &Condition) -> bool {
condition
.all_of
.iter()
.all(|pattern_expression| self.pattern_expression_applies(pattern_expression))
}

fn pattern_expression_applies(&self, p_e: &PatternExpression) -> bool {
Expand Down
14 changes: 8 additions & 6 deletions tests/rate_limited.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ fn it_limits() {
"rules": [
{
"conditions": [
{
"allOf": [
{
"selector": "request.url_path",
"operator": "startswith",
Expand All @@ -118,8 +120,8 @@ fn it_limits() {
"selector": "request.method",
"operator": "eq",
"value": "POST"
}
],
}]
}],
"actions": [
{
"extension": "limitador",
Expand Down Expand Up @@ -257,6 +259,8 @@ fn it_passes_additional_headers() {
"rules": [
{
"conditions": [
{
"allOf": [
{
"selector": "request.url_path",
"operator": "startswith",
Expand All @@ -271,8 +275,8 @@ fn it_passes_additional_headers() {
"selector": "request.method",
"operator": "eq",
"value": "POST"
}
],
}]
}],
"actions": [
{
"extension": "limitador",
Expand Down Expand Up @@ -423,7 +427,6 @@ fn it_rate_limits_with_empty_conditions() {
"hostnames": ["*.com"],
"rules": [
{
"conditions": [],
"actions": [
{
"extension": "limitador",
Expand Down Expand Up @@ -542,7 +545,6 @@ fn it_does_not_rate_limits_when_selector_does_not_exist_and_misses_default_value
"hostnames": ["*.com"],
"rules": [
{
"conditions": [],
"actions": [
{
"extension": "limitador",
Expand Down
61 changes: 38 additions & 23 deletions utils/deploy/envoy-notls.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,13 @@ data:
{
"conditions": [
{
"selector": "request.path",
"operator": "eq",
"value": "/get"
"allOf": [
{
"selector": "request.path",
"operator": "eq",
"value": "/get"
}
]
}
],
"actions": [
Expand All @@ -179,7 +183,6 @@ data:
],
"rules": [
{
"conditions": [],
"actions": [
{
"extension": "limitador",
Expand All @@ -205,9 +208,13 @@ data:
{
"conditions": [
{
"selector": "request.url_path",
"operator": "startswith",
"value": "/unknown-path"
"allOf": [
{
"selector": "request.url_path",
"operator": "startswith",
"value": "/unknown-path"
}
]
}
],
"actions": [
Expand Down Expand Up @@ -236,19 +243,23 @@ data:
{
"conditions": [
{
"selector": "request.url_path",
"operator": "startswith",
"value": "/get"
},
{
"selector": "request.host",
"operator": "eq",
"value": "test.c.rlp.com"
},
{
"selector": "request.method",
"operator": "eq",
"value": "GET"
"allOf": [
{
"selector": "request.url_path",
"operator": "startswith",
"value": "/get"
},
{
"selector": "request.host",
"operator": "eq",
"value": "test.c.rlp.com"
},
{
"selector": "request.method",
"operator": "eq",
"value": "GET"
}
]
}
],
"actions": [
Expand Down Expand Up @@ -293,9 +304,13 @@ data:
{
"conditions": [
{
"selector": "request.path",
"operator": "eq",
"value": "/get"
"allOf": [
{
"selector": "request.path",
"operator": "eq",
"value": "/get"
}
]
}
],
"actions": [
Expand Down
Loading
Loading