giong-postgres 1.3.0

A PostgreSQL client and connection pool for the giong thread-per-core runtime
Documentation
//! Connecting: sockets, TLS, authentication methods, and the errors when any
//! of it goes wrong.

use std::cell::Cell;
use std::rc::Rc;
use std::time::{Duration, Instant};

use giong::net::TcpListener;
use giong_postgres::{AuthMethod, Client, Config, ErrorKind, Tls, TlsConnector};

use crate::run;
use crate::server::{CLEAR_USER, MD5_USER, SCRAM_USER, TLS_USER};

async fn one(client: &Client) -> i32 {
    client
        .query_one("SELECT 1", &[])
        .await
        .unwrap()
        .get(0)
        .unwrap()
}

#[test]
fn trust_over_the_unix_socket() {
    run(|s| async move {
        let client = Client::connect(&s.local()).await.unwrap();
        assert_eq!(one(&client).await, 1);
        assert!(client.parameter("server_version").is_some());
        assert_eq!(client.parameter("client_encoding").as_deref(), Some("UTF8"));
    });
}

#[test]
fn a_url_works_as_well_as_the_setters() {
    run(|s| async move {
        let url = format!(
            "postgres:///postgres?host={}&port={}&user=postgres&application_name=from-url",
            s.socket_dir.display(),
            s.port
        );
        let client = Client::connect(&Config::from_url(&url).unwrap())
            .await
            .unwrap();
        let row = client
            .query_one("SELECT current_setting('application_name')", &[])
            .await
            .unwrap();
        assert_eq!(row.get::<&str>(0).unwrap(), "from-url");
    });
}

#[test]
fn scram_sha_256_with_the_right_and_the_wrong_password() {
    run(|s| async move {
        let client = Client::connect(&s.tcp(SCRAM_USER)).await.unwrap();
        let row = client
            .query_one("SELECT current_user::text", &[])
            .await
            .unwrap();
        assert_eq!(row.get::<&str>(0).unwrap(), SCRAM_USER.0);

        let e = Client::connect(&s.tcp((SCRAM_USER.0, "wrong")))
            .await
            .unwrap_err();
        assert_eq!(e.kind(), ErrorKind::Auth, "{e}");
        assert_eq!(e.server_code(), Some("28P01"), "{e}");
        assert!(
            e.to_string().contains("password authentication failed"),
            "{e}"
        );
    });
}

#[test]
fn md5_and_clear_text_passwords() {
    run(|s| async move {
        let client = Client::connect(&s.tcp(MD5_USER)).await.unwrap();
        assert_eq!(one(&client).await, 1);
        let e = Client::connect(&s.tcp((MD5_USER.0, "wrong")))
            .await
            .unwrap_err();
        assert_eq!(e.server_code(), Some("28P01"), "{e}");

        // Clear text is allowed to a loopback address.
        let client = Client::connect(&s.tcp(CLEAR_USER)).await.unwrap();
        assert_eq!(one(&client).await, 1);
    });
}

#[test]
fn tls_is_verified_against_the_given_authority() {
    run(|s| async move {
        for host in ["localhost", "127.0.0.1"] {
            let client = Client::connect(&s.tls(host, TLS_USER)).await.unwrap();
            let row = client
                .query_one(
                    "SELECT ssl FROM pg_stat_ssl WHERE pid = pg_backend_pid()",
                    &[],
                )
                .await
                .unwrap();
            assert!(row.get::<bool>(0).unwrap(), "{host}");
        }

        // An authority the server's certificate does not come from.
        let other = rcgen::generate_simple_self_signed(vec!["localhost".into()]).unwrap();
        let config = s.tcp(TLS_USER).host("localhost").tls(Tls::Verify(
            TlsConnector::with_root_pem(other.cert.pem().as_bytes()).unwrap(),
        ));
        let e = Client::connect(&config).await.unwrap_err();
        assert_eq!(e.kind(), ErrorKind::Connect, "{e}");
        assert!(e.to_string().contains("TLS handshake"), "{e}");

        // An address the certificate is not for (the server listens on it
        // too).
        let e = Client::connect(&s.tls("127.0.0.2", TLS_USER))
            .await
            .unwrap_err();
        assert!(e.to_string().contains("not valid for"), "{e}");
    });
}

#[test]
fn a_server_that_requires_tls_refuses_a_plain_connection() {
    run(|s| async move {
        let e = Client::connect(&s.tcp(TLS_USER)).await.unwrap_err();
        assert_eq!(e.kind(), ErrorKind::Auth, "{e}");
        assert_eq!(e.server_code(), Some("28000"), "{e}");
    });
}

