Skip to content

Commit

Permalink
Add AdminController (kuzzleio#1129)
Browse files Browse the repository at this point in the history
* KZL-143 KZL-145 add admin controller and reset cache action

* Boyscout: fix crash because kuzzle try to load protocols in simplefiles

* Add tests for adminController

* Add resetKuzzleData and resetSecurity

* fix features for admin controller

* Add resetDatabase

* fix tests

* fix tests

* fix tests

* Add default rights restrictions for admin controller

* Fix test

* Fix test

* Use API for all actions except dump

* Add generateDump

* Fix tests and remove old cli tests

* Remove reset security scenario

* Make sonarqube happy

* Remove references to cliController

* Fix features

* fix internal error

* remove internal broker

* re-add shutdown

* fix test

* Put shutdown in adminController

* Reset defaults roles and profiles with resetSecurity

* replace ws by uws

* Nitpicking

* increase coverage

* increase coverage

* increase coverage

* fix linter

* increase coverage

* put back proxyBroker

* fix tests

* remove tests

* fix tests

* nitpicking

* Updates for PR

* Put back defaut config

* Disable dump if it is specified in config

* fix message in cli

* add truncate() method on repositories + refactor admin controller to use truncate()

* fix admin controller

* fix test

* Add Janitor core component

* return number of deleted objects in truncate

* fix linter

* catch TypeError for scanStream

* fix linter

* Please sonarqube

* fix linter

* Nitpicking

* Updates for PR

* update headers

* fix linter

* fix tests
  • Loading branch information
Aschen authored and scottinet committed Jun 28, 2018
1 parent 3fcc2b3 commit 7379e9e
Show file tree
Hide file tree
Showing 151 changed files with 3,085 additions and 3,088 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,3 @@ plugins/available/*
plugins/enabled/*
!plugins/enabled/kuzzle-plugin-auth-passport-local
!plugins/enabled/kuzzle-plugin-logger


25 changes: 0 additions & 25 deletions .kuzzlerc.sample
Original file line number Diff line number Diff line change
Expand Up @@ -460,31 +460,6 @@
"cleanInterval": 86400000,
"maxDelete": 1000
},

// [internalBroker]
// The internalBroker is used for internal Kuzzle communication,
// notably to transport messages between the CLI and the server.
// * host:
// Host name of a Kuzzle server. Change this only if you want to use
// the CLI from a distant host.
// * port:
// Network port to open for internal communications
// * retryInterval:
// Time interval (in milliseconds) between reconnection retries
// when the connection to the broker server has been lost
// * pingInterval:
// Time (in milliseconds) between ping requests
// * pingTimeout:
// Timeout (in milliseconds) of a ping request. If reached, Kuzzle
// marks its connection to the distant host as lost.
"internalBroker": {
"aliases": ["broker"],
"host": "localhost",
"pingInterval": 60000,
"pingTimeout": 500,
"port": 7911,
"retryInterval": 1000
},
// [db]
// The default database layer is Elasticsearch and it is
// currently the only database layer we support.
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@

END OF TERMS AND CONDITIONS

Copyright 2015-2017 Kuzzle
Copyright 2015-2018 Kuzzle

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion bin/commands/colorOutput.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Kuzzle, a backend software, self-hostable and ready to use
* to power modern apps
*
* Copyright 2015-2017 Kuzzle
* Copyright 2015-2018 Kuzzle
* mailto: support AT kuzzle.io
* website: http://kuzzle.io
*
Expand Down
31 changes: 18 additions & 13 deletions bin/commands/createFirstAdmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Kuzzle, a backend software, self-hostable and ready to use
* to power modern apps
*
* Copyright 2015-2017 Kuzzle
* Copyright 2015-2018 Kuzzle
* mailto: support AT kuzzle.io
* website: http://kuzzle.io
*
Expand All @@ -24,9 +24,8 @@
const
Bluebird = require('bluebird'),
readlineSync = require('readline-sync'),
rc = require('rc'),
params = rc('kuzzle'),
ColorOutput = require('./colorOutput');
ColorOutput = require('./colorOutput'),
sendAction = require('./sendAction');

/** @type ColorOutput */
let cout;
Expand Down Expand Up @@ -74,7 +73,6 @@ function confirm (username, resetRoles) {
}

function commandCreateFirstAdmin (options) {
const kuzzle = new (require('../../lib/api/kuzzle'))();
let
username,
password,
Expand All @@ -83,8 +81,12 @@ function commandCreateFirstAdmin (options) {
cout = new ColorOutput(options);

process.stdin.setEncoding('utf8');
const action = {
controller: 'server',
action: 'adminExists'
};

return kuzzle.cli.doAction('adminExists', {})
return sendAction(options, action)
.then(adminExists => {
if (adminExists.result.exists) {
console.log('An administrator account already exists.');
Expand All @@ -111,22 +113,25 @@ function commandCreateFirstAdmin (options) {
console.log(cout.warn('Aborting'));
process.exit(0);
}

return kuzzle.cli.doAction('createFirstAdmin', {
_id: username,
const args = {
controller: 'security',
action: 'createFirstAdmin',
_id: username
};
const query = {
reset: resetRoles,
body: {
content: {

},
content: { },
credentials: {
local: {
username,
password
}
}
}
}, {pid: params.pid, debug: options.parent.debug});
};

return sendAction(options, args, query);
})
.then(() => {
console.log(cout.ok(`[✔] "${username}" administrator account created`));
Expand Down
23 changes: 14 additions & 9 deletions bin/commands/dump.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Kuzzle, a backend software, self-hostable and ready to use
* to power modern apps
*
* Copyright 2015-2017 Kuzzle
* Copyright 2015-2018 Kuzzle
* mailto: support AT kuzzle.io
* website: http://kuzzle.io
*
Expand All @@ -21,19 +21,22 @@

/* eslint-disable no-console */

const ColorOutput = require('./colorOutput');
const
ColorOutput = require('./colorOutput'),
sendAction = require('./sendAction');

/**
* @param {object} options
*/
module.exports = function commandDump (options) {
function commandDump (options) {
const
kuzzle = new (require('../../lib/api/kuzzle'))(),
cout = new ColorOutput(options);

console.log(cout.notice('[ℹ] Creating dump file...'));

kuzzle.cli.doAction('dump', {suffix: 'cli'})
const args = {
controller: 'admin',
action: 'dump'
};

return sendAction(options, args, { suffix: 'cli' })
.then(request => {
console.log(cout.ok('[✔] Done!'));
console.log('\n' + cout.warn(`[ℹ] Dump has been successfully generated in "${request.result}" folder`));
Expand All @@ -44,4 +47,6 @@ module.exports = function commandDump (options) {
console.log(cout.error(`[✖] ${err}`));
process.exit(1);
});
};
}

module.exports = commandDump;
27 changes: 15 additions & 12 deletions bin/commands/clearCache.js → bin/commands/resetCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Kuzzle, a backend software, self-hostable and ready to use
* to power modern apps
*
* Copyright 2015-2017 Kuzzle
* Copyright 2015-2018 Kuzzle
* mailto: support AT kuzzle.io
* website: http://kuzzle.io
*
Expand All @@ -25,21 +25,21 @@ const
rc = require('rc'),
params = rc('kuzzle'),
readlineSync = require('readline-sync'),
ColorOutput = require('./colorOutput');
ColorOutput = require('./colorOutput'),
sendAction = require('./sendAction');

function commandClearCache (database, options) {
function commandResetCache (database, options) {
let
opts = options,
db = database,
userIsSure;

if (options === undefined) {
opts = database;
db = null;
db = 'internalCache';
}

const cout = new ColorOutput(opts);
const data = {database: db};

if (db === 'memoryStorage') {
console.log(cout.warn('[ℹ] You are about to clear Kuzzle memoryStorage database.'));
Expand All @@ -50,21 +50,24 @@ function commandClearCache (database, options) {
}

if (userIsSure) {
const kuzzle = new (require('../../lib/api/kuzzle'))();

console.log(cout.notice('[ℹ] Processing...\n'));
return kuzzle.cli.doAction('clearCache', data)
const args = {
controller: 'admin',
action: 'resetCache'
};

return sendAction(opts, args, { database: db })
.then(() => {
console.log(cout.ok('[✔] Done!'));
console.log(cout.ok(`[✔] Kuzzle cache '${db}' has been successfully reset`));
process.exit(0);
})
.catch(err => {
console.log(cout.error(`[✖] ${err}`));
console.error(err);
process.exit(1);
});
}

console.log(cout.notice('[ℹ] Nothing have been done... you do not look that sure...'));
console.log(cout.notice('[ℹ] Aborted'));
}

module.exports = commandClearCache;
module.exports = commandResetCache;
69 changes: 69 additions & 0 deletions bin/commands/resetDatabase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Kuzzle, a backend software, self-hostable and ready to use
* to power modern apps
*
* Copyright 2015-2018 Kuzzle
* mailto: support AT kuzzle.io
* website: http://kuzzle.io
*
* 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
*
* https://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.
*/

/* eslint-disable no-console */

const
rc = require('rc'),
params = rc('kuzzle'),
readlineSync = require('readline-sync'),
ColorOutput = require('./colorOutput'),
sendAction = require('./sendAction');

function commandResetDatabase (options) {
const
cout = new ColorOutput(options);

let userIsSure = false;

console.log(cout.warn('[ℹ] You are about to clear all data stored on Kuzzle.'));
console.log(cout.warn('[ℹ] This operation cannot be undone.\n'));

if (!params.noint) {
userIsSure = readlineSync.question('[❓] Are you sure? If so, please type "I am sure": ') === 'I am sure';
}
else {
// non-interactive mode
userIsSure = true;
}

if (userIsSure) {
console.log(cout.notice('[ℹ] Processing...\n'));
const args = {
controller: 'admin',
action: 'resetDatabase'
};

return sendAction(options, args)
.then(() => {
console.log(cout.ok('[✔] Kuzzle databases have been successfully reset'));
process.exit(0);
})
.catch(err => {
console.error(err);
process.exit(1);
});
}

console.log(cout.notice('[ℹ] Aborted'));
}

module.exports = commandResetDatabase;
19 changes: 12 additions & 7 deletions bin/commands/reset.js → bin/commands/resetKuzzleData.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Kuzzle, a backend software, self-hostable and ready to use
* to power modern apps
*
* Copyright 2015-2017 Kuzzle
* Copyright 2015-2018 Kuzzle
* mailto: support AT kuzzle.io
* website: http://kuzzle.io
*
Expand All @@ -25,11 +25,11 @@ const
rc = require('rc'),
params = rc('kuzzle'),
readlineSync = require('readline-sync'),
ColorOutput = require('./colorOutput');
ColorOutput = require('./colorOutput'),
sendAction = require('./sendAction');

function commandReset (options) {
function commandResetKuzzleData (options) {
const
kuzzle = new (require('../../lib/api/kuzzle'))(),
cout = new ColorOutput(options);

let userIsSure = false;
Expand All @@ -39,15 +39,20 @@ function commandReset (options) {

if (!params.noint) {
userIsSure = readlineSync.question('[❓] Are you sure? If so, please type "I am sure": ') === 'I am sure';
}
}
else {
// non-interactive mode
userIsSure = true;
}

if (userIsSure) {
console.log(cout.notice('[ℹ] Processing...\n'));
return kuzzle.cli.doAction('cleanDb', {}, {debug: options.parent.debug})
const args = {
controller: 'admin',
action: 'resetKuzzleData'
};

return sendAction(options, args)
.then(() => {
console.log(cout.ok('[✔] Kuzzle has been successfully reset'));
process.exit(0);
Expand All @@ -61,4 +66,4 @@ function commandReset (options) {
console.log(cout.notice('[ℹ] Aborted'));
}

module.exports = commandReset;
module.exports = commandResetKuzzleData;
Loading

0 comments on commit 7379e9e

Please sign in to comment.