-
Notifications
You must be signed in to change notification settings - Fork 8
fix(bindings): resolve cloud Postgres passwords in Rust (ALIEN-334) #207
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
Open
lilienblum
wants to merge
11
commits into
main
Choose a base branch
from
dan/alien-334-cloud-postgres-binding
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
73a5946
feat(bindings): resolve cloud Postgres passwords in Rust (ALIEN-334)
lilienblum d2aa90d
refactor(bindings): re-read cloud Postgres secrets on every load
lilienblum 4a9305f
perf(bindings): cache the cloud Postgres secret-store client
lilienblum daff5a9
chore(greptile): record the postgres binding surface and TLS posture
lilienblum 44749c5
fix(package-layout): pin UnknownPostgresSslModeError in the fixture
lilienblum 769367c
docs(bindings): state the BYO plaintext downgrade plainly
lilienblum c4db739
fix(bindings): secure BYO Postgres TLS defaults
lilienblum 64bb002
fix(bindings): verify managed postgres certificates
lilienblum 08d8288
chore(greptile): drop obsolete postgres context
lilienblum 15e91ce
fix(bindings): harden Postgres resolution
lilienblum 9a4005d
Merge remote-tracking branch 'origin/main' into dan/alien-334-cloud-p…
lilienblum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| //! Postgres binding handle. Thin argument/error translation over the `Postgres` | ||
| //! trait. | ||
| //! | ||
| //! Postgres is connection-only: there are no operations to forward, just the connection | ||
| //! details the Rust provider already resolved (including reading a cloud backend's | ||
| //! password from its secret store). The handle is therefore synchronous — every field is | ||
| //! in memory by the time `BindingsHandle::postgres` returns. | ||
|
|
||
| use alien_bindings::{Postgres, PostgresConnectionParams}; | ||
| use napi_derive::napi; | ||
| use std::sync::Arc; | ||
|
|
||
| /// Everything a Postgres driver needs to connect. | ||
| #[napi(object)] | ||
| pub struct PostgresConnectionJs { | ||
| /// `postgres://user:password@host:port/database?sslmode=<mode>`, with the username, | ||
| /// password, and database percent-encoded. | ||
| pub connection_string: String, | ||
| /// Address to dial (the cluster writer endpoint for Aurora). | ||
| pub host: String, | ||
| /// TCP port. | ||
| pub port: u32, | ||
| /// Database name. | ||
| pub database: String, | ||
| /// Role to connect as. | ||
| pub username: String, | ||
| /// Connection password, already resolved from the cloud secret store where applicable. | ||
| pub password: String, | ||
| /// `disable` (local or explicit BYO plaintext), `verify-ca` (Cloud SQL), or | ||
| /// `verify-full` (BYO, Aurora, and Flexible Server). | ||
| pub sslmode: String, | ||
| /// PEM-encoded root CAs used by the verified TLS modes. | ||
| pub ca_certificates: Vec<String>, | ||
| } | ||
|
|
||
| /// Handle to a resolved Postgres binding. | ||
| #[napi] | ||
| pub struct PostgresHandle { | ||
| inner: Arc<dyn Postgres>, | ||
| } | ||
|
|
||
| impl PostgresHandle { | ||
| pub(crate) fn new(inner: Arc<dyn Postgres>) -> Self { | ||
| Self { inner } | ||
| } | ||
| } | ||
|
|
||
| /// Translate resolved connection parameters into their JS shape. | ||
| fn connection_to_js(params: &PostgresConnectionParams) -> PostgresConnectionJs { | ||
| PostgresConnectionJs { | ||
| connection_string: params.connection_string(), | ||
| host: params.host.clone(), | ||
| port: u32::from(params.port), | ||
| database: params.database.clone(), | ||
| username: params.username.clone(), | ||
| password: params.password.clone(), | ||
| sslmode: params.sslmode().as_str().to_string(), | ||
| ca_certificates: params.ca_certificates().to_vec(), | ||
| } | ||
| } | ||
|
|
||
| #[napi] | ||
| impl PostgresHandle { | ||
| /// Return the resolved connection details. | ||
| #[napi] | ||
| pub fn connection(&self) -> PostgresConnectionJs { | ||
| connection_to_js(self.inner.connection_params()) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use alien_bindings::PostgresTlsPolicy; | ||
|
|
||
| /// The JS shape must carry every field verbatim, with the connection string derived | ||
| /// from the same params (not stored separately) and `sslmode` as its wire string. | ||
| #[test] | ||
| fn connection_to_js_maps_every_field() { | ||
| let params = PostgresConnectionParams::new( | ||
| "cluster.rds.amazonaws.com".to_string(), | ||
| 5432, | ||
| "app".to_string(), | ||
| "alien".to_string(), | ||
| "p@ss/word".to_string(), | ||
| PostgresTlsPolicy::verify_full(vec![ | ||
| "-----BEGIN CERTIFICATE-----\nroot\n-----END CERTIFICATE-----".to_string(), | ||
| ]) | ||
| .unwrap(), | ||
| ); | ||
|
|
||
| let js = connection_to_js(¶ms); | ||
|
|
||
| assert_eq!(js.host, "cluster.rds.amazonaws.com"); | ||
| assert_eq!(js.port, 5432); | ||
| assert_eq!(js.database, "app"); | ||
| assert_eq!(js.username, "alien"); | ||
| assert_eq!(js.password, "p@ss/word"); | ||
| assert_eq!(js.sslmode, "verify-full"); | ||
| assert_eq!(js.ca_certificates, params.ca_certificates()); | ||
| assert_eq!( | ||
| js.connection_string, | ||
| "postgres://alien:p%40ss%2Fword@cluster.rds.amazonaws.com:5432/app?sslmode=verify-full" | ||
| ); | ||
| } | ||
|
|
||
| /// Each `SslMode` must reach JS as the exact string the `sslmode` query parameter | ||
| /// uses, so the TS layer can map it to a driver TLS setting without guessing. | ||
| #[test] | ||
| fn connection_to_js_reports_each_sslmode_as_its_wire_string() { | ||
| for (tls, expected) in [ | ||
| (PostgresTlsPolicy::disabled(), "disable"), | ||
| ( | ||
| PostgresTlsPolicy::verify_ca(vec![ | ||
| "-----BEGIN CERTIFICATE-----\nroot\n-----END CERTIFICATE-----".to_string(), | ||
| ]) | ||
| .unwrap(), | ||
| "verify-ca", | ||
| ), | ||
| ( | ||
| PostgresTlsPolicy::verify_full_with_system_roots(), | ||
| "verify-full", | ||
| ), | ||
| ] { | ||
| let params = PostgresConnectionParams::new( | ||
| "h".to_string(), | ||
| 5432, | ||
| "db".to_string(), | ||
| "u".to_string(), | ||
| "p".to_string(), | ||
| tls, | ||
| ); | ||
|
|
||
| assert_eq!(connection_to_js(¶ms).sslmode, expected); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.