1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add better handling of host-key-verify error with projects

This commit is contained in:
Nick O'Leary 2020-03-13 13:09:47 +00:00
parent 09d55a0cbd
commit 20f97d0d13
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
3 changed files with 101 additions and 79 deletions

View File

@ -977,7 +977,8 @@
"passphrase": "Passphrase", "passphrase": "Passphrase",
"retry": "Retry", "retry": "Retry",
"update-failed": "Failed to update auth", "update-failed": "Failed to update auth",
"unhandled": "Unhandled error response" "unhandled": "Unhandled error response",
"host-key-verify-failed": "<p>Host key verification failed.</p><p>The repository host key could not be verified. Please update your <code>known_hosts</code> file and try again."
}, },
"create-branch-list": { "create-branch-list": {
"invalid": "Invalid branch", "invalid": "Invalid branch",

View File

@ -1939,100 +1939,121 @@ RED.projects = (function() {
} }
}).fail(function(xhr,textStatus,err) { }).fail(function(xhr,textStatus,err) {
var responses; var responses;
if (options.responses && options.responses[xhr.status]) { if (options.responses && options.responses[xhr.status]) {
responses = options.responses[xhr.status]; responses = options.responses[xhr.status];
if (typeof responses === 'function') { if (typeof responses === 'function') {
resultCallback = responses; resultCallback = responses;
resultCallbackArgs = {error:responses.statusText}; resultCallbackArgs = {error:responses.statusText};
return; return;
} else if (options.handleAuthFail !== false && xhr.responseJSON.code === 'git_auth_failed') { } else if (options.handleAuthFail !== false && (xhr.responseJSON.code === 'git_auth_failed' || xhr.responseJSON.code === 'git_host_key_verification_failed')) {
var url = activeProject.git.remotes[xhr.responseJSON.remote||options.remote||'origin'].fetch; if (xhr.responseJSON.code === 'git_auth_failed') {
var url = activeProject.git.remotes[xhr.responseJSON.remote||options.remote||'origin'].fetch;
var message = $('<div>'+ var message = $('<div>'+
'<div class="form-row">'+RED._("projects.send-req.auth-req")+':</div>'+ '<div class="form-row">'+RED._("projects.send-req.auth-req")+':</div>'+
'<div class="form-row"><div style="margin-left: 20px;">'+url+'</div></div>'+ '<div class="form-row"><div style="margin-left: 20px;">'+url+'</div></div>'+
'</div>'); '</div>');
var isSSH = false; var isSSH = false;
if (/^https?:\/\//.test(url)) { if (/^https?:\/\//.test(url)) {
$('<div class="form-row"><label for="projects-user-auth-username">'+RED._("projects.send-req.username")+'</label><input id="projects-user-auth-username" type="text"></input></div>'+ $('<div class="form-row"><label for="projects-user-auth-username">'+RED._("projects.send-req.username")+'</label><input id="projects-user-auth-username" type="text"></input></div>'+
'<div class="form-row"><label for=projects-user-auth-password">'+RED._("projects.send-req.password")+'</label><input id="projects-user-auth-password" type="password"></input></div>').appendTo(message); '<div class="form-row"><label for=projects-user-auth-password">'+RED._("projects.send-req.password")+'</label><input id="projects-user-auth-password" type="password"></input></div>').appendTo(message);
} else if (/^(?:ssh|[\d\w\.\-_]+@[\w\.]+):(?:\/\/)?/.test(url)) { } else if (/^(?:ssh|[\d\w\.\-_]+@[\w\.]+):(?:\/\/)?/.test(url)) {
isSSH = true; isSSH = true;
var row = $('<div class="form-row"></div>').appendTo(message); var row = $('<div class="form-row"></div>').appendTo(message);
$('<label for="projects-user-auth-key">SSH Key</label>').appendTo(row); $('<label for="projects-user-auth-key">SSH Key</label>').appendTo(row);
var projectRepoSSHKeySelect = $('<select id="projects-user-auth-key">').width('70%').appendTo(row); var projectRepoSSHKeySelect = $('<select id="projects-user-auth-key">').width('70%').appendTo(row);
$.getJSON("settings/user/keys", function(data) { $.getJSON("settings/user/keys", function(data) {
var count = 0; var count = 0;
data.keys.forEach(function(key) { data.keys.forEach(function(key) {
projectRepoSSHKeySelect.append($("<option></option>").val(key.name).text(key.name)); projectRepoSSHKeySelect.append($("<option></option>").val(key.name).text(key.name));
count++; count++;
});
if (count === 0) {
//TODO: handle no keys yet setup
}
}); });
if (count === 0) { row = $('<div class="form-row"></div>').appendTo(message);
//TODO: handle no keys yet setup $('<label for="projects-user-auth-passphrase">'+RED._("projects.send-req.passphrase")+'</label>').appendTo(row);
} $('<input id="projects-user-auth-passphrase" type="password"></input>').appendTo(row);
}); }
row = $('<div class="form-row"></div>').appendTo(message);
$('<label for="projects-user-auth-passphrase">'+RED._("projects.send-req.passphrase")+'</label>').appendTo(row);
$('<input id="projects-user-auth-passphrase" type="password"></input>').appendTo(row);
}
var notification = RED.notify(message,{ var notification = RED.notify(message,{
type:"error", type:"error",
fixed: true, fixed: true,
modal: true, modal: true,
buttons: [ buttons: [
{ {
//id: "node-dialog-delete", //id: "node-dialog-delete",
//class: 'leftButton', //class: 'leftButton',
text: RED._("common.label.cancel"), text: RED._("common.label.cancel"),
click: function() { click: function() {
notification.close(); notification.close();
}
},{
text: '<span><i class="fa fa-refresh"></i> ' +RED._("projects.send-req.retry") +'</span>',
click: function() {
body = body || {};
var authBody = {};
if (isSSH) {
authBody.keyFile = $('#projects-user-auth-key').val();
authBody.passphrase = $('#projects-user-auth-passphrase').val();
} else {
authBody.username = $('#projects-user-auth-username').val();
authBody.password = $('#projects-user-auth-password').val();
} }
var done = function(err) { },{
if (err) { text: '<span><i class="fa fa-refresh"></i> ' +RED._("projects.send-req.retry") +'</span>',
console.log(RED._("projects.send-req.update-failed")); click: function() {
console.log(err); body = body || {};
var authBody = {};
if (isSSH) {
authBody.keyFile = $('#projects-user-auth-key').val();
authBody.passphrase = $('#projects-user-auth-passphrase').val();
} else { } else {
sendRequest(options,body); authBody.username = $('#projects-user-auth-username').val();
notification.close(); authBody.password = $('#projects-user-auth-password').val();
} }
var done = function(err) {
if (err) {
console.log(RED._("projects.send-req.update-failed"));
console.log(err);
} else {
sendRequest(options,body);
notification.close();
}
}
sendRequest({
url: "projects/"+activeProject.name+"/remotes/"+(xhr.responseJSON.remote||options.remote||'origin'),
type: "PUT",
responses: {
0: function(error) {
done(error,null);
},
200: function(data) {
done(null,data);
},
400: {
'unexpected_error': function(error) {
done(error,null);
}
},
} }
},{auth:authBody}); sendRequest({
url: "projects/"+activeProject.name+"/remotes/"+(xhr.responseJSON.remote||options.remote||'origin'),
type: "PUT",
responses: {
0: function(error) {
done(error,null);
},
200: function(data) {
done(null,data);
},
400: {
'unexpected_error': function(error) {
done(error,null);
}
},
}
},{auth:authBody});
}
} }
} ]
] });
}); return;
return; } else if (xhr.responseJSON.code === 'git_host_key_verification_failed') {
var message = $('<div>'+
'<div class="form-row">'+RED._("projects.send-req.host-key-verify-failed")+'</div>'+
'</div>');
var notification = RED.notify(message,{
type:"error",
fixed: true,
modal: true,
buttons: [
{
text: RED._("common.label.close"),
click: function() {
notification.close();
}
}
]
});
return;
}
} else if (responses[xhr.responseJSON.code]) { } else if (responses[xhr.responseJSON.code]) {
resultCallback = responses[xhr.responseJSON.code]; resultCallback = responses[xhr.responseJSON.code];
resultCallbackArgs = xhr.responseJSON; resultCallbackArgs = xhr.responseJSON;

View File

@ -41,6 +41,9 @@ function runGitCommand(args,cwd,env,emit) {
err.code = "git_connection_failed"; err.code = "git_connection_failed";
} else if (/Connection timed out/i.test(stderr)) { } else if (/Connection timed out/i.test(stderr)) {
err.code = "git_connection_failed"; err.code = "git_connection_failed";
} else if(/Host key verification failed/i.test(stderr)) {
// TODO: handle host key verification errors separately
err.code = "git_host_key_verification_failed";
} else if (/fatal: could not read/i.test(stderr)) { } else if (/fatal: could not read/i.test(stderr)) {
// Username/Password // Username/Password
err.code = "git_auth_failed"; err.code = "git_auth_failed";
@ -48,9 +51,6 @@ function runGitCommand(args,cwd,env,emit) {
err.code = "git_auth_failed"; err.code = "git_auth_failed";
} else if(/Permission denied \(publickey\)/i.test(stderr)) { } else if(/Permission denied \(publickey\)/i.test(stderr)) {
err.code = "git_auth_failed"; err.code = "git_auth_failed";
} else if(/Host key verification failed/i.test(stderr)) {
// TODO: handle host key verification errors separately
err.code = "git_auth_failed";
} else if (/commit your changes or stash/i.test(stderr)) { } else if (/commit your changes or stash/i.test(stderr)) {
err.code = "git_local_overwrite"; err.code = "git_local_overwrite";
} else if (/CONFLICT/.test(err.stdout)) { } else if (/CONFLICT/.test(err.stdout)) {