-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from Kuadrant/authservice
Add initial implementation of auth service
- Loading branch information
Showing
9 changed files
with
241 additions
and
112 deletions.
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
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
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 |
---|---|---|
@@ -1,8 +1,124 @@ | ||
pub(crate) mod auth; | ||
pub(crate) mod rate_limit; | ||
|
||
use crate::configuration::ExtensionType; | ||
use crate::service::auth::{AUTH_METHOD_NAME, AUTH_SERVICE_NAME}; | ||
use crate::service::rate_limit::{RATELIMIT_METHOD_NAME, RATELIMIT_SERVICE_NAME}; | ||
use crate::service::TracingHeader::{Baggage, Traceparent, Tracestate}; | ||
use protobuf::Message; | ||
use proxy_wasm::types::Status; | ||
use proxy_wasm::hostcalls; | ||
use proxy_wasm::hostcalls::dispatch_grpc_call; | ||
use proxy_wasm::types::{Bytes, MapType, Status}; | ||
use std::cell::OnceCell; | ||
use std::rc::Rc; | ||
use std::time::Duration; | ||
|
||
pub trait Service<M: Message> { | ||
fn send(&self, message: M) -> Result<u32, Status>; | ||
pub struct GrpcServiceHandler { | ||
endpoint: String, | ||
service_name: String, | ||
method_name: String, | ||
header_resolver: Rc<HeaderResolver>, | ||
} | ||
|
||
impl GrpcServiceHandler { | ||
fn build( | ||
endpoint: String, | ||
service_name: &str, | ||
method_name: &str, | ||
header_resolver: Rc<HeaderResolver>, | ||
) -> Self { | ||
Self { | ||
endpoint: endpoint.to_owned(), | ||
service_name: service_name.to_owned(), | ||
method_name: method_name.to_owned(), | ||
header_resolver, | ||
} | ||
} | ||
|
||
pub fn new( | ||
extension_type: ExtensionType, | ||
endpoint: String, | ||
header_resolver: Rc<HeaderResolver>, | ||
) -> Self { | ||
match extension_type { | ||
ExtensionType::Auth => Self::build( | ||
endpoint, | ||
AUTH_SERVICE_NAME, | ||
AUTH_METHOD_NAME, | ||
header_resolver, | ||
), | ||
ExtensionType::RateLimit => Self::build( | ||
endpoint, | ||
RATELIMIT_SERVICE_NAME, | ||
RATELIMIT_METHOD_NAME, | ||
header_resolver, | ||
), | ||
} | ||
} | ||
|
||
pub fn send<M: Message>(&self, message: M) -> Result<u32, Status> { | ||
let msg = Message::write_to_bytes(&message).unwrap(); | ||
let metadata = self | ||
.header_resolver | ||
.get() | ||
.iter() | ||
.map(|(header, value)| (*header, value.as_slice())) | ||
.collect(); | ||
|
||
dispatch_grpc_call( | ||
self.endpoint.as_str(), | ||
self.service_name.as_str(), | ||
self.method_name.as_str(), | ||
metadata, | ||
Some(&msg), | ||
Duration::from_secs(5), | ||
) | ||
} | ||
} | ||
|
||
pub struct HeaderResolver { | ||
headers: OnceCell<Vec<(&'static str, Bytes)>>, | ||
} | ||
|
||
impl HeaderResolver { | ||
pub fn new() -> Self { | ||
Self { | ||
headers: OnceCell::new(), | ||
} | ||
} | ||
|
||
pub fn get(&self) -> &Vec<(&'static str, Bytes)> { | ||
self.headers.get_or_init(|| { | ||
let mut headers = Vec::new(); | ||
for header in TracingHeader::all() { | ||
if let Ok(Some(value)) = | ||
hostcalls::get_map_value_bytes(MapType::HttpRequestHeaders, (*header).as_str()) | ||
{ | ||
headers.push(((*header).as_str(), value)); | ||
} | ||
} | ||
headers | ||
}) | ||
} | ||
} | ||
|
||
// tracing headers | ||
pub enum TracingHeader { | ||
Traceparent, | ||
Tracestate, | ||
Baggage, | ||
} | ||
|
||
impl TracingHeader { | ||
fn all() -> &'static [Self; 3] { | ||
&[Traceparent, Tracestate, Baggage] | ||
} | ||
|
||
pub fn as_str(&self) -> &'static str { | ||
match self { | ||
Traceparent => "traceparent", | ||
Tracestate => "tracestate", | ||
Baggage => "baggage", | ||
} | ||
} | ||
} |
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,81 @@ | ||
use crate::attribute::get_attribute; | ||
use crate::envoy::{ | ||
Address, AttributeContext, AttributeContext_HttpRequest, AttributeContext_Peer, | ||
AttributeContext_Request, CheckRequest, Metadata, SocketAddress, | ||
}; | ||
use chrono::{DateTime, FixedOffset, Timelike}; | ||
use protobuf::well_known_types::Timestamp; | ||
use proxy_wasm::hostcalls; | ||
use proxy_wasm::types::MapType; | ||
use std::collections::HashMap; | ||
|
||
pub const AUTH_SERVICE_NAME: &str = "envoy.service.auth.v3.Authorization"; | ||
pub const AUTH_METHOD_NAME: &str = "Check"; | ||
|
||
pub struct AuthService; | ||
|
||
#[allow(dead_code)] | ||
impl AuthService { | ||
pub fn message(ce_host: String) -> CheckRequest { | ||
AuthService::build_check_req(ce_host) | ||
} | ||
|
||
fn build_check_req(ce_host: String) -> CheckRequest { | ||
let mut auth_req = CheckRequest::default(); | ||
let mut attr = AttributeContext::default(); | ||
attr.set_request(AuthService::build_request()); | ||
attr.set_destination(AuthService::build_peer( | ||
get_attribute::<String>("destination.address").unwrap_or_default(), | ||
get_attribute::<i64>("destination.port").unwrap_or_default() as u32, | ||
)); | ||
attr.set_source(AuthService::build_peer( | ||
get_attribute::<String>("source.address").unwrap_or_default(), | ||
get_attribute::<i64>("source.port").unwrap_or_default() as u32, | ||
)); | ||
// the ce_host is the identifier for authorino to determine which authconfig to use | ||
let context_extensions = HashMap::from([("host".to_string(), ce_host)]); | ||
attr.set_context_extensions(context_extensions); | ||
attr.set_metadata_context(Metadata::default()); | ||
auth_req.set_attributes(attr); | ||
auth_req | ||
} | ||
|
||
fn build_request() -> AttributeContext_Request { | ||
let mut request = AttributeContext_Request::default(); | ||
let mut http = AttributeContext_HttpRequest::default(); | ||
let headers: HashMap<String, String> = hostcalls::get_map(MapType::HttpRequestHeaders) | ||
.unwrap() | ||
.into_iter() | ||
.collect(); | ||
|
||
http.set_host(get_attribute::<String>("request.host").unwrap_or_default()); | ||
http.set_method(get_attribute::<String>("request.method").unwrap_or_default()); | ||
http.set_scheme(get_attribute::<String>("request.scheme").unwrap_or_default()); | ||
http.set_path(get_attribute::<String>("request.path").unwrap_or_default()); | ||
http.set_protocol(get_attribute::<String>("request.protocol").unwrap_or_default()); | ||
|
||
http.set_headers(headers); | ||
request.set_time(get_attribute("request.time").map_or( | ||
Timestamp::new(), | ||
|date_time: DateTime<FixedOffset>| Timestamp { | ||
nanos: date_time.nanosecond() as i32, | ||
seconds: date_time.second() as i64, | ||
unknown_fields: Default::default(), | ||
cached_size: Default::default(), | ||
}, | ||
)); | ||
request.set_http(http); | ||
request | ||
} | ||
|
||
fn build_peer(host: String, port: u32) -> AttributeContext_Peer { | ||
let mut peer = AttributeContext_Peer::default(); | ||
let mut address = Address::default(); | ||
let mut socket_address = SocketAddress::default(); | ||
socket_address.set_address(host); | ||
socket_address.set_port_value(port); | ||
address.set_socket_address(socket_address); | ||
peer.set_address(address); | ||
peer | ||
} | ||
} |
Oops, something went wrong.