|
1 | 1 | //! 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 | +}; |
3 | 6 | use std::{net::SocketAddr, path::PathBuf}; |
4 | 7 | use tokio::net::ToSocketAddrs; |
5 | 8 | use tower_http::services::ServeDir; |
6 | 9 |
|
7 | 10 | /// Setup a static file server serving the given directory, and a proxy server targetting it |
8 | 11 | pub async fn attested_file_server( |
9 | 12 | 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>, |
12 | 16 | attestation_generator: AttestationGenerator, |
13 | 17 | attestation_verifier: AttestationVerifier, |
14 | 18 | client_auth: bool, |
15 | 19 | ) -> Result<(), ProxyError> { |
16 | 20 | 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 | + }; |
17 | 31 |
|
18 | 32 | let server = ProxyServer::new( |
19 | | - cert_and_key, |
20 | | - listen_addr, |
| 33 | + outer_session, |
| 34 | + inner_listen_addr, |
21 | 35 | target_addr.to_string(), |
22 | 36 | attestation_generator, |
23 | 37 | attestation_verifier, |
@@ -52,7 +66,7 @@ pub(crate) async fn static_file_server(path: PathBuf) -> Result<SocketAddr, Prox |
52 | 66 |
|
53 | 67 | #[cfg(test)] |
54 | 68 | mod tests { |
55 | | - use crate::{ProxyClient, attestation::AttestationType}; |
| 69 | + use crate::{OuterTlsConfig, OuterTlsMode, ProxyClient, attestation::AttestationType}; |
56 | 70 |
|
57 | 71 | use super::*; |
58 | 72 | use crate::test_helpers::{generate_certificate_chain_for_host, generate_tls_config}; |
@@ -98,13 +112,19 @@ mod tests { |
98 | 112 | let (server_config, client_config) = generate_tls_config(cert_chain.clone(), private_key); |
99 | 113 |
|
100 | 114 | // 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"), |
105 | 124 | target_addr.to_string(), |
106 | 125 | AttestationGenerator::new(AttestationType::DcapTdx, None).unwrap(), |
107 | 126 | AttestationVerifier::expect_none(), |
| 127 | + false, |
108 | 128 | ) |
109 | 129 | .await |
110 | 130 | .unwrap(); |
@@ -138,27 +158,21 @@ mod tests { |
138 | 158 | let client = reqwest::Client::new(); |
139 | 159 |
|
140 | 160 | // 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; |
146 | 164 | assert_eq!(content_type, "text/plain"); |
147 | 165 | assert_eq!(body, b"bar"); |
148 | 166 |
|
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; |
154 | 170 | assert_eq!(content_type, "text/html"); |
155 | 171 | assert_eq!(body, b"<html><body>foo</body></html>"); |
156 | 172 |
|
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; |
162 | 176 | assert_eq!(content_type, "application/octet-stream"); |
163 | 177 | assert_eq!(body, [0u8; 32]); |
164 | 178 | } |
|
0 commit comments