Skip to content

Commit 38c1b72

Browse files
authored
Merge pull request rust-lang#1952 from MarcoIeni/rds-ca
Update the RDS root CA list
2 parents 7965c10 + 9a77c01 commit 38c1b72

File tree

3 files changed

+146
-8
lines changed

3 files changed

+146
-8
lines changed

Cargo.lock

+115
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

database/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ log = "0.4"
2424
bytes = "1"
2525
csv = "1"
2626
clap = { version = "4.1", features = ["cargo"] }
27+
x509-cert = { version = "0.2.5", features = ["pem"] }

database/src/pool/postgres.rs

+30-8
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ impl Postgres {
2222
}
2323
}
2424

25-
const CERT_URL: &str = "https://s3.amazonaws.com/rds-downloads/rds-ca-2019-root.pem";
25+
const CERT_URL: &str = "https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem";
2626

2727
lazy_static::lazy_static! {
28-
static ref CERTIFICATE_PEM: Vec<u8> = {
28+
static ref CERTIFICATE_PEMS: Vec<u8> = {
2929
let client = reqwest::blocking::Client::new();
3030
let resp = client
3131
.get(CERT_URL)
@@ -37,12 +37,11 @@ lazy_static::lazy_static! {
3737

3838
async fn make_client(db_url: &str) -> anyhow::Result<tokio_postgres::Client> {
3939
if db_url.contains("rds.amazonaws.com") {
40-
let cert = &CERTIFICATE_PEM[..];
41-
let cert = Certificate::from_pem(cert).context("made certificate")?;
42-
let connector = TlsConnector::builder()
43-
.add_root_certificate(cert)
44-
.build()
45-
.context("built TlsConnector")?;
40+
let mut builder = TlsConnector::builder();
41+
for cert in make_certificates() {
42+
builder.add_root_certificate(cert);
43+
}
44+
let connector = builder.build().context("built TlsConnector")?;
4645
let connector = MakeTlsConnector::new(connector);
4746

4847
let (db_client, connection) = match tokio_postgres::connect(db_url, connector).await {
@@ -76,6 +75,16 @@ async fn make_client(db_url: &str) -> anyhow::Result<tokio_postgres::Client> {
7675
Ok(db_client)
7776
}
7877
}
78+
fn make_certificates() -> Vec<Certificate> {
79+
use x509_cert::der::pem::LineEnding;
80+
use x509_cert::der::EncodePem;
81+
82+
let certs = x509_cert::Certificate::load_pem_chain(&CERTIFICATE_PEMS[..]).unwrap();
83+
certs
84+
.into_iter()
85+
.map(|cert| Certificate::from_pem(cert.to_pem(LineEnding::LF).unwrap().as_bytes()).unwrap())
86+
.collect()
87+
}
7988

8089
static MIGRATIONS: &[&str] = &[
8190
"",
@@ -1349,3 +1358,16 @@ fn parse_artifact_id(ty: &str, sha: &str, date: Option<DateTime<Utc>>) -> Artifa
13491358
_ => panic!("unknown artifact type: {:?}", ty),
13501359
}
13511360
}
1361+
1362+
#[cfg(test)]
1363+
mod tests {
1364+
use super::make_certificates;
1365+
1366+
// Makes sure we successfully parse the RDS certificates and load them into native-tls compatible
1367+
// format.
1368+
#[test]
1369+
fn can_make_certificates() {
1370+
let certs = make_certificates();
1371+
assert!(!certs.is_empty());
1372+
}
1373+
}

0 commit comments

Comments
 (0)