demesne-postgres 7.2.0

Live-database support for demesne: schema introspection, policy-drift and coverage checks, and the RLS role audit.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
use crate::dsn::{self, ConnConfig, Roots, SessionAttrs, TlsPlan, Verify};
use crate::goerr::errno_text;
use postgres::config::{ChannelBinding, SslMode, SslNegotiation};
use postgres::{Client, NoTls};
use rustls::client::WebPkiServerVerifier;
use rustls::client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier};
use rustls::crypto::{CryptoProvider, verify_tls12_signature, verify_tls13_signature};
use rustls::pki_types::{CertificateDer, ServerName, UnixTime};
use rustls::{AlertDescription, CertificateError, ClientConfig, DigitallySignedStruct, RootCertStore, SignatureScheme};
use std::error::Error as StdError;
use std::net::{IpAddr, ToSocketAddrs};
use std::sync::Arc;

const PROTOCOL_3_0: i32 = 3 << 16;

const FIRST_PROTOCOL_3_2_SERVER: i64 = 180_000;

enum Addr {
    Tcp(IpAddr),
    Unix(String),
}

struct Attempt<'a> {
    addr: Addr,
    port: u16,
    address: String,
    original: &'a str,
    tls: Option<&'a Arc<TlsPlan>>,
}

struct Failure {
    text: String,
    code: Option<String>,
    not_preferred: bool,
}

impl Failure {
    fn plain(text: impl Into<String>) -> Self {
        Failure { text: text.into(), code: None, not_preferred: false }
    }
}

#[derive(Debug)]
struct NoVerify(Arc<CryptoProvider>);

impl ServerCertVerifier for NoVerify {
    fn verify_server_cert(
        &self,
        _end_entity: &CertificateDer<'_>,
        _intermediates: &[CertificateDer<'_>],
        _server_name: &ServerName<'_>,
        _ocsp_response: &[u8],
        _now: UnixTime,
    ) -> Result<ServerCertVerified, rustls::Error> {
        Ok(ServerCertVerified::assertion())
    }

    fn verify_tls12_signature(&self, message: &[u8], cert: &CertificateDer<'_>, dss: &DigitallySignedStruct) -> Result<HandshakeSignatureValid, rustls::Error> {
        verify_tls12_signature(message, cert, dss, &self.0.signature_verification_algorithms)
    }

    fn verify_tls13_signature(&self, message: &[u8], cert: &CertificateDer<'_>, dss: &DigitallySignedStruct) -> Result<HandshakeSignatureValid, rustls::Error> {
        verify_tls13_signature(message, cert, dss, &self.0.signature_verification_algorithms)
    }

    fn supported_verify_schemes(&self) -> Vec<SignatureScheme> {
        self.0.signature_verification_algorithms.supported_schemes()
    }
}

#[derive(Debug)]
struct ChainOnly(Arc<WebPkiServerVerifier>);

impl ServerCertVerifier for ChainOnly {
    fn verify_server_cert(
        &self,
        end_entity: &CertificateDer<'_>,
        intermediates: &[CertificateDer<'_>],
        server_name: &ServerName<'_>,
        ocsp_response: &[u8],
        now: UnixTime,
    ) -> Result<ServerCertVerified, rustls::Error> {
        match self.0.verify_server_cert(end_entity, intermediates, server_name, ocsp_response, now) {
            Err(rustls::Error::InvalidCertificate(CertificateError::NotValidForName | CertificateError::NotValidForNameContext { .. })) => {
                Ok(ServerCertVerified::assertion())
            }
            other => other,
        }
    }

    fn verify_tls12_signature(&self, message: &[u8], cert: &CertificateDer<'_>, dss: &DigitallySignedStruct) -> Result<HandshakeSignatureValid, rustls::Error> {
        self.0.verify_tls12_signature(message, cert, dss)
    }

    fn verify_tls13_signature(&self, message: &[u8], cert: &CertificateDer<'_>, dss: &DigitallySignedStruct) -> Result<HandshakeSignatureValid, rustls::Error> {
        self.0.verify_tls13_signature(message, cert, dss)
    }

    fn supported_verify_schemes(&self) -> Vec<SignatureScheme> {
        self.0.supported_verify_schemes()
    }
}

#[derive(Debug)]
struct NameFirst(Arc<WebPkiServerVerifier>);

impl ServerCertVerifier for NameFirst {
    fn verify_server_cert(
        &self,
        end_entity: &CertificateDer<'_>,
        intermediates: &[CertificateDer<'_>],
        server_name: &ServerName<'_>,
        ocsp_response: &[u8],
        now: UnixTime,
    ) -> Result<ServerCertVerified, rustls::Error> {
        let Ok(cert) = webpki::EndEntityCert::try_from(end_entity) else {
            return Err(rustls::Error::InvalidCertificate(CertificateError::BadEncoding));
        };
        if let Err(webpki::Error::CertNotValidForName(ctx)) = cert.verify_is_valid_for_subject_name(server_name) {
            return Err(rustls::Error::InvalidCertificate(CertificateError::NotValidForNameContext { expected: ctx.expected, presented: ctx.presented }));
        }
        self.0.verify_server_cert(end_entity, intermediates, server_name, ocsp_response, now)
    }

    fn verify_tls12_signature(&self, message: &[u8], cert: &CertificateDer<'_>, dss: &DigitallySignedStruct) -> Result<HandshakeSignatureValid, rustls::Error> {
        self.0.verify_tls12_signature(message, cert, dss)
    }

    fn verify_tls13_signature(&self, message: &[u8], cert: &CertificateDer<'_>, dss: &DigitallySignedStruct) -> Result<HandshakeSignatureValid, rustls::Error> {
        self.0.verify_tls13_signature(message, cert, dss)
    }

    fn supported_verify_schemes(&self) -> Vec<SignatureScheme> {
        self.0.supported_verify_schemes()
    }
}

fn webpki_verifier(roots: &Roots, provider: &Arc<CryptoProvider>) -> Result<Arc<WebPkiServerVerifier>, String> {
    let mut store = RootCertStore::empty();
    match roots {
        Roots::Pem(certs) => {
            store.add_parsable_certificates(certs.iter().cloned());
        }
        Roots::System => {
            store.add_parsable_certificates(rustls_native_certs::load_native_certs().certs);
        }
    }
    let Ok(verifier) = WebPkiServerVerifier::builder_with_provider(Arc::new(store), Arc::clone(provider)).build() else {
        return Err("tls: failed to verify certificate: x509: failed to load system roots and no roots provided".to_string());
    };
    Ok(verifier)
}

fn rustls_config(plan: &TlsPlan) -> Result<ClientConfig, String> {
    let provider = Arc::new(rustls::crypto::ring::default_provider());
    let verifier: Arc<dyn ServerCertVerifier> = match plan.verify {
        Verify::None => Arc::new(NoVerify(Arc::clone(&provider))),
        Verify::Chain => Arc::new(ChainOnly(webpki_verifier(&plan.roots, &provider)?)),
        Verify::Full => Arc::new(NameFirst(webpki_verifier(&plan.roots, &provider)?)),
    };
    let builder = ClientConfig::builder_with_provider(provider)
        .with_safe_default_protocol_versions()
        .map_err(|e| format!("tls: {e}"))?
        .dangerous()
        .with_custom_certificate_verifier(verifier);
    let mut cfg = match &plan.client {
        Some((certs, key)) => builder.with_client_auth_cert(certs.clone(), key.clone_key()).map_err(|e| format!("tls: {e}"))?,
        None => builder.with_no_client_auth(),
    };
    cfg.enable_sni = plan.sni;
    if plan.direct {
        cfg.alpn_protocols = vec![b"postgresql".to_vec()];
    }
    Ok(cfg)
}

fn alert_text(a: AlertDescription) -> String {
    use AlertDescription as A;
    match a {
        A::CloseNotify => "close notify".into(),
        A::UnexpectedMessage => "unexpected message".into(),
        A::BadRecordMac => "bad record MAC".into(),
        A::DecryptionFailed => "decryption failed".into(),
        A::RecordOverflow => "record overflow".into(),
        A::DecompressionFailure => "decompression failure".into(),
        A::HandshakeFailure => "handshake failure".into(),
        A::BadCertificate => "bad certificate".into(),
        A::UnsupportedCertificate => "unsupported certificate".into(),
        A::CertificateRevoked => "revoked certificate".into(),
        A::CertificateExpired => "expired certificate".into(),
        A::CertificateUnknown => "unknown certificate".into(),
        A::IllegalParameter => "illegal parameter".into(),
        A::UnknownCA => "unknown certificate authority".into(),
        A::AccessDenied => "access denied".into(),
        A::DecodeError => "error decoding message".into(),
        A::DecryptError => "error decrypting message".into(),
        A::ExportRestriction => "export restriction".into(),
        A::ProtocolVersion => "protocol version not supported".into(),
        A::InsufficientSecurity => "insufficient security level".into(),
        A::InternalError => "internal error".into(),
        A::InappropriateFallback => "inappropriate fallback".into(),
        A::UserCanceled => "user canceled".into(),
        A::NoRenegotiation => "no renegotiation".into(),
        A::MissingExtension => "missing extension".into(),
        A::UnsupportedExtension => "unsupported extension".into(),
        A::CertificateUnobtainable => "certificate unobtainable".into(),
        A::UnrecognisedName => "unrecognized name".into(),
        A::BadCertificateStatusResponse => "bad certificate status response".into(),
        A::BadCertificateHashValue => "bad certificate hash value".into(),
        A::UnknownPSKIdentity => "unknown PSK identity".into(),
        A::CertificateRequired => "certificate required".into(),
        A::NoApplicationProtocol => "no application protocol".into(),
        A::EncryptedClientHelloRequired => "encrypted client hello required".into(),
        other => format!("alert({})", u8::from(other)),
    }
}

fn hostname_error(host: &str, presented: &[String]) -> String {
    let inner = |p: &str, pre: &str, post: &str| p.strip_prefix(pre).and_then(|r| r.strip_suffix(post)).map(str::to_string);
    let dns: Vec<String> = presented.iter().filter_map(|p| inner(p, "DnsName(\"", "\")")).collect();
    let ips: Vec<String> = presented.iter().filter_map(|p| inner(p, "IpAddress(", ")")).collect();
    if host.parse::<IpAddr>().is_ok() {
        if ips.is_empty() {
            return format!("x509: cannot validate certificate for {host} because it doesn't contain any IP SANs");
        }
        return format!("x509: certificate is valid for {}, not {host}", ips.join(", "));
    }
    if dns.is_empty() {
        return format!("x509: certificate is not valid for any names, but wanted to match {host}");
    }
    format!("x509: certificate is valid for {}, not {host}", dns.join(", "))
}

fn go_tls_error(e: &rustls::Error, verify: Verify, host: &str) -> String {
    match e {
        rustls::Error::InvalidCertificate(c) => {
            let body = match c {
                CertificateError::UnknownIssuer => "x509: certificate signed by unknown authority".to_string(),
                CertificateError::NotValidForName => hostname_error(host, &[]),
                CertificateError::NotValidForNameContext { presented, .. } => hostname_error(host, presented),
                CertificateError::Expired
                | CertificateError::ExpiredContext { .. }
                | CertificateError::NotValidYet
                | CertificateError::NotValidYetContext { .. } => "x509: certificate has expired or is not yet valid".to_string(),
                other => format!("x509: {other:?}"),
            };
            match verify {
                Verify::Chain => body,
                _ => format!("tls: failed to verify certificate: {body}"),
            }
        }
        rustls::Error::AlertReceived(a) => format!("remote error: tls: {}", alert_text(*a)),
        other => format!("tls: {other}"),
    }
}

fn find_in_chain<'a, T: StdError + 'static>(e: &'a (dyn StdError + 'static)) -> Option<&'a T> {
    let mut cur: Option<&(dyn StdError + 'static)> = Some(e);
    while let Some(c) = cur {
        if let Some(t) = c.downcast_ref::<T>() {
            return Some(t);
        }
        if let Some(io) = c.downcast_ref::<std::io::Error>()
            && let Some(inner) = io.get_ref()
            && let Some(t) = inner.downcast_ref::<T>()
        {
            return Some(t);
        }
        cur = c.source();
    }
    None
}

fn chain_text(e: &(dyn StdError + 'static)) -> String {
    let mut out = e.to_string();
    let mut cur = e.source();
    while let Some(c) = cur {
        out.push_str(": ");
        out.push_str(&c.to_string());
        cur = c.source();
    }
    out
}

fn classify(e: &postgres::Error, a: &Attempt) -> Failure {
    if let Some(db) = e.as_db_error() {
        return Failure {
            text: format!("server error: {}: {} (SQLSTATE {})", db.severity(), db.message(), db.code().code()),
            code: Some(db.code().code().to_string()),
            not_preferred: false,
        };
    }
    let top = e.to_string();
    let source = e.source();
    if top.starts_with("error connecting to server") {
        let net = match a.addr {
            Addr::Tcp(_) => "tcp",
            Addr::Unix(_) => "unix",
        };
        return Failure::plain(match source.and_then(find_in_chain::<std::io::Error>) {
            Some(io) if io.kind() == std::io::ErrorKind::TimedOut && io.raw_os_error().is_none() => {
                "dial error: timeout: context deadline exceeded".to_string()
            }
            Some(io) => format!("dial error: dial {net} {}: connect: {}", a.address, errno_text(io)),
            None => "dial error".to_string(),
        });
    }
    if top.starts_with("error performing TLS handshake") {
        if source.is_some_and(|s| s.to_string() == "server does not support TLS") {
            return Failure::plain("tls error: server refused TLS connection");
        }
        let verify = a.tls.map_or(Verify::None, |p| p.verify);
        if let Some(r) = source.and_then(find_in_chain::<rustls::Error>) {
            return Failure::plain(format!("failed to write startup message: write failed: {}", go_tls_error(r, verify, a.original)));
        }
        return Failure::plain(format!("failed to write startup message: write failed: {}", source.map(chain_text).unwrap_or_default()));
    }
    Failure::plain(format!("failed to receive message: {}", chain_text(e)))
}

fn first_cell(client: &mut Client, sql: &str) -> Result<String, String> {
    for msg in client.simple_query(sql).map_err(|e| crate::query_error(&e))? {
        if let postgres::SimpleQueryMessage::Row(row) = msg {
            return Ok(row.get(0).unwrap_or("").to_string());
        }
    }
    Ok(String::new())
}

fn validate(client: &mut Client, attrs: SessionAttrs, ignore_not_preferred: bool) -> Result<(), Failure> {
    let fail = |m: String| Failure::plain(format!("ValidateConnect failed: {m}"));
    let check = |client: &mut Client, sql: &str| first_cell(client, sql).map_err(fail);
    match attrs {
        SessionAttrs::Any => Ok(()),
        SessionAttrs::ReadWrite => match check(client, "show transaction_read_only")?.as_str() {
            "on" => Err(fail("read only connection".into())),
            _ => Ok(()),
        },
        SessionAttrs::ReadOnly => match check(client, "show transaction_read_only")?.as_str() {
            "on" => Ok(()),
            _ => Err(fail("connection is not read only".into())),
        },
        SessionAttrs::Standby => match check(client, "select pg_is_in_recovery()")?.as_str() {
            "t" => Ok(()),
            _ => Err(fail("server is not in hot standby mode".into())),
        },
        SessionAttrs::Primary => match check(client, "select pg_is_in_recovery()")?.as_str() {
            "t" => Err(fail("server is in standby mode".into())),
            _ => Ok(()),
        },
        SessionAttrs::PreferStandby => match check(client, "select pg_is_in_recovery()")?.as_str() {
            "t" => Ok(()),
            _ if ignore_not_preferred => Ok(()),
            _ => {
                let mut f = fail("server is not in hot standby mode".into());
                f.not_preferred = true;
                Err(f)
            }
        },
    }
}

fn attempt(cfg: &ConnConfig, a: &Attempt, ignore_not_preferred: bool) -> Result<Client, Failure> {
    let mut pc = postgres::Config::new();
    pc.user(&cfg.user);
    pc.password(cfg.password.as_bytes());
    if !cfg.database.is_empty() {
        pc.dbname(&cfg.database);
    }
    let (options, app) = cfg.startup_options();
    if let Some(o) = &options {
        pc.options(o);
    }
    if let Some(n) = &app {
        pc.application_name(n);
    }
    match &a.addr {
        #[cfg(unix)]
        Addr::Unix(dir) => {
            pc.host_path(dir);
        }
        #[cfg(not(unix))]
        Addr::Unix(_) => {
            return Err(Failure::plain(format!("dial error: dial unix {}: connect: unix sockets are not supported on this platform", a.address)));
        }
        Addr::Tcp(ip) => {
            pc.host(a.original);
            pc.hostaddr(*ip);
        }
    }
    pc.port(a.port);
    if let Some(t) = cfg.connect_timeout
        && !t.is_zero()
    {
        pc.connect_timeout(t);
    }
    pc.channel_binding(match cfg.channel_binding {
        ChannelBinding::Require => ChannelBinding::Prefer,
        other => other,
    });
    let connected = match a.tls {
        None => {
            pc.ssl_mode(SslMode::Disable);
            pc.connect(NoTls)
        }
        Some(plan) => {
            pc.ssl_mode(SslMode::Require);
            if plan.direct {
                pc.ssl_negotiation(SslNegotiation::Direct);
            }
            let rc = rustls_config(plan).map_err(Failure::plain)?;
            pc.connect(tokio_postgres_rustls::MakeRustlsConnect::new(rc))
        }
    };
    let mut client = connected.map_err(|e| {
        let mut f = classify(&e, a);
        if f.code.as_deref() == Some("28P01") && auth_request_code(cfg, a) == Some(10) {
            f.text = f.text.replacen("server error: ", "failed SASL auth: ", 1);
        }
        f
    })?;
    if cfg.channel_binding == ChannelBinding::Require
        && a.tls.is_none()
        && first_cell(&mut client, "select system_user").is_ok_and(|u| u.starts_with("scram-sha-256:"))
    {
        return Err(Failure::plain("failed SASL auth: channel binding required but server does not support SCRAM-SHA-256-PLUS"));
    }
    if cfg.min_protocol != "3.0" {
        let v: i64 = first_cell(&mut client, "show server_version_num").ok().and_then(|s| s.parse().ok()).unwrap_or(0);
        if v < FIRST_PROTOCOL_3_2_SERVER {
            return Err(Failure::plain(format!(
                "server protocol version too low: min_protocol_version is {}, but the server speaks only protocol 3.0",
                cfg.min_protocol
            )));
        }
    }
    validate(&mut client, cfg.session_attrs, ignore_not_preferred)?;
    Ok(client)
}

fn startup_packet(cfg: &ConnConfig) -> Vec<u8> {
    let mut body = Vec::new();
    body.extend_from_slice(&PROTOCOL_3_0.to_be_bytes());
    let mut param = |k: &str, v: &str| {
        body.extend_from_slice(k.as_bytes());
        body.push(0);
        body.extend_from_slice(v.as_bytes());
        body.push(0);
    };
    param("user", &cfg.user);
    if !cfg.database.is_empty() {
        param("database", &cfg.database);
    }
    body.push(0);
    let mut out = i32::try_from(body.len() + 4).expect("a startup packet is a few hundred bytes").to_be_bytes().to_vec();
    out.extend(body);
    out
}

fn read_auth_code(stream: &mut impl std::io::Read) -> Option<i32> {
    let mut head = [0u8; 9];
    stream.read_exact(&mut head).ok()?;
    if head[0] != b'R' {
        return None;
    }
    Some(i32::from_be_bytes([head[5], head[6], head[7], head[8]]))
}

fn auth_request_code(cfg: &ConnConfig, a: &Attempt) -> Option<i32> {
    use std::io::{Read, Write};
    let timeout = Some(std::time::Duration::from_secs(10));
    let startup = startup_packet(cfg);
    match (&a.addr, a.tls) {
        #[cfg(unix)]
        (Addr::Unix(dir), _) => {
            let mut s = std::os::unix::net::UnixStream::connect(dsn::unix_socket_address(dir, a.port)).ok()?;
            s.set_read_timeout(timeout).ok()?;
            s.write_all(&startup).ok()?;
            read_auth_code(&mut s)
        }
        #[cfg(not(unix))]
        (Addr::Unix(_), _) => None,
        (Addr::Tcp(ip), tls) => {
            let mut tcp = std::net::TcpStream::connect_timeout(&std::net::SocketAddr::new(*ip, a.port), timeout?).ok()?;
            tcp.set_read_timeout(timeout).ok()?;
            let Some(plan) = tls else {
                tcp.write_all(&startup).ok()?;
                return read_auth_code(&mut tcp);
            };
            if !plan.direct {
                tcp.write_all(&[0, 0, 0, 8, 4, 210, 22, 47]).ok()?;
                let mut answer = [0u8; 1];
                tcp.read_exact(&mut answer).ok()?;
                if answer[0] != b'S' {
                    return None;
                }
            }
            let name = ServerName::try_from(a.original.to_string()).ok()?;
            let conn = rustls::ClientConnection::new(Arc::new(rustls_config(plan).ok()?), name).ok()?;
            let mut tls_stream = rustls::StreamOwned::new(conn, tcp);
            tls_stream.write_all(&startup).ok()?;
            read_auth_code(&mut tls_stream)
        }
    }
}

fn lookup_host(host: &str) -> Result<Vec<IpAddr>, String> {
    if let Ok(ip) = host.parse::<IpAddr>() {
        return Ok(vec![ip]);
    }
    let not_found = || format!("lookup {host}: no such host");
    let Ok(addrs) = (host, 0).to_socket_addrs() else { return Err(not_found()) };
    let mut ips: Vec<IpAddr> = Vec::new();
    for a in addrs {
        if !ips.contains(&a.ip()) {
            ips.push(a.ip());
        }
    }
    if ips.is_empty() { Err(not_found()) } else { Ok(ips) }
}

fn join_host_port(ip: IpAddr, port: u16) -> String {
    match ip {
        IpAddr::V4(v) => format!("{v}:{port}"),
        IpAddr::V6(v) => format!("[{v}]:{port}"),
    }
}

fn connect_error(cfg: &ConnConfig, details: &str) -> String {
    let prefix = format!("failed to connect to `user={} database={}`:", cfg.user, cfg.database);
    if details.contains('\n') { format!("{prefix}\n\t{}", details.replace('\n', "\n\t")) } else { format!("{prefix} {details}") }
}

/// Connects to Postgres from a URL or keyword/value string, the way libpq and pgx do.
///
/// # Errors
///
/// The connection string does not parse, or every host fails to connect, authenticate or meet the TLS mode. The message lists each attempt.
pub fn connect(conn: &str) -> Result<Client, crate::Error> {
    let cfg = dsn::parse_config(conn).map_err(|e| crate::Error::connection_string(e.to_string()))?;
    connect_with(&cfg).map_err(crate::Error::connect)
}

fn connect_with(cfg: &ConnConfig) -> Result<Client, String> {
    let mut errors: Vec<String> = Vec::new();
    let mut attempts: Vec<Attempt> = Vec::new();
    for fb in &cfg.fallbacks {
        if dsn::is_absolute_path(&fb.host) {
            attempts.push(Attempt {
                addr: Addr::Unix(fb.host.clone()),
                port: fb.port,
                address: dsn::unix_socket_address(&fb.host, fb.port),
                original: &fb.host,
                tls: fb.tls.as_ref(),
            });
            continue;
        }
        match lookup_host(&fb.host) {
            Ok(ips) => {
                for ip in ips {
                    attempts.push(Attempt {
                        addr: Addr::Tcp(ip),
                        port: fb.port,
                        address: join_host_port(ip, fb.port),
                        original: &fb.host,
                        tls: fb.tls.as_ref(),
                    });
                }
            }
            Err(e) => errors.push(e),
        }
    }
    if attempts.is_empty() {
        return Err(connect_error(cfg, &format!("hostname resolving error: {}", errors.join("\n"))));
    }
    let mut not_preferred: Option<&Attempt> = None;
    for a in &attempts {
        match attempt(cfg, a, false) {
            Ok(c) => return Ok(c),
            Err(f) => {
                errors.push(format!("{} ({}): {}", a.address, a.original, f.text));
                if matches!(f.code.as_deref(), Some("28P01" | "3D000" | "42501")) {
                    return Err(connect_error(cfg, &errors.join("\n")));
                }
                if f.not_preferred {
                    not_preferred = Some(a);
                }
            }
        }
    }
    if let Some(a) = not_preferred {
        match attempt(cfg, a, true) {
            Ok(c) => return Ok(c),
            Err(f) => errors.push(format!("{} ({}): {}", a.address, a.original, f.text)),
        }
    }
    Err(connect_error(cfg, &errors.join("\n")))
}