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

Add Pass & Drop filters, Refactor Match config #472

Merged
merged 2 commits into from
Feb 23, 2022
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
7 changes: 4 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
"proto/data-plane-api/envoy/service/discovery/v3/discovery.proto",
"proto/data-plane-api/envoy/type/metadata/v3/metadata.proto",
"proto/data-plane-api/envoy/type/tracing/v3/custom_tag.proto",
"proto/udpa/xds/core/v3/resource_name.proto",
"proto/quilkin/extensions/filters/capture/v1alpha1/capture.proto",
"proto/quilkin/extensions/filters/compress/v1alpha1/compress.proto",
"proto/quilkin/extensions/filters/concatenate_bytes/v1alpha1/concatenate_bytes.proto",
"proto/quilkin/extensions/filters/debug/v1alpha1/debug.proto",
"proto/quilkin/extensions/filters/debug/v1alpha1/debug.proto",
"proto/quilkin/extensions/filters/drop/v1alpha1/drop.proto",
"proto/quilkin/extensions/filters/firewall/v1alpha1/firewall.proto",
"proto/quilkin/extensions/filters/load_balancer/v1alpha1/load_balancer.proto",
"proto/quilkin/extensions/filters/local_rate_limit/v1alpha1/local_rate_limit.proto",
"proto/quilkin/extensions/filters/matches/v1alpha1/matches.proto",
"proto/quilkin/extensions/filters/match/v1alpha1/match.proto",
"proto/quilkin/extensions/filters/pass/v1alpha1/pass.proto",
"proto/quilkin/extensions/filters/token_router/v1alpha1/token_router.proto",
"proto/udpa/xds/core/v3/resource_name.proto",
]
.iter()
.map(|name| std::env::current_dir().unwrap().join(name))
Expand Down
4 changes: 3 additions & 1 deletion docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
- [Compress](./filters/compress.md)
- [Concatenate Bytes](./filters/concatenate_bytes.md)
- [Debug](./filters/debug.md)
- [Drop](./filters/drop.md)
- [Firewall](./filters/firewall.md)
- [Load Balancer](./filters/load_balancer.md)
- [Local Rate Limit](./filters/local_rate_limit.md)
- [Matches](./filters/matches.md)
- [Match](./filters/match.md)
- [Pass](./filters/pass.md)
- [Token Router](./filters/token_router.md)
- [Writing Custom Filters](./filters/writing_custom_filters.md)
- [Integrations](./integrations.md)
Expand Down
20 changes: 20 additions & 0 deletions docs/src/filters/drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Drop

The `Drop` filter always drops any packet passed through it. This is useful in
combination with conditional flow filters like [`Matches`](./match.md)

#### Filter name
```text
quilkin.extensions.filters.drop.v1alpha1.Drop
```

