oxedyne/fe2o3/fe2o3_steel/tests/vhost_rig/mod.rs
17.0 KiB, 22 runs
created by r1870400018:65949, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! A TLS front for `handle_https` holding one or more vhosts, for tests that ask what a vhost |
| 2 | //! answers on the wire rather than what one of its functions returns. |
| 3 | //! |
| 4 | //! One request is one fresh TLS connection into the real dispatcher, so what comes back has passed |
| 5 | //! through everything a visitor's request does: the SNI lookup, the redirect and proxy tables, the |
| 6 | //! handler and the response headers added last. |
| 7 | //! |
| 8 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 9 | //! Anthropic Claude |
| 10 | |
| 11 | #![allow(dead_code)] |
| 12 | |
| 13 | use oxedyne_fe2o3_steel::{ |
| 14 | app::https::AppWebHandler, |
| 15 | srv::{ |
| 16 | admin::{ |
| 17 | guard, |
| 18 | host_sampler::HostSampler, |
| 19 | state::AdminState, |
| 20 | traffic::TrafficRecorder, |
| 21 | }, |
| 22 | api::ApiHandlerRegistry, |
| 23 | cfg::{ |
| 24 | ApiRoute, |
| 25 | PathHeaders, |
| 26 | ServerConfig, |
| 27 | WebhookRoute, |
| 28 | }, |
| 29 | context::{ |
| 30 | Protocol, |
| 31 | ServerContext, |
| 32 | VhostRuntime, |
| 33 | }, |
| 34 | id, |
| 35 | webhook::WebhookRegistry, |
| 36 | ws::{ |
| 37 | handler::AppWebSocketHandler, |
| 38 | syntax::WebSocketSyntax, |
| 39 | }, |
| 40 | }, |
| 41 | }; |
| 42 | |
| 43 | use oxedyne_fe2o3_core::{ |
| 44 | file::OsPath, |
| 45 | path::NormalPath, |
| 46 | prelude::*, |
| 47 | }; |
| 48 | use oxedyne_fe2o3_crypto::{ |
| 49 | enc::EncryptionScheme, |
| 50 | keystore::Wallet, |
| 51 | }; |
| 52 | use oxedyne_fe2o3_hash::{ |
| 53 | csum::ChecksumScheme, |
| 54 | hash::HashScheme, |
| 55 | }; |
| 56 | use oxedyne_fe2o3_jdat::version::SemVer; |
| 57 | use oxedyne_fe2o3_o3db_sync::O3db; |
| 58 | |
| 59 | use std::{ |
| 60 | collections::HashMap, |
| 61 | path::{ |
| 62 | Path, |
| 63 | PathBuf, |
| 64 | }, |
| 65 | sync::{ |
| 66 | atomic::{ |
| 67 | AtomicU64, |
| 68 | Ordering, |
| 69 | }, |
| 70 | Arc, |
| 71 | RwLock, |
| 72 | }, |
| 73 | }; |
| 74 | |
| 75 | use tokio::{ |
| 76 | io::{ |
| 77 | AsyncReadExt, |
| 78 | AsyncWriteExt, |
| 79 | }, |
| 80 | net::{ |
| 81 | TcpListener, |
| 82 | TcpStream, |
| 83 | }, |
| 84 | }; |
| 85 | use tokio_rustls::{ |
| 86 | rustls::{ |
| 87 | pki_types::{ |
| 88 | CertificateDer, |
| 89 | PrivateKeyDer, |
| 90 | PrivatePkcs8KeyDer, |
| 91 | ServerName, |
| 92 | }, |
| 93 | ClientConfig, |
| 94 | RootCertStore, |
| 95 | ServerConfig as TlsServerConfig, |
| 96 | }, |
| 97 | TlsAcceptor, |
| 98 | TlsConnector, |
| 99 | }; |
| 100 | |
| 101 | type Db = O3db< |
| 102 | { id::UID_LEN }, |
| 103 | id::Uid, |
| 104 | EncryptionScheme, |
| 105 | HashScheme, |
| 106 | HashScheme, |
| 107 | ChecksumScheme, |
| 108 | >; |
| 109 | |
| 110 | type Handler = AppWebHandler<HashMap<String, OsPath>>; |
| 111 | |
| 112 | type Ctx = ServerContext< |
| 113 | { id::UID_LEN }, |
| 114 | id::Uid, |
| 115 | EncryptionScheme, |
| 116 | HashScheme, |
| 117 | Db, |
| 118 | Handler, |
| 119 | AppWebSocketHandler, |
| 120 | >; |
| 121 | |
| 122 | static DIR_SEQ: AtomicU64 = AtomicU64::new(0); |
| 123 | |
| 124 | /// A directory that removes itself. |
| 125 | pub struct Scratch(pub PathBuf); |
| 126 | |
| 127 | impl Scratch { |
| 128 | pub fn new(label: &str) -> Outcome<Self> { |
| 129 | let seq = DIR_SEQ.fetch_add(1, Ordering::Relaxed); |
| 130 | let dir = std::env::temp_dir() |
| 131 | .join(fmt!("steel-{}-{}-{}", label, std::process::id(), seq)); |
| 132 | let _ = std::fs::remove_dir_all(&dir); |
| 133 | res!(std::fs::create_dir_all(&dir), IO, File); |
| 134 | Ok(Self(dir)) |
| 135 | } |
| 136 | |
| 137 | /// Writes a file under the directory, making the directories above it. |
| 138 | pub fn put(&self, rel: &str, text: &str) -> Outcome<()> { |
| 139 | let path = self.0.join(rel); |
| 140 | if let Some(parent) = path.parent() { |
| 141 | res!(std::fs::create_dir_all(parent), IO, File); |
| 142 | } |
| 143 | res!(std::fs::write(&path, text), IO, File); |
| 144 | Ok(()) |
| 145 | } |
| 146 | |
| 147 | pub fn dir(&self, rel: &str) -> Outcome<()> { |
| 148 | res!(std::fs::create_dir_all(self.0.join(rel)), IO, File); |
| 149 | Ok(()) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | impl Drop for Scratch { |
| 154 | fn drop(&mut self) { |
| 155 | let _ = std::fs::remove_dir_all(&self.0); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /// What one vhost of the rig is made of. |
| 160 | pub struct Site { |
| 161 | pub host: String, |
| 162 | pub public_dir: PathBuf, |
| 163 | pub api_routes: Vec<ApiRoute>, |
| 164 | pub webhook_routes: Vec<WebhookRoute>, |
| 165 | pub admin_dashboard: bool, |
| 166 | pub response_headers: Vec<(String, String)>, |
| 167 | pub path_headers: Vec<PathHeaders>, |
| 168 | pub handlers: ApiHandlerRegistry, // in-process handlers its `api_routes` name |
| 169 | } |
| 170 | |
| 171 | impl Site { |
| 172 | pub fn new(host: &str, public_dir: &Path) -> Self { |
| 173 | Self { |
| 174 | host: host.to_string(), |
| 175 | public_dir: public_dir.to_path_buf(), |
| 176 | api_routes: Vec::new(), |
| 177 | webhook_routes: Vec::new(), |
| 178 | admin_dashboard: true, |
| 179 | response_headers: Vec::new(), |
| 180 | path_headers: Vec::new(), |
| 181 | handlers: ApiHandlerRegistry::new(), |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /// One whole response: the head with its names lower-cased, and the body. |
| 187 | #[derive(Clone, Debug)] |
| 188 | pub struct Reply { |
| 189 | pub head: String, |
| 190 | pub body: String, |
| 191 | } |
| 192 | |
| 193 | impl Reply { |
| 194 | pub fn status(&self) -> u16 { |
| 195 | self.head.split_whitespace().nth(1).and_then(|c| c.parse().ok()).unwrap_or(0) |
| 196 | } |
| 197 | |
| 198 | /// The first field of this name, if the response carries one. |
| 199 | pub fn field(&self, name: &str) -> Option<String> { |
| 200 | let want = fmt!("{}:", name.to_lowercase()); |
| 201 | self.head.lines() |
| 202 | .skip(1) |
| 203 | .find_map(|l| l.to_lowercase().starts_with(&want).then(|| { |
| 204 | l[want.len()..].trim().to_string() |
| 205 | })) |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | pub struct Rig { |
| 210 | context: Ctx, |
| 211 | server_tls: Arc<TlsServerConfig>, |
| 212 | client_tls: Arc<ClientConfig>, |
| 213 | } |
| 214 | |
| 215 | impl Rig { |
| 216 | pub fn new(sites: Vec<Site>) -> Outcome<Self> { |
| 217 | Self::with_config(sites, ServerConfig::default()) |
| 218 | } |
| 219 | |
| 220 | /// As `new`, with a server config of the caller's choosing (small limits, short deadlines). |
| 221 | pub fn with_config(sites: Vec<Site>, cfg: ServerConfig) -> Outcome<Self> { |
| 222 | oxedyne_fe2o3_net::tls::ensure_crypto_provider(); |
| 223 | let names: Vec<String> = sites.iter().map(|s| s.host.clone()).collect(); |
| 224 | let cert = res!(rcgen::generate_simple_self_signed(names), Init); |
| 225 | let der = res!(cert.serialize_der(), Init); |
| 226 | let key = PrivateKeyDer::Pkcs8(PrivatePkcs8KeyDer::from(cert.serialize_private_key_der())); |
| 227 | let server_tls = res!(TlsServerConfig::builder() |
| 228 | .with_no_client_auth() |
| 229 | .with_single_cert(vec![CertificateDer::from(der.clone())], key), Init); |
| 230 | let mut roots = RootCertStore::empty(); |
| 231 | res!(roots.add(CertificateDer::from(der)), Init); |
| 232 | let client_tls = ClientConfig::builder().with_root_certificates(roots).with_no_client_auth(); |
| 233 | |
| 234 | // A real dashboard state, so that a vhost which serves `/admin` has something to serve: |
| 235 | // without one every vhost answers 404 there, whatever it is set to, and a test of the |
| 236 | // setting could not tell one answer from the other. |
| 237 | let admin = res!(AdminState::new( |
| 238 | Arc::new(RwLock::new(Wallet::default())), |
| 239 | PathBuf::from("./wallet.jdat"), |
| 240 | Some([0u8; 32].to_vec()), |
| 241 | 0, |
| 242 | None, |
| 243 | TrafficRecorder::new_shared(0), |
| 244 | HostSampler::new_shared(), |
| 245 | res!(guard::new_shared()), |
| 246 | res!(guard::new_auth_shared()), |
| 247 | Vec::new(), |
| 248 | None, |
| 249 | )); |
| 250 | let admin = Arc::new(admin); |
| 251 | |
| 252 | let mut vhosts = HashMap::new(); |
| 253 | let mut first = String::new(); |
| 254 | for site in sites { |
| 255 | if first.is_empty() { |
| 256 | first = site.host.clone(); |
| 257 | } |
| 258 | let web: Handler = AppWebHandler::new( |
| 259 | cfg.clone(), |
| 260 | site.public_dir.clone(), |
| 261 | HashMap::new(), |
| 262 | vec![fmt!("index.html")], |
| 263 | false, |
| 264 | site.api_routes.clone(), |
| 265 | site.webhook_routes.clone(), |
| 266 | Arc::new(WebhookRegistry::new()), |
| 267 | Arc::new(site.handlers), |
| 268 | None, |
| 269 | Some(admin.clone()), |
| 270 | None, |
| 271 | None, |
| 272 | None, |
| 273 | Arc::new(Vec::new()), |
| 274 | ).with_admin_dashboard(site.admin_dashboard); |
| 275 | let runtime = Arc::new(VhostRuntime { |
| 276 | hostnames: vec![site.host.clone()], |
| 277 | web_handler: web, |
| 278 | ws_handler: AppWebSocketHandler::new(None), |
| 279 | ws_syntax: res!(WebSocketSyntax::new( |
| 280 | "steel_ws", &SemVer::new(0, 1, 0), "Vhost rig")), |
| 281 | redirects: Vec::new(), |
| 282 | proxy_routes: Vec::new(), |
| 283 | ws_routes: Vec::new(), |
| 284 | term_manager: None, |
| 285 | uses_sessions: false, |
| 286 | permissions_policy: None, |
| 287 | tiles: None, |
| 288 | access_log: false, |
| 289 | response_headers: site.response_headers.iter() |
| 290 | .map(|(k, v)| (oxedyne_fe2o3_net::http::fields::HeaderName::from(k.as_str()), v.clone())) |
| 291 | .collect(), |
| 292 | path_headers: site.path_headers.clone(), |
| 293 | }); |
| 294 | vhosts.insert(site.host.to_lowercase(), runtime); |
| 295 | } |
| 296 | let protocol = Protocol::Web { |
| 297 | vhosts: Arc::new(vhosts), |
| 298 | default_vhost: first, |
| 299 | dev_mode: false, |
| 300 | }; |
| 301 | let root = std::env::temp_dir().normalise().absolute(); |
| 302 | let dbs: Arc<RwLock<HashMap<String, (Arc<RwLock<Db>>, id::Uid)>>> = |
| 303 | Arc::new(RwLock::new(HashMap::new())); |
| 304 | let context = ServerContext::new(cfg, root, dbs, Vec::new(), protocol, None, |
| 305 | Some(admin)); |
| 306 | Ok(Self { |
| 307 | context, |
| 308 | server_tls: Arc::new(server_tls), |
| 309 | client_tls: Arc::new(client_tls), |
| 310 | }) |
| 311 | } |
| 312 | |
| 313 | /// One request over a fresh TLS connection to the named vhost. `fields` are extra request |
| 314 | /// header lines, `Name: value`. |
| 315 | pub async fn fetch( |
| 316 | &self, |
| 317 | host: &str, |
| 318 | method: &str, |
| 319 | path: &str, |
| 320 | fields: &[&str], |
| 321 | body: &str, |
| 322 | ) |
| 323 | -> Outcome<Reply> |
| 324 | { |
| 325 | self.fetch_as(host, "HTTP/1.1", method, path, fields, body).await |
| 326 | } |
| 327 | |
| 328 | /// As [`Rig::fetch`], naming the HTTP version the request line carries. |
| 329 | pub async fn fetch_as( |
| 330 | &self, |
| 331 | host: &str, |
| 332 | version: &str, |
| 333 | method: &str, |
| 334 | path: &str, |
| 335 | fields: &[&str], |
| 336 | body: &str, |
| 337 | ) |
| 338 | -> Outcome<Reply> |
| 339 | { |
| 340 | let listener = res!(TcpListener::bind("127.0.0.1:0").await, Network, Init); |
| 341 | let addr = res!(listener.local_addr(), Network, Init); |
| 342 | let ctx = self.context.clone(); |
| 343 | let acceptor = TlsAcceptor::from(self.server_tls.clone()); |
| 344 | let host_owned = host.to_string(); |
| 345 | let server = tokio::spawn(async move { |
| 346 | let (tcp, peer) = match listener.accept().await { |
| 347 | Ok(c) => c, |
| 348 | Err(e) => return Err(err!(e, "Accept failed."; Network)), |
| 349 | }; |
| 350 | let stream = match acceptor.accept(tcp).await { |
| 351 | Ok(s) => s, |
| 352 | Err(e) => return Err(err!(e, "TLS accept failed."; Network)), |
| 353 | }; |
| 354 | ctx.handle_https(stream, Some(host_owned), peer, false).await |
| 355 | }); |
| 356 | let tcp = res!(TcpStream::connect(addr).await, Network, Init); |
| 357 | let name = res!(ServerName::try_from(host.to_string()), Invalid); |
| 358 | let mut stream = res!(TlsConnector::from(self.client_tls.clone()).connect(name, tcp).await, |
| 359 | Network); |
| 360 | let mut request = fmt!("{} {} {}\r\nHost: {}\r\n", method, path, version, host); |
| 361 | for f in fields { |
| 362 | request.push_str(f); |
| 363 | request.push_str("\r\n"); |
| 364 | } |
| 365 | request.push_str(&fmt!("Content-Length: {}\r\nConnection: close\r\n\r\n{}", |
| 366 | body.len(), body)); |
| 367 | res!(stream.write_all(request.as_bytes()).await, Network, Write); |
| 368 | res!(stream.flush().await, Network, Write); |
| 369 | // One whole response, framed by its Content-Length and no further: Steel holds a |
| 370 | // connection open for the next request, so reading to the end would wait out its |
| 371 | // header-read timeout on every answer. A response with no Content-Length ends at its head. |
| 372 | let mut reply = Vec::new(); |
| 373 | let mut chunk = [0u8; 4096]; |
| 374 | loop { |
| 375 | if let Some(i) = reply.windows(4).position(|w| w == b"\r\n\r\n") { |
| 376 | let head = String::from_utf8_lossy(&reply[..i]).to_lowercase(); |
| 377 | // A HEAD answer states the length of a body it does not send. |
| 378 | let want = match method == "HEAD" { |
| 379 | true => 0, |
| 380 | false => head.lines() |
| 381 | .filter_map(|l| l.strip_prefix("content-length:")) |
| 382 | .filter_map(|v| v.trim().parse::<usize>().ok()) |
| 383 | .next() |
| 384 | .unwrap_or(0), |
| 385 | }; |
| 386 | if reply.len() >= i + 4 + want { |
| 387 | break; |
| 388 | } |
| 389 | } |
| 390 | match stream.read(&mut chunk).await { |
| 391 | Ok(0) | Err(_) => break, |
| 392 | Ok(n) => reply.extend_from_slice(&chunk[..n]), |
| 393 | } |
| 394 | } |
| 395 | drop(stream); |
| 396 | let _ = server.await; |
| 397 | let split = match reply.windows(4).position(|w| w == b"\r\n\r\n") { |
| 398 | Some(i) => i, |
| 399 | None => return Err(err!("{} {} on {} answered with no complete head: {:?}.", |
| 400 | method, path, host, String::from_utf8_lossy(&reply); Test)), |
| 401 | }; |
| 402 | Ok(Reply { |
| 403 | head: String::from_utf8_lossy(&reply[..split]).to_string(), |
| 404 | body: String::from_utf8_lossy(&reply[split + 4..]).to_string(), |
| 405 | }) |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | /// One request, written exactly as given, with what the server did to the connection afterwards. |
| 410 | #[derive(Clone, Debug)] |
| 411 | pub struct Exchange { |
| 412 | pub reply: Reply, |
| 413 | pub closed: bool, // the server ended the connection within the patience given |
| 414 | } |
| 415 | |
| 416 | impl Rig { |
| 417 | |
| 418 | /// A request as raw text, with nothing added (no `Connection` field, no `Content-Length`), over |
| 419 | /// a fresh TLS connection to the named vhost. After one whole response is read, the connection |
| 420 | /// is watched for `patience`: `closed` says whether the server ended it in that time. |
| 421 | pub async fn exchange( |
| 422 | &self, |
| 423 | host: &str, |
| 424 | raw: &str, |
| 425 | patience: std::time::Duration, |
| 426 | ) |
| 427 | -> Outcome<Exchange> |
| 428 | { |
| 429 | let listener = res!(TcpListener::bind("127.0.0.1:0").await, Network, Init); |
| 430 | let addr = res!(listener.local_addr(), Network, Init); |
| 431 | let ctx = self.context.clone(); |
| 432 | let acceptor = TlsAcceptor::from(self.server_tls.clone()); |
| 433 | let host_owned = host.to_string(); |
| 434 | let server = tokio::spawn(async move { |
| 435 | let (tcp, peer) = match listener.accept().await { |
| 436 | Ok(c) => c, |
| 437 | Err(e) => return Err(err!(e, "Accept failed."; Network)), |
| 438 | }; |
| 439 | let stream = match acceptor.accept(tcp).await { |
| 440 | Ok(s) => s, |
| 441 | Err(e) => return Err(err!(e, "TLS accept failed."; Network)), |
| 442 | }; |
| 443 | ctx.handle_https(stream, Some(host_owned), peer, false).await |
| 444 | }); |
| 445 | let tcp = res!(TcpStream::connect(addr).await, Network, Init); |
| 446 | let name = res!(ServerName::try_from(host.to_string()), Invalid); |
| 447 | let mut stream = res!(TlsConnector::from(self.client_tls.clone()).connect(name, tcp).await, |
| 448 | Network); |
| 449 | res!(stream.write_all(raw.as_bytes()).await, Network, Write); |
| 450 | res!(stream.flush().await, Network, Write); |
| 451 | let mut reply = Vec::new(); |
| 452 | let mut chunk = [0u8; 4096]; |
| 453 | let mut closed = false; |
| 454 | loop { |
| 455 | if let Some(i) = reply.windows(4).position(|w| w == b"\r\n\r\n") { |
| 456 | let head = String::from_utf8_lossy(&reply[..i]).to_lowercase(); |
| 457 | let want = head.lines() |
| 458 | .filter_map(|l| l.strip_prefix("content-length:")) |
| 459 | .filter_map(|v| v.trim().parse::<usize>().ok()) |
| 460 | .next() |
| 461 | .unwrap_or(0); |
| 462 | if reply.len() >= i + 4 + want { |
| 463 | break; |
| 464 | } |
| 465 | } |
| 466 | match stream.read(&mut chunk).await { |
| 467 | Ok(0) | Err(_) => { |
| 468 | closed = true; |
| 469 | break; |
| 470 | }, |
| 471 | Ok(n) => reply.extend_from_slice(&chunk[..n]), |
| 472 | } |
| 473 | } |
| 474 | if !closed { |
| 475 | closed = match tokio::time::timeout(patience, stream.read(&mut chunk)).await { |
| 476 | Ok(Ok(0)) | Ok(Err(_)) => true, |
| 477 | Ok(Ok(_)) => false, |
| 478 | Err(_) => false, |
| 479 | }; |
| 480 | } |
| 481 | drop(stream); |
| 482 | let _ = server.await; |
| 483 | let split = match reply.windows(4).position(|w| w == b"\r\n\r\n") { |
| 484 | Some(i) => i, |
| 485 | None => return Err(err!("{:?} on {} answered with no complete head: {:?}.", |
| 486 | raw, host, String::from_utf8_lossy(&reply); Test)), |
| 487 | }; |
| 488 | Ok(Exchange { |
| 489 | reply: Reply { |
| 490 | head: String::from_utf8_lossy(&reply[..split]).to_string(), |
| 491 | body: String::from_utf8_lossy(&reply[split + 4..]).to_string(), |
| 492 | }, |
| 493 | closed, |
| 494 | }) |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | /// The runtime these tests drive `async` code with from a plain `#[test]`. |
| 499 | pub fn runtime() -> Outcome<tokio::runtime::Runtime> { |
| 500 | Ok(res!(tokio::runtime::Runtime::new(), Init)) |
| 501 | } |