Skip to content

Conversation

@tankyleo
Copy link
Contributor

@tankyleo tankyleo commented Nov 30, 2025

Fixes #56

@ldk-reviews-bot
Copy link

ldk-reviews-bot commented Nov 30, 2025

👋 Thanks for assigning @tnull as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

@tankyleo tankyleo requested a review from tnull November 30, 2025 09:20
@tankyleo tankyleo force-pushed the db-tls branch 2 times, most recently from 34bcf93 to 127ba60 Compare November 30, 2025 09:34
bb8-postgres = "0.7"
bytes = "1.4.0"
tokio = { version = "1.38.0", default-features = false }
openssl = "0.10.75"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add these with default-features = false to ensure we're not pulling in any unnecessary dependencies?

bb8-postgres = "0.7"
bytes = "1.4.0"
tokio = { version = "1.38.0", default-features = false }
openssl = "0.10.75"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than always using openssl, should we rather use native-tls to account for different TLS backends on different platforms? https://github.com/sfackler/rust-native-tls

eprintln!("Connection error: {}", e);
}
});
let client = if let Some(ca_file) = ca_file {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really always require a certificate file for the service? Can't we 'usually' just use PKI?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

certainly thank you

eprintln!("Connection error: {}", e);
}
});
let client = if let Some(ca_file) = ca_file {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this could be DRYed up with the code above?

@tankyleo tankyleo requested a review from tnull December 2, 2025 03:37
Copy link
Contributor

@tnull tnull left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly looks good I think, but the copied code makes it unnecessarily hard to review, IMO. Could we try to DRY it up a bit more?

As follow-ups we also might want to consider to enable certificate pinning and TOFU as best practices. Maybe worth opening issues for these?


enum DbConnectionType {
Plain,
Tls(Option<String>),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do wonder if it makes sense to read the file once rather than in different places, and have the Tls variant hold a override_cert: Option<Certificate> or similar?


impl PostgresBackendImpl {
/// Constructs a [`PostgresBackendImpl`] using `dsn` for PostgreSQL connection information.
impl PostgresBackendImplTls {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, not the biggest fan of duplicating the init logic. Can we maybe move everything that's not related to TLS to shared helper methods?

@tankyleo tankyleo requested a review from tnull December 3, 2025 01:46
@tankyleo
Copy link
Contributor Author

tankyleo commented Dec 3, 2025

Mostly looks good I think, but the copied code makes it unnecessarily hard to review, IMO. Could we try to DRY it up a bit more?

Thanks for the nudge, I think things look better now take a look

As follow-ups we also might want to consider to enable certificate pinning and TOFU as best practices. Maybe worth opening issues for these?

Done

Copy link
Contributor

@tnull tnull left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly looks good, feel free to squash the fixups.

One question

);
let store: Arc<dyn KvStore> = if let Some(tls_config) = postgresql_config.tls {
let additional_certificate = tls_config.ca_file.map(|file| {
let cert = std::fs::read(&file).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we print an error message and error out via std::process::exit(-1); rather than just unwraping here and elsewhere?

In particular, this might happen frequently if the user gives a wrong path or the file is not readable.

Also add the option to specify an additional root certificate for
the client to trust.
@tankyleo
Copy link
Contributor Author

tankyleo commented Dec 3, 2025

Thanks squashed with the following diff:

diff --git a/rust/server/src/main.rs b/rust/server/src/main.rs
index 2b36df6..38fdccd 100644
--- a/rust/server/src/main.rs
+++ b/rust/server/src/main.rs
@@ -73,14 +73,40 @@ fn main() {
 		let db_name = postgresql_config.database;
 		let store: Arc<dyn KvStore> = if let Some(tls_config) = postgresql_config.tls {
 			let additional_certificate = tls_config.ca_file.map(|file| {
-				let cert = std::fs::read(&file).unwrap();
-				Certificate::from_pem(&cert).unwrap()
+				let certificate = match std::fs::read(&file) {
+					Ok(cert) => cert,
+					Err(e) => {
+						println!("Failed to read certificate file: {}", e);
+						std::process::exit(-1);
+					},
+				};
+				match Certificate::from_pem(&certificate) {
+					Ok(cert) => cert,
+					Err(e) => {
+						println!("Failed to parse certificate file: {}", e);
+						std::process::exit(-1);
+					},
+				}
 			});
-			Arc::new(
-				PostgresTlsBackend::new(&endpoint, &db_name, additional_certificate).await.unwrap(),
-			)
+			let postgres_tls_backend =
+				match PostgresTlsBackend::new(&endpoint, &db_name, additional_certificate).await {
+					Ok(backend) => backend,
+					Err(e) => {
+						println!("Failed to start postgres tls backend: {}", e);
+						std::process::exit(-1);
+					},
+				};
+			Arc::new(postgres_tls_backend)
 		} else {
-			Arc::new(PostgresPlaintextBackend::new(&endpoint, &db_name).await.unwrap())
+			let postgres_plaintext_backend =
+				match PostgresPlaintextBackend::new(&endpoint, &db_name).await {
+					Ok(backend) => backend,
+					Err(e) => {
+						println!("Failed to start postgres plaintext backend: {}", e);
+						std::process::exit(-1);
+					},
+				};
+			Arc::new(postgres_plaintext_backend)
 		};
 		println!("Connected to PostgreSQL backend with DSN: {}/{}", endpoint, db_name);
 		let rest_svc_listener =

@tankyleo tankyleo requested a review from tnull December 3, 2025 15:40
@tnull tnull merged commit 56be2d9 into lightningdevkit:main Dec 4, 2025
2 checks passed
@tankyleo tankyleo self-assigned this Dec 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[rust-server] Add option to make TLS connections to the postgres database

3 participants