Get proper path to local keyfile when selected

This commit is contained in:
Nick O'Leary 2018-01-11 11:19:04 +00:00
parent 6516e0dfd2
commit 3306d30094
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
5 changed files with 25 additions and 7 deletions

View File

@ -941,7 +941,7 @@ RED.projects = (function() {
remotes: {
'origin': {
url: repoUrl,
key_file: selected,
keyFile: selected,
passphrase: projectRepoPassphrase.val()
}
}

View File

@ -338,7 +338,6 @@ module.exports = {
res.status(204).end();
})
.catch(function(err) {
console.log(err.stack);
if (err.code) {
res.status(400).json({error:err.code, message: err.message});
} else {

View File

@ -23,7 +23,7 @@ var os = require('os');
var gitTools = require("./git");
var util = require("../util");
var defaultFileSet = require("./defaultFileSet");
var sshKeys = require("../sshkeys");
var settings;
var runtime;
var log;
@ -645,7 +645,7 @@ Project.prototype.updateRemote = function(user,remote,options) {
if (options.auth) {
var url = this.remotes[remote].fetch;
if (options.auth.keyFile) {
options.auth.key_path = fspath.join(projectsDir, ".sshkeys", ((username === '_')?'__default':username) + '_' + options.auth.keyFile);
options.auth.key_path = sshKeys.getPrivateKeyPath(username, options.auth.keyFile);
}
authCache.set(this.name,url,username,options.auth);
}
@ -870,10 +870,9 @@ function createProject(user, metadata) {
);
auth = authCache.get(project,originRemote.url,username);
}
else if (originRemote.hasOwnProperty("key_file") && originRemote.hasOwnProperty("passphrase")) {
var key_file_name = (username === '_') ? '__default' + '_' + originRemote.key_file : username + '_' + originRemote.key_file;
else if (originRemote.hasOwnProperty("keyFile") && originRemote.hasOwnProperty("passphrase")) {
authCache.set(project,originRemote.url,username,{ // TODO: hardcoded remote name
key_path: fspath.join(projectsDir, ".sshkeys", key_file_name),
key_path: sshKeys.getPrivateKeyPath(username, originRemote.keyFile),
passphrase: originRemote.passphrase
}
);

View File

@ -444,6 +444,8 @@ module.exports = {
err.code = 'git_push_failed';
}
throw err;
} else {
throw err;
}
});
},

View File

@ -186,10 +186,28 @@ function generateSSHKeyPair(name, privateKeyPath, comment, password, size) {
});
}
function getPrivateKeyPath(username, name) {
var sshKeyFileBasename = username + '_' + name;
var privateKeyFilePath = fspath.join(sshkeyDir, sshKeyFileBasename);
try {
fs.accessSync(privateKeyFilePath, (fs.constants || fs).R_OK);
return privateKeyFilePath;
} catch(err) {
privateKeyFilePath = fspath.join(userSSHKeyDir,name);
try {
fs.accessSync(privateKeyFilePath, (fs.constants || fs).R_OK);
return privateKeyFilePath;
} catch(err2) {
return null;
}
}
}
module.exports = {
init: init,
listSSHKeys: listSSHKeys,
getSSHKey: getSSHKey,
getPrivateKeyPath: getPrivateKeyPath,
generateSSHKey: generateSSHKey,
deleteSSHKey: deleteSSHKey
};