2017-12-07 15:11:24 +01:00
|
|
|
/**
|
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
var fs = require('fs-extra');
|
|
|
|
var when = require('when');
|
|
|
|
var fspath = require("path");
|
2018-01-19 00:13:55 +01:00
|
|
|
var keygen = require("./keygen");
|
2017-12-07 15:11:24 +01:00
|
|
|
|
|
|
|
var settings;
|
|
|
|
var runtime;
|
|
|
|
var log;
|
|
|
|
var sshkeyDir;
|
2018-01-10 18:37:41 +01:00
|
|
|
var userSSHKeyDir;
|
2017-12-07 15:11:24 +01:00
|
|
|
|
|
|
|
function init(_settings, _runtime) {
|
|
|
|
settings = _settings;
|
|
|
|
runtime = _runtime;
|
|
|
|
log = runtime.log;
|
2018-02-27 00:45:21 +01:00
|
|
|
sshkeyDir = fspath.resolve(fspath.join(settings.userDir, "projects", ".sshkeys"));
|
2018-01-10 18:37:41 +01:00
|
|
|
userSSHKeyDir = fspath.join(process.env.HOME || process.env.USERPROFILE || process.env.HOMEPATH, ".ssh");
|
2017-12-07 15:11:24 +01:00
|
|
|
// console.log('sshkeys.init()');
|
|
|
|
return fs.ensureDir(sshkeyDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
function listSSHKeys(username) {
|
2018-01-10 18:37:41 +01:00
|
|
|
return listSSHKeysInDir(sshkeyDir,username + '_').then(function(customKeys) {
|
|
|
|
return listSSHKeysInDir(userSSHKeyDir).then(function(existingKeys) {
|
|
|
|
existingKeys.forEach(function(k){
|
|
|
|
k.system = true;
|
|
|
|
customKeys.push(k);
|
|
|
|
})
|
|
|
|
return customKeys;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function listSSHKeysInDir(dir,startStr) {
|
|
|
|
startStr = startStr || "";
|
|
|
|
return fs.readdir(dir).then(function(fns) {
|
2017-12-07 15:11:24 +01:00
|
|
|
var ret = fns.sort()
|
|
|
|
.filter(function(fn) {
|
2018-01-10 18:37:41 +01:00
|
|
|
var fullPath = fspath.join(dir,fn);
|
2017-12-20 11:44:57 +01:00
|
|
|
if (fn.length > 2 || fn[0] != ".") {
|
2017-12-07 15:11:24 +01:00
|
|
|
var stats = fs.lstatSync(fullPath);
|
|
|
|
if (stats.isFile()) {
|
|
|
|
return fn.startsWith(startStr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
})
|
|
|
|
.map(function(filename) {
|
|
|
|
return filename.substr(startStr.length);
|
|
|
|
})
|
|
|
|
.reduce(function(prev, current) {
|
|
|
|
var parsePath = fspath.parse(current);
|
|
|
|
if ( parsePath ) {
|
|
|
|
if ( parsePath.ext !== '.pub' ) {
|
|
|
|
// Private Keys
|
|
|
|
prev.keyFiles.push(parsePath.base);
|
|
|
|
}
|
|
|
|
else if ( parsePath.ext === '.pub' && (prev.keyFiles.some(function(elem){ return elem === parsePath.name; }))) {
|
|
|
|
prev.privateKeyFiles.push(parsePath.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return prev;
|
|
|
|
}, { keyFiles: [], privateKeyFiles: [] });
|
|
|
|
return ret.privateKeyFiles.map(function(filename) {
|
|
|
|
return {
|
|
|
|
name: filename
|
|
|
|
};
|
|
|
|
});
|
2018-01-10 18:37:41 +01:00
|
|
|
}).then(function(result) {
|
|
|
|
return result;
|
|
|
|
}).catch(function() {
|
|
|
|
return []
|
2017-12-21 00:45:17 +01:00
|
|
|
});
|
2017-12-07 15:11:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function getSSHKey(username, name) {
|
|
|
|
return checkSSHKeyFileAndGetPublicKeyFileName(username, name)
|
|
|
|
.then(function(publicSSHKeyPath) {
|
|
|
|
return fs.readFile(publicSSHKeyPath, 'utf-8');
|
2017-12-21 18:40:24 +01:00
|
|
|
}).catch(function() {
|
2018-01-10 18:37:41 +01:00
|
|
|
var privateKeyPath = fspath.join(userSSHKeyDir,name);
|
|
|
|
var publicKeyPath = privateKeyPath+".pub";
|
|
|
|
return checkFilePairExist(privateKeyPath,publicKeyPath).then(function() {
|
|
|
|
return fs.readFile(publicKeyPath, 'utf-8');
|
|
|
|
}).catch(function() {
|
|
|
|
return null
|
|
|
|
});
|
2017-12-07 15:11:24 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-19 14:57:40 +01:00
|
|
|
function generateSSHKey(username, options) {
|
|
|
|
options = options || {};
|
|
|
|
var name = options.name || "";
|
2018-04-15 12:18:10 +02:00
|
|
|
if (!/^[a-zA-Z0-9\-_]+$/.test(options.name)) {
|
|
|
|
var err = new Error("Invalid SSH Key name");
|
|
|
|
e.code = "invalid_key_name";
|
|
|
|
return Promise.reject(err);
|
|
|
|
}
|
2017-12-15 15:48:52 +01:00
|
|
|
return checkExistSSHKeyFiles(username, name)
|
|
|
|
.then(function(result) {
|
|
|
|
if ( result ) {
|
2017-12-21 18:40:24 +01:00
|
|
|
var e = new Error("SSH Key name exists");
|
|
|
|
e.code = "key_exists";
|
|
|
|
throw e;
|
|
|
|
} else {
|
2017-12-21 00:45:17 +01:00
|
|
|
var comment = options.comment || "";
|
2017-12-19 14:57:40 +01:00
|
|
|
var password = options.password || "";
|
|
|
|
var size = options.size || 2048;
|
2017-12-15 15:48:52 +01:00
|
|
|
var sshKeyFileBasename = username + '_' + name;
|
2018-02-27 12:11:02 +01:00
|
|
|
var privateKeyFilePath = fspath.normalize(fspath.join(sshkeyDir, sshKeyFileBasename));
|
2017-12-21 18:40:24 +01:00
|
|
|
return generateSSHKeyPair(name, privateKeyFilePath, comment, password, size)
|
2017-12-15 15:48:52 +01:00
|
|
|
}
|
|
|
|
})
|
2017-12-07 15:11:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function deleteSSHKey(username, name) {
|
|
|
|
return checkSSHKeyFileAndGetPublicKeyFileName(username, name)
|
|
|
|
.then(function() {
|
|
|
|
return deleteSSHKeyFiles(username, name);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-15 15:48:52 +01:00
|
|
|
function checkExistSSHKeyFiles(username, name) {
|
|
|
|
var sshKeyFileBasename = username + '_' + name;
|
|
|
|
var privateKeyFilePath = fspath.join(sshkeyDir, sshKeyFileBasename);
|
|
|
|
var publicKeyFilePath = fspath.join(sshkeyDir, sshKeyFileBasename + '.pub');
|
2018-01-10 18:37:41 +01:00
|
|
|
return checkFilePairExist(privateKeyFilePath,publicKeyFilePath)
|
|
|
|
.then(function() {
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
.catch(function() {
|
|
|
|
return false;
|
|
|
|
});
|
2017-12-15 15:48:52 +01:00
|
|
|
}
|
|
|
|
|
2017-12-07 15:11:24 +01:00
|
|
|
function checkSSHKeyFileAndGetPublicKeyFileName(username, name) {
|
|
|
|
var sshKeyFileBasename = username + '_' + name;
|
|
|
|
var privateKeyFilePath = fspath.join(sshkeyDir, sshKeyFileBasename);
|
|
|
|
var publicKeyFilePath = fspath.join(sshkeyDir, sshKeyFileBasename + '.pub');
|
2018-01-10 18:37:41 +01:00
|
|
|
return checkFilePairExist(privateKeyFilePath,publicKeyFilePath).then(function() {
|
|
|
|
return publicKeyFilePath;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkFilePairExist(privateKeyFilePath,publicKeyFilePath) {
|
2017-12-15 15:48:52 +01:00
|
|
|
return Promise.all([
|
2017-12-07 15:11:24 +01:00
|
|
|
fs.access(privateKeyFilePath, (fs.constants || fs).R_OK),
|
|
|
|
fs.access(publicKeyFilePath , (fs.constants || fs).R_OK)
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteSSHKeyFiles(username, name) {
|
|
|
|
var sshKeyFileBasename = username + '_' + name;
|
|
|
|
var privateKeyFilePath = fspath.join(sshkeyDir, sshKeyFileBasename);
|
|
|
|
var publicKeyFilePath = fspath.join(sshkeyDir, sshKeyFileBasename + '.pub');
|
2017-12-15 15:48:52 +01:00
|
|
|
return Promise.all([
|
2017-12-07 15:11:24 +01:00
|
|
|
fs.remove(privateKeyFilePath),
|
|
|
|
fs.remove(publicKeyFilePath)
|
2018-01-05 17:12:01 +01:00
|
|
|
])
|
2017-12-07 15:11:24 +01:00
|
|
|
}
|
|
|
|
|
2017-12-21 18:40:24 +01:00
|
|
|
function generateSSHKeyPair(name, privateKeyPath, comment, password, size) {
|
|
|
|
log.trace("ssh-keygen["+[name,privateKeyPath,comment,size,"hasPassword?"+!!password].join(",")+"]");
|
2018-01-19 00:13:55 +01:00
|
|
|
return keygen.generateKey({location: privateKeyPath, comment: comment, password: password, size: size})
|
2018-01-05 17:12:01 +01:00
|
|
|
.then(function(stdout) {
|
|
|
|
return name;
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
log.log('[SSHKey generation] error:', err);
|
|
|
|
throw err;
|
|
|
|
});
|
2017-12-07 15:11:24 +01:00
|
|
|
}
|
|
|
|
|
2018-01-11 12:19:04 +01:00
|
|
|
function getPrivateKeyPath(username, name) {
|
|
|
|
var sshKeyFileBasename = username + '_' + name;
|
2018-02-27 11:58:54 +01:00
|
|
|
var privateKeyFilePath = fspath.normalize(fspath.join(sshkeyDir, sshKeyFileBasename));
|
2018-01-11 12:19:04 +01:00
|
|
|
try {
|
|
|
|
fs.accessSync(privateKeyFilePath, (fs.constants || fs).R_OK);
|
|
|
|
} catch(err) {
|
|
|
|
privateKeyFilePath = fspath.join(userSSHKeyDir,name);
|
|
|
|
try {
|
|
|
|
fs.accessSync(privateKeyFilePath, (fs.constants || fs).R_OK);
|
|
|
|
} catch(err2) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2018-02-27 14:05:10 +01:00
|
|
|
if (fspath.sep === '\\') {
|
|
|
|
privateKeyFilePath = privateKeyFilePath.replace(/\\/g,'\\\\');
|
|
|
|
}
|
|
|
|
return privateKeyFilePath;
|
2018-01-11 12:19:04 +01:00
|
|
|
}
|
|
|
|
|
2017-12-07 15:11:24 +01:00
|
|
|
module.exports = {
|
|
|
|
init: init,
|
|
|
|
listSSHKeys: listSSHKeys,
|
|
|
|
getSSHKey: getSSHKey,
|
2018-01-11 12:19:04 +01:00
|
|
|
getPrivateKeyPath: getPrivateKeyPath,
|
2017-12-07 15:11:24 +01:00
|
|
|
generateSSHKey: generateSSHKey,
|
|
|
|
deleteSSHKey: deleteSSHKey
|
|
|
|
};
|