Skip to content

Commit 5881188

Browse files
authored
Merge pull request #1971 from Kobzol/certificates-async
Make loading of PEM certificates async
2 parents 32a6ce2 + caccf6e commit 5881188

File tree

4 files changed

+28
-22
lines changed

4 files changed

+28
-22
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

collector/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ libc = "0.2"
1818
chrono = { version = "0.4", features = ["serde"] }
1919
lazy_static = "1"
2020
semver = "1.0"
21-
reqwest = { version = "0.11", features = ["json"] }
21+
reqwest = { version = "0.11", features = ["json", "blocking"] }
2222
xz2 = "0.1.3"
2323
tar = "0.4"
2424
tokio = { version = "1.6", features = ["rt", "process"] }

database/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ rusqlite = { version = "0.28", features = ["bundled"] }
1111
tokio-postgres = { version = "0.7", features = ["with-chrono-0_4", "runtime"] }
1212
anyhow = "1"
1313
async-trait = "0.1"
14-
tokio = { version = "1.6", features = ["sync", "macros"] }
14+
tokio = { version = "1.6", features = ["sync", "macros", "parking_lot"] }
1515
intern = { path = "../intern" }
1616
chrono = { version = "0.4.38", features = ["serde"] }
17-
reqwest = { version = "0.11", features = ["blocking"] }
17+
reqwest = { version = "0.11" }
1818
postgres-native-tls = "0.5"
1919
native-tls = "0.2"
20-
lazy_static = "1"
2120
env_logger = "0.10"
2221
futures-util = "0.3.5"
2322
log = "0.4"

database/src/pool/postgres.rs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use postgres_native_tls::MakeTlsConnector;
1111
use std::str::FromStr;
1212
use std::sync::Arc;
1313
use std::time::Duration;
14+
use tokio::sync::Mutex;
1415
use tokio_postgres::GenericClient;
1516
use tokio_postgres::Statement;
1617

@@ -24,21 +25,10 @@ impl Postgres {
2425

2526
const CERT_URL: &str = "https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem";
2627

27-
lazy_static::lazy_static! {
28-
static ref CERTIFICATE_PEMS: Vec<u8> = {
29-
let client = reqwest::blocking::Client::new();
30-
let resp = client
31-
.get(CERT_URL)
32-
.send()
33-
.expect("failed to get RDS cert");
34-
resp.bytes().expect("failed to get RDS cert body").to_vec()
35-
};
36-
}
37-
3828
async fn make_client(db_url: &str) -> anyhow::Result<tokio_postgres::Client> {
3929
if db_url.contains("rds.amazonaws.com") {
4030
let mut builder = TlsConnector::builder();
41-
for cert in make_certificates() {
31+
for cert in make_certificates().await {
4232
builder.add_root_certificate(cert);
4333
}
4434
let connector = builder.build().context("built TlsConnector")?;
@@ -75,11 +65,28 @@ async fn make_client(db_url: &str) -> anyhow::Result<tokio_postgres::Client> {
7565
Ok(db_client)
7666
}
7767
}
78-
fn make_certificates() -> Vec<Certificate> {
68+
async fn make_certificates() -> Vec<Certificate> {
7969
use x509_cert::der::pem::LineEnding;
8070
use x509_cert::der::EncodePem;
8171

82-
let certs = x509_cert::Certificate::load_pem_chain(&CERTIFICATE_PEMS[..]).unwrap();
72+
static CERTIFICATE_PEMS: Mutex<Option<Vec<u8>>> = Mutex::const_new(None);
73+
74+
let mut guard = CERTIFICATE_PEMS.lock().await;
75+
if guard.is_none() {
76+
let client = reqwest::Client::new();
77+
let resp = client
78+
.get(CERT_URL)
79+
.send()
80+
.await
81+
.expect("failed to get RDS cert");
82+
let certificate_pems = resp
83+
.bytes()
84+
.await
85+
.expect("failed to get RDS cert body")
86+
.to_vec();
87+
*guard = Some(certificate_pems.clone());
88+
}
89+
let certs = x509_cert::Certificate::load_pem_chain(&guard.as_ref().unwrap()[..]).unwrap();
8390
certs
8491
.into_iter()
8592
.map(|cert| Certificate::from_pem(cert.to_pem(LineEnding::LF).unwrap().as_bytes()).unwrap())
@@ -1365,9 +1372,9 @@ mod tests {
13651372

13661373
// Makes sure we successfully parse the RDS certificates and load them into native-tls compatible
13671374
// format.
1368-
#[test]
1369-
fn can_make_certificates() {
1370-
let certs = make_certificates();
1375+
#[tokio::test]
1376+
async fn can_make_certificates() {
1377+
let certs = make_certificates().await;
13711378
assert!(!certs.is_empty());
13721379
}
13731380
}

0 commit comments

Comments
 (0)