servlet 1.2.0

A small application runtime with an explicit operational contract
Documentation
//! Native options are built only after explicit ambient-policy checks.

use super::{PgConfig, PgError, PgTls};
use sqlx::{
    ConnectOptions,
    postgres::{PgConnectOptions, PgSslMode},
};

impl PgConfig {
    pub(super) fn check_ambient_policy() -> Result<(), PgError> {
        // SQLx 0.9 has no environment-free options constructor. Reject the inputs
        // it consumes; do not mutate the process environment or expose their values.
        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),
        })
    }
}