From 2b7490cea36dc22ba5579aa830d0b04c4edb77af Mon Sep 17 00:00:00 2001 From: Jonathan Frankel Date: Mon, 30 Nov 2026 20:02:58 +0800 Subject: [PATCH 1/2] feat(postgres): adding optional password and tls certs for postgres --- .envrc | 1 + Cargo.lock | 172 +++++++++++++- Cargo.toml | 4 +- flake.nix | 48 ++++ src/domain/postgres/cluster/database.rs | 48 +++- src/domain/postgres/connection.rs | 298 +++++++++++++++++++++++- src/domain/postgres/database.rs | 40 +++- src/services/config/builder.rs | 13 +- src/services/config/models.rs | 10 + 9 files changed, 609 insertions(+), 25 deletions(-) create mode 100644 .envrc create mode 100644 flake.nix diff --git a/.envrc b/.envrc new file mode 100644 index 00000000..3550a30f --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/Cargo.lock b/Cargo.lock index 18f22e97..33a6f907 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -95,6 +95,45 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "03918c3dbd7701a85c6b9887732e2921175f26c350b4563841d0958c21d57e6d" +[[package]] +name = "asn1-rs" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7f43a50ac4fdca5df8e885c21b835997f0a1cdee65494a6847694a98652d9d8" +dependencies = [ + "asn1-rs-derive", + "asn1-rs-impl", + "displaydoc", + "nom", + "num-traits", + "rusticata-macros", + "thiserror", + "time", +] + +[[package]] +name = "asn1-rs-derive" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3109e49b1e4909e9db6515a30c633684d68cdeaa252f215214cb4fa1a5bfee2c" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", + "synstructure 0.13.2", +] + +[[package]] +name = "asn1-rs-impl" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b18050c2cd6fe86c3a76584ef5e0baf286d038cda203eb6223df2cc413565f7" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "assert-json-diff" version = "2.0.2" @@ -1240,6 +1279,20 @@ dependencies = [ "thiserror", ] +[[package]] +name = "der-parser" +version = "10.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07da5016415d5a3c4dd39b11ed26f915f52fc4e0dc197d87908bc916e51bc1a6" +dependencies = [ + "asn1-rs", + "displaydoc", + "nom", + "num-bigint", + "num-traits", + "rusticata-macros", +] + [[package]] name = "deranged" version = "0.5.9" @@ -2541,6 +2594,16 @@ dependencies = [ "zeroize", ] +[[package]] +name = "keccak" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8f198d1db720e4940b5a493201d199d9f24f568f8f746bd13706243a2f71598" +dependencies = [ + "cfg-if", + "cpufeatures", +] + [[package]] name = "lazy_static" version = "1.5.1" @@ -2705,6 +2768,12 @@ dependencies = [ "unicase", ] +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "miniz_oxide" version = "0.9.1" @@ -2830,6 +2899,16 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "27b02d87554356db9e9a873add8782d4ea6e3e58ea071a9adb9a2e8ddb884a8b" +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + [[package]] name = "nu-ansi-term" version = "0.50.3" @@ -2954,6 +3033,15 @@ dependencies = [ "objc2-core-foundation", ] +[[package]] +name = "oid-registry" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12f40cff3dde1b6087cc5d5f5d4d65712f34016a03ed60e9c08dcc392736b5b7" +dependencies = [ + "asn1-rs", +] + [[package]] name = "once_cell" version = "1.21.4" @@ -3174,6 +3262,8 @@ dependencies = [ "redis", "reqwest", "rustls", + "rustls-pemfile", + "rustls-tokio-postgres", "serde", "serde_json", "sha2", @@ -3660,6 +3750,15 @@ dependencies = [ "semver", ] +[[package]] +name = "rusticata-macros" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "faf0c4a6ece9950b9abdb62b1cfcf2a68b3b67a10ba445b3bb85be2a293d0632" +dependencies = [ + "nom", +] + [[package]] name = "rustix" version = "1.1.6" @@ -3701,6 +3800,15 @@ dependencies = [ "security-framework", ] +[[package]] +name = "rustls-pemfile" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dce314e5fee3f39953d46bb63bb8a46d40c2f8fb7cc5a3b6cab2bde9721d6e50" +dependencies = [ + "rustls-pki-types", +] + [[package]] name = "rustls-pki-types" version = "1.15.1" @@ -3738,6 +3846,21 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eec689c0bc40ff2458a5977b6619cb718087084a18e02a131c599b62d05e1a5f" +[[package]] +name = "rustls-tokio-postgres" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16db1200772b63ef86dad5d0a1afa5107f8a7fd81c162d054fb7343cd036e1ac" +dependencies = [ + "rustls", + "sha2", + "sha3", + "tokio", + "tokio-postgres", + "tokio-rustls", + "x509-parser", +] + [[package]] name = "rustls-webpki" version = "0.103.16" @@ -3990,6 +4113,17 @@ dependencies = [ "digest 0.11.3", ] +[[package]] +name = "sha3" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc9bad02c26382724b2d2692c6f179285e4b54eeecd7968f52a50059c3c11759" +dependencies = [ + "digest 0.11.3", + "keccak", + "sponge-cursor", +] + [[package]] name = "sharded-slab" version = "0.1.7" @@ -4080,6 +4214,12 @@ version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "023a211cb3138dbc438680b32560ad89f699977624c9f8dbb95a47d5b4c07dd3" +[[package]] +name = "sponge-cursor" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a0219bd7d979d58245a4f41f695e1ac9f8befdffadd7f61f1bae9e39abc6620" + [[package]] name = "stable_deref_trait" version = "1.2.1" @@ -4169,6 +4309,17 @@ dependencies = [ "futures-core", ] +[[package]] +name = "synstructure" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "synstructure" version = "0.14.0" @@ -5380,6 +5531,23 @@ dependencies = [ "tap", ] +[[package]] +name = "x509-parser" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d43b0f71ce057da06bc0851b23ee24f3f86190b07203dd8f567d0b706a185202" +dependencies = [ + "asn1-rs", + "data-encoding", + "der-parser", + "lazy_static", + "nom", + "oid-registry", + "rusticata-macros", + "thiserror", + "time", +] + [[package]] name = "xattr" version = "1.7.0" @@ -5422,7 +5590,7 @@ dependencies = [ "proc-macro2", "quote", "syn 3.0.9", - "synstructure", + "synstructure 0.14.0", ] [[package]] @@ -5463,7 +5631,7 @@ dependencies = [ "proc-macro2", "quote", "syn 3.0.9", - "synstructure", + "synstructure 0.14.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 910f46c6..8bb2e762 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,7 +48,9 @@ tokio-stream = "0.1.18" url = "2.5.8" percent-encoding = "2.3.2" bollard = "0.20.0" -rustls = "0.23" +rustls-tokio-postgres = "0.5" +rustls = { version = "0.23", features = ["ring"] } +rustls-pemfile = "2.2" globset = "0.4" walkdir = "2.5" zstd = "0.14.3" diff --git a/flake.nix b/flake.nix new file mode 100644 index 00000000..035fa9cb --- /dev/null +++ b/flake.nix @@ -0,0 +1,48 @@ +{ + description = "Rust development environment"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + + rust-overlay = { + url = "github:oxalica/rust-overlay"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + }; + + outputs = + { + self, + nixpkgs, + flake-utils, + rust-overlay, + }: + flake-utils.lib.eachDefaultSystem ( + system: + let + pkgs = import nixpkgs { + inherit system; + overlays = [ + rust-overlay.overlays.default + ]; + }; + + rustToolchain = pkgs.rust-bin.stable.latest.default; + in + { + devShells.default = pkgs.mkShell { + packages = [ + rustToolchain + pkgs.rust-analyzer + pkgs.pkg-config + pkgs.openssl + ]; + + shellHook = '' + export OPENSSL_DIR="${pkgs.openssl.dev}" + ''; + }; + } + ); +} diff --git a/src/domain/postgres/cluster/database.rs b/src/domain/postgres/cluster/database.rs index fd677c44..9e065d01 100644 --- a/src/domain/postgres/cluster/database.rs +++ b/src/domain/postgres/cluster/database.rs @@ -22,6 +22,38 @@ impl PostgresClusterDatabase { fn build_env(&self) -> HashMap { let mut envs = std::env::vars().collect::>(); + + if self.cfg.use_tls { + envs.insert( + "PGSSLMODE".to_string(), + self.cfg + .ssl_mode + .as_deref() + .unwrap_or("disable") + .to_string(), + ); + envs.insert( + "PGSSLROOTCERT".to_string(), + self.cfg.ca_path.as_deref().unwrap_or("disable").to_string(), + ); + envs.insert( + "PGSSLCERT".to_string(), + self.cfg + .tls_cert_path + .as_deref() + .unwrap_or("disable") + .to_string(), + ); + envs.insert( + "PGSSLKEY".to_string(), + self.cfg + .tls_key_path + .as_deref() + .unwrap_or("disable") + .to_string(), + ); + } + envs.insert("PGPASSWORD".to_string(), self.cfg.password.to_string()); envs } @@ -39,14 +71,26 @@ impl Database for PostgresClusterDatabase { async fn backup(&self, dir: &Path, logger: Arc) -> Result { FileLock::acquire(&self.cfg.generated_id, DbOpLock::Backup.as_str()).await?; - let res = backup::run(self.cfg.clone(), dir.to_path_buf(), self.build_env(), logger).await; + let res = backup::run( + self.cfg.clone(), + dir.to_path_buf(), + self.build_env(), + logger, + ) + .await; FileLock::release(&self.cfg.generated_id).await?; res } async fn restore(&self, file: &Path, logger: Arc) -> Result<()> { FileLock::acquire(&self.cfg.generated_id, DbOpLock::Restore.as_str()).await?; - let res = restore::run(self.cfg.clone(), file.to_path_buf(), self.build_env(), logger).await; + let res = restore::run( + self.cfg.clone(), + file.to_path_buf(), + self.build_env(), + logger, + ) + .await; FileLock::release(&self.cfg.generated_id).await?; res } diff --git a/src/domain/postgres/connection.rs b/src/domain/postgres/connection.rs index f8f55dc2..b973cf0d 100644 --- a/src/domain/postgres/connection.rs +++ b/src/domain/postgres/connection.rs @@ -1,13 +1,18 @@ use crate::domain::postgres::format::PostgresDumpFormat; use crate::services::config::DatabaseConfig; use crate::settings::CONFIG; -use anyhow::Result; +use anyhow::{Context, Result}; +use std::io::Cursor; use std::path::Path; -use tokio_postgres::{Client, Config, NoTls}; -use tracing::{error, info}; +use tokio_postgres::{Client, Config, config::SslMode}; +use tracing::{debug, error, info}; + +use rustls::ClientConfig; +use rustls_tokio_postgres::MakeRustlsConnect; pub async fn connect(cfg: &DatabaseConfig) -> Result { info!("Connecting to postgres database {}:{}", cfg.host, cfg.port); + debug!("postgres::connection::connect: cfg {:?}", cfg); let mut config = Config::new(); config @@ -17,13 +22,287 @@ pub async fn connect(cfg: &DatabaseConfig) -> Result { .password(&cfg.password) .dbname(&cfg.database); - let (client, connection) = config.connect(NoTls).await?; - tokio::spawn(async move { - if let Err(e) = connection.await { - error!("Postgres connection error: {}", e); + if cfg.use_tls { + if let Some(mode_str) = &cfg.ssl_mode { + debug!("postgres::connection::connect: mode_str {}", mode_str); + let ssl_mode = match mode_str.as_str() { + "prefer" => SslMode::Prefer, + "require" => SslMode::Require, + _ => SslMode::Disable, + }; + debug!("postgres::connection::connect: ssl_mode {:?}", ssl_mode); + config.ssl_mode(ssl_mode); + } + + debug!( + "postgres::connection::connect: About to load root certs from {:?}", + cfg.ca_path + ); + let root_store = load_root_certs(cfg.ca_path.as_deref()).await?; + debug!("postgres::connection::connect: Root certs loaded"); + + debug!( + "postgres::connection::connect: About to load client certs from {:?} and key from {:?}", + cfg.tls_cert_path, cfg.tls_key_path + ); + let (client_certs, client_key) = + load_client_certs_and_key(cfg.tls_cert_path.as_deref(), cfg.tls_key_path.as_deref()) + .await?; + debug!("postgres::connection::connect: Client certs and key loaded"); + + debug!( + "postgres::connection::connect: Creating MakeRustlsConnect with {} root certs", + root_store.len() + ); + debug!( + "postgres::connection::connect: Client certs count: {}", + client_certs.len() + ); + debug!( + "postgres::connection::connect: About to connect to {}:{}", + cfg.host, cfg.port + ); + + let client_config = ClientConfig::builder() + .with_root_certificates(root_store) + .with_client_auth_cert(client_certs, client_key)?; + + let tls = MakeRustlsConnect::new(client_config); + + debug!("postgres::connection::connect: Attempting connection with timeout (10s)"); + + match tokio::time::timeout(std::time::Duration::from_secs(10), config.connect(tls)).await { + Ok(Ok((client, connection))) => { + debug!("postgres::connection::connect: Connection successful"); + tokio::spawn(async move { + if let Err(e) = connection.await { + error!("Postgres connection error: {}", e); + } + }); + Ok(client) + } + Ok(Err(e)) => { + error!( + "postgres::connection::connect: TLS connection failed: {:?}", + e + ); + Err(anyhow::anyhow!("Failed to connect to PostgreSQL: {}", e)) + } + Err(elapsed) => { + error!( + "postgres::connection::connect: Connection timeout after 10s: {}", + elapsed + ); + Err(anyhow::anyhow!("PostgreSQL connection timeout")) + } + } + } else { + use tokio_postgres::NoTls; + let (client, connection) = config.connect(NoTls).await?; + + tokio::spawn(async move { + if let Err(e) = connection.await { + error!("Postgres connection error: {}", e); + } + }); + + Ok(client) + } +} + +async fn load_root_certs(ca_path: Option<&str>) -> Result { + let mut root_store = rustls::RootCertStore::empty(); + + if let Some(path) = ca_path { + debug!( + "postgres::connection::load_root_certs: Reading CA from {}", + path + ); + + // Add a timeout to catch hangs + let ca_file = match tokio::time::timeout( + std::time::Duration::from_secs(5), + tokio::fs::read(path), + ) + .await + { + Ok(Ok(data)) => { + debug!( + "postgres::connection::load_root_certs: CA file read successfully, {} bytes", + data.len() + ); + data + } + Ok(Err(e)) => { + error!( + "postgres::connection::load_root_certs: Failed to read CA file: {}", + e + ); + return Err(anyhow::anyhow!("Failed to read CA certificate file: {}", e)); + } + Err(_) => { + error!( + "postgres::connection::load_root_certs: Timeout reading CA file after 5 seconds" + ); + return Err(anyhow::anyhow!("Timeout reading CA certificate file")); + } + }; + + debug!( + "postgres::connection::load_root_certs: About to parse {} bytes", + ca_file.len() + ); + + let certs = rustls_pemfile::certs(&mut std::io::Cursor::new(&ca_file)) + .collect::, _>>() + .context("Failed to parse CA certificate")?; + + debug!( + "postgres::connection::load_root_certs: Parsed {} CA certificates", + certs.len() + ); + + for cert in &certs { + root_store + .add(cert.clone()) + .context("Failed to add CA certificate to root store")?; + } + + debug!( + "postgres::connection::load_root_certs: All {} certificates added to root store", + certs.len() + ); + } + + debug!("postgres::connection::load_root_certs: Returning root store"); + Ok(root_store) +} + +async fn load_client_certs_and_key( + cert_path: Option<&str>, + key_path: Option<&str>, +) -> Result<( + Vec>, + rustls::pki_types::PrivateKeyDer<'static>, +)> { + let cert_path = + cert_path.ok_or_else(|| anyhow::anyhow!("Client certificate path is required"))?; + let key_path = key_path.ok_or_else(|| anyhow::anyhow!("Client key path is required"))?; + + debug!( + "postgres::connection::load_client_certs_and_key: Reading cert from {}", + cert_path + ); + let cert_file = match tokio::time::timeout( + std::time::Duration::from_secs(5), + tokio::fs::read(cert_path), + ) + .await + { + Ok(Ok(data)) => { + debug!( + "postgres::connection::load_client_certs_and_key: Cert file read, {} bytes", + data.len() + ); + data + } + Ok(Err(e)) => { + error!( + "postgres::connection::load_client_certs_and_key: Failed to read cert file: {}", + e + ); + return Err(anyhow::anyhow!( + "Failed to read client certificate file: {}", + e + )); } - }); - Ok(client) + Err(_) => { + error!("postgres::connection::load_client_certs_and_key: Timeout reading cert file"); + return Err(anyhow::anyhow!("Timeout reading client certificate file")); + } + }; + + debug!( + "postgres::connection::load_client_certs_and_key: Reading key from {}", + key_path + ); + let key_file = + match tokio::time::timeout(std::time::Duration::from_secs(5), tokio::fs::read(key_path)) + .await + { + Ok(Ok(data)) => { + debug!( + "postgres::connection::load_client_certs_and_key: Key file read, {} bytes", + data.len() + ); + data + } + Ok(Err(e)) => { + error!( + "postgres::connection::load_client_certs_and_key: Failed to read key file: {}", + e + ); + return Err(anyhow::anyhow!("Failed to read client key file: {}", e)); + } + Err(_) => { + error!("postgres::connection::load_client_certs_and_key: Timeout reading key file"); + return Err(anyhow::anyhow!("Timeout reading client key file")); + } + }; + + debug!("postgres::connection::load_client_certs_and_key: Parsing certificates"); + let certs = rustls_pemfile::certs(&mut std::io::Cursor::new(&cert_file)) + .collect::, _>>() + .context("Failed to parse client certificate")?; + + debug!( + "postgres::connection::load_client_certs_and_key: Parsed {} certs, loading key", + certs.len() + ); + + let key = load_private_key(&key_file)?; + + debug!("postgres::connection::load_client_certs_and_key: Successfully loaded cert and key"); + + Ok((certs, key)) +} + +fn load_private_key(key_data: &[u8]) -> Result> { + let key_str = String::from_utf8_lossy(key_data); + + if key_str.contains("-----BEGIN PRIVATE KEY-----") { + // PKCS#8 format + let keys = rustls_pemfile::pkcs8_private_keys(&mut Cursor::new(key_data)) + .collect::, _>>() + .context("Failed to parse PKCS#8 private key")?; + + keys.into_iter() + .next() + .map(|k| k.into()) + .ok_or_else(|| anyhow::anyhow!("No PKCS#8 private key found")) + } else if key_str.contains("-----BEGIN EC PRIVATE KEY-----") { + // SEC1/EC format + let keys = rustls_pemfile::ec_private_keys(&mut Cursor::new(key_data)) + .collect::, _>>() + .context("Failed to parse EC private key")?; + + keys.into_iter() + .next() + .map(|k| k.into()) + .ok_or_else(|| anyhow::anyhow!("No EC private key found")) + } else if key_str.contains("-----BEGIN RSA PRIVATE KEY-----") { + // Traditional RSA format (PKCS#1) + let keys = rustls_pemfile::rsa_private_keys(&mut Cursor::new(key_data)) + .collect::, _>>() + .context("Failed to parse RSA private key")?; + + keys.into_iter() + .next() + .map(|k| k.into()) + .ok_or_else(|| anyhow::anyhow!("No RSA private key found")) + } else { + Err(anyhow::anyhow!("Unsupported private key format")) + } } pub async fn server_version(cfg: &DatabaseConfig) -> Result { @@ -64,7 +343,6 @@ pub async fn can_drop_database(cfg: &DatabaseConfig) -> Result { Ok(row.get(0)) } - pub fn select_pg_path(version: &str) -> std::path::PathBuf { select_pg_path_with(version, &CONFIG.pg_bin_dir) } diff --git a/src/domain/postgres/database.rs b/src/domain/postgres/database.rs index b1f160a8..853a06af 100644 --- a/src/domain/postgres/database.rs +++ b/src/domain/postgres/database.rs @@ -1,13 +1,13 @@ -use anyhow::Result; -use async_trait::async_trait; -use std::collections::HashMap; -use std::path::{Path, PathBuf}; -use std::sync::Arc; use super::{backup, format::PostgresDumpFormat, ping, restore}; use crate::domain::factory::Database; use crate::services::backup::logger::JobLogger; use crate::services::config::DatabaseConfig; use crate::utils::locks::{DbOpLock, FileLock}; +use anyhow::Result; +use async_trait::async_trait; +use std::collections::HashMap; +use std::path::{Path, PathBuf}; +use std::sync::Arc; pub struct PostgresDatabase { pub cfg: DatabaseConfig, @@ -21,6 +21,36 @@ impl PostgresDatabase { fn build_env(&self) -> HashMap { let mut envs = std::env::vars().collect::>(); + if self.cfg.use_tls { + envs.insert( + "PGSSLMODE".to_string(), + self.cfg + .ssl_mode + .as_deref() + .unwrap_or("disable") + .to_string(), + ); + envs.insert( + "PGSSLROOTCERT".to_string(), + self.cfg.ca_path.as_deref().unwrap_or("disable").to_string(), + ); + envs.insert( + "PGSSLCERT".to_string(), + self.cfg + .tls_cert_path + .as_deref() + .unwrap_or("disable") + .to_string(), + ); + envs.insert( + "PGSSLKEY".to_string(), + self.cfg + .tls_key_path + .as_deref() + .unwrap_or("disable") + .to_string(), + ); + } envs.insert("PGPASSWORD".to_string(), self.cfg.password.to_string()); envs } diff --git a/src/services/config/builder.rs b/src/services/config/builder.rs index ae6fd627..9599961a 100644 --- a/src/services/config/builder.rs +++ b/src/services/config/builder.rs @@ -30,11 +30,9 @@ pub fn build_config(db: InputDatabaseConfig) -> Result { _ => optional(&db.username), }; let password = match db.db_type { - DbType::Postgresql - | DbType::PostgresqlCluster - | DbType::Mysql - | DbType::Mariadb - | DbType::Mssql => required(&db.password, &db.name, "password")?, + DbType::Mysql | DbType::Mariadb | DbType::Mssql => { + required(&db.password, &db.name, "password")? + } _ => optional(&db.password), }; let host = match db.db_type { @@ -117,5 +115,10 @@ pub fn build_config(db: InputDatabaseConfig) -> Result { volume_name, container_name: db.container_name.clone(), options: db.options.unwrap_or_default(), + ca_path: db.ca_path, // or read from config if available + tls_cert_path: db.tls_cert_path, + tls_key_path: db.tls_key_path, + use_tls: db.use_tls.unwrap_or_default(), + ssl_mode: db.ssl_mode, }) } diff --git a/src/services/config/models.rs b/src/services/config/models.rs index 3ac47cf2..01118637 100644 --- a/src/services/config/models.rs +++ b/src/services/config/models.rs @@ -56,6 +56,11 @@ pub struct DatabaseConfig { pub volume_name: String, pub container_name: Option, pub options: HashMap, + pub use_tls: bool, + pub ssl_mode: Option, + pub ca_path: Option, + pub tls_cert_path: Option, + pub tls_key_path: Option, } #[allow(dead_code)] @@ -81,6 +86,11 @@ pub struct InputDatabaseConfig { pub volume_name: Option, pub container_name: Option, pub options: Option>, + pub use_tls: Option, + pub ssl_mode: Option, + pub ca_path: Option, + pub tls_cert_path: Option, + pub tls_key_path: Option, } #[allow(dead_code)] From 5ae7770f8900af2684f57c9e7d6fbef27eacf978 Mon Sep 17 00:00:00 2001 From: Jonathan Frankel Date: Mon, 30 Nov 2026 20:02:58 +0800 Subject: [PATCH 2/2] feat(postgres): adding optional password and tls certs for postgres --- .envrc | 1 - flake.nix | 48 ------------------------------------------------ 2 files changed, 49 deletions(-) delete mode 100644 .envrc delete mode 100644 flake.nix diff --git a/.envrc b/.envrc deleted file mode 100644 index 3550a30f..00000000 --- a/.envrc +++ /dev/null @@ -1 +0,0 @@ -use flake diff --git a/flake.nix b/flake.nix deleted file mode 100644 index 035fa9cb..00000000 --- a/flake.nix +++ /dev/null @@ -1,48 +0,0 @@ -{ - description = "Rust development environment"; - - inputs = { - nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; - flake-utils.url = "github:numtide/flake-utils"; - - rust-overlay = { - url = "github:oxalica/rust-overlay"; - inputs.nixpkgs.follows = "nixpkgs"; - }; - }; - - outputs = - { - self, - nixpkgs, - flake-utils, - rust-overlay, - }: - flake-utils.lib.eachDefaultSystem ( - system: - let - pkgs = import nixpkgs { - inherit system; - overlays = [ - rust-overlay.overlays.default - ]; - }; - - rustToolchain = pkgs.rust-bin.stable.latest.default; - in - { - devShells.default = pkgs.mkShell { - packages = [ - rustToolchain - pkgs.rust-analyzer - pkgs.pkg-config - pkgs.openssl - ]; - - shellHook = '' - export OPENSSL_DIR="${pkgs.openssl.dev}" - ''; - }; - } - ); -}