-
Notifications
You must be signed in to change notification settings - Fork 151
Add acceptance test to neo4j-deno-driver
and implement deno specific channel
#900
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
Merged
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
6763bc8
Add Testkit support to DenoJS
bigmontz 0dff616
Add `deno-channel` for non-encrypted connection and fix deno-backend
bigmontz 9c72390
Add support to TLS
bigmontz 83a77a0
Ajust testkit scripts
bigmontz 4d89c83
Adjust neo4j import and tls import
bigmontz 286d208
Revert browser channel
bigmontz 65d2691
Fix missing neo4j definition
bigmontz 6ea7a14
Move Deno implemenation for its own folder
bigmontz f2e9436
Refactory DenoJS implementation
bigmontz 2de0f8d
deno fmt
bigmontz 8385ef6
Revert changes in the log config
bigmontz 04894e6
Adjust linter issues
bigmontz 2af4009
Improve logs in the backend
bigmontz 00fbae2
Add dependencies
bigmontz 8f5022e
Prevent write in a broken socket
bigmontz f541c0b
Capture and log unhandledrejection
bigmontz 577a375
Move dependencies to deps and fix text encode.
bigmontz 9edf9ec
Move deno-channel implemantation to bolt-connection
bigmontz a7beadc
Fix deps
bigmontz 1cbdeca
Disable eslint for Deno files
bigmontz ccdc4af
Add copyright notice to version.ts
bigmontz befa3c8
Configuring linter and formatter fr the deno backend files
bigmontz 6d56f08
Fix `writeError` function
bigmontz ed8f1c4
Skip test which timesout
bigmontz a79e61f
Fix listener
bigmontz b6e659a
Handling connection write errors
bigmontz 468dc5b
Skip timedout test
bigmontz 46f95b4
fix connection timeout
bigmontz d950957
Apply suggestions from code review
bigmontz bfb81bc
Move binder creation and log env-variable read to the controller.
bigmontz e31cddd
Waiting for connection be closed during timeout
bigmontz 3d68810
docs improvement
bigmontz 6852df6
Apply suggestions from code review
bigmontz b47d05e
Fix CypherNativeBinder import
bigmontz d9c32e1
Install specific Deno version
bigmontz 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
336 changes: 336 additions & 0 deletions
336
packages/bolt-connection/src/channel/deno/deno-channel.js
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,336 @@ | ||
/** | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
/* eslint-disable */ | ||
import ChannelBuffer from '../channel-buf' | ||
import { newError, internal } from 'neo4j-driver-core' | ||
import { iterateReader } from 'https://deno.land/std@0.157.0/streams/conversion.ts'; | ||
|
||
const { | ||
util: { ENCRYPTION_OFF, ENCRYPTION_ON } | ||
} = internal | ||
|
||
let _CONNECTION_IDGEN = 0 | ||
/** | ||
* Create a new DenoChannel to be used in Deno runtime. | ||
* @access private | ||
*/ | ||
export default class DenoChannel { | ||
/** | ||
* Create new instance | ||
* @param {ChannelConfig} config - configuration for this channel. | ||
*/ | ||
constructor ( | ||
config, | ||
connect = _connect | ||
) { | ||
this.id = _CONNECTION_IDGEN++ | ||
this._conn = null | ||
this._pending = [] | ||
this._open = true | ||
this._error = null | ||
this._handleConnectionError = this._handleConnectionError.bind(this) | ||
this._handleConnectionTerminated = this._handleConnectionTerminated.bind( | ||
this | ||
) | ||
this._connectionErrorCode = config.connectionErrorCode | ||
this._receiveTimeout = null | ||
this._receiveTimeoutStarted = false | ||
this._receiveTimeoutId = null | ||
|
||
this._config = config | ||
|
||
connect(config) | ||
.then(conn => { | ||
this._clearConnectionTimeout() | ||
if (!this._open) { | ||
return conn.close() | ||
} | ||
this._conn = conn | ||
|
||
setupReader(this) | ||
.catch(this._handleConnectionError) | ||
|
||
const pending = this._pending | ||
this._pending = null | ||
for (let i = 0; i < pending.length; i++) { | ||
this.write(pending[i]) | ||
} | ||
}) | ||
.catch(this._handleConnectionError) | ||
|
||
this._connectionTimeoutFired = false | ||
this._connectionTimeoutId = this._setupConnectionTimeout() | ||
} | ||
|
||
_setupConnectionTimeout () { | ||
const timeout = this._config.connectionTimeout | ||
if (timeout) { | ||
return setTimeout(() => { | ||
this._connectionTimeoutFired = true | ||
this.close() | ||
.then(e => this._handleConnectionError(newError(`Connection timeout after ${timeout} ms`))) | ||
.catch(this._handleConnectionError) | ||
}, timeout) | ||
} | ||
return null | ||
} | ||
|
||
/** | ||
* Remove active connection timeout, if any. | ||
* @private | ||
*/ | ||
_clearConnectionTimeout () { | ||
const timeoutId = this._connectionTimeoutId | ||
if (timeoutId !== null) { | ||
this._connectionTimeoutFired = false | ||
this._connectionTimeoutId = null | ||
clearTimeout(timeoutId) | ||
} | ||
} | ||
|
||
_handleConnectionError (err) { | ||
let msg = | ||
'Failed to connect to server. ' + | ||
'Please ensure that your database is listening on the correct host and port ' + | ||
'and that you have compatible encryption settings both on Neo4j server and driver. ' + | ||
'Note that the default encryption setting has changed in Neo4j 4.0.' | ||
if (err.message) msg += ' Caused by: ' + err.message | ||
this._error = newError(msg, this._connectionErrorCode) | ||
if (this.onerror) { | ||
this.onerror(this._error) | ||
} | ||
} | ||
|
||
_handleConnectionTerminated () { | ||
this._open = false | ||
this._error = newError( | ||
'Connection was closed by server', | ||
this._connectionErrorCode | ||
) | ||
if (this.onerror) { | ||
this.onerror(this._error) | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Write the passed in buffer to connection | ||
* @param {ChannelBuffer} buffer - Buffer to write | ||
*/ | ||
write (buffer) { | ||
if (this._pending !== null) { | ||
this._pending.push(buffer) | ||
} else if (buffer instanceof ChannelBuffer) { | ||
this._conn.write(buffer._buffer).catch(this._handleConnectionError) | ||
} else { | ||
throw newError("Don't know how to send buffer: " + buffer) | ||
} | ||
} | ||
|
||
/** | ||
* Close the connection | ||
* @returns {Promise} A promise that will be resolved after channel is closed | ||
*/ | ||
async close () { | ||
if (this._open) { | ||
this._open = false | ||
if (this._conn != null) { | ||
await this._conn.close() | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Setup the receive timeout for the channel. | ||
* | ||
* Not supported for the browser channel. | ||
* | ||
* @param {number} receiveTimeout The amount of time the channel will keep without receive any data before timeout (ms) | ||
* @returns {void} | ||
*/ | ||
setupReceiveTimeout (receiveTimeout) { | ||
this._receiveTimeout = receiveTimeout | ||
} | ||
|
||
/** | ||
* Stops the receive timeout for the channel. | ||
*/ | ||
stopReceiveTimeout () { | ||
if (this._receiveTimeout !== null && this._receiveTimeoutStarted) { | ||
this._receiveTimeoutStarted = false | ||
if (this._receiveTimeoutId != null) { | ||
clearTimeout(this._receiveTimeoutId) | ||
} | ||
this._receiveTimeoutId = null | ||
} | ||
} | ||
|
||
/** | ||
* Start the receive timeout for the channel. | ||
*/ | ||
startReceiveTimeout () { | ||
if (this._receiveTimeout !== null && !this._receiveTimeoutStarted) { | ||
this._receiveTimeoutStarted = true | ||
this._resetTimeout() | ||
} | ||
} | ||
|
||
_resetTimeout () { | ||
if (!this._receiveTimeoutStarted) { | ||
return | ||
} | ||
|
||
if (this._receiveTimeoutId !== null) { | ||
clearTimeout(this._receiveTimeoutId) | ||
} | ||
|
||
this._receiveTimeoutId = setTimeout(() => { | ||
this._receiveTimeoutId = null | ||
this.stopReceiveTimeout() | ||
this._error = newError( | ||
`Connection lost. Server didn't respond in ${this._receiveTimeout}ms`, | ||
this._config.connectionErrorCode | ||
) | ||
|
||
this.close() | ||
.catch(() => { | ||
// ignoring error during the close timeout connections since they | ||
// not valid | ||
}) | ||
.finally(() => { | ||
if (this.onerror) { | ||
this.onerror(this._error) | ||
} | ||
}) | ||
}, this._receiveTimeout) | ||
} | ||
} | ||
|
||
const TrustStrategy = { | ||
TRUST_CUSTOM_CA_SIGNED_CERTIFICATES: async function (config) { | ||
if ( | ||
!config.trustedCertificates || | ||
config.trustedCertificates.length === 0 | ||
) { | ||
throw newError( | ||
'You are using TRUST_CUSTOM_CA_SIGNED_CERTIFICATES as the method ' + | ||
'to verify trust for encrypted connections, but have not configured any ' + | ||
'trustedCertificates. You must specify the path to at least one trusted ' + | ||
'X.509 certificate for this to work. Two other alternatives is to use ' + | ||
'TRUST_ALL_CERTIFICATES or to disable encryption by setting encrypted="' + | ||
ENCRYPTION_OFF + | ||
'"' + | ||
'in your driver configuration.' | ||
); | ||
} | ||
|
||
const caCerts = await Promise.all( | ||
config.trustedCertificates.map(f => Deno.readTextFile(f)) | ||
) | ||
|
||
return Deno.connectTls({ | ||
hostname: config.address.resolvedHost(), | ||
port: config.address.port(), | ||
caCerts | ||
}) | ||
}, | ||
TRUST_SYSTEM_CA_SIGNED_CERTIFICATES: function (config) { | ||
return Deno.connectTls({ | ||
hostname: config.address.resolvedHost(), | ||
port: config.address.port() | ||
}) | ||
}, | ||
TRUST_ALL_CERTIFICATES: function (config) { | ||
throw newError( | ||
`"${config.trust}" is not available in DenoJS. ` + | ||
'For trust in any certificates, you should use the DenoJS flag ' + | ||
'"--unsafely-ignore-certificate-errors". '+ | ||
'See, https://deno.com/blog/v1.13#disable-tls-verification' | ||
) | ||
} | ||
} | ||
|
||
async function _connect (config) { | ||
if (!isEncrypted(config)) { | ||
return Deno.connect({ | ||
hostname: config.address.resolvedHost(), | ||
port: config.address.port() | ||
}) | ||
} | ||
const trustStrategyName = getTrustStrategyName(config) | ||
const trustStrategy = TrustStrategy[trustStrategyName] | ||
|
||
if (trustStrategy != null) { | ||
return await trustStrategy(config) | ||
} | ||
|
||
throw newError( | ||
'Unknown trust strategy: ' + | ||
config.trust + | ||
'. Please use either ' + | ||
"trust:'TRUST_CUSTOM_CA_SIGNED_CERTIFICATES' configuration " + | ||
'or the System CA. ' + | ||
'Alternatively, you can disable encryption by setting ' + | ||
'`encrypted:"' + | ||
ENCRYPTION_OFF + | ||
'"`. There is no mechanism to use encryption without trust verification, ' + | ||
'because this incurs the overhead of encryption without improving security. If ' + | ||
'the driver does not verify that the peer it is connected to is really Neo4j, it ' + | ||
'is very easy for an attacker to bypass the encryption by pretending to be Neo4j.' | ||
|
||
) | ||
} | ||
|
||
function isEncrypted (config) { | ||
const encryptionNotConfigured = | ||
config.encrypted == null || config.encrypted === undefined | ||
if (encryptionNotConfigured) { | ||
// default to using encryption if trust-all-certificates is available | ||
return false | ||
} | ||
return config.encrypted === true || config.encrypted === ENCRYPTION_ON | ||
} | ||
|
||
function getTrustStrategyName (config) { | ||
if (config.trust) { | ||
return config.trust | ||
} | ||
return 'TRUST_SYSTEM_CA_SIGNED_CERTIFICATES' | ||
} | ||
|
||
async function setupReader (channel) { | ||
try { | ||
for await (const message of iterateReader(channel._conn)) { | ||
channel._resetTimeout() | ||
|
||
if (!channel._open) { | ||
return | ||
} | ||
if (channel.onmessage) { | ||
channel.onmessage(new ChannelBuffer(message)) | ||
} | ||
} | ||
channel._handleConnectionTerminated() | ||
} catch (error) { | ||
if (channel._open) { | ||
channel._handleConnectionError(error) | ||
} | ||
} | ||
} | ||
|
29 changes: 29 additions & 0 deletions
29
packages/bolt-connection/src/channel/deno/deno-host-name-resolver.js
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,29 @@ | ||
/** | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { internal } from 'neo4j-driver-core' | ||
|
||
const { | ||
resolver: { BaseHostNameResolver } | ||
} = internal | ||
|
||
export default class DenoHostNameResolver extends BaseHostNameResolver { | ||
resolve (address) { | ||
return this._resolveToItself(address) | ||
robsdedude marked this conversation as resolved.
Show resolved
Hide resolved
bigmontz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
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.