diff --git a/server/config.js b/server/config.js index 9e0d7246..f2bb44a3 100644 --- a/server/config.js +++ b/server/config.js @@ -10,6 +10,7 @@ const fs = require('fs'); const exit = require(DIR_SERVER + 'exit'); const CloudFunc = require(DIR_COMMON + 'cloudfunc'); +const squad = require('squad'); const pullout = require('pullout/legacy'); const ponse = require('ponse'); const jonny = require('jonny'); @@ -20,6 +21,8 @@ const exec = require('execon'); const criton = require('criton'); const HOME = require('os-homedir')(); +const manageConfig = squad(traverse, cryptoPass); + const apiURL = CloudFunc.apiURL; const ConfigPath = path.join(DIR, 'json/config.json'); @@ -32,11 +35,10 @@ const readjsonSync = (name) => { }; const rootConfig = readjsonSync(ConfigPath); - const key = (a) => Object.keys(a).pop(); let configHome; -let error = tryCatch(() => { +const error = tryCatch(() => { configHome = readjsonSync(ConfigHome); }); @@ -95,9 +97,8 @@ function connection(socket) { socket.on('message', (json) => { if (typeof json !== 'object') return socket.emit('err', 'Error: Wrong data type!'); - - cryptoPass(json); - traverse(json); + + manageConfig(json); save((error) => { if (error) @@ -117,7 +118,7 @@ function middle(req, res, next) { if (req.url !== apiURL + '/config') return next(); - + switch(req.method) { case 'GET': get(req, res, next); @@ -162,8 +163,7 @@ function patch(req, res, callback) { if (error) return callback(error); - cryptoPass(json); - traverse(json); + manageConfig(json); save((error) => { if (error) @@ -182,11 +182,18 @@ function traverse(json) { }); } +module.exports._cryptoPass = cryptoPass; function cryptoPass(json) { const algo = manage('algo'); - if (json && json.password) - json.password = criton(json.password, algo); + if (!json.password) + return json; + + const password = criton(json.password, algo); + + return Object.assign({}, json, { + password, + }); } function check(socket, authCheck) { diff --git a/test/server/config.fixture.json b/test/server/config.fixture.json new file mode 100644 index 00000000..530ec120 --- /dev/null +++ b/test/server/config.fixture.json @@ -0,0 +1,3 @@ +{ + "password": "9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043" +} diff --git a/test/server/config.js b/test/server/config.js index 8371a700..7da554de 100644 --- a/test/server/config.js +++ b/test/server/config.js @@ -9,9 +9,11 @@ const readjson = require('readjson'); const root = '../../'; const dir = root + 'server/'; const config = require(dir + 'config'); +const {_cryptoPass} = config; const pathHomeConfig = path.join(os.homedir(), '.cloudcmd.json'); const pathConfig = path.join(__dirname, '..', '..', 'json', 'config.json'); +const fixture = require('./config.fixture'); const clean = (name) => { delete require.cache[require.resolve(name)]; @@ -76,3 +78,31 @@ test('config: listen: authCheck: not function', (t) => { t.end(); }); +test('config: cryptoPass: no password', (t) => { + const json = { + hello: 'world', + }; + + const result = _cryptoPass(json); + + t.equal(result, json, 'should not change json'); + t.end(); +}); + +test('config: cryptoPass', (t) => { + const json = { + password: 'hello', + }; + + const {password} = fixture; + + const expected = { + password, + }; + + const result = _cryptoPass(json); + + t.deepEqual(result, expected, 'should crypt password'); + t.end(); +}); +