Skip to content

Create full path to file in knownHostsPath #116

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 1 commit into from
Jul 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions src/v1/internal/ch-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,35 @@ function userHome() {
return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
}

function mkFullPath(pathToCreate) {
let joinedDirs = [];
let pathToKnownHostsParts = pathToCreate.split(path.sep)
pathToKnownHostsParts = pathToKnownHostsParts.map((part) => {
if( !part.length ) return path.sep
return part;
});
pathToKnownHostsParts.forEach((dir) => {
joinedDirs.push(dir);
let newPath = path.join.apply(null, joinedDirs)
try {
fs.accessSync( newPath );
return;
}
catch (_) {
try {
fs.mkdirSync( newPath );
} catch(e) {
if ( e.code != 'EEXIST' ) throw e;
}
}
});
}

function loadFingerprint( serverId, knownHostsPath, cb ) {
if( !fs.existsSync( knownHostsPath )) {
cb(null);
return;
try {
fs.accessSync( knownHostsPath );
} catch(e) {
return cb(null)
}
let found = false;
require('readline').createInterface({
Expand All @@ -57,6 +82,13 @@ function loadFingerprint( serverId, knownHostsPath, cb ) {
}

function storeFingerprint(serverId, knownHostsPath, fingerprint) {
// If file doesn't exist, create full path to it
try {
fs.accessSync(knownHostsPath);
} catch (_) {
let pathWithoutFile = [].concat(knownHostsPath.split(path.sep)).slice(0, -1).join(path.sep);
mkFullPath(pathWithoutFile);
}
fs.appendFile(knownHostsPath, serverId + " " + fingerprint + EOL, "utf8", (err) => {
if (err) {
console.log(err);
Expand Down
33 changes: 33 additions & 0 deletions test/internal/tls.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,39 @@ describe('trust-on-first-use', function() {

var driver;

it("should create known_hosts file including full path if it doesn't exist", function(done) {
// Assuming we only run this test on NodeJS with TOFU support
if( !hasFeature("trust_on_first_use") ) {
done();
return;
}

// Given
// Non existing directory
var knownHostsDir = "build/hosts"
var knownHostsPath = knownHostsDir + "/known_hosts";
try {
fs.unlinkSync(knownHostsPath);
} catch (_) { }
try {
fs.rmdirSync(knownHostsDir);
} catch (_) { }

var driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"), {
encrypted: true,
trust: "TRUST_ON_FIRST_USE",
knownHosts: knownHostsPath
});

// When
driver.session().run( "RETURN 1").then( function() {
// Then we get to here.
// And then the known_hosts file should have been created
expect( function() { fs.accessSync(knownHostsPath) }).not.toThrow()
done();
});
});

it('should not throw an error if the host file contains two host duplicates', function(done) {
'use strict';
// Assuming we only run this test on NodeJS with TOFU support
Expand Down