-
Notifications
You must be signed in to change notification settings - Fork 270
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
tls: add DetectSni
middleware
#3199
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6c07804
chore: split TLS detection logic
zaharidichev d909be7
remove println
zaharidichev cfcfa0a
Revert "remove println"
zaharidichev d354cc3
Revert "chore: split TLS detection logic"
zaharidichev 22c3a9c
add independent DetectSni middleware
zaharidichev 4956e8f
Merge branch 'main' into zd/split-tls-detection
zaharidichev 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
use crate::{ | ||
server::{detect_sni, DetectIo, Timeout}, | ||
ServerName, | ||
}; | ||
use linkerd_error::Error; | ||
use linkerd_io as io; | ||
use linkerd_stack::{layer, ExtractParam, InsertParam, NewService, Service, ServiceExt}; | ||
use std::{ | ||
future::Future, | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
}; | ||
use thiserror::Error; | ||
use tokio::time; | ||
use tracing::debug; | ||
|
||
#[derive(Clone, Debug, Error)] | ||
#[error("SNI detection timed out")] | ||
pub struct SniDetectionTimeoutError; | ||
|
||
#[derive(Clone, Debug, Error)] | ||
#[error("Could not find SNI")] | ||
pub struct NoSniFoundError; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct NewDetectSni<P, N> { | ||
params: P, | ||
inner: N, | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct DetectSni<T, P, N> { | ||
target: T, | ||
inner: N, | ||
timeout: Timeout, | ||
params: P, | ||
} | ||
|
||
impl<P, N> NewDetectSni<P, N> { | ||
pub fn new(params: P, inner: N) -> Self { | ||
Self { inner, params } | ||
} | ||
|
||
pub fn layer(params: P) -> impl layer::Layer<N, Service = Self> + Clone | ||
where | ||
P: Clone, | ||
{ | ||
layer::mk(move |inner| Self::new(params.clone(), inner)) | ||
} | ||
} | ||
|
||
impl<T, P, N> NewService<T> for NewDetectSni<P, N> | ||
where | ||
P: ExtractParam<Timeout, T> + Clone, | ||
N: Clone, | ||
{ | ||
type Service = DetectSni<T, P, N>; | ||
|
||
fn new_service(&self, target: T) -> Self::Service { | ||
let timeout = self.params.extract_param(&target); | ||
DetectSni { | ||
target, | ||
timeout, | ||
inner: self.inner.clone(), | ||
params: self.params.clone(), | ||
} | ||
} | ||
} | ||
|
||
impl<T, P, I, N, S> Service<I> for DetectSni<T, P, N> | ||
where | ||
T: Clone + Send + Sync + 'static, | ||
P: InsertParam<ServerName, T> + Clone + Send + Sync + 'static, | ||
P::Target: Send + 'static, | ||
I: io::AsyncRead + io::Peek + io::AsyncWrite + Send + Sync + Unpin + 'static, | ||
N: NewService<P::Target, Service = S> + Clone + Send + 'static, | ||
S: Service<DetectIo<I>> + Send, | ||
S::Error: Into<Error>, | ||
S::Future: Send, | ||
{ | ||
type Response = S::Response; | ||
type Error = Error; | ||
type Future = Pin<Box<dyn Future<Output = Result<S::Response, Error>> + Send + 'static>>; | ||
|
||
#[inline] | ||
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
Poll::Ready(Ok(())) | ||
} | ||
|
||
fn call(&mut self, io: I) -> Self::Future { | ||
let target = self.target.clone(); | ||
let new_accept = self.inner.clone(); | ||
let params = self.params.clone(); | ||
|
||
// Detect the SNI from a ClientHello (or timeout). | ||
let Timeout(timeout) = self.timeout; | ||
let detect = time::timeout(timeout, detect_sni(io)); | ||
Box::pin(async move { | ||
let (sni, io) = detect.await.map_err(|_| SniDetectionTimeoutError)??; | ||
let sni = sni.ok_or(NoSniFoundError)?; | ||
|
||
debug!("detected SNI: {:?}", sni); | ||
let svc = new_accept.new_service(params.insert_param(sni, target)); | ||
svc.oneshot(io).await.map_err(Into::into) | ||
}) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
#![forbid(unsafe_code)] | ||
|
||
pub mod client; | ||
pub mod detect_sni; | ||
pub mod server; | ||
|
||
pub use self::{ | ||
|
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.
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'm curious about the timeout handling here and whether this should be instrumented here. On a known TLS connection, what do we do in the face of a timeout? Error? The timeout case exists on the existing detection logic so that we can forward connections as opaque after a timeout elapses, but there's no such fallback logic here, is there? I'd probably have to go back to the higher level draft PR to get a sense of how this fits together, but my gut instinct is that we do not need a timeout here.
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.
Good point. So yes, essentially we would error with a SniDetectionTimeoutError if the timeout elapses. Does that make sense here?