use super::{PgConfig, PgError, PgTls};
use sqlx::{
ConnectOptions,
postgres::{PgConnectOptions, PgSslMode},
};
impl PgConfig {
pub(super) fn check_ambient_policy() -> Result<(), PgError> {
if [
"PGPORT",
"PGHOSTADDR",
"PGHOST",
"PGUSER",
"PGDATABASE",
"PGPASSWORD",
"PGSSLROOTCERT",
"PGSSLCERT",
"PGSSLKEY",
"PGSSLMODE",
"PGAPPNAME",
"PGOPTIONS",
]
.iter()
.any(|name| std::env::var_os(name).is_some())
{
return Err(PgError::AmbientConfiguration);
}
Ok(())
}
pub(super) fn options(&self) -> Result<PgConnectOptions, PgError> {
Self::check_ambient_policy()?;
let endpoint = &self.endpoint;
let options = PgConnectOptions::new_without_pgpass()
.host(&endpoint.host)
.port(endpoint.port)
.username(&endpoint.username)
.password(&endpoint.password)
.database(&endpoint.database)
.application_name(&self.application_name)
.options(self.session.settings())
.disable_statement_logging();
Ok(match &self.tls {
PgTls::VerifyFull => options.ssl_mode(PgSslMode::VerifyFull),
PgTls::VerifyFullWithRoot(root) => options
.ssl_mode(PgSslMode::VerifyFull)
.ssl_root_cert_from_pem(root.clone()),
PgTls::PlaintextLoopback => options.ssl_mode(PgSslMode::Disable),
})
}
}