#[test]
fn startup_errors_say_what_is_missing() {
    run(|s| async move {
        let e = Client::connect(
            &s.tcp((SCRAM_USER.0, "x"))
                .database("no_such_db")
                .password(SCRAM_USER.1),
        )
        .await
        .unwrap_err();
        assert_eq!(e.kind(), ErrorKind::Connect, "{e}");
        assert_eq!(e.server_code(), Some("3D000"), "{e}");

        let e = Client::connect(
            &Config::new()
                .host("127.0.0.1")
                .port(s.port)
                .user(SCRAM_USER.0),
        )
        .await
        .unwrap_err();
        assert_eq!(e.kind(), ErrorKind::Auth, "{e}");
        assert!(e.to_string().contains("Config::password"), "{e}");

        let e = Client::connect(&Config::new().host("127.0.0.1").port(s.port))
            .await
            .unwrap_err();
        assert_eq!(e.kind(), ErrorKind::Config, "{e}");

        let e = Client::connect(&s.local().user("nobody"))
            .await
            .unwrap_err();
        assert_eq!(e.kind(), ErrorKind::Auth, "{e}");
    });
}

#[test]
fn nothing_listening_is_a_connect_error() {
    run(|_| async move {
        // Bound but not listening, so the connect is refused for certain
        // (a port from a closed listener could be taken in the meantime).
        let refusing =
            socket2::Socket::new(socket2::Domain::IPV4, socket2::Type::STREAM, None).unwrap();
        refusing
            .bind(&std::net::SocketAddr::from(([127, 0, 0, 1], 0)).into())
            .unwrap();
        let port = refusing.local_addr().unwrap().as_socket().unwrap().port();
        let e = Client::connect(&Config::new().host("127.0.0.1").port(port).user("u"))
            .await
            .unwrap_err();
        assert_eq!(e.kind(), ErrorKind::Connect, "{e}");
        assert!(e.to_string().contains(&format!("127.0.0.1:{port}")), "{e}");
    });
}

#[test]
fn a_server_that_never_answers_hits_the_connect_timeout() {
    run(|_| async move {
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let port = listener.local_addr().unwrap().port();
        let _held = giong::spawn(async move {
            let _conn = listener.accept().await;
            giong::time::sleep(Duration::from_secs(10)).await;
        });
        let started = Instant::now();
        let config = Config::new()
            .host("127.0.0.1")
            .port(port)
            .user("u")
            .connect_timeout(Duration::from_millis(200));
        let e = Client::connect(&config).await.unwrap_err();
        assert_eq!(e.kind(), ErrorKind::Connect, "{e}");
        assert!(e.to_string().contains("connect timeout"), "{e}");
        assert!(started.elapsed() < Duration::from_secs(5));
    });
}

/// A fake server that answers the TLS request with `answer`.
async fn tls_answer(answer: &'static [u8]) -> giong_postgres::Error {
    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let port = listener.local_addr().unwrap().port();
    let _server = giong::spawn(async move {
        let conn = listener.accept().await.unwrap();
        let request = conn.recv().await.unwrap();
        assert_eq!(request.len(), 8);
        drop(request);
        conn.write_all(answer).await.0.unwrap();
        giong::time::sleep(Duration::from_secs(5)).await;
    });
    let connector = TlsConnector::with_root_pem(
        rcgen::generate_simple_self_signed(vec!["localhost".into()])
            .unwrap()
            .cert
            .pem()
            .as_bytes(),
    )
    .unwrap();
    let config = Config::new()
        .host("127.0.0.1")
        .port(port)
        .user("u")
        .tls(Tls::Verify(connector));
    Client::connect(&config).await.unwrap_err()
}

#[test]
fn a_server_without_tls_or_with_extra_bytes_is_refused() {
    run(|_| async move {
        let e = tls_answer(b"N").await;
        assert_eq!(e.kind(), ErrorKind::Connect, "{e}");
        assert!(e.to_string().contains("sslmode=disable"), "{e}");
        // Bytes after the 'S' would be taken as coming through TLS.
        let e = tls_answer(b"SX").await;
        assert_eq!(e.kind(), ErrorKind::Protocol, "{e}");
    });
}

/// This machine's address on its default route, if it has one.
fn non_loopback_address() -> Option<std::net::IpAddr> {
    let socket = std::net::UdpSocket::bind("0.0.0.0:0").ok()?;
    socket.connect("192.0.2.1:9").ok()?;
    let ip = socket.local_addr().ok()?.ip();
    (!ip.is_loopback() && !ip.is_unspecified()).then_some(ip)
}

