Skip to content

Commit be7581a

Browse files
authored
Merge pull request #153 from flashbots/peg/nested-tls-and-inner-only
Proxy server should offer both nested TLS and inner-TLS only on different ports
2 parents 4fb29e2 + a9bb332 commit be7581a

4 files changed

Lines changed: 619 additions & 263 deletions

File tree

src/attested_get.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ async fn attested_get_with_client(
5555
mod tests {
5656
use super::*;
5757
use crate::{
58-
ProxyServer,
58+
OuterTlsConfig, OuterTlsMode, ProxyServer,
5959
attestation::AttestationType,
6060
file_server::static_file_server,
6161
test_helpers::{generate_certificate_chain_for_host, generate_tls_config},
@@ -77,13 +77,19 @@ mod tests {
7777
let (server_config, client_config) = generate_tls_config(cert_chain.clone(), private_key);
7878

7979
// Setup a proxy server targetting the static file server
80-
let proxy_server = ProxyServer::new_with_tls_config(
81-
cert_chain,
82-
server_config,
83-
"127.0.0.1:0",
80+
let proxy_server = ProxyServer::new(
81+
Some(OuterTlsConfig {
82+
listen_addr: "127.0.0.1:0",
83+
tls: OuterTlsMode::Preconfigured {
84+
server_config,
85+
certificate_name: "localhost".to_string(),
86+
},
87+
}),
88+
Some("127.0.0.1:0"),
8489
target_addr.to_string(),
8590
AttestationGenerator::new(AttestationType::DcapTdx, None).unwrap(),
8691
AttestationVerifier::expect_none(),
92+
false,
8793
)
8894
.await
8995
.unwrap();

src/file_server.rs

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
11
//! Static HTTP file server provided by an attested TLS proxy server
2-
use crate::{AttestationGenerator, AttestationVerifier, ProxyError, ProxyServer, TlsCertAndKey};
2+
use crate::{
3+
AttestationGenerator, AttestationVerifier, OuterTlsConfig, OuterTlsMode, ProxyError,
4+
ProxyServer, TlsCertAndKey,
5+
};
36
use std::{net::SocketAddr, path::PathBuf};
47
use tokio::net::ToSocketAddrs;
58
use tower_http::services::ServeDir;
69

710
/// Setup a static file server serving the given directory, and a proxy server targetting it
811
pub async fn attested_file_server(
912
path_to_serve: PathBuf,
10-
cert_and_key: TlsCertAndKey,
11-
listen_addr: impl ToSocketAddrs,
13+
outer_cert_and_key: Option<TlsCertAndKey>,
14+
outer_listen_addr: Option<impl ToSocketAddrs>,
15+
inner_listen_addr: Option<impl ToSocketAddrs>,
1216
attestation_generator: AttestationGenerator,
1317
attestation_verifier: AttestationVerifier,
1418
client_auth: bool,
1519
) -> Result<(), ProxyError> {
1620
let target_addr = static_file_server(path_to_serve).await?;
21+
let outer_session = match (outer_cert_and_key, outer_listen_addr) {
22+
(Some(cert_and_key), Some(listen_addr)) => Some(OuterTlsConfig {
23+
listen_addr,
24+
tls: OuterTlsMode::CertAndKey(cert_and_key),
25+
}),
26+
(Some(_), None) | (None, Some(_)) => {
27+
return Err(ProxyError::NoListenersConfigured);
28+
}
29+
(None, None) => None,
30+
};
1731

1832
let server = ProxyServer::new(
19-
cert_and_key,
20-
listen_addr,
33+
outer_session,
34+
inner_listen_addr,
2135
target_addr.to_string(),
2236
attestation_generator,
2337
attestation_verifier,
@@ -52,7 +66,7 @@ pub(crate) async fn static_file_server(path: PathBuf) -> Result<SocketAddr, Prox
5266

5367
#[cfg(test)]
5468
mod tests {
55-
use crate::{ProxyClient, attestation::AttestationType};
69+
use crate::{OuterTlsConfig, OuterTlsMode, ProxyClient, attestation::AttestationType};
5670

5771
use super::*;
5872
use crate::test_helpers::{generate_certificate_chain_for_host, generate_tls_config};
@@ -98,13 +112,19 @@ mod tests {
98112
let (server_config, client_config) = generate_tls_config(cert_chain.clone(), private_key);
99113

100114
// Setup a proxy server targetting the static file server
101-
let proxy_server = ProxyServer::new_with_tls_config(
102-
cert_chain,
103-
server_config,
104-
"127.0.0.1:0",
115+
let proxy_server = ProxyServer::new(
116+
Some(OuterTlsConfig {
117+
listen_addr: "127.0.0.1:0",
118+
tls: OuterTlsMode::Preconfigured {
119+
server_config,
120+
certificate_name: "localhost".to_string(),
121+
},
122+
}),
123+
Some("127.0.0.1:0"),
105124
target_addr.to_string(),
106125
AttestationGenerator::new(AttestationType::DcapTdx, None).unwrap(),
107126
AttestationVerifier::expect_none(),
127+
false,
108128
)
109129
.await
110130
.unwrap();
@@ -138,27 +158,21 @@ mod tests {
138158
let client = reqwest::Client::new();
139159

140160
// This makes the request
141-
let (body, content_type) = get_body_and_content_type(
142-
format!("http://{}/foo.txt", proxy_client_addr.to_string()),
143-
&client,
144-
)
145-
.await;
161+
let (body, content_type) =
162+
get_body_and_content_type(format!("http://{}/foo.txt", proxy_client_addr), &client)
163+
.await;
146164
assert_eq!(content_type, "text/plain");
147165
assert_eq!(body, b"bar");
148166

149-
let (body, content_type) = get_body_and_content_type(
150-
format!("http://{}/index.html", proxy_client_addr.to_string()),
151-
&client,
152-
)
153-
.await;
167+
let (body, content_type) =
168+
get_body_and_content_type(format!("http://{}/index.html", proxy_client_addr), &client)
169+
.await;
154170
assert_eq!(content_type, "text/html");
155171
assert_eq!(body, b"<html><body>foo</body></html>");
156172

157-
let (body, content_type) = get_body_and_content_type(
158-
format!("http://{}/data.bin", proxy_client_addr.to_string()),
159-
&client,
160-
)
161-
.await;
173+
let (body, content_type) =
174+
get_body_and_content_type(format!("http://{}/data.bin", proxy_client_addr), &client)
175+
.await;
162176
assert_eq!(content_type, "application/octet-stream");
163177
assert_eq!(body, [0u8; 32]);
164178
}

0 commit comments

Comments
 (0)