Skip to content

Commit c09ab5b

Browse files
author
Zhen Li
committed
Merge branch '1.0' into 1.1
2 parents 0f3759e + 4d19e69 commit c09ab5b

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

neokit

Submodule neokit updated 1 file

src/v1/internal/ch-node.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,29 @@ function userHome() {
3737
return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
3838
}
3939

40+
function mkFullPath(pathToCreate) {
41+
try {
42+
fs.mkdirSync( pathToCreate );
43+
} catch (e) {
44+
if(e.code === 'ENOENT') {
45+
// Create parent dir
46+
mkFullPath(path.dirname( pathToCreate ));
47+
// And now try again
48+
mkFullPath( pathToCreate );
49+
return;
50+
}
51+
if (e.code === 'EEXIST') {
52+
return;
53+
}
54+
throw e;
55+
}
56+
}
57+
4058
function loadFingerprint( serverId, knownHostsPath, cb ) {
41-
if( !fs.existsSync( knownHostsPath )) {
42-
cb(null);
43-
return;
59+
try {
60+
fs.accessSync( knownHostsPath );
61+
} catch(e) {
62+
return cb(null)
4463
}
4564
let found = false;
4665
require('readline').createInterface({
@@ -58,6 +77,12 @@ function loadFingerprint( serverId, knownHostsPath, cb ) {
5877
}
5978

6079
function storeFingerprint(serverId, knownHostsPath, fingerprint) {
80+
// If file doesn't exist, create full path to it
81+
try {
82+
fs.accessSync(knownHostsPath);
83+
} catch (_) {
84+
mkFullPath(path.dirname(knownHostsPath));
85+
}
6186
fs.appendFile(knownHostsPath, serverId + " " + fingerprint + EOL, "utf8", (err) => {
6287
if (err) {
6388
console.log(err);

test/internal/tls.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
var NodeChannel = require('../../lib/v1/internal/ch-node.js');
2020
var neo4j = require("../../lib/v1");
2121
var fs = require("fs");
22+
var path = require('path');
2223
var hasFeature = require("../../lib/v1/internal/features");
2324
var isLocalHost = require("../../lib/v1/internal/util").isLocalHost;
2425

@@ -77,6 +78,44 @@ describe('trust-on-first-use', function() {
7778

7879
var driver;
7980

81+
it("should create known_hosts file including full path if it doesn't exist", function(done) {
82+
// Assuming we only run this test on NodeJS with TOFU support
83+
if( !hasFeature("trust_on_first_use") ) {
84+
done();
85+
return;
86+
}
87+
88+
// Given
89+
// Non existing directory
90+
var knownHostsDir = path.join("build", "hosts");
91+
var knownHostsPath = path.join(knownHostsDir, "known_hosts");
92+
try {
93+
fs.unlinkSync(knownHostsPath);
94+
} catch (_) { }
95+
try {
96+
fs.rmdirSync(knownHostsDir);
97+
} catch (_) { }
98+
99+
var driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"), {
100+
encrypted: true,
101+
trust: "TRUST_ON_FIRST_USE",
102+
knownHosts: knownHostsPath
103+
});
104+
105+
// When
106+
driver.session().run( "RETURN 1").then( function() {
107+
// Then we get to here.
108+
// And then the known_hosts file should have been created
109+
expect( function() { fs.accessSync(knownHostsPath) }).not.toThrow()
110+
done();
111+
}).catch( function(){
112+
// Just here to gracefully exit test on failure so we don't get timeouts
113+
// when done() isn't called.
114+
expect( 'this' ).toBe( 'to never happen' );
115+
done();
116+
});
117+
});
118+
80119
it('should not throw an error if the host file contains two host duplicates', function(done) {
81120
'use strict';
82121
// Assuming we only run this test on NodeJS with TOFU support

0 commit comments

Comments
 (0)