#[test]
fn a_clear_text_password_is_never_sent_unencrypted_over_the_network() {
    run(|_| async move {
        let Some(ip) = non_loopback_address() else {
            eprintln!("skipping: no non-loopback address");
            return;
        };
        let listener = TcpListener::bind((ip, 0)).unwrap();
        let addr = listener.local_addr().unwrap();
        let server = giong::spawn(async move {
            let conn = listener.accept().await.unwrap();
            drop(conn.recv().await.unwrap()); // the startup message
                                              // AuthenticationCleartextPassword
            conn.write_all(&[b'R', 0, 0, 0, 8, 0, 0, 0, 3][..])
                .await
                .0
                .unwrap();
            let next = conn.recv().await.unwrap();
            assert!(
                next.is_empty(),
                "the client sent something after the request: {:?}",
                &next[..]
            );
        });
        let config = Config::new()
            .host(&ip.to_string())
            .port(addr.port())
            .user("u")
            .password("secret")
            .tls(Tls::Disable);
        let e = Client::connect(&config).await.unwrap_err();
        assert_eq!(e.kind(), ErrorKind::Auth, "{e}");
        assert!(e.to_string().contains("clear text"), "{e}");
        server.await.unwrap();
    });
}

#[test]
fn require_auth_refuses_any_other_method() {
    run(|s| async move {
        let scram_only = [AuthMethod::ScramSha256];
        let client = Client::connect(&s.tcp(SCRAM_USER).require_auth(&scram_only))
            .await
            .unwrap();
        assert_eq!(one(&client).await, 1);
        // Refused before anything is sent for the password.
        for user in [MD5_USER, CLEAR_USER] {
            let e = Client::connect(&s.tcp(user).require_auth(&scram_only))
                .await
                .unwrap_err();
            assert_eq!(e.kind(), ErrorKind::Auth, "{e}");
            assert!(e.to_string().contains("allows only scram-sha-256"), "{e}");
        }
        // A server (or an impostor) that lets the client in without asking.
        let e = Client::connect(&s.local().require_auth(&scram_only))
            .await
            .unwrap_err();
        assert_eq!(e.kind(), ErrorKind::Auth, "{e}");
        assert!(e.to_string().contains("without authentication"), "{e}");
        Client::connect(&s.local().require_auth(&[AuthMethod::None]))
            .await
            .unwrap();
        // The URL parameter, as in libpq.
        let url = format!(
            "postgres://{}:{}@127.0.0.1:{}/postgres?require_auth=scram-sha-256",
            MD5_USER.0, MD5_USER.1, s.port
        );
        let e = Client::connect(&Config::from_url(&url).unwrap())
            .await
            .unwrap_err();
        assert_eq!(e.kind(), ErrorKind::Auth, "{e}");
    });
}

#[test]
fn scram_keys_are_derived_once_and_off_the_worker() {
    run(|s| async move {
        let version: u32 = s.psql("SHOW server_version_num").trim().parse().unwrap();
        if version < 160000 {
            eprintln!("skipping: scram_iterations needs PostgreSQL 16");
            return;
        }
        // About 50 ms of PBKDF2 in a release build; ThreadSanitizer makes it
        // a hundred times slower.
        let iterations = if cfg!(giong_tsan) { 20_000 } else { 300_000 };
        s.psql(&format!(
            "DROP ROLE IF EXISTS scram_slow; SET scram_iterations = {iterations}; \
             CREATE ROLE scram_slow LOGIN PASSWORD 'slow-secret'"
        ));
        let config = s
            .tcp(("scram_slow", "slow-secret"))
            .connect_timeout(Duration::from_secs(60));
        // A task on the same worker that notices when it stops being run.
        let stop = Rc::new(Cell::new(false));
        let longest = Rc::new(Cell::new(Duration::ZERO));
        let ticker = giong::spawn({
            let (stop, longest) = (stop.clone(), longest.clone());
            async move {
                while !stop.get() {
                    let t = Instant::now();
                    giong::time::sleep(Duration::from_millis(1)).await;
                    longest.set(longest.get().max(t.elapsed()));
                }
            }
        });
        giong::time::sleep(Duration::from_millis(5)).await;
        let started = Instant::now();
        let client = Client::connect(&config).await.unwrap();
        let first = started.elapsed();
        assert_eq!(one(&client).await, 1);
        stop.set(true);
        ticker.await.unwrap();
        // The derivation ran on another thread: the worker kept running its
        // tasks meanwhile.
        assert!(
            longest.get() < first / 2,
            "the worker stalled for {:?} of a {first:?} connect",
            longest.get()
        );
        // The key is kept: the next connections skip the derivation, also
        // through a clone of the config.
        let started = Instant::now();
        Client::connect(&config.clone()).await.unwrap();
        let second = started.elapsed();
        assert!(second < first / 3, "{first:?}, then {second:?}");
        // A different password is not served from the cache.
        let e = Client::connect(&config.clone().password("wrong"))
            .await
            .unwrap_err();
        assert_eq!(e.kind(), ErrorKind::Auth, "{e}");
    });
}