|
| 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 | +}) |
0 commit comments