-
Notifications
You must be signed in to change notification settings - Fork 0
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 #15 from Lachstec/feature/clab-testing
Implement skipping of certificate validation for testing purposes
- Loading branch information
Showing
9 changed files
with
277 additions
and
107 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,30 @@ | ||
use http::{HeaderValue, Request}; | ||
use std::error::Error; | ||
use std::sync::Arc; | ||
use std::task::{Context, Poll}; | ||
use tonic::codegen::Body; | ||
use tower_service::Service; | ||
use tonic::metadata::AsciiMetadataValue; | ||
use tonic::service::Interceptor; | ||
use tonic::{Request, Status}; | ||
|
||
/// Service that injects username and password into the request metadata | ||
#[derive(Debug, Clone)] | ||
pub struct AuthService<S> { | ||
inner: S, | ||
username: Option<Arc<HeaderValue>>, | ||
password: Option<Arc<HeaderValue>>, | ||
pub struct AuthInterceptor { | ||
username: AsciiMetadataValue, | ||
password: AsciiMetadataValue, | ||
} | ||
|
||
impl<S> AuthService<S> { | ||
#[inline] | ||
pub fn new( | ||
inner: S, | ||
username: Option<Arc<HeaderValue>>, | ||
password: Option<Arc<HeaderValue>>, | ||
) -> Self { | ||
impl AuthInterceptor { | ||
pub fn new(username: Option<AsciiMetadataValue>, password: Option<AsciiMetadataValue>) -> Self { | ||
Self { | ||
inner, | ||
username, | ||
password, | ||
username: username.unwrap_or(AsciiMetadataValue::from_static("")), | ||
password: password.unwrap_or(AsciiMetadataValue::from_static("")), | ||
} | ||
} | ||
} | ||
|
||
/// Implementation of Service so that it plays nicely with tonic. | ||
/// Trait bounds have to match those specified on [`tonic::client::GrpcService`] | ||
impl<S, ReqBody, ResBody> Service<Request<ReqBody>> for AuthService<S> | ||
where | ||
S: Service<Request<ReqBody>, Response = ResBody>, | ||
S::Error:, | ||
ResBody: Body, | ||
<ResBody as Body>::Error: Into<Box<dyn Error + Send + Sync>>, | ||
{ | ||
type Response = S::Response; | ||
type Error = S::Error; | ||
type Future = S::Future; | ||
|
||
#[inline] | ||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
self.inner.poll_ready(cx) | ||
} | ||
|
||
#[inline] | ||
fn call(&mut self, mut request: Request<ReqBody>) -> Self::Future { | ||
if let Some(user) = &self.username { | ||
if let Some(pass) = &self.password { | ||
request | ||
.headers_mut() | ||
.insert("username", user.as_ref().clone()); | ||
request | ||
.headers_mut() | ||
.insert("password", pass.as_ref().clone()); | ||
} | ||
} | ||
|
||
self.inner.call(request) | ||
impl Interceptor for AuthInterceptor { | ||
fn call(&mut self, mut request: Request<()>) -> Result<Request<()>, Status> { | ||
request | ||
.metadata_mut() | ||
.insert("username", self.username.clone()); | ||
request | ||
.metadata_mut() | ||
.insert("password", self.password.clone()); | ||
Ok(request) | ||
} | ||
} |
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
Oops, something went wrong.