sentinel-driver 2.0.0

High-performance PostgreSQL wire protocol driver for Rust
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
use std::path::PathBuf;
use std::time::Duration;

use crate::error::{Error, Result};

/// TLS mode for the connection.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SslMode {
    /// No TLS. Connections are unencrypted.
    Disable,
    /// Try TLS, fall back to plaintext if server doesn't support it.
    #[default]
    Prefer,
    /// Require TLS. Fail if server doesn't support it.
    Require,
    /// Require TLS and verify the server certificate.
    VerifyCa,
    /// Require TLS, verify certificate, and verify hostname matches.
    VerifyFull,
}

/// Connection configuration for sentinel-driver.
///
/// # Connection String
///
/// ```text
/// postgres://user:password@host:port/database?sslmode=prefer&application_name=myapp
/// ```
///
/// # Builder
///
/// ```rust,no_run
/// use sentinel_driver::Config;
///
/// let config = Config::builder()
///     .host("localhost")
///     .port(5432)
///     .database("mydb")
///     .user("postgres")
///     .password("secret")
///     .build();
/// ```
#[derive(Debug, Clone)]
pub struct Config {
    pub(crate) hosts: Vec<(String, u16)>,
    pub(crate) database: String,
    pub(crate) user: String,
    pub(crate) password: Option<String>,
    pub(crate) ssl_mode: SslMode,
    pub(crate) application_name: Option<String>,
    pub(crate) connect_timeout: Duration,
    pub(crate) statement_timeout: Option<Duration>,
    pub(crate) _keepalive: Option<Duration>,
    pub(crate) _keepalive_idle: Option<Duration>,
    pub(crate) target_session_attrs: TargetSessionAttrs,
    pub(crate) _extra_float_digits: Option<i32>,
    pub(crate) load_balance_hosts: LoadBalanceHosts,
    /// Path to client certificate file for certificate authentication.
    pub(crate) ssl_client_cert: Option<std::path::PathBuf>,
    /// Path to client private key file for certificate authentication.
    pub(crate) ssl_client_key: Option<std::path::PathBuf>,
    /// Use direct TLS connection (PG 17+) — skip SSLRequest negotiation.
    pub(crate) ssl_direct: bool,
    /// Enable SCRAM-SHA-256 channel binding (SCRAM-PLUS) when TLS is active.
    pub(crate) channel_binding: ChannelBinding,
}

/// Channel binding preference for SCRAM authentication.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ChannelBinding {
    /// Use channel binding if available (default).
    #[default]
    Prefer,
    /// Require channel binding — fail if server doesn't support it.
    Require,
    /// Disable channel binding.
    Disable,
}

/// Target session attributes for connection validation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TargetSessionAttrs {
    /// Any server is acceptable.
    #[default]
    Any,
    /// Only accept read-write servers (primary).
    ReadWrite,
    /// Only accept read-only servers (replica).
    ReadOnly,
}

/// Load balancing strategy for multi-host connections.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LoadBalanceHosts {
    /// Try hosts in order (default).
    #[default]
    Disable,
    /// Shuffle hosts before trying.
    Random,
}

