Skip to content

Commit ac9de13

Browse files
committed
Add a test to verify encryption and tls level combinations
1 parent 9d930b7 commit ac9de13

File tree

4 files changed

+137
-25
lines changed

4 files changed

+137
-25
lines changed

src/index.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,19 +138,22 @@ const logging = {
138138
* // this is that if you don't know who you are talking to, it is easy for an
139139
* // attacker to hijack your encrypted connection, rendering encryption pointless.
140140
* //
141-
* // TRUST_ALL_CERTIFICATES is the default choice for NodeJS deployments. It only requires
142-
* // new host to provide a certificate and does no verification of the provided certificate.
141+
* // TRUST_SYSTEM_CA_SIGNED_CERTIFICATES is the default choice. For NodeJS environments, this
142+
* // means that you trust whatever certificates are in the default trusted certificate
143+
* // store of the underlying system. For Browser environments, the trusted certificate
144+
* // store is usually managed by the browser. Refer to your system or browser documentation
145+
* // if you want to explicitly add a certificate as trusted.
143146
* //
144-
* // TRUST_CUSTOM_CA_SIGNED_CERTIFICATES is the classic approach to trust verification -
147+
* // TRUST_CUSTOM_CA_SIGNED_CERTIFICATES is another option for trust verification -
145148
* // whenever we establish an encrypted connection, we ensure the host is using
146-
* // an encryption certificate that is in, or is signed by, a certificate listed
147-
* // as trusted. In the web bundle, this list of trusted certificates is maintained
148-
* // by the web browser. In NodeJS, you configure the list with the next config option.
149+
* // an encryption certificate that is in, or is signed by, a certificate given
150+
* // as trusted through configuration. This option is only available for NodeJS environments.
149151
* //
150-
* // TRUST_SYSTEM_CA_SIGNED_CERTIFICATES means that you trust whatever certificates
151-
* // are in the default certificate chain of the underlying system.
152-
* trust: "TRUST_ALL_CERTIFICATES" | "TRUST_CUSTOM_CA_SIGNED_CERTIFICATES" |
153-
* "TRUST_SYSTEM_CA_SIGNED_CERTIFICATES",
152+
* // TRUST_ALL_CERTIFICATES means that you trust everything without any verifications
153+
* // steps carried out. This option is only available for NodeJS environments and should not
154+
* // be used on production systems.
155+
* trust: "TRUST_SYSTEM_CA_SIGNED_CERTIFICATES" | "TRUST_CUSTOM_CA_SIGNED_CERTIFICATES" |
156+
* "TRUST_ALL_CERTIFICATES",
154157
*
155158
* // List of one or more paths to trusted encryption certificates. This only
156159
* // works in the NodeJS bundle, and only matters if you use "TRUST_CUSTOM_CA_SIGNED_CERTIFICATES".

test/internal/node/encryption.test.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* Copyright (c) 2002-2019 "Neo4j,"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
import neo4j from '../../../src'
21+
import sharedNeo4j from '../shared-neo4j'
22+
23+
describe('#integration encryption', () => {
24+
let originalTimeout
25+
26+
beforeEach(() => {
27+
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL
28+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000
29+
})
30+
31+
afterEach(() => {
32+
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout
33+
})
34+
35+
afterAll(() => {
36+
sharedNeo4j.restart()
37+
})
38+
39+
it('should be able to connect when encryption is off and tls_level is DISABLED', () =>
40+
verifyEncryption(false, sharedNeo4j.tlsConfig.levels.disabled, null, true))
41+
42+
it('should not be able to connect when encryption is on and tls_level is DISABLED', () =>
43+
verifyEncryption(true, sharedNeo4j.tlsConfig.levels.disabled, null, false))
44+
45+
it('should be able to connect when encryption is off and tls_level is OPTIONAL', () =>
46+
verifyEncryption(false, sharedNeo4j.tlsConfig.levels.optional, null, true))
47+
48+
it('should not be able to connect when encryption is on and tls_level is OPTIONAL', () =>
49+
verifyEncryption(true, sharedNeo4j.tlsConfig.levels.optional, null, false))
50+
51+
it('should be able to connect when encryption is on, tls_level is OPTIONAL and trust is TRUST_ALL', () =>
52+
verifyEncryption(
53+
true,
54+
sharedNeo4j.tlsConfig.levels.optional,
55+
'TRUST_ALL_CERTIFICATES',
56+
true
57+
))
58+
59+
it('should not be able to connect when encryption is off and tls_level is REQUIRED', () =>
60+
verifyEncryption(false, sharedNeo4j.tlsConfig.levels.required, null, false))
61+
62+
it('should not be able to connect when encryption is on and tls_level is REQUIRED', () =>
63+
verifyEncryption(true, sharedNeo4j.tlsConfig.levels.required, null, false))
64+
65+
it('should be able to connect when encryption is on, tls_level is REQUIRED and trust is TRUST_ALL', () =>
66+
verifyEncryption(
67+
true,
68+
sharedNeo4j.tlsConfig.levels.required,
69+
'TRUST_ALL_CERTIFICATES',
70+
true
71+
))
72+
73+
async function verifyEncryption (encrypted, tlsLevel, trust, expectToSucceed) {
74+
sharedNeo4j.restart(tlsConfig(tlsLevel))
75+
76+
const config = {
77+
encrypted: encrypted,
78+
logging: sharedNeo4j.logging
79+
}
80+
if (trust) {
81+
config.trust = trust
82+
}
83+
const driver = neo4j.driver(
84+
'bolt://localhost',
85+
sharedNeo4j.authToken,
86+
config
87+
)
88+
const session = driver.session()
89+
90+
if (expectToSucceed) {
91+
await expectAsync(session.run('CREATE (n) RETURN n')).toBeResolved()
92+
} else {
93+
await expectAsync(session.run('CREATE (n) RETURN n')).toBeRejected()
94+
}
95+
96+
await session.close()
97+
await driver.close()
98+
}
99+
100+
function tlsConfig (tlsLevel) {
101+
const config = {}
102+
config[sharedNeo4j.tlsConfig.key] = tlsLevel
103+
return config
104+
}
105+
})

test/internal/node/tls.test.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@
1818
*/
1919

2020
import neo4j from '../../../src'
21-
import path from 'path'
2221
import sharedNeo4j from '../shared-neo4j'
23-
import {
24-
ServerVersion,
25-
VERSION_4_0_0
26-
} from '../../../src/internal/server-version'
2722

2823
describe('#integration trust', () => {
2924
let serverVersion

test/internal/shared-neo4j.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,13 @@ const username = 'neo4j'
124124
const password = 'password'
125125
const authToken = neo4j.auth.basic(username, password)
126126

127-
const boltTlsLevel = {
128-
optional: 'OPTIONAL',
129-
required: 'REQUIRED',
130-
disabled: 'DISABLED'
127+
const tlsConfig = {
128+
key: 'dbms.connector.bolt.tls_level',
129+
levels: {
130+
optional: 'OPTIONAL',
131+
required: 'REQUIRED',
132+
disabled: 'DISABLED'
133+
}
131134
}
132135

133136
const defaultConfig = {
@@ -147,7 +150,7 @@ const defaultConfig = {
147150
'dbms.memory.pagecache.size': '512m',
148151

149152
// make TLS optional
150-
'dbms.connector.bolt.tls_level': boltTlsLevel.optional
153+
'dbms.connector.bolt.tls_level': tlsConfig.levels.optional
151154
}
152155

153156
const NEOCTRLARGS = 'NEOCTRLARGS'
@@ -217,9 +220,7 @@ function configure (config) {
217220
`Configuring neo4j at "${neo4jDir()}" with "${JSON.stringify(config)}"`
218221
)
219222

220-
const configEntries = Object.keys(config).map(
221-
key => `${key}=${defaultConfig[key]}`
222-
)
223+
const configEntries = Object.keys(config).map(key => `${key}=${config[key]}`)
223224
if (configEntries.length > 0) {
224225
const result = runCommand('neoctrl-configure', [
225226
neo4jDir(),
@@ -289,8 +290,15 @@ function stop () {
289290
stopNeo4j()
290291
}
291292

292-
function restart () {
293+
function restart (configOverride) {
293294
stopNeo4j()
295+
const newConfig = Object.assign({}, defaultConfig)
296+
if (configOverride) {
297+
Object.keys(configOverride).forEach(
298+
key => (newConfig[key] = configOverride[key])
299+
)
300+
}
301+
configure(newConfig)
294302
startNeo4j()
295303
}
296304

@@ -345,5 +353,6 @@ export default {
345353
password: password,
346354
authToken: authToken,
347355
logging: debugLogging,
348-
cleanupAndGetVersion: cleanupAndGetVersion
356+
cleanupAndGetVersion: cleanupAndGetVersion,
357+
tlsConfig: tlsConfig
349358
}

0 commit comments

Comments
 (0)