Add root_ca_certificate

This commit is contained in:
Michael Vines 2020-07-17 13:36:11 -07:00
parent 59d266a111
commit faa016e4b7
3 changed files with 23 additions and 1 deletions

View File

@ -9,9 +9,10 @@ homepage = "https://solana.com/"
edition = "2018"
[dependencies]
log = "0.4.8"
goauth = "0.7.1"
log = "0.4.8"
smpl_jwt = "0.5.0"
tonic = {version="0.3.0", features = ["tls", "transport"]}
[lib]
crate-type = ["lib"]

View File

@ -1 +1,2 @@
mod access_token;
mod root_ca_certificate;

View File

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