Fix reauthentication of remote repositories

This commit is contained in:
Nick O'Leary
2017-12-21 17:40:24 +00:00
parent 3c6ba72a2a
commit 9c350311e8
9 changed files with 184 additions and 286 deletions

View File

@@ -120,12 +120,14 @@ Project.prototype.loadRemotes = function() {
}).then(function() {
var allRemotes = Object.keys(project.remotes);
var match = "";
allRemotes.forEach(function(remote) {
if (project.branches.remote.indexOf(remote) === 0 && match.length < remote.length) {
match = remote;
}
});
project.currentRemote = project.parseRemoteBranch(project.branches.remote).remote;
if (project.branches.remote) {
allRemotes.forEach(function(remote) {
if (project.branches.remote.indexOf(remote) === 0 && match.length < remote.length) {
match = remote;
}
});
project.currentRemote = project.parseRemoteBranch(project.branches.remote).remote;
}
});
}
@@ -541,8 +543,21 @@ Project.prototype.addRemote = function(user,remote,options) {
});
}
Project.prototype.updateRemote = function(user,remote,options) {
// TODO: once the sshkey support is added, move the updating of remotes,
// including their auth details, down here.
var username;
if (!user) {
username = "_";
} else {
username = user.username;
}
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);
}
authCache.set(this.name,url,username,options.auth);
}
return Promise.resolve();
}
Project.prototype.removeRemote = function(user, remote) {
// TODO: if this was the last remote using this url, then remove the authCache
@@ -764,7 +779,7 @@ 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;
var key_file_name = (username === '_') ? '__default' + '_' + originRemote.key_file : username + '_' + originRemote.key_file;
authCache.set(project,originRemote.url,username,{ // TODO: hardcoded remote name
key_path: fspath.join(projectsDir, ".sshkeys", key_file_name),
passphrase: originRemote.passphrase

View File

@@ -266,6 +266,10 @@ function removeRemote(user, project, remote) {
checkActiveProject(project);
return activeProject.removeRemote(user, remote);
}
function updateRemote(user, project, remote, body) {
checkActiveProject(project);
return activeProject.updateRemote(user, remote, body);
}
function getActiveProject(user) {
return activeProject;
@@ -491,6 +495,7 @@ module.exports = {
getRemotes: getRemotes,
addRemote: addRemote,
removeRemote: removeRemote,
updateRemote: updateRemote,
getFlows: getFlows,
saveFlows: saveFlows,

View File

@@ -80,6 +80,8 @@ function getSSHKey(username, name) {
return checkSSHKeyFileAndGetPublicKeyFileName(username, name)
.then(function(publicSSHKeyPath) {
return fs.readFile(publicSSHKeyPath, 'utf-8');
}).catch(function() {
return null;
});
}
@@ -89,29 +91,32 @@ function generateSSHKey(username, options) {
return checkExistSSHKeyFiles(username, name)
.then(function(result) {
if ( result ) {
throw new Error('Some SSH Keyfile exists');
}
else {
var e = new Error("SSH Key name exists");
e.code = "key_exists";
throw e;
} else {
var comment = options.comment || "";
var password = options.password || "";
if (password.length > 0 && password.length < 5) {
var e2 = new Error("SSH Key passphrase too short");
e2.code = "key_passphrase_too_short";
throw e2;
}
var size = options.size || 2048;
var sshKeyFileBasename = username + '_' + name;
var privateKeyFilePath = fspath.join(sshkeyDir, sshKeyFileBasename);
return generateSSHKeyPair(privateKeyFilePath, comment, password, size)
.then(function() {
return name;
});
return generateSSHKeyPair(name, privateKeyFilePath, comment, password, size)
}
})
.then(function(keyfile_name) {
return checkSSHKeyFileAndGetPublicKeyFileName(username, name)
.then(function() {
return keyfile_name;
})
.catch(function() {
throw new Error('Failed to generate ssh key files');
});
});
// .then(function(keyfile_name) {
// return checkSSHKeyFileAndGetPublicKeyFileName(username, name)
// .then(function() {
// return keyfile_name;
// })
// .catch(function(err) {
// throw new Error('Failed to generate ssh key files');
// });
// });
}
function deleteSSHKey(username, name) {
@@ -125,7 +130,7 @@ function checkExistSSHKeyFiles(username, name) {
var sshKeyFileBasename = username + '_' + name;
var privateKeyFilePath = fspath.join(sshkeyDir, sshKeyFileBasename);
var publicKeyFilePath = fspath.join(sshkeyDir, sshKeyFileBasename + '.pub');
return Promise.race([
return Promise.all([
fs.access(privateKeyFilePath, (fs.constants || fs).R_OK),
fs.access(publicKeyFilePath , (fs.constants || fs).R_OK)
])
@@ -157,13 +162,11 @@ function deleteSSHKeyFiles(username, name) {
return Promise.all([
fs.remove(privateKeyFilePath),
fs.remove(publicKeyFilePath)
])
.then(function(retArray) {
return true;
});
]);
}
function generateSSHKeyPair(privateKeyPath, comment, password, size) {
function generateSSHKeyPair(name, privateKeyPath, comment, password, size) {
log.trace("ssh-keygen["+[name,privateKeyPath,comment,size,"hasPassword?"+!!password].join(",")+"]");
return new Promise(function(resolve, reject) {
keygen({
location: privateKeyPath,
@@ -172,10 +175,11 @@ function generateSSHKeyPair(privateKeyPath, comment, password, size) {
size: size
}, function(err, out) {
if ( err ) {
err.code = "key_generation_failed";
reject(err);
}
else {
resolve();
resolve(name);
}
});
});