Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(pgcollector): use native cert store for TLS connections (#94808)

Nick Bestb806bc419fc6
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index 64c850128bd1..3448d5e0269c 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -8988,6 +8988,7 @@ dependencies = [
"rmcp",
"rust_decimal",
"rustls 0.23.37",
+ "rustls-native-certs 0.8.3",
"schemars 1.2.1",
"serde",
"serde_json",
@@ -9022,6 +9023,7 @@ dependencies = [
"regex",
"rust_decimal",
"rustls 0.23.37",
+ "rustls-native-certs 0.8.3",
"serde",
"serde_json",
"serde_yaml",
diff --git a/rust/pgapi/Cargo.toml b/rust/pgapi/Cargo.toml
index c9d5952ed431..4457c368865b 100644
--- a/rust/pgapi/Cargo.toml
+++ b/rust/pgapi/Cargo.toml
@@ -22,6 +22,7 @@ reqwest = { workspace = true }
rmcp = { version = "3", features = ["server", "macros", "transport-streamable-http-server"] }
rust_decimal = { version = "1", features = ["db-tokio-postgres"] }
rustls = { workspace = true }
+rustls-native-certs = "0.8"
schemars = "1"
serde = { workspace = true }
serde_json = { workspace = true }
diff --git a/rust/pgapi/src/db.rs b/rust/pgapi/src/db.rs
index 76b595560b4c..c39ac4aacca2 100644
--- a/rust/pgapi/src/db.rs
+++ b/rust/pgapi/src/db.rs
@@ -20,10 +20,22 @@ impl Db {
pub async fn connect(url: &str) -> Result<Self> {
let mut cfg: tokio_postgres::Config = url.parse().context("parsing PGAPI_DATABASE_URL")?;
cfg.ssl_mode(tokio_postgres::config::SslMode::Require);
+ let mut roots = rustls::RootCertStore::empty();
+ let native = rustls_native_certs::load_native_certs();
+ for err in &native.errors {
+ tracing::warn!(error = %err, "failed to load a native certificate");
+ }
+ for cert in native.certs {
+ if let Err(e) = roots.add(cert) {
+ tracing::warn!(error = %e, "failed to add a native certificate to root store");
+ }
+ }
+ if roots.is_empty() {
+ tracing::info!("no native certs found, falling back to webpki-roots");
+ roots.roots = webpki_roots::TLS_SERVER_ROOTS.to_vec();
+ }
let tls_cfg = rustls::ClientConfig::builder()
- .with_root_certificates(rustls::RootCertStore {
- roots: webpki_roots::TLS_SERVER_ROOTS.to_vec(),
- })
+ .with_root_certificates(roots)
.with_no_client_auth();
let tls = tokio_postgres_rustls::MakeRustlsConnect::new(tls_cfg);
let mgr = Manager::from_config(
diff --git a/rust/pgcollector/Cargo.toml b/rust/pgcollector/Cargo.toml
index 4dc4f316a6ff..3c1c08afa406 100644
--- a/rust/pgcollector/Cargo.toml
+++ b/rust/pgcollector/Cargo.toml
@@ -25,6 +25,7 @@ prometheus = "0.13"
regex = { workspace = true }
rust_decimal = { version = "1", features = ["db-tokio-postgres"] }
rustls = { workspace = true }
+rustls-native-certs = "0.8"
serde = { workspace = true }
serde_json = { workspace = true }
serde_yaml = "0.9"
diff --git a/rust/pgcollector/src/pg.rs b/rust/pgcollector/src/pg.rs
index f555f30c1d6d..9851119e2f7d 100644
--- a/rust/pgcollector/src/pg.rs
+++ b/rust/pgcollector/src/pg.rs
@@ -21,10 +21,22 @@ pub fn tls_policy(url: &str) -> tokio_postgres::config::SslMode {
}
pub fn tls_connector() -> tokio_postgres_rustls::MakeRustlsConnect {
+ let mut roots = rustls::RootCertStore::empty();
+ let native = rustls_native_certs::load_native_certs();
+ for err in &native.errors {
+ tracing::warn!(error = %err, "failed to load a native certificate");
+ }
+ for cert in native.certs {
+ if let Err(e) = roots.add(cert) {
+ tracing::warn!(error = %e, "failed to add a native certificate to root store");
+ }
+ }
+ if roots.is_empty() {
+ tracing::info!("no native certs found, falling back to webpki-roots");
+ roots.roots = webpki_roots::TLS_SERVER_ROOTS.to_vec();
+ }
let tls_cfg = rustls::ClientConfig::builder()
- .with_root_certificates(rustls::RootCertStore {
- roots: webpki_roots::TLS_SERVER_ROOTS.to_vec(),
- })
+ .with_root_certificates(roots)
.with_no_client_auth();
tokio_postgres_rustls::MakeRustlsConnect::new(tls_cfg)
}