Skip to content

Commit 983adc5

Browse files
committed
Read the additional root certificate directly from the config file...
...just like we do for the RSA public key used to authenticate incoming JWTs. Also parse the root certificate in `PostgresTlsBackend::new`. This makes `PostgresTlsBackend` consistent with `JWTAuthorizer`; both structs take cryptographic objects as `&str`'s and parse them in their constructors.
1 parent 18d6ff2 commit 983adc5

File tree

4 files changed

+23
-31
lines changed

4 files changed

+23
-31
lines changed

rust/impls/src/postgres_store.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,17 @@ impl PostgresPlaintextBackend {
141141
impl PostgresTlsBackend {
142142
/// Constructs a [`PostgresTlsBackend`] using `postgres_endpoint` for PostgreSQL connection information.
143143
pub async fn new(
144-
postgres_endpoint: &str, db_name: &str, additional_certificate: Option<Certificate>,
144+
postgres_endpoint: &str, db_name: &str, crt_pem: Option<&str>,
145145
) -> Result<Self, Error> {
146146
let mut builder = TlsConnector::builder();
147-
if let Some(cert) = additional_certificate {
148-
builder.add_root_certificate(cert);
147+
if let Some(pem) = crt_pem {
148+
let crt = Certificate::from_pem(pem.as_bytes()).map_err(|e| {
149+
Error::new(
150+
ErrorKind::Other,
151+
format!("Failed to parse the PEM formatted certificate: {}", e),
152+
)
153+
})?;
154+
builder.add_root_certificate(crt);
149155
}
150156
let connector = builder.build().map_err(|e| {
151157
Error::new(ErrorKind::Other, format!("Error building tls connector: {}", e))

rust/server/src/main.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use api::kv_store::KvStore;
2424
use auth_impls::jwt::JWTAuthorizer;
2525
#[cfg(feature = "sigs")]
2626
use auth_impls::signature::SignatureValidatingAuthorizer;
27-
use impls::postgres_store::{Certificate, PostgresPlaintextBackend, PostgresTlsBackend};
27+
use impls::postgres_store::{PostgresPlaintextBackend, PostgresTlsBackend};
2828
use util::config::{Config, ServerConfig};
2929
use vss_service::VssService;
3030

@@ -115,24 +115,10 @@ fn main() {
115115
let endpoint = postgresql_config.to_postgresql_endpoint();
116116
let db_name = postgresql_config.database;
117117
let store: Arc<dyn KvStore> = if let Some(tls_config) = postgresql_config.tls {
118-
let additional_certificate = tls_config.ca_file.map(|file| {
119-
let certificate = match std::fs::read(&file) {
120-
Ok(cert) => cert,
121-
Err(e) => {
122-
println!("Failed to read certificate file: {}", e);
123-
std::process::exit(-1);
124-
},
125-
};
126-
match Certificate::from_pem(&certificate) {
127-
Ok(cert) => cert,
128-
Err(e) => {
129-
println!("Failed to parse certificate file: {}", e);
130-
std::process::exit(-1);
131-
},
132-
}
133-
});
134118
let postgres_tls_backend =
135-
match PostgresTlsBackend::new(&endpoint, &db_name, additional_certificate).await {
119+
match PostgresTlsBackend::new(&endpoint, &db_name, tls_config.crt_pem.as_deref())
120+
.await
121+
{
136122
Ok(backend) => backend,
137123
Err(e) => {
138124
println!("Failed to start postgres tls backend: {}", e);

rust/server/src/util/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub(crate) struct PostgreSQLConfig {
3030

3131
#[derive(Deserialize)]
3232
pub(crate) struct TlsConfig {
33-
pub(crate) ca_file: Option<String>,
33+
pub(crate) crt_pem: Option<String>,
3434
}
3535

3636
impl PostgreSQLConfig {

rust/server/vss-server-config.toml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ port = 8080
77
# [jwt_auth_config]
88
# rsa_pem = """
99
# -----BEGIN PUBLIC KEY-----
10-
# MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAstPJs4ut+tFAI0qrOyGt
11-
# /3FN5jWc5gLv/j9Rc6lgr4hm7lyR05PU/G+4rfxdXGNyGTlQ6dRqcVy78CjxWz9f
12-
# 8l08EKLERPh8JhE5el6vr+ehWD5iQxSP3ejpx0Mr977fKMNKg6jlFiL+y50hOEp2
13-
# 6iN9QzZQjLxotDT3aQvbCA/DZpI+fV6WKDKWGS+pZGDVgOz5x/RcStJQXxkX3ACK
14-
# WhVdrtN3h6mHlhIt7ZIqVvQmY4NL03QPyljt13sYHoiFaoxINF/funBMCjrfSLcB
15-
# ko1rWE2BWdOrFqi27RtBs5AHOSAWXuz/2SUGpFuTQuJi7U68QUfjKeQO46JpQf+v
16-
# kQIDAQAB
1710
# -----END PUBLIC KEY-----
1811
# """
1912

@@ -23,5 +16,12 @@ password = "postgres" # Optional in TOML, can be overridden by env var `VSS_POS
2316
host = "localhost"
2417
port = 5432
2518
database = "postgres"
26-
# tls = { } # Uncomment to make TLS connections to the postgres database using your machine's PKI
27-
# tls = { ca_file = "ca.pem" } # Uncomment to make TLS connections to the postgres database with an additional root certificate
19+
20+
# [postgresql_config.tls] # Uncomment to make TLS connections to the postgres database
21+
#
22+
# Uncomment the lines below to add a root certificate to your trusted root certificates
23+
#
24+
# crt_pem = """
25+
# -----BEGIN CERTIFICATE-----
26+
# -----END CERTIFICATE-----
27+
# """

0 commit comments

Comments
 (0)