-
Notifications
You must be signed in to change notification settings - Fork 5
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 for external auth. Vol II #66
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ mod filter; | |
mod glob; | ||
mod policy; | ||
mod policy_index; | ||
mod service; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
pub(crate) mod rate_limit; | ||
|
||
use protobuf::Message; | ||
use proxy_wasm::types::Status; | ||
|
||
pub trait Service<M: Message> { | ||
fn send(&self, message: M) -> Result<u32, Status>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
use crate::envoy::{RateLimitDescriptor, RateLimitRequest}; | ||
use crate::filter::http_context::TracingHeader; | ||
use crate::service::Service; | ||
use protobuf::{Message, RepeatedField}; | ||
use proxy_wasm::hostcalls::dispatch_grpc_call; | ||
use proxy_wasm::types::{Bytes, Status}; | ||
use std::time::Duration; | ||
|
||
const RATELIMIT_SERVICE_NAME: &str = "envoy.service.ratelimit.v3.RateLimitService"; | ||
const RATELIMIT_METHOD_NAME: &str = "ShouldRateLimit"; | ||
pub struct RateLimitService { | ||
endpoint: String, | ||
tracing_headers: Vec<(TracingHeader, Bytes)>, | ||
} | ||
|
||
impl RateLimitService { | ||
pub fn new(endpoint: &str, metadata: Vec<(TracingHeader, Bytes)>) -> RateLimitService { | ||
Self { | ||
endpoint: String::from(endpoint), | ||
tracing_headers: metadata, | ||
} | ||
} | ||
pub fn message( | ||
domain: String, | ||
descriptors: RepeatedField<RateLimitDescriptor>, | ||
) -> RateLimitRequest { | ||
RateLimitRequest { | ||
domain, | ||
descriptors, | ||
hits_addend: 1, | ||
unknown_fields: Default::default(), | ||
cached_size: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
fn grpc_call( | ||
upstream_name: &str, | ||
initial_metadata: Vec<(&str, &[u8])>, | ||
message: RateLimitRequest, | ||
) -> Result<u32, Status> { | ||
let msg = Message::write_to_bytes(&message).unwrap(); // TODO(didierofrivia): Error Handling | ||
dispatch_grpc_call( | ||
upstream_name, | ||
RATELIMIT_SERVICE_NAME, | ||
RATELIMIT_METHOD_NAME, | ||
initial_metadata, | ||
Some(&msg), | ||
Duration::from_secs(5), | ||
) | ||
} | ||
|
||
impl Service<RateLimitRequest> for RateLimitService { | ||
fn send(&self, message: RateLimitRequest) -> Result<u32, Status> { | ||
grpc_call( | ||
self.endpoint.as_str(), | ||
self.tracing_headers | ||
.iter() | ||
.map(|(header, value)| (header.as_str(), value.as_slice())) | ||
.collect(), | ||
message, | ||
) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::envoy::{RateLimitDescriptor, RateLimitDescriptor_Entry, RateLimitRequest}; | ||
use crate::service::rate_limit::RateLimitService; | ||
//use crate::service::Service; | ||
use protobuf::{CachedSize, RepeatedField, UnknownFields}; | ||
//use proxy_wasm::types::Status; | ||
//use crate::filter::http_context::{Filter}; | ||
|
||
fn build_message() -> RateLimitRequest { | ||
let domain = "rlp1"; | ||
let mut field = RateLimitDescriptor::new(); | ||
let mut entry = RateLimitDescriptor_Entry::new(); | ||
entry.set_key("key1".to_string()); | ||
entry.set_value("value1".to_string()); | ||
field.set_entries(RepeatedField::from_vec(vec![entry])); | ||
let descriptors = RepeatedField::from_vec(vec![field]); | ||
|
||
RateLimitService::message(domain.to_string(), descriptors.clone()) | ||
} | ||
#[test] | ||
fn builds_correct_message() { | ||
let msg = build_message(); | ||
|
||
assert_eq!(msg.hits_addend, 1); | ||
assert_eq!(msg.domain, "rlp1".to_string()); | ||
assert_eq!(msg.descriptors.first().unwrap().entries[0].key, "key1"); | ||
assert_eq!(msg.descriptors.first().unwrap().entries[0].value, "value1"); | ||
assert_eq!(msg.unknown_fields, UnknownFields::default()); | ||
assert_eq!(msg.cached_size, CachedSize::default()); | ||
} | ||
/*#[test] | ||
fn sends_message() { | ||
let msg = build_message(); | ||
let metadata = vec![("header-1", "value-1".as_bytes())]; | ||
let rls = RateLimitService::new("limitador-cluster", metadata); | ||
|
||
// TODO(didierofrivia): When we have a grpc response type, assert the async response | ||
} | ||
|
||
fn grpc_call( | ||
_upstream_name: &str, | ||
_initial_metadata: Vec<(&str, &[u8])>, | ||
_message: RateLimitRequest, | ||
) -> Result<u32, Status> { | ||
Ok(1) | ||
} */ | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this should be
self.metadata
andinitial_metadata
disappears from that method signature, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've made the
grpc_call
outside theimpl
block so we can mock it in the tests, that's why it doesn't take aself
param. Should it be include it within theRateLimitService
impl?