impl Config {
    /// Parse a PostgreSQL connection string.
    ///
    /// Supported formats:
    /// - `postgres://user:password@host:port/database?param=value`
    /// - `postgresql://user:password@host:port/database?param=value`
    pub fn parse(s: &str) -> Result<Self> {
        let s = s.trim();

        let without_scheme = s
            .strip_prefix("postgres://")
            .or_else(|| s.strip_prefix("postgresql://"))
            .ok_or_else(|| {
                Error::Config(
                    "connection string must start with postgres:// or postgresql://".into(),
                )
            })?;

        let (userinfo, rest) = match without_scheme.split_once('@') {
            Some((ui, rest)) => (Some(ui), rest),
            None => (None, without_scheme),
        };

        let (user, password) = match userinfo {
            Some(ui) => match ui.split_once(':') {
                Some((u, p)) => (percent_decode(u)?, Some(percent_decode(p)?)),
                None => (percent_decode(ui)?, None),
            },
            None => (String::new(), None),
        };

        // Split host:port from database?params
        let (hostport, db_and_params) = match rest.split_once('/') {
            Some((hp, rest)) => (hp, Some(rest)),
            None => (rest, None),
        };

        // Parse comma-separated host:port pairs
        let mut hosts: Vec<(String, u16)> = Vec::new();
        if hostport.is_empty() {
            // Empty host — will be set via ?host= parameter (Unix socket)
        } else {
            for entry in hostport.split(',') {
                let (h, p) = match entry.rsplit_once(':') {
                    Some((h, p)) => {
                        let port: u16 = p
                            .parse()
                            .map_err(|_| Error::Config(format!("invalid port: {p}")))?;
                        (h.to_string(), port)
                    }
                    None => (entry.to_string(), 5432),
                };
                hosts.push((h, p));
            }
        }

        let (database, params_str) = match db_and_params {
            Some(dp) => match dp.split_once('?') {
                Some((db, params)) => (percent_decode(db)?, Some(params.to_string())),
                None => (percent_decode(dp)?, None),
            },
            None => (String::new(), None),
        };

        let mut config = ConfigBuilder::new();
        for (h, p) in &hosts {
            config = config.host_port(h.clone(), *p);
        }
        config = config.database(database).user(user);

        if let Some(pw) = password {
            config = config.password(pw);
        }

        // Parse query parameters
        if let Some(params) = params_str {
            for param in params.split('&') {
                let (key, value) = param
                    .split_once('=')
                    .ok_or_else(|| Error::Config(format!("invalid parameter: {param}")))?;
                let value = percent_decode(value)?;

                match key {
                    "sslmode" => {
                        config = config.ssl_mode(match value.as_str() {
                            "disable" => SslMode::Disable,
                            "prefer" => SslMode::Prefer,
                            "require" => SslMode::Require,
                            "verify-ca" => SslMode::VerifyCa,
                            "verify-full" => SslMode::VerifyFull,
                            _ => return Err(Error::Config(format!("invalid sslmode: {value}"))),
                        });
                    }
                    "application_name" => {
                        config = config.application_name(value);
                    }
                    "connect_timeout" => {
                        let secs: u64 = value.parse().map_err(|_| {
                            Error::Config(format!("invalid connect_timeout: {value}"))
                        })?;
                        config = config.connect_timeout(Duration::from_secs(secs));
                    }
                    "statement_timeout" => {
                        let secs: u64 = value.parse().map_err(|_| {
                            Error::Config(format!("invalid statement_timeout: {value}"))
                        })?;
                        config = config.statement_timeout(Duration::from_secs(secs));
                    }
                    "target_session_attrs" => {
                        config = config.target_session_attrs(match value.as_str() {
                            "any" => TargetSessionAttrs::Any,
                            "read-write" => TargetSessionAttrs::ReadWrite,
                            "read-only" => TargetSessionAttrs::ReadOnly,
                            _ => {
                                return Err(Error::Config(format!(
                                    "invalid target_session_attrs: {value}"
                                )))
                            }
                        });
                    }
                    "sslcert" => {
                        config = config.ssl_client_cert(PathBuf::from(value));
                    }
                    "sslkey" => {
                        config = config.ssl_client_key(PathBuf::from(value));
                    }
                    "ssldirect" | "sslnegotiation" => {
                        let direct = match value.as_str() {
                            "true" | "direct" => true,
                            "false" | "postgres" => false,
                            _ => return Err(Error::Config(format!("invalid {key}: {value}"))),
                        };
                        config = config.ssl_direct(direct);
                    }
                    "channel_binding" => {
                        config = config.channel_binding(match value.as_str() {
                            "prefer" => ChannelBinding::Prefer,
                            "require" => ChannelBinding::Require,
                            "disable" => ChannelBinding::Disable,
                            _ => {
                                return Err(Error::Config(format!(
                                    "invalid channel_binding: {value}"
                                )))
                            }
                        });
                    }
                    "load_balance_hosts" => {
                        config = config.load_balance_hosts(match value.as_str() {
                            "disable" => LoadBalanceHosts::Disable,
                            "random" => LoadBalanceHosts::Random,
                            _ => {
                                return Err(Error::Config(format!(
                                    "invalid load_balance_hosts: {value}"
                                )))
                            }
                        });
                    }
                    "host" => {
                        // Support ?host=/var/run/postgresql for Unix sockets
                        config = config.host_port(value, 5432);
                    }
                    _ => {
                        // Ignore unknown parameters for forward compatibility
                    }
                }
            }
        }

        Ok(config.build())
    }

