From c24b0c6bb4849f493160153c75ba0998c4c89d99 Mon Sep 17 00:00:00 2001 From: Hideki Nakamura Date: Tue, 19 Dec 2017 22:57:40 +0900 Subject: [PATCH] Change "generateSSHKey" function signature --- red/api/editor/sshkeys.js | 2 +- .../storage/localfilesystem/sshkeys.js | 9 ++- .../storage/localfilesystem/sshkeys_spec.js | 71 ++++++++++--------- 3 files changed, 44 insertions(+), 38 deletions(-) diff --git a/red/api/editor/sshkeys.js b/red/api/editor/sshkeys.js index 3318d6a98..d1338e19f 100644 --- a/red/api/editor/sshkeys.js +++ b/red/api/editor/sshkeys.js @@ -82,7 +82,7 @@ module.exports = { var username = getUsername(req.user); // console.log('req.body:', req.body); if ( req.body && req.body.name ) { - runtime.storage.sshkeys.generateSSHKey(username, req.body.email || "", req.body.name, req.body) + runtime.storage.sshkeys.generateSSHKey(username, req.body) .then(function(name) { // console.log('generate key --- success name:', name); res.json({ diff --git a/red/runtime/storage/localfilesystem/sshkeys.js b/red/runtime/storage/localfilesystem/sshkeys.js index 96097aeba..fce84b469 100644 --- a/red/runtime/storage/localfilesystem/sshkeys.js +++ b/red/runtime/storage/localfilesystem/sshkeys.js @@ -83,16 +83,21 @@ function getSSHKey(username, name) { }); } -function generateSSHKey(username, email, name, data) { +function generateSSHKey(username, options) { + options = options || {}; + var name = options.name || ""; return checkExistSSHKeyFiles(username, name) .then(function(result) { if ( result ) { throw new Error('Some SSH Keyfile exists'); } else { + var email = options.email || ""; + var password = options.password || ""; + var size = options.size || 2048; var sshKeyFileBasename = username + '_' + name; var privateKeyFilePath = fspath.join(sshkeyDir, sshKeyFileBasename); - return generateSSHKeyPair(privateKeyFilePath, email, data.password, data.size) + return generateSSHKeyPair(privateKeyFilePath, email, password, size) .then(function() { return name; }); diff --git a/test/red/runtime/storage/localfilesystem/sshkeys_spec.js b/test/red/runtime/storage/localfilesystem/sshkeys_spec.js index a705e703b..370ba9908 100644 --- a/test/red/runtime/storage/localfilesystem/sshkeys_spec.js +++ b/test/red/runtime/storage/localfilesystem/sshkeys_spec.js @@ -245,15 +245,16 @@ describe("storage/localfilesystem/sshkeys", function() { it('should generate sshkey file with empty data', function(done) { var sshkeyDirPath = path.join(userDir, 'projects', '.sshkeys'); var username = 'test'; - var email = 'test@test.com'; - var filename = 'test-key01'; - var data = {}; + var options = { + email: 'test@test.com', + name: 'test-key01' + }; localfilesystem.init(mockSettings, mockRuntime).then(function() { sshkeys.init(mockSettings, mockRuntime).then(function() { - sshkeys.generateSSHKey(username, email, filename, data).then(function(retObj) { - retObj.should.be.equal(filename); - fs.existsSync(path.join(sshkeyDirPath,username+'_'+filename)).should.be.true(); - fs.existsSync(path.join(sshkeyDirPath,username+'_'+filename+'.pub')).should.be.true(); + sshkeys.generateSSHKey(username, options).then(function(retObj) { + retObj.should.be.equal(options.name); + fs.existsSync(path.join(sshkeyDirPath,username+'_'+options.name)).should.be.true(); + fs.existsSync(path.join(sshkeyDirPath,username+'_'+options.name+'.pub')).should.be.true(); done(); }).catch(function(err) { done(err); @@ -269,17 +270,17 @@ describe("storage/localfilesystem/sshkeys", function() { it('should generate sshkey file with password data', function(done) { var sshkeyDirPath = path.join(userDir, 'projects', '.sshkeys'); var username = 'test'; - var email = 'test@test.com'; - var filename = 'test-key01'; - var data = { + var options = { + email: 'test@test.com', + name: 'test-key01', password: 'testtest' }; localfilesystem.init(mockSettings, mockRuntime).then(function() { sshkeys.init(mockSettings, mockRuntime).then(function() { - sshkeys.generateSSHKey(username, email, filename, data).then(function(retObj) { - retObj.should.be.equal(filename); - fs.existsSync(path.join(sshkeyDirPath,username+'_'+filename)).should.be.true(); - fs.existsSync(path.join(sshkeyDirPath,username+'_'+filename+'.pub')).should.be.true(); + sshkeys.generateSSHKey(username, options).then(function(retObj) { + retObj.should.be.equal(options.name); + fs.existsSync(path.join(sshkeyDirPath,username+'_'+options.name)).should.be.true(); + fs.existsSync(path.join(sshkeyDirPath,username+'_'+options.name+'.pub')).should.be.true(); done(); }).catch(function(err) { done(err); @@ -295,17 +296,17 @@ describe("storage/localfilesystem/sshkeys", function() { it('should generate sshkey file with size data', function(done) { var sshkeyDirPath = path.join(userDir, 'projects', '.sshkeys'); var username = 'test'; - var email = 'test@test.com'; - var filename = 'test-key01'; - var data = { + var options = { + email: 'test@test.com', + name: 'test-key01', size: 4096 }; localfilesystem.init(mockSettings, mockRuntime).then(function() { sshkeys.init(mockSettings, mockRuntime).then(function() { - sshkeys.generateSSHKey(username, email, filename, data).then(function(retObj) { - retObj.should.be.equal(filename); - fs.existsSync(path.join(sshkeyDirPath,username+'_'+filename)).should.be.true(); - fs.existsSync(path.join(sshkeyDirPath,username+'_'+filename+'.pub')).should.be.true(); + sshkeys.generateSSHKey(username, options).then(function(retObj) { + retObj.should.be.equal(options.name); + fs.existsSync(path.join(sshkeyDirPath,username+'_'+options.name)).should.be.true(); + fs.existsSync(path.join(sshkeyDirPath,username+'_'+options.name+'.pub')).should.be.true(); done(); }).catch(function(err) { done(err); @@ -322,18 +323,18 @@ describe("storage/localfilesystem/sshkeys", function() { this.timeout(5000); var sshkeyDirPath = path.join(userDir, 'projects', '.sshkeys'); var username = 'test'; - var email = 'test@test.com'; - var filename = 'test-key01'; - var data = { + var options = { + email: 'test@test.com', + name: 'test-key01', password: 'testtest', size: 4096 }; localfilesystem.init(mockSettings, mockRuntime).then(function() { sshkeys.init(mockSettings, mockRuntime).then(function() { - sshkeys.generateSSHKey(username, email, filename, data).then(function(retObj) { - retObj.should.be.equal(filename); - fs.existsSync(path.join(sshkeyDirPath,username+'_'+filename)).should.be.true(); - fs.existsSync(path.join(sshkeyDirPath,username+'_'+filename+'.pub')).should.be.true(); + sshkeys.generateSSHKey(username, options).then(function(retObj) { + retObj.should.be.equal(options.name); + fs.existsSync(path.join(sshkeyDirPath,username+'_'+options.name)).should.be.true(); + fs.existsSync(path.join(sshkeyDirPath,username+'_'+options.name+'.pub')).should.be.true(); done(); }).catch(function(err) { done(err); @@ -350,17 +351,17 @@ describe("storage/localfilesystem/sshkeys", function() { this.timeout(5000); var sshkeyDirPath = path.join(userDir, 'projects', '.sshkeys'); var username = 'test'; - var email = 'test@test.com'; - var filename = 'test-key01'; - var data = { + var options = { + email: 'test@test.com', + name: 'test-key01', size: 3333 }; localfilesystem.init(mockSettings, mockRuntime).then(function() { sshkeys.init(mockSettings, mockRuntime).then(function() { - sshkeys.generateSSHKey(username, email, filename, data).then(function(retObj) { - retObj.should.be.equal(filename); - fs.existsSync(path.join(sshkeyDirPath,username+'_'+filename)).should.be.true(); - fs.existsSync(path.join(sshkeyDirPath,username+'_'+filename+'.pub')).should.be.true(); + sshkeys.generateSSHKey(username, options).then(function(retObj) { + retObj.should.be.equal(options.name); + fs.existsSync(path.join(sshkeyDirPath,username+'_'+options.name)).should.be.true(); + fs.existsSync(path.join(sshkeyDirPath,username+'_'+options.name+'.pub')).should.be.true(); done(); }).catch(function(err) { done(err);