Skip to content

Update the RDS root CA list #1952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions database/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ log = "0.4"
bytes = "1"
csv = "1"
clap = { version = "4.1", features = ["cargo"] }
x509-cert = { version = "0.2.5", features = ["pem"] }
38 changes: 30 additions & 8 deletions database/src/pool/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ impl Postgres {
}
}

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

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

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

let (db_client, connection) = match tokio_postgres::connect(db_url, connector).await {
Expand Down Expand Up @@ -76,6 +75,16 @@ async fn make_client(db_url: &str) -> anyhow::Result<tokio_postgres::Client> {
Ok(db_client)
}
}
fn make_certificates() -> Vec<Certificate> {
use x509_cert::der::pem::LineEnding;
use x509_cert::der::EncodePem;

let certs = x509_cert::Certificate::load_pem_chain(&CERTIFICATE_PEMS[..]).unwrap();
certs
.into_iter()
.map(|cert| Certificate::from_pem(cert.to_pem(LineEnding::LF).unwrap().as_bytes()).unwrap())
.collect()
}

static MIGRATIONS: &[&str] = &[
"",
Expand Down Expand Up @@ -1349,3 +1358,16 @@ fn parse_artifact_id(ty: &str, sha: &str, date: Option<DateTime<Utc>>) -> Artifa
_ => panic!("unknown artifact type: {:?}", ty),
}
}

#[cfg(test)]
mod tests {
use super::make_certificates;

// Makes sure we successfully parse the RDS certificates and load them into native-tls compatible
// format.
#[test]
fn can_make_certificates() {
let certs = make_certificates();
assert!(!certs.is_empty());
}
}
Loading