Skip to content

Commit fb3e5a6

Browse files
nipunn1313Erkka Tahvanainen
authored andcommitted
convex-backend PR 66: add support for CA file for postgress (#35905)
This add support to define CA file for postgres via env variable PG_CA_FILE ---- By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice. Co-authored-by: Erkka Tahvanainen <[email protected]> GitOrigin-RevId: cd98d8510ced6c49879bac1041f25e927bbb2a56
1 parent 8c620ec commit fb3e5a6

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

crates/postgres/src/lib.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ use std::{
1414
BTreeSet,
1515
HashMap,
1616
},
17+
env,
1718
error::Error,
1819
fmt::Write,
20+
fs,
1921
future::Future,
2022
ops::Bound,
23+
path::Path,
2124
pin::Pin,
2225
str::FromStr,
2326
sync::{
@@ -110,7 +113,10 @@ use itertools::{
110113
iproduct,
111114
Itertools,
112115
};
113-
use native_tls::TlsConnector;
116+
use native_tls::{
117+
Certificate,
118+
TlsConnector,
119+
};
114120
use postgres_native_tls::MakeTlsConnector;
115121
use serde::Deserialize as _;
116122
use serde_json::Value as JsonValue;
@@ -197,7 +203,17 @@ impl PostgresPersistence {
197203

198204
fn create_pool(url: &str) -> anyhow::Result<ConvexPgPool> {
199205
let pg_config = tokio_postgres::Config::from_str(url)?;
200-
let connector = TlsConnector::builder().build()?;
206+
let mut builder = TlsConnector::builder();
207+
if let Ok(ca_file_path) = env::var("PG_CA_FILE")
208+
&& !ca_file_path.is_empty()
209+
{
210+
let ca_file_content = fs::read(Path::new(&ca_file_path))
211+
.with_context(|| format!("Failed to read CA file: {}", ca_file_path))?;
212+
let ca_cert = Certificate::from_pem(&ca_file_content)
213+
.with_context(|| format!("Failed to parse CA file as PEM: {}", ca_file_path))?;
214+
builder.add_root_certificate(ca_cert);
215+
}
216+
let connector = builder.build()?;
201217
let connector = MakeTlsConnector::new(connector);
202218

203219
let manager = Manager::new(pg_config, connector);

0 commit comments

Comments
 (0)