Skip to content
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

fix: pgsrv should initialize its own crypto provider #3121

Merged
merged 1 commit into from
Jul 30, 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
1 change: 1 addition & 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 crates/pgsrv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ parser = { path = "../parser" }
pgrepr = { path = "../pgrepr" }
proxyutil = { path = "../proxyutil" }
sqlexec = { path = "../sqlexec" }
once_cell = "1.19.0"
rustls-pemfile = "2.1.2"
tokio-postgres = "0.7.11"
tokio-rustls = "0.26.0"
Expand Down
4 changes: 2 additions & 2 deletions crates/pgsrv/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub enum PgSrvError {
CloudResponse(String),

#[error("internal error: {0}")]
InternalError(String),
Internal(String),

/// We've received an unexpected message identifier from the frontend.
/// Includes the char representation to allow for easy cross referencing
Expand All @@ -71,7 +71,7 @@ pub enum PgSrvError {
SqlExec(#[from] sqlexec::errors::ExecError),

#[error(transparent)]
Datafusion(#[from] datafusion::error::DataFusionError),
DataFusion(#[from] datafusion::error::DataFusionError),

#[error(transparent)]
Arrow(#[from] datafusion::arrow::error::ArrowError),
Expand Down
2 changes: 1 addition & 1 deletion crates/pgsrv/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ impl<'a> FieldDescriptionBuilder<'a> {
}

pub fn build(self) -> Result<FieldDescription> {
let pg_type = self.pg_type.ok_or(PgSrvError::InternalError(
let pg_type = self.pg_type.ok_or(PgSrvError::Internal(
"type cannot be `None` in field description".to_string(),
))?;

Expand Down
23 changes: 19 additions & 4 deletions crates/pgsrv/src/ssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

use once_cell::sync::OnceCell;
use rustls::pki_types::{CertificateDer, PrivateKeyDer};
use rustls::{server, sign, ServerConfig};
use tokio::fs;
Expand Down Expand Up @@ -33,15 +34,32 @@ impl SslConfig {
for key in rustls_pemfile::pkcs8_private_keys(&mut key_bs.as_slice()) {
keys.push(key?.secret_pkcs8_der().to_vec());
}


let key = match keys.len() {
0 => return Err(PgSrvError::ReadCertsAndKeys("No keys found")),
1 => PrivateKeyDer::try_from(keys.pop().unwrap())
.map_err(|e| PgSrvError::InternalError(e.to_owned()))?,
.map_err(|e| PgSrvError::Internal(e.to_owned()))?,
_ => return Err(PgSrvError::ReadCertsAndKeys("Expected exactly one key")),
};

let resolver = CertResolver::new(chain, &key)?;

static CRYPTO_PROVIDER: OnceCell<()> = OnceCell::new();
CRYPTO_PROVIDER.get_or_try_init(|| {
if rustls::crypto::CryptoProvider::get_default().is_none() {
rustls::crypto::CryptoProvider::install_default(
rustls::crypto::aws_lc_rs::default_provider(),
)
.map(|r| r.to_owned())
.map_err(|_| {
PgSrvError::Internal("unable to register CryptoProvider".to_string())
})?;
}

Ok::<(), PgSrvError>(())
})?;

let config = ServerConfig::builder()
.with_no_client_auth()
.with_cert_resolver(Arc::new(resolver));
Expand Down Expand Up @@ -151,7 +169,6 @@ where
mod tests {
use std::io::Write;

use rustls::crypto::{aws_lc_rs, CryptoProvider};
use tempfile::NamedTempFile;

use super::*;
Expand Down Expand Up @@ -192,8 +209,6 @@ MC4CAQAwBQYDK2VwBCIEIDGe13glRciPej49XvEZqqq4oZ5yUuL9HD2Pw1rSue2j

#[tokio::test]
async fn create_with_valid_cert() {
CryptoProvider::install_default(aws_lc_rs::default_provider()).unwrap();

let cert = create_file(TEST_CERT);
let key = create_file(TEST_KEY);

Expand Down