Skip to content
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
1 change: 1 addition & 0 deletions src/attested_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ mod tests {
},
}),
Some("127.0.0.1:0"),
None,
target_addr.to_string(),
AttestationGenerator::new(AttestationType::DcapTdx, None).unwrap(),
AttestationVerifier::expect_none(),
Expand Down
46 changes: 37 additions & 9 deletions src/file_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,42 @@ use std::{net::SocketAddr, path::PathBuf};
use tokio::net::ToSocketAddrs;
use tower_http::services::ServeDir;

/// Configuration for serving a local directory over the attested proxy
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This struct is added because we went over the allowed number of function args for clippy

pub struct AttestedFileServerConfig<A> {
/// Filesystem path to expose over HTTP
pub path_to_serve: PathBuf,
/// TLS certificate and key for the optional outer listener
pub outer_cert_and_key: Option<TlsCertAndKey>,
/// Bind address for the optional outer nested-TLS listener
pub outer_listen_addr: Option<A>,
/// Bind address for the optional inner attested-TLS listener
pub inner_listen_addr: Option<A>,
/// Certificate name to embed in the inner attested certificate
pub inner_certificate_name: Option<String>,
/// Attestation generator used by the proxy server
pub attestation_generator: AttestationGenerator,
/// Attestation verifier used for the remote peer
pub attestation_verifier: AttestationVerifier,
/// Whether inner TLS should require client authentication
pub client_auth: bool,
}

/// Setup a static file server serving the given directory, and a proxy server targetting it
pub async fn attested_file_server(
path_to_serve: PathBuf,
outer_cert_and_key: Option<TlsCertAndKey>,
outer_listen_addr: Option<impl ToSocketAddrs>,
inner_listen_addr: Option<impl ToSocketAddrs>,
attestation_generator: AttestationGenerator,
attestation_verifier: AttestationVerifier,
client_auth: bool,
) -> Result<(), ProxyError> {
pub async fn attested_file_server<A>(config: AttestedFileServerConfig<A>) -> Result<(), ProxyError>
where
A: ToSocketAddrs,
{
let AttestedFileServerConfig {
path_to_serve,
outer_cert_and_key,
outer_listen_addr,
inner_listen_addr,
inner_certificate_name,
attestation_generator,
attestation_verifier,
client_auth,
} = config;

let target_addr = static_file_server(path_to_serve).await?;
let outer_session = match (outer_cert_and_key, outer_listen_addr) {
(Some(cert_and_key), Some(listen_addr)) => Some(OuterTlsConfig {
Expand All @@ -32,6 +58,7 @@ pub async fn attested_file_server(
let server = ProxyServer::new(
outer_session,
inner_listen_addr,
inner_certificate_name,
target_addr.to_string(),
attestation_generator,
attestation_verifier,
Expand Down Expand Up @@ -121,6 +148,7 @@ mod tests {
},
}),
Some("127.0.0.1:0"),
None,
target_addr.to_string(),
AttestationGenerator::new(AttestationType::DcapTdx, None).unwrap(),
AttestationVerifier::expect_none(),
Expand Down
26 changes: 26 additions & 0 deletions src/http_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub const ALPN_HTTP11: &[u8] = b"http/1.1";

type ProxyClientTlsStream =
tokio_rustls::client::TlsStream<tokio_rustls::client::TlsStream<tokio::net::TcpStream>>;
type ProxyClientInnerOnlyTlsStream = tokio_rustls::client::TlsStream<tokio::net::TcpStream>;

/// Supported HTTP versions
#[derive(Debug)]
Expand Down Expand Up @@ -60,12 +61,21 @@ type Http2Sender = hyper::client::conn::http2::SendRequest<hyper::body::Incoming

type Http1Connection =
hyper::client::conn::http1::Connection<TokioIo<ProxyClientTlsStream>, hyper::body::Incoming>;
type Http1InnerOnlyConnection = hyper::client::conn::http1::Connection<
TokioIo<ProxyClientInnerOnlyTlsStream>,
hyper::body::Incoming,
>;

type Http2Connection = hyper::client::conn::http2::Connection<
TokioIo<ProxyClientTlsStream>,
hyper::body::Incoming,
crate::TokioExecutor,
>;
type Http2InnerOnlyConnection = hyper::client::conn::http2::Connection<
TokioIo<ProxyClientInnerOnlyTlsStream>,
hyper::body::Incoming,
crate::TokioExecutor,
>;

/// A protocol version agnostic HTTP sender
pub enum HttpSender {
Expand Down Expand Up @@ -102,7 +112,9 @@ pin_project_lite::pin_project! {
#[project = HttpConnectionProj]
pub enum HttpConnection {
Http1 { #[pin] inner: Http1Connection },
Http1InnerOnly { #[pin] inner: Http1InnerOnlyConnection },
Http2 { #[pin] inner: Http2Connection },
Http2InnerOnly { #[pin] inner: Http2InnerOnlyConnection },
}
}

Expand All @@ -118,13 +130,27 @@ impl From<Http2Connection> for HttpConnection {
}
}

impl From<Http1InnerOnlyConnection> for HttpConnection {
fn from(inner: Http1InnerOnlyConnection) -> Self {
Self::Http1InnerOnly { inner }
}
}

impl From<Http2InnerOnlyConnection> for HttpConnection {
fn from(inner: Http2InnerOnlyConnection) -> Self {
Self::Http2InnerOnly { inner }
}
}

impl Future for HttpConnection {
type Output = Result<(), hyper::Error>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.project() {
HttpConnectionProj::Http1 { inner } => inner.poll(cx),
HttpConnectionProj::Http1InnerOnly { inner } => inner.poll(cx),
HttpConnectionProj::Http2 { inner } => inner.poll(cx),
HttpConnectionProj::Http2InnerOnly { inner } => inner.poll(cx),
}
}
}
Loading
Loading