    /// Create a new builder for `Config`.
    pub fn builder() -> ConfigBuilder {
        ConfigBuilder::new()
    }

    // Accessor methods

    /// Returns the first host (for backward compatibility and single-host use).
    pub fn host(&self) -> &str {
        self.hosts.first().map_or("localhost", |(h, _)| h.as_str())
    }

    /// Returns the first port (for backward compatibility and single-host use).
    pub fn port(&self) -> u16 {
        self.hosts.first().map_or(5432, |(_, p)| *p)
    }

    /// Returns all configured host/port pairs.
    pub fn hosts(&self) -> &[(String, u16)] {
        &self.hosts
    }

    /// Load balancing strategy for multi-host connections.
    pub fn load_balance_hosts(&self) -> LoadBalanceHosts {
        self.load_balance_hosts
    }

    /// Target session attributes for connection routing.
    pub fn target_session_attrs(&self) -> TargetSessionAttrs {
        self.target_session_attrs
    }

    pub fn database(&self) -> &str {
        &self.database
    }

    pub fn user(&self) -> &str {
        &self.user
    }

    pub fn password(&self) -> Option<&str> {
        self.password.as_deref()
    }

    pub fn ssl_mode(&self) -> SslMode {
        self.ssl_mode
    }

    pub fn application_name(&self) -> Option<&str> {
        self.application_name.as_deref()
    }

    pub fn connect_timeout(&self) -> Duration {
        self.connect_timeout
    }

    pub fn statement_timeout(&self) -> Option<Duration> {
        self.statement_timeout
    }

    /// Path to client certificate for certificate authentication.
    pub fn ssl_client_cert(&self) -> Option<&std::path::Path> {
        self.ssl_client_cert.as_deref()
    }

    /// Path to client private key for certificate authentication.
    pub fn ssl_client_key(&self) -> Option<&std::path::Path> {
        self.ssl_client_key.as_deref()
    }

    /// Whether direct TLS (PG 17+) is enabled.
    pub fn ssl_direct(&self) -> bool {
        self.ssl_direct
    }

    /// Channel binding preference for SCRAM authentication.
    pub fn channel_binding(&self) -> ChannelBinding {
        self.channel_binding
    }
}

/// Builder for [`Config`].
#[derive(Debug, Clone)]
pub struct ConfigBuilder {
    hosts: Vec<(String, u16)>,
    default_port: u16,
    database: String,
    user: String,
    password: Option<String>,
    ssl_mode: SslMode,
    application_name: Option<String>,
    connect_timeout: Duration,
    statement_timeout: Option<Duration>,
    keepalive: Option<Duration>,
    keepalive_idle: Option<Duration>,
    target_session_attrs: TargetSessionAttrs,
    extra_float_digits: Option<i32>,
    load_balance_hosts: LoadBalanceHosts,
    ssl_client_cert: Option<PathBuf>,
    ssl_client_key: Option<PathBuf>,
    ssl_direct: bool,
    channel_binding: ChannelBinding,
}

impl ConfigBuilder {
    fn new() -> Self {
        Self {
            hosts: Vec::new(),
            default_port: 5432,
            database: String::new(),
            user: String::new(),
            password: None,
            ssl_mode: SslMode::default(),
            application_name: None,
            connect_timeout: Duration::from_secs(10),
            statement_timeout: None,
            keepalive: Some(Duration::from_secs(60)),
            keepalive_idle: None,
            target_session_attrs: TargetSessionAttrs::default(),
            extra_float_digits: Some(3),
            load_balance_hosts: LoadBalanceHosts::default(),
            ssl_client_cert: None,
            ssl_client_key: None,
            ssl_direct: false,
            channel_binding: ChannelBinding::default(),
        }
    }

    /// Append a host with the current default port.
    pub fn host(mut self, host: impl Into<String>) -> Self {
        self.hosts.push((host.into(), self.default_port));
        self
    }

    /// Append a host with a specific port.
    pub fn host_port(mut self, host: impl Into<String>, port: u16) -> Self {
        self.hosts.push((host.into(), port));
        self
    }

    /// Set the default port for subsequent `.host()` calls and update
    /// any hosts that still have the old default port.
    pub fn port(mut self, port: u16) -> Self {
        let old_default = self.default_port;
        self.default_port = port;
        for (_, p) in &mut self.hosts {
            if *p == old_default {
                *p = port;
            }
        }
        self
    }

    pub fn load_balance_hosts(mut self, strategy: LoadBalanceHosts) -> Self {
        self.load_balance_hosts = strategy;
        self
    }

    pub fn database(mut self, database: impl Into<String>) -> Self {
        self.database = database.into();
        self
    }

    pub fn user(mut self, user: impl Into<String>) -> Self {
        self.user = user.into();
        self
    }

    pub fn password(mut self, password: impl Into<String>) -> Self {
        self.password = Some(password.into());
        self
    }

    pub fn ssl_mode(mut self, ssl_mode: SslMode) -> Self {
        self.ssl_mode = ssl_mode;
        self
    }

    pub fn application_name(mut self, name: impl Into<String>) -> Self {
        self.application_name = Some(name.into());
        self
    }

    pub fn connect_timeout(mut self, timeout: Duration) -> Self {
        self.connect_timeout = timeout;
        self
    }

    pub fn statement_timeout(mut self, timeout: Duration) -> Self {
        self.statement_timeout = Some(timeout);
        self
    }

    pub fn keepalive(mut self, interval: Duration) -> Self {
        self.keepalive = Some(interval);
        self
    }

    pub fn target_session_attrs(mut self, attrs: TargetSessionAttrs) -> Self {
        self.target_session_attrs = attrs;
        self
    }

    /// Set the path to the client certificate file for certificate authentication.
    pub fn ssl_client_cert(mut self, path: impl Into<PathBuf>) -> Self {
        self.ssl_client_cert = Some(path.into());
        self
    }

    /// Set the path to the client private key file for certificate authentication.
    pub fn ssl_client_key(mut self, path: impl Into<PathBuf>) -> Self {
        self.ssl_client_key = Some(path.into());
        self
    }

    /// Enable direct TLS connection (PG 17+), skipping SSLRequest negotiation.
    pub fn ssl_direct(mut self, direct: bool) -> Self {
        self.ssl_direct = direct;
        self
    }

    /// Set the channel binding preference for SCRAM authentication.
    pub fn channel_binding(mut self, binding: ChannelBinding) -> Self {
        self.channel_binding = binding;
        self
    }

    /// Build the final `Config`.
    pub fn build(self) -> Config {
        let hosts = if self.hosts.is_empty() {
            vec![("localhost".to_string(), self.default_port)]
        } else {
            self.hosts
        };
        Config {
            hosts,
            database: self.database,
            user: self.user,
            password: self.password,
            ssl_mode: self.ssl_mode,
            application_name: self.application_name,
            connect_timeout: self.connect_timeout,
            statement_timeout: self.statement_timeout,
            _keepalive: self.keepalive,
            _keepalive_idle: self.keepalive_idle,
            target_session_attrs: self.target_session_attrs,
            _extra_float_digits: self.extra_float_digits,
            load_balance_hosts: self.load_balance_hosts,
            ssl_client_cert: self.ssl_client_cert,
            ssl_client_key: self.ssl_client_key,
            ssl_direct: self.ssl_direct,
            channel_binding: self.channel_binding,
        }
    }
}

/// Percent-decode a URL component.
fn percent_decode(s: &str) -> Result<String> {
    let mut result = String::with_capacity(s.len());
    let mut chars = s.as_bytes().iter();

    while let Some(&b) = chars.next() {
        if b == b'%' {
            let hi = chars
                .next()
                .ok_or_else(|| Error::Config("incomplete percent encoding".into()))?;
            let lo = chars
                .next()
                .ok_or_else(|| Error::Config("incomplete percent encoding".into()))?;
            let byte = hex_digit(*hi)? << 4 | hex_digit(*lo)?;
            result.push(byte as char);
        } else {
            result.push(b as char);
        }
    }

    Ok(result)
}

fn hex_digit(b: u8) -> Result<u8> {
    match b {
        b'0'..=b'9' => Ok(b - b'0'),
        b'a'..=b'f' => Ok(b - b'a' + 10),
        b'A'..=b'F' => Ok(b - b'A' + 10),
        _ => Err(Error::Config(format!("invalid hex digit: {}", b as char))),
    }
}