mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Detect a SSH key generation error
This commit is contained in:
parent
d1106f53e0
commit
3a311c9584
@ -84,12 +84,29 @@ function getSSHKey(username, name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function generateSSHKey(username, email, name, data) {
|
function generateSSHKey(username, email, name, data) {
|
||||||
|
return checkExistSSHKeyFiles(username, name)
|
||||||
|
.then(function(result) {
|
||||||
|
if ( result ) {
|
||||||
|
throw new Error('Some SSH Keyfile exists');
|
||||||
|
}
|
||||||
|
else {
|
||||||
var sshKeyFileBasename = username + '_' + name;
|
var sshKeyFileBasename = username + '_' + name;
|
||||||
var privateKeyFilePath = fspath.join(sshkeyDir, sshKeyFileBasename);
|
var privateKeyFilePath = fspath.join(sshkeyDir, sshKeyFileBasename);
|
||||||
return generateSSHKeyPair(privateKeyFilePath, email, data.password, data.size)
|
return generateSSHKeyPair(privateKeyFilePath, email, data.password, data.size)
|
||||||
.then(function() {
|
.then(function() {
|
||||||
return name;
|
return name;
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(function(keyfile_name) {
|
||||||
|
return checkSSHKeyFileAndGetPublicKeyFileName(username, name)
|
||||||
|
.then(function() {
|
||||||
|
return keyfile_name;
|
||||||
|
})
|
||||||
|
.catch(function() {
|
||||||
|
throw new Error('Failed to generate ssh key files');
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function deleteSSHKey(username, name) {
|
function deleteSSHKey(username, name) {
|
||||||
@ -99,16 +116,32 @@ function deleteSSHKey(username, name) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkSSHKeyFileAndGetPublicKeyFileName(username, name) {
|
function checkExistSSHKeyFiles(username, name) {
|
||||||
var sshKeyFileBasename = username + '_' + name;
|
var sshKeyFileBasename = username + '_' + name;
|
||||||
var privateKeyFilePath = fspath.join(sshkeyDir, sshKeyFileBasename);
|
var privateKeyFilePath = fspath.join(sshkeyDir, sshKeyFileBasename);
|
||||||
var publicKeyFilePath = fspath.join(sshkeyDir, sshKeyFileBasename + '.pub');
|
var publicKeyFilePath = fspath.join(sshkeyDir, sshKeyFileBasename + '.pub');
|
||||||
return when.all([
|
return Promise.race([
|
||||||
fs.access(privateKeyFilePath, (fs.constants || fs).R_OK),
|
fs.access(privateKeyFilePath, (fs.constants || fs).R_OK),
|
||||||
fs.access(publicKeyFilePath , (fs.constants || fs).R_OK)
|
fs.access(publicKeyFilePath , (fs.constants || fs).R_OK)
|
||||||
])
|
])
|
||||||
.then(function() {
|
.then(function() {
|
||||||
return when.resolve(publicKeyFilePath);
|
return true;
|
||||||
|
})
|
||||||
|
.catch(function() {
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkSSHKeyFileAndGetPublicKeyFileName(username, name) {
|
||||||
|
var sshKeyFileBasename = username + '_' + name;
|
||||||
|
var privateKeyFilePath = fspath.join(sshkeyDir, sshKeyFileBasename);
|
||||||
|
var publicKeyFilePath = fspath.join(sshkeyDir, sshKeyFileBasename + '.pub');
|
||||||
|
return Promise.all([
|
||||||
|
fs.access(privateKeyFilePath, (fs.constants || fs).R_OK),
|
||||||
|
fs.access(publicKeyFilePath , (fs.constants || fs).R_OK)
|
||||||
|
])
|
||||||
|
.then(function() {
|
||||||
|
return publicKeyFilePath;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,17 +149,17 @@ function deleteSSHKeyFiles(username, name) {
|
|||||||
var sshKeyFileBasename = username + '_' + name;
|
var sshKeyFileBasename = username + '_' + name;
|
||||||
var privateKeyFilePath = fspath.join(sshkeyDir, sshKeyFileBasename);
|
var privateKeyFilePath = fspath.join(sshkeyDir, sshKeyFileBasename);
|
||||||
var publicKeyFilePath = fspath.join(sshkeyDir, sshKeyFileBasename + '.pub');
|
var publicKeyFilePath = fspath.join(sshkeyDir, sshKeyFileBasename + '.pub');
|
||||||
return when.all([
|
return Promise.all([
|
||||||
fs.remove(privateKeyFilePath),
|
fs.remove(privateKeyFilePath),
|
||||||
fs.remove(publicKeyFilePath)
|
fs.remove(publicKeyFilePath)
|
||||||
])
|
])
|
||||||
.then(function(retArray) {
|
.then(function(retArray) {
|
||||||
return when.resolve(true);
|
return true;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateSSHKeyPair(privateKeyPath, comment, password, size) {
|
function generateSSHKeyPair(privateKeyPath, comment, password, size) {
|
||||||
return when.promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
keygen({
|
keygen({
|
||||||
location: privateKeyPath,
|
location: privateKeyPath,
|
||||||
comment: comment,
|
comment: comment,
|
||||||
|
@ -330,7 +330,7 @@ describe("api/editor/sshkeys", function() {
|
|||||||
if (err) {
|
if (err) {
|
||||||
return done(err);
|
return done(err);
|
||||||
}
|
}
|
||||||
res.body.should.be.true();
|
res.body.should.be.deepEqual({});
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user