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
//!
//! Connection string parsing with support for service file
//! and a subset of psql environment variables.
//!
//! ## Environment variables
//!
//! * `PGSERVICE` - Name of the postgres service used for connection params.
//! * `PGSYSCONFDIR` - Location of the service files.
//! * `PGSERVICEFILE` - Name of the service file.
//! * `PGHOST` - behaves the same as the `host` connection parameter.
//! * `PGPORT` - behaves the same as the `port` connection parameter.
//! * `PGDATABASE` - behaves the same as the `dbname` connection parameter.
//! * `PGUSER` - behaves the same as the user connection parameter.
//! * `PGOPTIONS` - behaves the same as the `options` parameter.
//! * `PGAPPNAME` - behaves the same as the `application_name` connection parameter.
//! * `PGCONNECT_TIMEOUT` - behaves the same as the `connect_timeout` connection parameter.
//! * `PGPASSFILE` - Specifies the name of the file used to store password.
//!
//! ## Passfile support 
//!
//! Passfile is actually supported only on linux platform
//!
//! ## Example
//!
//! ```no_run
//! use pg_client_config::load_config;
//!
//! let config = load_config(Some("service=myservice")).unwrap();
//! println!("{config:#?}");
//! ```
//!
//! ## See also
//!
//! * [Pg service file](https://www.postgresql.org/docs/current/libpq-pgservice.html)
//! * [Pg pass file](https://www.postgresql.org/docs/current/libpq-pgpass.html)
//!

use ini::Ini;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::time::Duration;
use tokio_postgres::config::{ChannelBinding, Config, SslMode};

#[cfg(all(target_family = "unix", feature = "with-passfile"))]
mod passfile;

#[cfg(not(all(target_family = "unix", feature = "with-passfile")))]
mod passfile {
    use super::*;
    pub(crate) fn get_password_from_passfile(_: &mut Config) -> Result<()> {
        Ok(())
    }
}

/// Error while parsing service file or
/// retrieving parameter from environment
#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("IO Error")]
    IOError(#[from] std::io::Error),
    #[error("Service File Error")]
    PgServiceFileError(#[from] ini::Error),
    #[error("Service file not found: {0}")]
    PgServiceFileNotFound(String),
    #[error("Definition of service {0} not found")]
    PgServiceNotFound(String),
    #[error("Invalid ssl mode, expecting 'prefer', 'require' or 'disable': found '{0}'")]
    InvalidSslMode(String),
    #[error("Invalid port, expecting integer, found '{0}'")]
    InvalidPort(String),
    #[error("Invalid connect_timeout, expecting number of secs, found '{0}'")]
    InvalidConnectTimeout(String),
    #[error("Invalid keepalives, '1' or '0', found '{0}'")]
    InvalidKeepalives(String),
    #[error("Invalid keepalives, expecting number of secs, found '{0}'")]
    InvalidKeepalivesIdle(String),
    #[error("Invalid Channel Binding, expecting 'prefer', 'require' or 'disable': found '{0}'")]
    InvalidChannelBinding(String),
    #[error("Missing service name in connection string")]
    MissingServiceName,
    #[error("Postgres config error")]
    PostgresConfig(#[from] tokio_postgres::Error),
    #[error("Invalid passfile mode")]
    InvalidPassFileMode,
    #[error("Error parsing passfile")]
    PassfileParseError,
    #[error("Pass file not found: {0}")]
    PgPassFileNotFound(String),
}

pub type Result<T, E = Error> = std::result::Result<T, E>;

/// Load postgres connection configuration
///
/// The configuration will handle PG environment variable.
///
/// If the connection string start with `service=<service>`
/// the service will be searched in the file given by `PGSERVICEFILE`,
/// or in `~/.pg_service.conf` and `PGSYSCONFDIR/pg_service.conf`.
/// The remaining of the connection string is used directly for
/// initializing [`Config`]
///
/// If the connection string do no start with `service=` the connection
/// string is directly passed to [`Config`] along with parameters
/// defined for any service defined in `PGSERVICE`.
///
/// If the connection string is None the [`Config`] is initialized
/// from environment variables and/or service defined in `PGSERVICE`.
///
/// In all cases, parameters from the connection string take precedence.
///
pub fn load_config(config: Option<&str>) -> Result<Config> {
    fn load_service_config(service: &str, cnxstr: &str) -> Result<Config> {
        let mut config = if cnxstr.is_empty() {
            Config::new()
        } else {
            Config::from_str(cnxstr)?
        };
        load_config_from_service(&mut config, service)?;
        load_config_from_env(&mut config)?;
        Ok(config)
    }

    if let Some(cnxstr) = config {
        let cnxstr = cnxstr.trim_start();
        if cnxstr.starts_with("service=") {
            // Get the service name
            // Assume the the tail is valid connection string
            if let Some((service, tail)) = cnxstr.split_once('=').map(|(_, tail)| {
                tail.split_once(|c: char| c.is_whitespace())
                    .unwrap_or((tail, ""))
            }) {
                load_service_config(service, tail.trim())
            } else {
                Err(Error::MissingServiceName)
            }
        } else if let Ok(service) = std::env::var("PGSERVICE") {
            // Service file defined
            // But overridable from connection string
            load_service_config(&service, cnxstr)
        } else {
            // No service defined
            let mut config = Config::from_str(cnxstr)?;
            load_config_from_env(&mut config)?;
            Ok(config)
        }
    } else if let Ok(service) = std::env::var("PGSERVICE") {
        load_service_config(&service, "")
    } else {
        // No service defined
        // Initialize from env vars.
        let mut config = Config::new();
        load_config_from_env(&mut config)?;
        Ok(config)
    }
    .and_then(|mut config| {
        if config.get_password().is_none() {
            passfile::get_password_from_passfile(&mut config)?;
        }
        Ok(config)
    })
}

/// Load connection parameters from service config_file
fn load_config_from_service(config: &mut Config, service_name: &str) -> Result<()> {
    fn user_service_file() -> Option<PathBuf> {
        std::env::var("PGSERVICEFILE")
            .map(|path| Path::new(&path).into())
            .or_else(|_| {
                std::env::var("HOME").map(|path| Path::new(&path).join(".pg_service.conf"))
            })
            .ok()
    }

    fn sysconf_service_file() -> Option<PathBuf> {
        std::env::var("PGSYSCONFDIR")
            .map(|path| Path::new(&path).join("pg_service.conf"))
            .ok()
    }

    fn get_service_params(config: &mut Config, path: &Path, service_name: &str) -> Result<bool> {
        if path.exists() {
            Ini::load_from_file(path)
                .map_err(Error::from)
                .and_then(|ini| {
                    if let Some(params) = ini.section(Some(service_name)) {
                        params
                            .iter()
                            .try_for_each(|(k, v)| set_parameter(config, k, v))
                            .map(|_| true)
                    } else {
                        Ok(false)
                    }
                })
        } else {
            Err(Error::PgServiceFileNotFound(
                path.to_string_lossy().into_owned(),
            ))
        }
    }

    let found = match user_service_file().and_then(|p| p.as_path().exists().then_some(p)) {
        Some(path) => get_service_params(config, &path, service_name)?,
        None => false,
    } || match sysconf_service_file() {
        Some(path) => get_service_params(config, &path, service_name)?,
        None => false,
    };

    if !found {
        Err(Error::PgServiceNotFound(service_name.into()))
    } else {
        Ok(())
    }
}

/// Load configuration from environment variables
fn load_config_from_env(config: &mut Config) -> Result<()> {
    static ENV: [(&str, &str); 7] = [
        ("PGHOST", "host"),
        ("PGPORT", "port"),
        ("PGDATABASE", "dbname"),
        ("PGUSER", "user"),
        ("PGOPTIONS", "options"),
        ("PGAPPNAME", "application_name"),
        ("PGCONNECT_TIMEOUT", "connect_timeout"),
    ];

    ENV.iter().try_for_each(|(varname, k)| {
        if let Ok(v) = std::env::var(varname) {
            set_parameter(config, k, &v)
        } else {
            Ok(())
        }
    })
}

fn set_parameter(config: &mut Config, k: &str, v: &str) -> Result<()> {
    fn parse_ssl_mode(mode: &str) -> Result<SslMode> {
        match mode {
            "disable" => Ok(SslMode::Disable),
            "prefer" => Ok(SslMode::Prefer),
            "require" => Ok(SslMode::Require),
            _ => Err(Error::InvalidSslMode(mode.into())),
        }
    }

    fn parse_channel_binding(mode: &str) -> Result<ChannelBinding> {
        match mode {
            "disable" => Ok(ChannelBinding::Disable),
            "prefer" => Ok(ChannelBinding::Prefer),
            "require" => Ok(ChannelBinding::Require),
            _ => Err(Error::InvalidChannelBinding(mode.into())),
        }
    }

    match k {
        // The following values may be set from
        // environment variables
        "user" => {
            if config.get_user().is_none() {
                config.user(v);
            }
        }
        "password" => {
            if config.get_password().is_none() {
                config.password(v);
            }
        }
        "dbname" => {
            if config.get_dbname().is_none() {
                config.dbname(v);
            }
        }
        "options" => {
            if config.get_options().is_none() {
                config.options(v);
            }
        }
        "host" | "hostaddr" => {
            if config.get_hosts().is_empty() {
                config.host(v);
            }
        }
        "port" => {
            if config.get_ports().is_empty() {
                config.port(v.parse().map_err(|_| Error::InvalidPort(v.into()))?);
            }
        }
        "connect_timeout" => {
            if config.get_connect_timeout().is_none() {
                config.connect_timeout(Duration::from_secs(
                    v.parse()
                        .map_err(|_| Error::InvalidConnectTimeout(v.into()))?,
                ));
            }
        }
        // The following are not set from environment variables
        // values are always overriden (i.e service configuration takes
        // precedence)
        "sslmode" => {
            config.ssl_mode(parse_ssl_mode(v)?);
        }
        "keepalives" => {
            config.keepalives(match v {
                "1" => Ok(true),
                "0" => Ok(false),
                _ => Err(Error::InvalidKeepalives(v.into())),
            }?);
        }
        "keepalives_idle" => {
            config.keepalives_idle(Duration::from_secs(
                v.parse()
                    .map_err(|_| Error::InvalidKeepalivesIdle(v.into()))?,
            ));
        }
        "channel_binding" => {
            config.channel_binding(parse_channel_binding(v)?);
        }
        _ => (),
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use tokio_postgres::config::Host;

    #[test]
    fn from_environment() {
        std::env::set_var("PGUSER", "foo");
        std::env::set_var("PGHOST", "foo.com");
        std::env::set_var("PGDATABASE", "foodb");
        std::env::set_var("PGPORT", "1234");

        let config = load_config(None).unwrap();

        assert_eq!(config.get_user(), Some("foo"));
        assert_eq!(config.get_ports(), [1234]);
        assert_eq!(config.get_hosts(), [Host::Tcp("foo.com".into())]);
        assert_eq!(config.get_dbname(), Some("foodb"));
    }

    #[test]
    fn from_service_file() {
        std::env::set_var(
            "PGSYSCONFDIR",
            Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap())
                .join("fixtures")
                .to_str()
                .unwrap(),
        );

        let config = load_config(Some("service=bar")).unwrap();

        assert_eq!(config.get_user(), Some("bar"));
        assert_eq!(config.get_ports(), [1234]);
        assert_eq!(config.get_hosts(), [Host::Tcp("bar.com".into())]);
        assert_eq!(config.get_dbname(), Some("bardb"));
    }

    #[test]
    fn service_override() {
        std::env::set_var(
            "PGSYSCONFDIR",
            Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap())
                .join("fixtures")
                .to_str()
                .unwrap(),
        );

        let config = load_config(Some("service=bar user=baz")).unwrap();

        assert_eq!(config.get_user(), Some("baz"));
    }
}