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

Refactoring actions #88

Closed
wants to merge 2 commits into from
Closed
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
213 changes: 84 additions & 129 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,10 +462,12 @@ impl TryFrom<PluginConfiguration> for FilterConfig {

for rlp in config.policies.iter() {
for rule in &rlp.rules {
for datum in &rule.data {
let result = datum.item.compile();
if result.is_err() {
return Err(result.err().unwrap());
for action in &rule.actions {
for datum in &action.data {
let result = datum.item.compile();
if result.is_err() {
return Err(result.err().unwrap());
}
}
}
for condition in &rule.conditions {
Expand Down Expand Up @@ -534,7 +536,7 @@ pub struct Extension {
pub struct Action {
pub extension: String,
#[allow(dead_code)]
pub data: DataType,
pub data: Vec<DataItem>,
}

#[cfg(test)]
Expand Down Expand Up @@ -575,30 +577,24 @@ mod test {
"value": "cars.toystore.com"
}]
}],
"data": [
{
"static": {
"key": "rlp-ns-A/rlp-name-A",
"value": "1"
}
},
{
"selector": {
"selector": "auth.metadata.username"
}
}]
}],
"actions": [
"actions": [
{
"extension": "limitador",
"data": {
"static": {
"key": "rlp-ns-A/rlp-name-A",
"value": "1"
"data": [
{
"static": {
"key": "rlp-ns-A/rlp-name-A",
"value": "1"
}
},
{
"selector": {
"selector": "auth.metadata.username"
}
}
}
}
]
]
}]
}]
}]
}"#;

Expand All @@ -622,7 +618,7 @@ mod test {
let all_of_conditions = &conditions[0].all_of;
assert_eq!(all_of_conditions.len(), 3);

let data_items = &rules[0].data;
let data_items = &rules[0].actions[0].data;
assert_eq!(data_items.len(), 2);

// TODO(eastizle): DataItem does not implement PartialEq, add it only for testing?
Expand Down Expand Up @@ -685,26 +681,19 @@ mod test {
"hostnames": ["*.toystore.com", "example.com"],
"rules": [
{
"data": [
"actions": [
{
"selector": {
"selector": "my.selector.path",
"key": "mykey",
"default": "my_selector_default_value"
}
"extension": "limitador",
"data": [
{
"selector": {
"selector": "my.selector.path",
"key": "mykey",
"default": "my_selector_default_value"
}
}]
}]
}],
"actions": [
{
"extension": "limitador",
"data": {
"static": {
"key": "rlp-ns-A/rlp-name-A",
"value": "1"
}
}
}
]
}]
}]
}"#;
let res = serde_json::from_str::<PluginConfiguration>(config);
Expand All @@ -719,7 +708,7 @@ mod test {
let rules = &filter_config.policies[0].rules;
assert_eq!(rules.len(), 1);

let data_items = &rules[0].data;
let data_items = &rules[0].actions[0].data;
assert_eq!(data_items.len(), 1);

if let DataType::Selector(selector_item) = &data_items[0].item {
Expand Down Expand Up @@ -780,19 +769,12 @@ mod test {
"value": "*.com"
}]
}],
"data": [ { "selector": { "selector": "my.selector.path" } }]
}],
"actions": [
{
"extension": "limitador",
"data": {
"static": {
"key": "rlp-ns-A/rlp-name-A",
"value": "1"
}
}
}
]
"actions": [
{
"extension": "limitador",
"data": [ { "selector": { "selector": "my.selector.path" } }]
}]
}]
}]
}"#;
let res = serde_json::from_str::<PluginConfiguration>(config);
Expand Down Expand Up @@ -846,30 +828,23 @@ mod test {
"hostnames": ["*.toystore.com", "example.com"],
"rules": [
{
"data": [
"actions": [
{
"static": {
"key": "rlp-ns-A/rlp-name-A",
"value": "1"
}
},
{
"selector": {
"selector": "auth.metadata.username"
}
"extension": "limitador",
"data": [
{
"static": {
"key": "rlp-ns-A/rlp-name-A",
"value": "1"
}
},
{
"selector": {
"selector": "auth.metadata.username"
}
}]
}]
}],
"actions": [
{
"extension": "limitador",
"data": {
"static": {
"key": "rlp-ns-A/rlp-name-A",
"value": "1"
}
}
}
]
}]
}]
}"#;
let res = serde_json::from_str::<PluginConfiguration>(config);
Expand Down Expand Up @@ -906,28 +881,22 @@ mod test {
"hostnames": ["*.toystore.com", "example.com"],
"rules": [
{
"data": [
{
"static": {
"key": "rlp-ns-A/rlp-name-A",
"value": "1"
},
"selector": {
"selector": "auth.metadata.username"
}
}]
}],
"actions": [
{
"extension": "limitador",
"data": {
"static": {
"key": "rlp-ns-A/rlp-name-A",
"value": "1"
}
"actions": [
{
"extension": "limitador",
"data": [
{
"static": {
"key": "rlp-ns-A/rlp-name-A",
"value": "1"
},
"selector": {
"selector": "auth.metadata.username"
}
}]
}
}
]
]
}]
}]
}"#;
let res = serde_json::from_str::<PluginConfiguration>(bad_config);
Expand All @@ -950,25 +919,18 @@ mod test {
"hostnames": ["*.toystore.com", "example.com"],
"rules": [
{
"data": [
{
"unknown": {
"key": "rlp-ns-A/rlp-name-A",
"value": "1"
}
}]
}],
"actions": [
"actions": [
{
"extension": "limitador",
"data": {
"static": {
"data": [
{
"unknown": {
"key": "rlp-ns-A/rlp-name-A",
"value": "1"
}
}
}
]
}]
}]
}]
}]
}"#;
let res = serde_json::from_str::<PluginConfiguration>(bad_config);
Expand Down Expand Up @@ -999,19 +961,12 @@ mod test {
"value": "/admin/toy"
}]
}],
"data": [ { "selector": { "selector": "my.selector.path" } }]
}],
"actions": [
{
"extension": "limitador",
"data": {
"static": {
"key": "rlp-ns-A/rlp-name-A",
"value": "1"
}
}
}
]
"actions": [
{
"extension": "limitador",
"data": [ { "selector": { "selector": "my.selector.path" } }]
}]
}]
}]
}"#;
let res = serde_json::from_str::<PluginConfiguration>(bad_config);
Expand Down
21 changes: 12 additions & 9 deletions src/filter/http_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,21 @@ impl Filter {
}

