-
-
Notifications
You must be signed in to change notification settings - Fork 150
fix: add missing ssl models #666
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ import { type ConnectionOptions } from '@slonik/types'; | |
| import { readFileSync } from 'node:fs'; | ||
| import { z } from 'zod'; | ||
|
|
||
| // eslint-disable-next-line complexity | ||
| export const parseDsn = (dsn: string): ConnectionOptions => { | ||
| if (dsn.trim() === '') { | ||
| return {}; | ||
|
|
@@ -56,7 +57,17 @@ export const parseDsn = (dsn: string): ConnectionOptions => { | |
| .describe( | ||
| 'Specifies the location for the secret key used for the client certificate.', | ||
| ), | ||
| sslmode: z.enum(['disable', 'no-verify', 'require']).optional(), | ||
| sslmode: z | ||
| .enum([ | ||
| 'allow', | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Also, it is not used in the switch line 147. |
||
| 'disable', | ||
| 'no-verify', | ||
| 'prefer', | ||
| 'require', | ||
| 'verify-ca', | ||
| 'verify-full', | ||
| ]) | ||
| .optional(), | ||
| sslrootcert: z | ||
| .string() | ||
| .optional() | ||
|
|
@@ -78,49 +89,83 @@ export const parseDsn = (dsn: string): ConnectionOptions => { | |
| connectionOptions.sslMode = searchParameters.sslmode; | ||
| } | ||
|
|
||
| let sslCert: string | undefined; | ||
| let sslKey: string | undefined; | ||
| let sslRootCert: string | undefined; | ||
| /** | ||
| * Refer to https://github.com/brianc/node-postgres/pull/2709 | ||
| */ | ||
| if ( | ||
| searchParameters.sslcert || | ||
| searchParameters.sslkey || | ||
| searchParameters.sslrootcert || | ||
| searchParameters.sslmode | ||
| ) { | ||
| let sslCert: string | undefined; | ||
| let sslKey: string | undefined; | ||
| let sslRootCert: string | undefined; | ||
|
|
||
| if (searchParameters.sslcert) { | ||
| try { | ||
| sslCert = readFileSync(searchParameters.sslcert, 'utf8'); | ||
| } catch { | ||
| throw new UnexpectedStateError('Failed to read SSL certificate file.'); | ||
| } | ||
| } | ||
|
|
||
| if (searchParameters.sslcert) { | ||
| try { | ||
| sslCert = readFileSync(searchParameters.sslcert, 'utf8'); | ||
| } catch { | ||
| throw new UnexpectedStateError('Failed to read SSL certificate file.'); | ||
| if (searchParameters.sslkey) { | ||
| try { | ||
| sslKey = readFileSync(searchParameters.sslkey, 'utf8'); | ||
| } catch { | ||
| throw new UnexpectedStateError('Failed to read SSL key file.'); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (searchParameters.sslkey) { | ||
| try { | ||
| sslKey = readFileSync(searchParameters.sslkey, 'utf8'); | ||
| } catch { | ||
| throw new UnexpectedStateError('Failed to read SSL key file.'); | ||
| if (searchParameters.sslrootcert) { | ||
| try { | ||
| sslRootCert = readFileSync(searchParameters.sslrootcert, 'utf8'); | ||
| } catch { | ||
| throw new UnexpectedStateError( | ||
| 'Failed to read SSL root certificate file.', | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (searchParameters.sslrootcert) { | ||
| try { | ||
| sslRootCert = readFileSync(searchParameters.sslrootcert, 'utf8'); | ||
| } catch { | ||
| throw new UnexpectedStateError( | ||
| 'Failed to read SSL root certificate file.', | ||
| ); | ||
| if (sslCert || sslKey || sslRootCert) { | ||
| if ((sslCert && !sslKey) || (!sslCert && sslKey)) { | ||
| throw new UnexpectedStateError( | ||
| 'Both sslcert and sslkey must be provided together.', | ||
| ); | ||
| } | ||
|
|
||
| connectionOptions.ssl = { | ||
| ca: sslRootCert, | ||
| cert: sslCert, | ||
| key: sslKey, | ||
| rejectUnauthorized: searchParameters.sslmode !== 'no-verify', | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| if (sslCert || sslKey || sslRootCert) { | ||
| if ((sslCert && !sslKey) || (!sslCert && sslKey)) { | ||
| throw new UnexpectedStateError( | ||
| 'Both sslcert and sslkey must be provided together.', | ||
| ); | ||
| switch (connectionOptions.sslMode) { | ||
| case 'disable': { | ||
| connectionOptions.ssl = false; | ||
| break; | ||
| } | ||
|
|
||
| connectionOptions.ssl = { | ||
| ca: sslRootCert, | ||
| cert: sslCert, | ||
| key: sslKey, | ||
| rejectUnauthorized: searchParameters.sslmode !== 'no-verify', | ||
| }; | ||
| case 'no-verify': { | ||
| const ssl = connectionOptions.ssl ?? {}; | ||
|
|
||
| connectionOptions.ssl = { | ||
| ...ssl, | ||
| rejectUnauthorized: false, | ||
| }; | ||
| break; | ||
| } | ||
|
|
||
| case 'prefer': | ||
| case 'require': | ||
| case 'verify-ca': | ||
| case 'verify-full': { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return connectionOptions; | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment here