Skip to content

Postgres custom root certificates#3395

Open
itowlson wants to merge 3 commits intospinframework:mainfrom
itowlson:pg-certs-6
Open

Postgres custom root certificates#3395
itowlson wants to merge 3 commits intospinframework:mainfrom
itowlson:pg-certs-6

Conversation

@itowlson
Copy link
Collaborator

Take seven. This is part of the "guests have to include their root cert as an asset" genre. Based on Lann's suggestion at #3381 (comment).

This is pretty unergonomic as you can see from the example (in tests/manual - look at the commented out 'for testing with TLS' stuff), but hopefully we can wrapper it in the SDKs, and if not... well... at least folks have an escape hatch eh.

Comment on lines 49 to 54
let pool_key = format!(
"{address}{}",
root_ca
.map(|s| format!(" **with root-ca** {s}"))
.unwrap_or_default()
);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should be able to use (String, Option<String>) (or a struct with all of the necessary derives) as the cache key here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

🤦 I'm an idiot. Thank you.

let mut builder = TlsConnector::builder();
if let Some(root_ca) = root_ca {
let cert_bytes = root_ca.as_bytes();
builder.add_root_certificate(native_tls::Certificate::from_pem(cert_bytes)?);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I imagine .context for from_pem might be helpful here.

.get_mut(self_.rep())
.ok_or_else(|| v4::Error::ConnectionFailed("no builder found".into()))?;
builder.root_ca = Some(certificate);
Ok(())
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd either eagerly parse the cert here or make this WIT method infallible.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I tried making it infallible but had to handle the case of resource lookup failure. But yeah, eager parse would give a better locale for the error anyway.

.builders
.get_mut(self_.rep())
.ok_or_else(|| v4::Error::ConnectionFailed("no builder found".into()))?;
// borrow checker gets pedantic here, so we need to outsmart it
Copy link
Collaborator

@lann lann Feb 12, 2026

Choose a reason for hiding this comment

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

Oh right, I forgot how to WIT. I guess the "right" way (what WASI does) would look something like:

resource connection-options {
  constructor();
  set-root-ca: ...
}

resource connection {
  open-with-options: static func(address: string, options: connection-options) -> ...
  ...
}

...or some variation thereof... that would make it safe to take the resource out of the table when you need it.

I don't have strong feelings here. 🤷

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Is there any reason why connection-options would benefit from being a resource in this case? It feels like it's not doing anything a record couldn't do, and passing a record directly to open-with-options would significantly reduce implementation noise. Is it forward compatibility considerations?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yep - records cannot be extended backward-compatibly.

Signed-off-by: itowlson <ivan.towlson@fermyon.com>
Signed-off-by: itowlson <ivan.towlson@fermyon.com>
Signed-off-by: itowlson <ivan.towlson@fermyon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants