diff --git a/src/cli/catalog.js b/src/cli/catalog.js index 00ab2b3fa..b8b321fb3 100644 --- a/src/cli/catalog.js +++ b/src/cli/catalog.js @@ -174,7 +174,7 @@ const _listCatalogItems = async function () { const _listCatalogItem = async function (obj) { logger.info(JSON.stringify(obj)); const result = await CatalogItemService.getCatalogItem(obj.itemId, {}, true); - logger.info(JSON.stringify(result)); + logger.info(JSON.stringify(result, null, 2)); logger.info('Catalog item has been successfully retrieved.'); }; diff --git a/src/cli/config.js b/src/cli/config.js index e2e7c969d..6a1cf50f7 100644 --- a/src/cli/config.js +++ b/src/cli/config.js @@ -16,6 +16,7 @@ const config = require('../config'); const constants = require('../helpers/constants'); const AppHelper = require('../helpers/app-helper'); const ErrorMessages = require('../helpers/error-messages'); +const Validator = require('../schemas'); const logger = require('../logger'); class Config extends BaseCLIHandler { @@ -77,84 +78,107 @@ const _executeCase = async function (catalogCommand, commandName, f) { }; const _addConfigOption = async function (options) { - if (options.port != null) { + await Validator.validate(options, Validator.schemas.configUpdate); + + updateConfig(options.port, 'port', 'Server:Port', async (onSuccess) => { const port = options.port; - if (!AppHelper.isValidPort(port)) { - logger.error(ErrorMessages.INVALID_PORT_FORMAT); - return; + const status = await AppHelper.checkPortAvailability(port); + if (status === 'closed') { + config.set('Server:Port', port); + onSuccess(); + } else { + logger.error(AppHelper.formatMessage(ErrorMessages.PORT_NOT_AVAILABLE, port)); } - AppHelper.checkPortAvailability(port).then(availability => { - if (availability === 'closed') { - config.set('Server:Port', port); - } - else { - logger.error(AppHelper.formatMessage(ErrorMessages.PORT_NOT_AVAILABLE, port)); - } - }); - } + }); - if (options.sslCert != null) { + updateConfig(options.sslCert, 'ssl-cert', 'Server:SslCert', (onSuccess) => { const sslCert = options.sslCert; if (!AppHelper.isFileExists(sslCert)) { logger.error(ErrorMessages.INVALID_FILE_PATH); return; } - config.set('Server:SslCert', sslCert) - } + config.set('Server:SslCert', sslCert); + onSuccess(); + }); - if (options.sslKey != null) { + if (options.sslKey) { const sslKey = options.sslKey; if (!AppHelper.isFileExists(sslKey)) { logger.error(ErrorMessages.INVALID_FILE_PATH); return; } - config.set('Server:SslKey', sslKey) + config.set('Server:SslKey', sslKey); + logger.info('Config option ssl-key has been updated.'); } - if (options.intermediateCert != null) { + updateConfig(options.intermediateCert, 'intermediate-cert', 'Server:IntermediateCert', (onSuccess) => { const intermediateCert = options.intermediateCert; if (!AppHelper.isFileExists(intermediateCert)) { logger.error(ErrorMessages.INVALID_FILE_PATH); return; } - config.set('Server:IntermediateCert', intermediateCert) - } - - if (options.emailActivationOn != null) { - config.set('Email:ActivationEnabled', true) - } - - if (options.emailActivationOff != null) { - config.set('Email:ActivationEnabled', false) - } - - if (options.homeUrl != null && config.get('Email:HomeUrl') !== options.homeUrl) { - config.set('Email:HomeUrl', options.homeUrl) - } - - if (options.emailAddress != null && config.get('Email:Address') !== options.emailAddress) { - if (options.emailPassword == null) { - return console.log('When changing email address, password must be provided.') + config.set('Server:IntermediateCert', intermediateCert); + onSuccess(); + }); + + updateConfig(options.emailActivationOn, 'email-activation-on', 'Email:ActivationEnabled', (onSuccess) => { + config.set('Email:ActivationEnabled', true); + onSuccess(); + }); + + updateConfig(options.emailActivationOff, 'email-activation-off', 'Email:ActivationEnabled', (onSuccess) => { + config.set('Email:ActivationEnabled', false); + onSuccess(); + }); + + updateConfig(options.homeUrl, 'home-url', 'Email:HomeUrl', (onSuccess) => { + config.set('Email:HomeUrl', options.homeUrl); + onSuccess(); + }); + + updateConfig(options.emailAddress, 'email-address', 'Email:Address', (onSuccess) => { + if (options.emailPassword) { + logger.info('When changing email address, password must be provided.'); + return; } - config.set('Email:Address', options.emailAddress) - } + config.set('Email:Address', options.emailAddress); + onSuccess(); + }); - if (options.emailPassword != null) { - config.set('Email:Password', AppHelper.encryptText(options.emailPassword, config.get('Email:Address'))) + if (options.emailPassword) { + config.set('Email:Password', AppHelper.encryptText(options.emailPassword, config.get('Email:Address'))); + logger.info('Config option email-password has been updated.'); } - if (options.emailService != null) { - config.set('Email:Service', options.emailService) - } + updateConfig(options.emailService, 'email-service', 'Email:Service', (onSuccess) => { + config.set('Email:Service', options.emailService); + onSuccess(); + }); - if (options.logDir != null) { - config.set('Service:LogsDirectory', options.logDir) - } + updateConfig(options.logDir, 'log-dir', 'Service:LogsDirectory', (onSuccess) => { + config.set('Service:LogsDirectory', options.logDir); + onSuccess(); + }); - if (options.logSize != null) { - config.set('Service:LogsFileSize', options.logSize * 1024) + updateConfig(options.logSize, 'log-size', 'Service:LogsFileSize', (onSuccess) => { + config.set('Service:LogsFileSize', options.logSize * 1024); + onSuccess(); + }) +}; + +const updateConfig = async function (newConfigValue, cliConfigName, configName, fn) { + if (newConfigValue) { + const oldConfigValue = config.get(configName); + if (newConfigValue !== oldConfigValue) { + await fn(function() { + const currentConfigValue = config.get(configName); + logger.info(`Config option ${cliConfigName} has been set to ${currentConfigValue}`); + }); + } else { + logger.info(`Config option ${cliConfigName} is already set to ${newConfigValue}`); + } } -} +}; const _listConfigOptions = function () { const configuration = { diff --git a/src/cli/controller.js b/src/cli/controller.js index 8a9324312..f76e91b7e 100644 --- a/src/cli/controller.js +++ b/src/cli/controller.js @@ -78,22 +78,22 @@ const _executeCase = async function (userCommand, commandName, f, isUserRequired const _getStatus = async function () { const response = await ControllerService.statusController(true); - logger.info(JSON.stringify(response)); + logger.info(JSON.stringify(response, null, 2)); }; const _emailActivation = async function () { const response = await ControllerService.emailActivation(true); - logger.info(JSON.stringify(response)); + logger.info(JSON.stringify(response, null, 2)); }; const _getFogTypes = async function () { const response = await ControllerService.getFogTypes(true); - logger.info(JSON.stringify(response)); + logger.info(JSON.stringify(response, null, 2)); }; const _getVersion = async function() { const response = await ControllerService.getVersion(true); - logger.info(response); + logger.info(response, null, 2); }; module.exports = new Controller(); \ No newline at end of file diff --git a/src/cli/registry.js b/src/cli/registry.js index 31be587d4..93c722d36 100644 --- a/src/cli/registry.js +++ b/src/cli/registry.js @@ -78,7 +78,7 @@ async function _createRegistry(obj, user) { async function _getRegistries(obj, user) { const result = await RegistryService.findRegistries(user, true); - logger.info(JSON.stringify(result)); + logger.info(JSON.stringify(result, null, 2)); logger.info('List of Registries has been received successfully.'); } diff --git a/src/cli/user.js b/src/cli/user.js index 6b6dbed29..61416b4e4 100644 --- a/src/cli/user.js +++ b/src/cli/user.js @@ -14,10 +14,8 @@ const BaseCLIHandler = require('./base-cli-handler'); const constants = require('../helpers/constants'); const UserService = require('../services/user-service'); -const UserManager = require('../sequelize/managers/user-manager'); const logger = require('../logger'); const AppHelper = require('../helpers/app-helper'); -const EmailActivationCodeService = require('../services/email-activation-code-service'); const AuthDecorator = require('../decorators/cli-decorator'); const Validator = require('../schemas'); diff --git a/src/helpers/app-helper.js b/src/helpers/app-helper.js index e66660f95..3dd7a2b59 100644 --- a/src/helpers/app-helper.js +++ b/src/helpers/app-helper.js @@ -50,9 +50,9 @@ function generateRandomString(size) { // Checks the status of a single port // returns 'closed' if port is available // returns 'open' if port is not available -function checkPortAvailability(port) { - return portscanner.checkPortStatus(port).then(function (status) { - return status; +async function checkPortAvailability(port) { + return new Promise((resolve) => { + return resolve(portscanner.checkPortStatus(port)); }); } diff --git a/src/helpers/error-messages.js b/src/helpers/error-messages.js index f72a4ca18..c7cc1317c 100644 --- a/src/helpers/error-messages.js +++ b/src/helpers/error-messages.js @@ -34,7 +34,7 @@ module.exports = { EMAIL_SENDER_NOT_CONFIGURED: 'Email sender not configured', INVALID_PORT_FORMAT: 'Invalid port format', INVALID_FILE_PATH: 'Invalid file path', - PORT_NOT_AVAILABLE: 'Port {} not available', + PORT_NOT_AVAILABLE: 'Port {} is not available', UNABLE_TO_WRITE_STRACE: 'Error while writing strace data to file. File name: {}, err: {}', UNABLE_TO_DELETE_STRACE: 'Error while deleting strace data file. File name: {}, err: {}', FTP_ERROR: 'FTP error: {}', diff --git a/src/schemas/config.js b/src/schemas/config.js new file mode 100644 index 000000000..17d5f9018 --- /dev/null +++ b/src/schemas/config.js @@ -0,0 +1,39 @@ +/* + * ******************************************************************************* + * * Copyright (c) 2018 Edgeworx, Inc. + * * + * * This program and the accompanying materials are made available under the + * * terms of the Eclipse Public License v. 2.0 which is available at + * * http://www.eclipse.org/legal/epl-2.0 + * * + * * SPDX-License-Identifier: EPL-2.0 + * ******************************************************************************* + * + */ + +const configUpdate = { + "id": "/configUpdate", + "type": "object", + "properties": { + "port": {"type": "integer", "minimum" : 0, "maximum" : 65535}, + "sslCert": {"type": "string"}, + "sslKey": {"type": "string"}, + "intermediateCert": {"type": "string"}, + "emailActivationOn": {"type": "boolean"}, + "emailActivationOff": {"type": "boolean"}, + "homeUrl": {"type": "string"}, + "emailAddress": { + "type": "string", + "pattern": "^(([^<>()\\[\\]\\\\.,;:\\s@\"]+(\\.[^<>()\\[\\]\\\\.,;:\\s@\"]+)*)|(\".+\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$" + }, + "emailPassword": {"type": "string", "minLength": 1}, + "emailService": {"type": "string"}, + "logDir": {"type": "string"}, + "logSize": {"type": "integer"} + } +} + + module.exports = { + mainSchemas: [configUpdate], + innerSchemas: [] +}; diff --git a/src/schemas/tunnel.js b/src/schemas/tunnel.js index 2a1e77a78..87f9397aa 100644 --- a/src/schemas/tunnel.js +++ b/src/schemas/tunnel.js @@ -19,8 +19,8 @@ const tunnelCreate = { "username": {"type": "string", "minLength": 1}, "password": {"type": "string"}, "rsakey": {"type": "string"}, - "lport": {"type": "integer", "minimum" : 0}, - "rport": {"type": "integer", "minimum" : 0} + "lport": {"type": "integer", "minimum" : 0, "maximum" : 65535}, + "rport": {"type": "integer", "minimum" : 0, "maximum" : 65535} }, "required": ["iofogUuid", "username", "password", "lport", "rport"] };