### Configuration Examples
{{#include match.md:example}}

### Configuration

No defined configuration options.

### Metrics

This filter currently exports no metrics.
18 changes: 9 additions & 9 deletions docs/src/filters/matches.md → docs/src/filters/match.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ in Rust or `switch` statements in other languages.

#### Filter name
```text
quilkin.extensions.filters.matches.v1alpha1.Matches
quilkin.extensions.filters.match.v1alpha1.Match
```

### Configuration Examples
<!-- ANCHOR: example -->
```rust
# let yaml = "
version: v1alpha1
Expand All @@ -24,26 +25,25 @@ static:
metadataKey: myapp.com/token
size: 3
remove: false
- name: quilkin.extensions.filters.matches.v1alpha1.Matches
- name: quilkin.extensions.filters.match.v1alpha1.Match
config:
on_read:
metadataKey: myapp.com/token
branches:
- value: abc
filter: quilkin.extensions.filters.concatenate_bytes.v1alpha1.ConcatenateBytes
config:
on_read: APPEND
bytes: eHl6 # "xyz"
filter: quilkin.extensions.filters.pass.v1alpha1.Pass
fallthrough: quilkin.extensions.filters.drop.v1alpha1.Drop
# ";
# let config = quilkin::config::Config::from_reader(yaml.as_bytes()).unwrap();
# assert_eq!(config.source.get_static_filters().unwrap().len(), 1);
# quilkin::Builder::from(std::sync::Arc::new(config)).validate().unwrap();
```
<!-- ANCHOR_END: example -->

### Configuration Options ([Rust Doc](../../api/quilkin/filters/matches/struct.Config.html))
### Configuration Options ([Rust Doc](../../api/quilkin/filters/match/struct.Config.html))

```yaml
{{#include ../../../target/quilkin.extensions.filters.matches.v1alpha1.yaml}}
{{#include ../../../target/quilkin.extensions.filters.match.v1alpha1.yaml}}
```

View the [Matches](../../api/quilkin/filters/matches/struct.Config.html) filter documentation for more details.
View the [Matches](../../api/quilkin/filters/match/struct.Config.html) filter documentation for more details.
20 changes: 20 additions & 0 deletions docs/src/filters/pass.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Pass

The `Pass` filter that always passes any packet through it. This is useful in
combination with conditional flow filters like [`Matches`](./match.md)

#### Filter name
```text
quilkin.extensions.filters.pass.v1alpha1.Pass
```

### Configuration Examples
{{#include match.md:example}}

### Configuration

No defined configuration options.

### Metrics

This filter currently exports no metrics.
23 changes: 23 additions & 0 deletions proto/quilkin/extensions/filters/drop/v1alpha1/drop.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

syntax = "proto3";

package quilkin.extensions.filters.drop.v1alpha1;

message Drop {
}

Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,23 @@ import "google/protobuf/wrappers.proto";
import "google/protobuf/struct.proto";
import "google/protobuf/any.proto";

message Matches {
message Match {
message Branch {
google.protobuf.Value value = 1;
google.protobuf.StringValue filter = 2;
optional google.protobuf.Any config = 3;
Filter filter = 2;
}

message FallthroughFilter {
google.protobuf.StringValue filter = 2;
optional google.protobuf.Any config = 3;
}

message DirectionalConfig {
message Config {
google.protobuf.StringValue metadata_key = 1;
repeated Branch branches = 2;
oneof fallthrough {
google.protobuf.NullValue pass = 3;
google.protobuf.NullValue drop = 4;
FallthroughFilter filter = 5;
}
Filter fallthrough = 4;
}

message Filter {
google.protobuf.StringValue id = 1;
optional google.protobuf.Any config = 2;
}

optional DirectionalConfig on_read = 1;
optional DirectionalConfig on_write = 2;
optional Config on_read = 1;
optional Config on_write = 2;
}
25 changes: 25 additions & 0 deletions proto/quilkin/extensions/filters/pass/v1alpha1/pass.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

syntax = "proto3";

package quilkin.extensions.filters.pass.v1alpha1;

import "google/protobuf/wrappers.proto";

message Pass {
}

4 changes: 3 additions & 1 deletion src/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ pub mod capture;
pub mod compress;
pub mod concatenate_bytes;
pub mod debug;
pub mod drop;
pub mod firewall;
pub mod load_balancer;
pub mod local_rate_limit;
pub mod matches;
pub mod r#match;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(non blocking): I assume the r# is because match is a keyword?

pub mod metadata;
pub mod pass;
pub mod token_router;

/// Prelude containing all types and traits required to implement [`Filter`] and
Expand Down
98 changes: 98 additions & 0 deletions src/filters/drop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

use std::convert::TryFrom;

use crate::filters::prelude::*;
use serde::{Deserialize, Serialize};

crate::include_proto!("quilkin.extensions.filters.drop.v1alpha1");
use self::quilkin::extensions::filters::drop::v1alpha1 as proto;

/// Always drops a packet, mostly useful in combination with other filters.
struct Drop;

pub const NAME: &str = "quilkin.extensions.filters.drop.v1alpha1.Drop";

/// Creates a new factory for generating debug filters.
pub fn factory() -> DynFilterFactory {
Box::from(DropFactory::new())
}

impl Drop {
fn new() -> Self {
Self
}
}

impl Filter for Drop {
#[cfg_attr(feature = "instrument", tracing::instrument(skip(self, ctx)))]
fn read(&self, _: ReadContext) -> Option<ReadResponse> {
None
}

#[cfg_attr(feature = "instrument", tracing::instrument(skip(self, ctx)))]
fn write(&self, _: WriteContext) -> Option<WriteResponse> {
None
}
}

/// Factory for the Debug
struct DropFactory;

impl DropFactory {
pub fn new() -> Self {
Self
}
}

impl FilterFactory for DropFactory {
fn name(&self) -> &'static str {
NAME
}

fn config_schema(&self) -> schemars::schema::RootSchema {
schemars::schema_for!(Config)
}

fn create_filter(&self, args: CreateFilterArgs) -> Result<FilterInstance, Error> {
let config: Option<(_, Config)> = args
.config
.map(|config| config.deserialize::<Config, proto::Drop>(self.name()))
.transpose()?;

let (config_json, _) = config
.map(|(config_json, config)| (config_json, Some(config)))
.unwrap_or_else(|| (serde_json::Value::Null, None));

Ok(FilterInstance::new(
config_json,
Box::new(Drop::new()) as Box<dyn Filter>,
))
}
}

/// `pass` filter's configuration.
#[derive(Serialize, Deserialize, Debug, schemars::JsonSchema)]
pub struct Config;

impl TryFrom<proto::Drop> for Config {
type Error = ConvertProtoConfigError;

fn try_from(_: proto::Drop) -> Result<Self, Self::Error> {
Ok(Config)
}
}
Loading