fn process_policy(&self, policy: &Policy) -> Action {
let descriptors = policy.build_descriptors(self);
if descriptors.is_empty() {
debug!(
"#{} process_rate_limit_policy: empty descriptors",
self.context_id
);
if let Some(rule) = policy.find_rule_that_applies() {
//TODO: Move the building of descriptors at operation trigger time
let descriptors = policy.build_descriptors(rule);
if descriptors.is_empty() {
debug!("#{} process_policy: empty descriptors", self.context_id);
return Action::Continue;
}

self.operation_dispatcher
.build_operations(policy.domain.clone(), rule, descriptors);
} else {
debug!("#{} process_policy: no rule applied", self.context_id);
return Action::Continue;
}

self.operation_dispatcher
.build_operations(policy, descriptors);

if let Some(operation) = self.operation_dispatcher.next() {
match operation.get_result() {
Ok(call_id) => {
Expand Down
9 changes: 5 additions & 4 deletions src/operation_dispatcher.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::configuration::{Extension, ExtensionType, FailureMode};
use crate::envoy::RateLimitDescriptor;
use crate::policy::Policy;
use crate::policy::Rule;
use crate::service::grpc_message::GrpcMessageRequest;
use crate::service::{GetMapValuesBytesFn, GrpcCallFn, GrpcServiceHandler};
use protobuf::RepeatedField;
Expand Down Expand Up @@ -122,16 +122,17 @@ impl OperationDispatcher {

pub fn build_operations(
&self,
policy: &Policy,
domain: String, // TODO(didierofrivia): See if can work with &str
rule: &Rule,
descriptors: RepeatedField<RateLimitDescriptor>,
) {
let mut operations: Vec<Operation> = vec![];
policy.actions.iter().for_each(|action| {
rule.actions.iter().for_each(|action| {
// TODO(didierofrivia): Error handling
if let Some(service) = self.service_handlers.get(&action.extension) {
let message = GrpcMessageRequest::new(
service.get_extension_type(),
policy.domain.clone(),
domain.clone(),
descriptors.clone(),
);
operations.push(Operation::new(
Expand Down
Loading
Loading