bigtable_rs/
root_ca_certificate.rs

1use std::{fs::File, io::Read};
2use tonic::transport::Certificate;
3
4pub fn load() -> Result<Certificate, String> {
5    // Respect the standard GRPC_DEFAULT_SSL_ROOTS_FILE_PATH environment variable if present,
6    // otherwise use the built-in root certificate
7    let pem = match std::env::var("GRPC_DEFAULT_SSL_ROOTS_FILE_PATH").ok() {
8        Some(cert_file) => File::open(&cert_file)
9            .and_then(|mut file| {
10                let mut pem = Vec::new();
11                file.read_to_end(&mut pem).map(|_| pem)
12            })
13            .map_err(|err| format!("Failed to read {}: {}", cert_file, err))?,
14        None => {
15            // PEM file from Google Trust Services (https://pki.goog/roots.pem)
16            include_bytes!("roots.pem").to_vec()
17        }
18    };
19    Ok(Certificate::from_pem(&pem))
20}