-
-
Notifications
You must be signed in to change notification settings - Fork 665
feat: dynamic TLS configuration support #4191
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
jeengbe
wants to merge
3
commits into
sidorares:master
Choose a base branch
from
jeengbe:je-tls-factory
base: master
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
3 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -378,17 +378,40 @@ class BaseConnection extends EventEmitter { | |
| if (this.config.debug) { | ||
| console.log('Upgrading connection to TLS'); | ||
| } | ||
|
|
||
| const sslConfig = | ||
| typeof this.config.ssl === 'function' | ||
| ? this.config.ssl(this.config) | ||
| : this.config.ssl; | ||
|
|
||
|
Owner
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. worth adding extra guard if (typeof sslConfig !== 'object' || sslConfig === null) {
onSecure(new TypeError('SSL factory must return an object'));
return;
}so that if user supplied factory returns garbage its much easier to debug ( the typings do cover that, but won't hurt imo ) |
||
| if (typeof sslConfig.then === 'function') { | ||
| sslConfig.then( | ||
| (resolvedSslConfig) => { | ||
| this._onSslConfig(resolvedSslConfig, onSecure); | ||
| }, | ||
| (err) => { | ||
| onSecure(err); | ||
| } | ||
| ); | ||
| } else { | ||
| this._onSslConfig(sslConfig, onSecure); | ||
| } | ||
| } | ||
|
|
||
| _onSslConfig(sslConfig, onSecure) { | ||
| const secureContext = Tls.createSecureContext({ | ||
| ca: this.config.ssl.ca, | ||
| cert: this.config.ssl.cert, | ||
| ciphers: this.config.ssl.ciphers, | ||
| key: this.config.ssl.key, | ||
| passphrase: this.config.ssl.passphrase, | ||
| minVersion: this.config.ssl.minVersion, | ||
| maxVersion: this.config.ssl.maxVersion, | ||
| ca: sslConfig.ca, | ||
| cert: sslConfig.cert, | ||
| ciphers: sslConfig.ciphers, | ||
| key: sslConfig.key, | ||
| passphrase: sslConfig.passphrase, | ||
| minVersion: sslConfig.minVersion, | ||
| maxVersion: sslConfig.maxVersion, | ||
| }); | ||
| const rejectUnauthorized = this.config.ssl.rejectUnauthorized; | ||
| const verifyIdentity = this.config.ssl.verifyIdentity; | ||
|
|
||
| // Default rejectUnauthorized to true | ||
| const rejectUnauthorized = sslConfig.rejectUnauthorized !== false; | ||
| const verifyIdentity = sslConfig.verifyIdentity; | ||
| const servername = Net.isIP(this.config.host) | ||
| ? undefined | ||
| : this.config.host; | ||
|
|
||
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
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,127 @@ | ||
| import EventEmitter from 'node:events'; | ||
| import { describe, it, strict } from 'poku'; | ||
| import BaseConnection from '../../../lib/base/connection.js'; | ||
| import ConnectionConfig from '../../../lib/connection_config.js'; | ||
|
|
||
| type SslFactoryResult = { | ||
| ca?: string; | ||
| cert?: string; | ||
| key?: string; | ||
| rejectUnauthorized?: boolean; | ||
| }; | ||
|
|
||
| function createMockConnection( | ||
| ssl: | ||
| | false | ||
| | (( | ||
| config: ConnectionConfig | ||
| ) => SslFactoryResult | Promise<SslFactoryResult>) | ||
| ) { | ||
| const config = new ConnectionConfig({ | ||
| host: 'localhost', | ||
| user: 'test', | ||
| password: 'test', | ||
| database: 'test', | ||
| connectTimeout: 0, | ||
| ssl, | ||
| }); | ||
|
|
||
| const mockStream = Object.assign(new EventEmitter(), { | ||
| write: () => true, | ||
| end: () => {}, | ||
| destroy() { | ||
| this.destroyed = true; | ||
| }, | ||
| destroyed: false, | ||
| setKeepAlive: () => {}, | ||
| setNoDelay: () => {}, | ||
| removeAllListeners: EventEmitter.prototype.removeAllListeners, | ||
| }); | ||
|
|
||
| config.stream = mockStream; | ||
| config.isServer = true; | ||
|
|
||
| return new BaseConnection({ config }); | ||
| } | ||
|
|
||
| await describe('dynamic SSL options', async () => { | ||
| await it('should resolve SSL options from a synchronous function', async () => { | ||
| let capturedSslFactoryArg: ConnectionConfig | undefined; | ||
| const connection = createMockConnection((config) => { | ||
| capturedSslFactoryArg = config; | ||
| return { | ||
| ca: 'ca-data', | ||
| rejectUnauthorized: false, | ||
| }; | ||
| }); | ||
|
|
||
| let capturedSslConfig: SslFactoryResult | undefined; | ||
| connection._onSslConfig = (sslConfig, onSecure) => { | ||
| capturedSslConfig = sslConfig; | ||
| onSecure(); | ||
| }; | ||
|
|
||
| await new Promise<void>((resolve, reject) => { | ||
| connection.startTLS((err: unknown) => { | ||
| if (err) { | ||
| reject(err); | ||
| return; | ||
| } | ||
| resolve(); | ||
| }); | ||
| }); | ||
|
|
||
| strict.equal(capturedSslFactoryArg, connection.config); | ||
| strict.deepEqual(capturedSslConfig, { | ||
| ca: 'ca-data', | ||
| rejectUnauthorized: false, | ||
| }); | ||
| }); | ||
|
|
||
| await it('should resolve SSL options from an asynchronous function', async () => { | ||
| const connection = createMockConnection(async () => ({ | ||
| ca: 'async-ca', | ||
| })); | ||
|
|
||
| let capturedSslConfig: SslFactoryResult | undefined; | ||
| connection._onSslConfig = (sslConfig, onSecure) => { | ||
| capturedSslConfig = sslConfig; | ||
| onSecure(); | ||
| }; | ||
|
|
||
| await new Promise<void>((resolve, reject) => { | ||
| connection.startTLS((err: unknown) => { | ||
| if (err) { | ||
| reject(err); | ||
| return; | ||
| } | ||
| resolve(); | ||
| }); | ||
| }); | ||
|
|
||
| strict.deepEqual(capturedSslConfig, { | ||
| ca: 'async-ca', | ||
| }); | ||
| }); | ||
|
|
||
| await it('should pass factory rejections to onSecure callback', async () => { | ||
| const connection = createMockConnection(async () => { | ||
| throw new Error('dynamic ssl failed'); | ||
| }); | ||
|
|
||
| let onSslConfigCalled = false; | ||
| connection._onSslConfig = (_sslConfig, _onSecure) => { | ||
| onSslConfigCalled = true; | ||
| }; | ||
|
|
||
| await new Promise<void>((resolve) => { | ||
| connection.startTLS((err: unknown) => { | ||
| strict.ok(err instanceof Error); | ||
| strict.equal((err as Error).message, 'dynamic ssl failed'); | ||
| resolve(); | ||
| }); | ||
| }); | ||
|
|
||
| strict.equal(onSslConfigCalled, false); | ||
| }); | ||
| }); |
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.
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.
what happens if this throws synchronously? The caller does not catches error, and unlike async error we'll have connection in a broken state