use std::fmt;
use std::path::PathBuf;
use std::time::Duration;
use crate::error::{PgError, PgResult};
#[non_exhaustive]
#[derive(Clone)]
pub struct PgConfig {
pub url: String,
pub host: String,
pub port: u16,
pub database: String,
pub user: String,
pub password: Option<String>,
pub ssl_mode: SslMode,
pub ssl_root_cert: Option<PathBuf>,
pub connect_timeout: Duration,
pub statement_timeout: Option<Duration>,
pub application_name: Option<String>,
pub options: Vec<(String, String)>,
}
impl fmt::Debug for PgConfig {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PgConfig")
.field("url", &redact_url(&self.url, self.password.as_deref()))
.field("host", &self.host)
.field("port", &self.port)
.field("database", &self.database)
.field("user", &self.user)
.field("password", &self.password.as_ref().map(|_| REDACTED))
.field("ssl_mode", &self.ssl_mode)
.field("ssl_root_cert", &self.ssl_root_cert)
.field("connect_timeout", &self.connect_timeout)
.field("statement_timeout", &self.statement_timeout)
.field("application_name", &self.application_name)
.field(
"options",
&self
.options
.iter()
.map(|(key, value)| {
let value = if is_password_key(key) {
REDACTED
} else {
value
};
(key.as_str(), value)
})
.collect::<Vec<_>>(),
)
.finish()
}
}
const REDACTED: &str = "REDACTED";
pub(crate) fn redact_url(url: &str, password: Option<&str>) -> String {
let Some(start) = url.find("://").map(|i| i + 3) else {
return url.to_owned();
};
let redacted = redact_userinfo(url, start, password);
redact_query_passwords(&redacted, start)
}
fn redact_userinfo(url: &str, start: usize, password: Option<&str>) -> String {
if let Some(password) = password.filter(|p| !p.is_empty())
&& let Some(redacted) = splice_password(url, start, password)
{
return redacted;
}
match url::Url::parse(url) {
Ok(parsed) => match parsed.password() {
Some(password) => splice_password(url, start, password)
.unwrap_or_else(|| redact_to_last_at(url, start)),
None if parsed.username().is_empty() && url[start..].contains('@') => {
redact_to_last_at(url, start)
}
None => url.to_owned(),
},
Err(_) => redact_to_last_at(url, start),
}
}
fn redact_query_passwords(url: &str, start: usize) -> String {
let Some(query_start) = url[start..].find('?').map(|i| start + i + 1) else {
return url.to_owned();
};
let query_end = url[query_start..]
.find('#')
.map_or(url.len(), |i| query_start + i);
let query = url[query_start..query_end]
.split('&')
.map(|pair| {
let key = pair.split_once('=').map_or(pair, |(key, _)| key);
if is_password_key(key) {
format!("{key}={REDACTED}")
} else {
pair.to_owned()
}
})
.collect::<Vec<_>>()
.join("&");
format!("{}{query}{}", &url[..query_start], &url[query_end..])
}
fn is_password_key(key: &str) -> bool {
url::form_urlencoded::parse(key.as_bytes())
.next()
.is_some_and(|(key, _)| key.eq_ignore_ascii_case("password"))
}
fn splice_password(url: &str, start: usize, password: &str) -> Option<String> {
let from = start + url[start..].find(':')? + 1;
let after = url[from..].strip_prefix(password)?;
after
.starts_with('@')
.then(|| format!("{}{REDACTED}{after}", &url[..from]))
}
fn redact_to_last_at(url: &str, start: usize) -> String {
match url[start..].rfind('@') {
Some(at) => format!("{}{REDACTED}{}", &url[..start], &url[start + at..]),
None => url.to_owned(),
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SslMode {
Disable,
#[default]
Prefer,
Require,
VerifyCa,
VerifyFull,
}
fn is_valid_guc_key(key: &str) -> bool {
let mut chars = key.chars();
matches!(chars.next(), Some(c) if c.is_ascii_alphabetic() || c == '_')
&& chars.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '.')
}
fn is_safe_guc_value(value: &str) -> bool {
!value
.chars()
.any(|c| c.is_whitespace() || c == '\\' || c == '\'')
}
const PERCENT_ENCODE_HINT: &str = "percent-encode reserved characters in the password \
(`@` as %40, `/` as %2F, `?` as %3F, `#` as %23, `:` as %3A)";
fn reject_unencoded_userinfo(parsed: &url::Url) -> PgResult<()> {
let in_query_key = parsed.query().is_some_and(|query| {
query.split('&').any(|pair| {
pair.split_once('=')
.map_or(pair, |(key, _)| key)
.contains('@')
})
});
if parsed.path().contains('@')
|| parsed
.fragment()
.is_some_and(|fragment| fragment.contains('@'))
|| in_query_key
{
return Err(PgError::config(format!(
"invalid database URL: an `@` follows the host, so the password \
seems to contain unencoded reserved characters; {PERCENT_ENCODE_HINT}"
)));
}
Ok(())
}
fn percent_decode(component: &str, what: &str) -> PgResult<String> {
percent_encoding::percent_decode_str(component)
.decode_utf8()
.map(|decoded| decoded.into_owned())
.map_err(|_| {
PgError::config(format!(
"invalid database URL: the {what} is not valid UTF-8 once percent-decoded"
))
})
}
impl PgConfig {
pub fn from_url(url: impl Into<String>) -> PgResult<Self> {
let url = url.into();
let parsed = url::Url::parse(&url).map_err(|e| {
let hint = if url.contains('@') {
format!("; {PERCENT_ENCODE_HINT}")
} else {
String::new()
};
PgError::config(format!("invalid database URL: {e}{hint}"))
})?;
reject_unencoded_userinfo(&parsed)?;
if parsed.scheme() != "postgresql" && parsed.scheme() != "postgres" {
return Err(PgError::config(format!(
"invalid scheme: expected 'postgresql' or 'postgres', got '{}'",
parsed.scheme()
)));
}
let host = parsed
.host_str()
.ok_or_else(|| PgError::config("missing host in URL"))?
.to_string();
let port = parsed.port().unwrap_or(5432);
let database = percent_decode(parsed.path().trim_start_matches('/'), "database name")?;
if database.is_empty() {
return Err(PgError::config("missing database name in URL"));
}
let mut user = if parsed.username().is_empty() {
"postgres".to_string()
} else {
percent_decode(parsed.username(), "user name")?
};
let mut password = parsed
.password()
.map(|password| percent_decode(password, "password"))
.transpose()?;
for pair in parsed.query().unwrap_or_default().split('&') {
let (key, value) = pair.split_once('=').unwrap_or((pair, ""));
match percent_decode(key, "query parameter name")?.as_str() {
"user" => user = percent_decode(value, "user name")?,
"password" => password = Some(percent_decode(value, "password")?),
_ => {}
}
}
let mut ssl_mode = SslMode::Prefer;
let mut connect_timeout = Duration::from_secs(30);
let mut statement_timeout = None;
let mut application_name = None;
let mut ssl_root_cert = None;
let mut options = Vec::new();
for (key, value) in parsed.query_pairs() {
let key_str: &str = &key;
let value_str: &str = &value;
match key_str {
"sslmode" => {
ssl_mode = match value_str {
"disable" => SslMode::Disable,
"prefer" => SslMode::Prefer,
"require" => SslMode::Require,
"verify-ca" => SslMode::VerifyCa,
"verify-full" => SslMode::VerifyFull,
other => {
return Err(PgError::config(format!("invalid sslmode: {}", other)));
}
};
}
"connect_timeout" => {
let secs: u64 = value_str
.parse()
.map_err(|_| PgError::config("invalid connect_timeout"))?;
connect_timeout = Duration::from_secs(secs);
}
"statement_timeout" => {
let ms: u64 = value_str
.parse()
.map_err(|_| PgError::config("invalid statement_timeout"))?;
statement_timeout = Some(Duration::from_millis(ms));
}
"application_name" => {
application_name = Some(value_str.to_string());
}
"sslrootcert" => {
ssl_root_cert = Some(PathBuf::from(value_str));
}
"user" | "password" => {}
_ => {
options.push((key_str.to_string(), value_str.to_string()));
}
}
}
Ok(Self {
url,
host,
port,
database,
user,
password,
ssl_mode,
ssl_root_cert,
connect_timeout,
statement_timeout,
application_name,
options,
})
}
pub fn builder() -> PgConfigBuilder {
PgConfigBuilder::new()
}
pub fn to_pg_config(&self) -> tokio_postgres::Config {
let mut config = tokio_postgres::Config::new();
config.host(&self.host);
config.port(self.port);
config.dbname(&self.database);
config.user(&self.user);
if let Some(ref password) = self.password {
config.password(password);
}
if let Some(ref app_name) = self.application_name {
config.application_name(app_name);
}
config.connect_timeout(self.connect_timeout);
let driver_ssl_mode = match self.ssl_mode {
SslMode::Disable => tokio_postgres::config::SslMode::Disable,
SslMode::Prefer => tokio_postgres::config::SslMode::Prefer,
SslMode::Require | SslMode::VerifyCa | SslMode::VerifyFull => {
tokio_postgres::config::SslMode::Require
}
};
config.ssl_mode(driver_ssl_mode);
let mut options = Vec::new();
if let Some(timeout) = self.statement_timeout {
options.push(format!("-c statement_timeout={}", timeout.as_millis()));
}
for (key, value) in &self.options {
if !is_valid_guc_key(key) {
tracing::warn!(key = %key, "dropping connection option with invalid GUC name");
continue;
}
if !is_safe_guc_value(value) {
tracing::warn!(
key = %key,
"dropping connection option whose value contains whitespace or quoting characters"
);
continue;
}
options.push(format!("-c {}={}", key, value));
}
if !options.is_empty() {
config.options(options.join(" "));
}
config
}
}
#[derive(Default)]
pub struct PgConfigBuilder {
url: Option<String>,
host: Option<String>,
port: Option<u16>,
database: Option<String>,
user: Option<String>,
password: Option<String>,
ssl_mode: Option<SslMode>,
ssl_root_cert: Option<PathBuf>,
connect_timeout: Option<Duration>,
statement_timeout: Option<Duration>,
application_name: Option<String>,
}
impl fmt::Debug for PgConfigBuilder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PgConfigBuilder")
.field(
"url",
&self
.url
.as_deref()
.map(|url| redact_url(url, self.password.as_deref())),
)
.field("host", &self.host)
.field("port", &self.port)
.field("database", &self.database)
.field("user", &self.user)
.field("password", &self.password.as_ref().map(|_| REDACTED))
.field("ssl_mode", &self.ssl_mode)
.field("ssl_root_cert", &self.ssl_root_cert)
.field("connect_timeout", &self.connect_timeout)
.field("statement_timeout", &self.statement_timeout)
.field("application_name", &self.application_name)
.finish()
}
}
impl PgConfigBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn url(mut self, url: impl Into<String>) -> Self {
self.url = Some(url.into());
self
}
pub fn host(mut self, host: impl Into<String>) -> Self {
self.host = Some(host.into());
self
}
pub fn port(mut self, port: u16) -> Self {
self.port = Some(port);
self
}
pub fn database(mut self, database: impl Into<String>) -> Self {
self.database = Some(database.into());
self
}
pub fn user(mut self, user: impl Into<String>) -> Self {
self.user = Some(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, mode: SslMode) -> Self {
self.ssl_mode = Some(mode);
self
}
pub fn ssl_root_cert(mut self, path: impl Into<PathBuf>) -> Self {
self.ssl_root_cert = Some(path.into());
self
}
pub fn connect_timeout(mut self, timeout: Duration) -> Self {
self.connect_timeout = Some(timeout);
self
}
pub fn statement_timeout(mut self, timeout: Duration) -> Self {
self.statement_timeout = Some(timeout);
self
}
pub fn application_name(mut self, name: impl Into<String>) -> Self {
self.application_name = Some(name.into());
self
}
pub fn build(self) -> PgResult<PgConfig> {
if let Some(url) = self.url {
let mut config = PgConfig::from_url(url)?;
if let Some(host) = self.host {
config.host = host;
}
if let Some(port) = self.port {
config.port = port;
}
if let Some(database) = self.database {
config.database = database;
}
if let Some(user) = self.user {
config.user = user;
}
if let Some(password) = self.password {
config.password = Some(password);
}
if let Some(ssl_root_cert) = self.ssl_root_cert {
config.ssl_root_cert = Some(ssl_root_cert);
}
if let Some(ssl_mode) = self.ssl_mode {
config.ssl_mode = ssl_mode;
}
if let Some(timeout) = self.connect_timeout {
config.connect_timeout = timeout;
}
if let Some(timeout) = self.statement_timeout {
config.statement_timeout = Some(timeout);
}
if let Some(name) = self.application_name {
config.application_name = Some(name);
}
Ok(config)
} else {
let host = self.host.unwrap_or_else(|| "localhost".to_string());
let port = self.port.unwrap_or(5432);
let database = self
.database
.ok_or_else(|| PgError::config("database name is required"))?;
let user = self.user.unwrap_or_else(|| "postgres".to_string());
let url = format!(
"postgresql://{}{}@{}:{}/{}",
user,
self.password
.as_ref()
.map(|p| format!(":{}", p))
.unwrap_or_default(),
host,
port,
database
);
Ok(PgConfig {
url,
host,
port,
database,
user,
password: self.password,
ssl_mode: self.ssl_mode.unwrap_or_default(),
ssl_root_cert: self.ssl_root_cert,
connect_timeout: self.connect_timeout.unwrap_or(Duration::from_secs(30)),
statement_timeout: self.statement_timeout,
application_name: self.application_name,
options: Vec::new(),
})
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_config_from_url() {
let config = PgConfig::from_url("postgresql://user:pass@localhost:5432/mydb").unwrap();
assert_eq!(config.host, "localhost");
assert_eq!(config.port, 5432);
assert_eq!(config.database, "mydb");
assert_eq!(config.user, "user");
assert_eq!(config.password, Some("pass".to_string()));
}
#[test]
fn test_config_from_url_with_params() {
let config =
PgConfig::from_url("postgresql://localhost/mydb?sslmode=require&application_name=prax")
.unwrap();
assert_eq!(config.ssl_mode, SslMode::Require);
assert_eq!(config.application_name, Some("prax".to_string()));
}
#[test]
fn test_to_pg_config_applies_statement_timeout_and_options() {
let config = PgConfig::from_url(
"postgresql://localhost/mydb?statement_timeout=5000&search_path=public",
)
.unwrap();
let pg_config = config.to_pg_config();
assert_eq!(
pg_config.get_options(),
Some("-c statement_timeout=5000 -c search_path=public")
);
}
#[test]
fn test_to_pg_config_without_timeouts_or_options_sets_none() {
let config = PgConfig::from_url("postgresql://localhost/mydb").unwrap();
let pg_config = config.to_pg_config();
assert_eq!(pg_config.get_options(), None);
}
#[test]
fn test_to_pg_config_drops_option_with_smuggled_value() {
let config =
PgConfig::from_url("postgresql://localhost/mydb?x=1%20-c%20search_path%3Devil")
.unwrap();
let pg_config = config.to_pg_config();
assert_eq!(pg_config.get_options(), None);
}
#[test]
fn test_to_pg_config_drops_option_with_invalid_key() {
let config =
PgConfig::from_url("postgresql://localhost/mydb?bad%20key=1&search_path=public")
.unwrap();
let pg_config = config.to_pg_config();
assert_eq!(pg_config.get_options(), Some("-c search_path=public"));
}
#[test]
fn test_to_pg_config_drops_option_with_quoting_chars() {
let config = PgConfig::from_url("postgresql://localhost/mydb?a=b%5Cc&d=e%27f").unwrap();
let pg_config = config.to_pg_config();
assert_eq!(pg_config.get_options(), None);
}
#[test]
fn parses_sslrootcert_from_the_url() {
let config = PgConfig::from_url(
"postgresql://localhost/mydb?sslmode=verify-full&sslrootcert=/etc/ssl/rds.pem",
)
.unwrap();
assert_eq!(
config.ssl_root_cert,
Some(std::path::PathBuf::from("/etc/ssl/rds.pem"))
);
assert!(!config.options.iter().any(|(k, _)| k == "sslrootcert"));
}
#[test]
fn sslrootcert_defaults_to_none() {
let config = PgConfig::from_url("postgresql://localhost/mydb").unwrap();
assert_eq!(config.ssl_root_cert, None);
}
#[test]
fn builder_sets_sslrootcert() {
let config = PgConfig::builder()
.url("postgresql://localhost/mydb")
.ssl_root_cert("/etc/ssl/override.pem")
.build()
.unwrap();
assert_eq!(
config.ssl_root_cert,
Some(std::path::PathBuf::from("/etc/ssl/override.pem"))
);
}
#[test]
fn test_to_pg_config_maps_sslmode_require() {
let config = PgConfig::from_url("postgresql://localhost/mydb?sslmode=require").unwrap();
let pg_config = config.to_pg_config();
assert_eq!(
pg_config.get_ssl_mode(),
tokio_postgres::config::SslMode::Require
);
}
#[test]
fn test_from_url_parses_verify_modes() {
let config = PgConfig::from_url("postgresql://localhost/mydb?sslmode=verify-ca").unwrap();
assert_eq!(config.ssl_mode, SslMode::VerifyCa);
let pg_config = config.to_pg_config();
assert_eq!(
pg_config.get_ssl_mode(),
tokio_postgres::config::SslMode::Require
);
let config = PgConfig::from_url("postgresql://localhost/mydb?sslmode=verify-full").unwrap();
assert_eq!(config.ssl_mode, SslMode::VerifyFull);
assert!(PgConfig::from_url("postgresql://localhost/mydb?sslmode=bogus").is_err());
}
#[test]
fn test_to_pg_config_maps_sslmode_disable() {
let config = PgConfig::from_url("postgresql://localhost/mydb?sslmode=disable").unwrap();
let pg_config = config.to_pg_config();
assert_eq!(
pg_config.get_ssl_mode(),
tokio_postgres::config::SslMode::Disable
);
}
#[test]
fn test_config_builder() {
let config = PgConfig::builder()
.host("localhost")
.port(5432)
.database("mydb")
.user("postgres")
.build()
.unwrap();
assert_eq!(config.host, "localhost");
assert_eq!(config.database, "mydb");
}
const SENTINEL: &str = "hunter2-sentinel";
#[test]
fn debug_redacts_the_password_and_the_url_password() {
let config = PgConfig::from_url(format!(
"postgres://prax:{SENTINEL}@db.internal:5432/prax?application_name=app"
))
.unwrap();
let debug = format!("{config:?}");
assert!(!debug.contains(SENTINEL), "{debug}");
for visible in [
"db.internal",
"5432",
"prax",
"application_name=app",
"REDACTED",
] {
assert!(debug.contains(visible), "{visible}: {debug}");
}
let alternate = format!("{config:#?}");
assert!(!alternate.contains(SENTINEL), "{alternate}");
}
#[test]
fn debug_redacts_unencoded_builder_passwords() {
let config = PgConfig::builder()
.host("db.internal")
.database("prax")
.user("prax")
.password(format!("{SENTINEL}@x/y"))
.build()
.unwrap();
let debug = format!("{config:?}");
assert!(!debug.contains("hunter2"), "{debug}");
assert!(!debug.contains("x/y"), "{debug}");
let mut config = config;
config.url = format!("postgres://prax:{SENTINEL}@bad host/prax");
let debug = format!("{config:?}");
assert!(!debug.contains(SENTINEL), "{debug}");
}
#[test]
fn url_redaction_touches_only_the_password() {
assert_eq!(
redact_url(
"postgres://postgres:postgres@localhost:5432/postgres",
Some("postgres")
),
"postgres://postgres:REDACTED@localhost:5432/postgres"
);
assert_eq!(
redact_url(
"postgres://app:a@db.internal:5432/app_db?sslmode=disable",
Some("a")
),
"postgres://app:REDACTED@db.internal:5432/app_db?sslmode=disable"
);
assert_eq!(
redact_url("postgres://app:a@db.internal/app_db", None),
"postgres://app:REDACTED@db.internal/app_db"
);
let config =
PgConfig::from_url("postgres://postgres:postgres@localhost:5432/postgres").unwrap();
let debug = format!("{config:?}");
assert!(
debug.contains("\"postgres://postgres:REDACTED@localhost:5432/postgres\""),
"{debug}"
);
assert!(debug.contains("database: \"postgres\""), "{debug}");
}
#[test]
fn url_redaction_without_a_password_hint() {
for url in [
format!("postgres://u:8/{SENTINEL}@host/db"),
format!("postgres://u:123#{SENTINEL}@host/db"),
format!("postgres://u:123?{SENTINEL}@host/db"),
] {
let redacted = redact_url(&url, None);
assert!(!redacted.contains(SENTINEL), "{url} -> {redacted}");
assert_eq!(redacted, "postgres://REDACTED@host/db", "{url}");
}
for url in [
format!("postgres://u@host/db?password={SENTINEL}&sslmode=disable"),
format!("postgres://u@host/db?sslmode=disable&PASSWORD={SENTINEL}#frag"),
format!("postgres://u@host/db?pass%77ord={SENTINEL}"),
] {
let redacted = redact_url(&url, None);
assert!(!redacted.contains(SENTINEL), "{url} -> {redacted}");
assert!(redacted.starts_with("postgres://u@host/db?"), "{redacted}");
}
assert_eq!(
redact_url(
&format!("postgres://u@host/db?password={SENTINEL}&sslmode=disable"),
None
),
"postgres://u@host/db?password=REDACTED&sslmode=disable"
);
assert_eq!(
redact_url("postgres://u@host/db", None),
"postgres://u@host/db"
);
}
#[test]
fn debug_redacts_password_options_and_query() {
let url = format!("postgres://u@db.internal/prax?password={SENTINEL}&search_path=app");
let config = PgConfig::from_url(&url).unwrap();
let debug = format!("{config:?}");
assert!(!debug.contains(SENTINEL), "{debug}");
assert!(debug.contains("search_path"), "{debug}");
let config =
PgConfig::from_url(format!("postgres://u@db.internal/prax?PASSWORD={SENTINEL}"))
.unwrap();
assert!(config.options.iter().any(|(k, _)| k == "PASSWORD"));
let debug = format!("{config:?}");
assert!(!debug.contains(SENTINEL), "{debug}");
let builder = crate::pool::PgPoolBuilder::new().url(&url);
let debug = format!("{builder:?}");
assert!(!debug.contains(SENTINEL), "{debug}");
let builder = crate::pool::PgPoolBuilder::new()
.url(format!("postgres://u:8/{SENTINEL}@db.internal/prax"));
let debug = format!("{builder:?}");
assert!(!debug.contains(SENTINEL), "{debug}");
}
#[test]
fn debug_keeps_urls_without_a_password() {
let config = PgConfig::from_url("postgres://prax@db.internal/prax").unwrap();
let debug = format!("{config:?}");
assert!(
debug.contains("postgres://prax@db.internal/prax"),
"{debug}"
);
assert!(debug.contains("password: None"), "{debug}");
}
#[test]
fn config_builder_debug_redacts_secrets() {
let builder = PgConfig::builder()
.url(format!("postgres://prax:{SENTINEL}@db.internal/prax"))
.password(SENTINEL);
let debug = format!("{builder:?}");
assert!(!debug.contains(SENTINEL), "{debug}");
assert!(debug.contains("db.internal"), "{debug}");
}
#[test]
fn from_url_rejects_passwords_with_unencoded_reserved_characters() {
for url in [
"postgres://u:8/Hx@host/db",
"postgres://u:123#456@host/db",
"postgres://u:123?x@host/db",
"postgres://u:a@b/c@host/db",
"postgres://u:8/x:y@host:5432/db?sslmode=disable",
] {
match PgConfig::from_url(url) {
Err(PgError::Config(message)) => {
assert!(message.contains("percent-encode"), "{url}: {message}");
assert!(
!message.contains("Hx") && !message.contains("456"),
"{message}"
);
}
other => panic!("{url}: expected a config error, got {other:?}"),
}
}
}
#[test]
fn from_url_decodes_percent_encoded_credentials_and_database() {
let config = PgConfig::from_url("postgres://u:8%2FHx@host/db").unwrap();
assert_eq!(
(
config.user.as_str(),
config.password.as_deref(),
config.host.as_str()
),
("u", Some("8/Hx"), "host")
);
assert_eq!((config.port, config.database.as_str()), (5432, "db"));
let config = PgConfig::from_url("postgres://u:123%23456@host:6543/db").unwrap();
assert_eq!(config.password.as_deref(), Some("123#456"));
assert_eq!(config.port, 6543);
let config =
PgConfig::from_url("postgres://app%40corp:p%40ss%3Aw%2B@host/my%40db").unwrap();
assert_eq!(config.user, "app@corp");
assert_eq!(config.password.as_deref(), Some("p@ss:w+"));
assert_eq!(config.database, "my@db");
let pg_config = config.to_pg_config();
assert_eq!(pg_config.get_password(), Some(&b"p@ss:w+"[..]));
assert_eq!(pg_config.get_user(), Some("app@corp"));
let config =
PgConfig::from_url("postgres://u:pw@host/db?application_name=me@work").unwrap();
assert_eq!(config.application_name.as_deref(), Some("me@work"));
assert_eq!(config.password.as_deref(), Some("pw"));
}
#[test]
fn from_url_takes_credentials_from_query_parameters() {
let config = PgConfig::from_url(format!(
"postgres://db.internal/prax?user=alice&password={SENTINEL}&sslmode=disable"
))
.unwrap();
assert_eq!(config.user, "alice");
assert_eq!(config.password.as_deref(), Some(SENTINEL));
assert_eq!(config.ssl_mode, SslMode::Disable);
assert!(config.options.is_empty(), "{:?}", config.options);
let pg_config = config.to_pg_config();
assert_eq!(pg_config.get_user(), Some("alice"));
assert_eq!(pg_config.get_password(), Some(SENTINEL.as_bytes()));
assert_eq!(pg_config.get_options(), None);
let debug = format!("{config:?}");
assert!(!debug.contains(SENTINEL), "{debug}");
assert!(debug.contains("alice"), "{debug}");
let config =
PgConfig::from_url("postgres://bob:old@host/db?password=n%26w+1&user=carol").unwrap();
assert_eq!(config.user, "carol");
assert_eq!(config.password.as_deref(), Some("n&w+1"));
assert!(config.options.is_empty(), "{:?}", config.options);
}
#[test]
fn test_config_invalid_scheme() {
let result = PgConfig::from_url("mysql://localhost/db");
assert!(result.is_err());
}
}