|
| 1 | +use std::{ |
| 2 | + net::{IpAddr, Ipv4Addr}, |
| 3 | + sync::Arc, |
| 4 | + time::Duration, |
| 5 | +}; |
| 6 | +use tokio::{ |
| 7 | + io::{AsyncReadExt, AsyncWriteExt as _}, |
| 8 | + net::TcpListener, |
| 9 | +}; |
| 10 | +use tokio_rustls::{ |
| 11 | + TlsAcceptor, |
| 12 | + rustls::{ |
| 13 | + self, ServerConfig, |
| 14 | + pki_types::{CertificateDer, PrivateKeyDer}, |
| 15 | + }, |
| 16 | +}; |
| 17 | +use typesense::ExponentialBackoff; |
| 18 | + |
| 19 | +/// Reqwest custom builder test. |
| 20 | +/// |
| 21 | +/// In this test we exercise the `reqwest_builder` option by setting up a custom root TLS certificate. |
| 22 | +/// If the cusomization doesn't work, reqwest would be unable to connect to the mocked Typesense node. |
| 23 | +/// |
| 24 | +/// This test is non-WASM as it needs TCP. |
| 25 | +pub(super) async fn test_http_builder_tls() { |
| 26 | + rustls::crypto::aws_lc_rs::default_provider() |
| 27 | + .install_default() |
| 28 | + .expect("Failed to install crypto provider"); |
| 29 | + |
| 30 | + let api_key = "xxx-api-key"; |
| 31 | + |
| 32 | + // generate a self-signed key pair and build TLS config out of it |
| 33 | + let (cert, key) = generate_self_signed_cert(); |
| 34 | + let tls_config = ServerConfig::builder() |
| 35 | + .with_no_client_auth() |
| 36 | + .with_single_cert(vec![cert.clone()], key) |
| 37 | + .expect("failed to build TLS config"); |
| 38 | + |
| 39 | + let localhost = IpAddr::V4(Ipv4Addr::LOCALHOST); |
| 40 | + let listener = TcpListener::bind((localhost, 0)) |
| 41 | + .await |
| 42 | + .expect("Failed to bind to address"); |
| 43 | + let server_addr = listener.local_addr().expect("Failed to get local address"); |
| 44 | + |
| 45 | + // spawn a handler which handles one /health request over a TLS connection |
| 46 | + let handler = tokio::spawn(mock_node_handler(listener, tls_config, api_key)); |
| 47 | + |
| 48 | + // create the client, configuring the certificate with reqwest |
| 49 | + let client_cert = reqwest::Certificate::from_der(&cert) |
| 50 | + .expect("Failed to convert certificate to Certificate"); |
| 51 | + let client = typesense::Client::builder() |
| 52 | + .nodes(vec![format!("https://localhost:{}", server_addr.port())]) |
| 53 | + .api_key(api_key) |
| 54 | + .http_builder(move || { |
| 55 | + reqwest::Client::builder() |
| 56 | + .add_root_certificate(client_cert.clone()) |
| 57 | + .https_only(true) |
| 58 | + }) |
| 59 | + .healthcheck_interval(Duration::from_secs(9001)) // we'll do a healthcheck manually |
| 60 | + .retry_policy(ExponentialBackoff::builder().build_with_max_retries(0)) // no retries |
| 61 | + .connection_timeout(Duration::from_secs(1)) // short |
| 62 | + .build() |
| 63 | + .expect("Failed to create Typesense client"); |
| 64 | + |
| 65 | + // request /health |
| 66 | + client |
| 67 | + .operations() |
| 68 | + .health() |
| 69 | + .await |
| 70 | + .expect("Failed to get collection health"); |
| 71 | + |
| 72 | + handler.await.expect("Failed to join handler"); |
| 73 | +} |
| 74 | + |
| 75 | +fn generate_self_signed_cert() -> (CertificateDer<'static>, PrivateKeyDer<'static>) { |
| 76 | + let pair = rcgen::generate_simple_self_signed(["localhost".into()]) |
| 77 | + .expect("Failed to generate self-signed certificate"); |
| 78 | + let cert = pair.cert.der().clone(); |
| 79 | + let signing_key = pair.signing_key.serialize_der(); |
| 80 | + let signing_key = PrivateKeyDer::try_from(signing_key) |
| 81 | + .expect("Failed to convert signing key to PrivateKeyDer"); |
| 82 | + (cert, signing_key) |
| 83 | +} |
| 84 | + |
| 85 | +async fn mock_node_handler(listener: TcpListener, tls_config: ServerConfig, api_key: &'static str) { |
| 86 | + let tls_acceptor = TlsAcceptor::from(Arc::new(tls_config)); |
| 87 | + let (stream, _addr) = listener |
| 88 | + .accept() |
| 89 | + .await |
| 90 | + .expect("Failed to accept connection"); |
| 91 | + let mut stream = tls_acceptor |
| 92 | + .accept(stream) |
| 93 | + .await |
| 94 | + .expect("Failed to accept TLS connection"); |
| 95 | + |
| 96 | + let mut buf = vec![0u8; 1024]; |
| 97 | + stream |
| 98 | + .read(&mut buf[..]) |
| 99 | + .await |
| 100 | + .expect("Failed to read request"); |
| 101 | + let request = String::from_utf8(buf).expect("Failed to parse request as UTF-8"); |
| 102 | + assert!(request.contains("/health")); |
| 103 | + assert!(request.contains(api_key)); |
| 104 | + |
| 105 | + // mock a /health response |
| 106 | + let response = r#"HTTP/1.1 200 OK\r\n\ |
| 107 | +Content-Type: application/json;\r\n\ |
| 108 | +Connection: close\r\n |
| 109 | +
|
| 110 | +{"ok": true}"#; |
| 111 | + stream |
| 112 | + .write_all(&response.as_bytes()) |
| 113 | + .await |
| 114 | + .expect("Failed to write to stream"); |
| 115 | + stream.shutdown().await.expect("Failed to shutdown stream"); |
| 116 | +} |
0 commit comments