diff --git a/src/fireedge/package.json b/src/fireedge/package.json index 105d637910..087a871c6a 100644 --- a/src/fireedge/package.json +++ b/src/fireedge/package.json @@ -12,6 +12,7 @@ "pot": "node potfile.js", "po2json": "node po2json.js", "lint": "eslint ./src/client/", + "lint-server": "eslint ./src/server/", "lint-fix": "eslint ./src/client/ --fix" }, "author": "opennebula.io", diff --git a/src/fireedge/src/server/index.js b/src/fireedge/src/server/index.js index 55ab24891a..2b4ab3d68c 100644 --- a/src/fireedge/src/server/index.js +++ b/src/fireedge/src/server/index.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ import path from 'path' import express from 'express' @@ -51,7 +52,7 @@ import { entrypointApp } from './routes/entrypoints' import { websockets } from './routes/websockets' -import { vmrcUpgrade } from './routes/websockets/vmrc' +import { vmrc } from './routes/websockets/vmrc' import { guacamole } from './routes/websockets/guacamole' import { messageTerminal, getConfig } from './utils' @@ -177,10 +178,13 @@ appServer.listen(port, host, err => { } messageTerminal(config) }) -vmrcUpgrade(appServer) +vmrc(appServer) guacamole(appServer) -function handleBreak (code) { +/** + * Handle sigterm and sigint. + */ +const handleBreak = () => { if (appServer && appServer.close && typeof appServer.close === 'function') { appServer.close(() => { // this close sockets diff --git a/src/fireedge/src/server/routes/api/2fa/functions.js b/src/fireedge/src/server/routes/api/2fa/functions.js deleted file mode 100644 index 85ee974a0b..0000000000 --- a/src/fireedge/src/server/routes/api/2fa/functions.js +++ /dev/null @@ -1,279 +0,0 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ - -const speakeasy = require('speakeasy') -const qrcode = require('qrcode') -const { Map } = require('immutable') -const { httpResponse } = require('server/utils/server') // '../../../utils/server' -const { getConfig } = require('server/utils/yml') // '../../../utils/yml' -const { Actions } = require('server/utils/constants/commands/user') -const { - httpMethod, - default2FAIssuer, - default2FAOpennebulaVar, - default2FAOpennebulaTmpVar -} = require('server/utils/constants/defaults') - -const { POST, GET } = httpMethod -const { - responseOpennebula, - checkOpennebulaCommand, - generateNewTemplate, - check2Fa -} = require('server/utils/opennebula') -const { from: fromData } = require('server/utils/constants/defaults') -const { - ok, - unauthorized, - internalServerError -} = require('server/utils/constants/http-codes') - -// user config -const appConfig = getConfig() - -const twoFactorAuthIssuer = - appConfig.TWO_FACTOR_AUTH_ISSUER || default2FAIssuer - -const getUserInfoAuthenticated = (connect, userId, callback, next) => { - if ( - connect && - !!userId && - callback && - next && - typeof connect === 'function' && - typeof callback === 'function' && - typeof next === 'function' && - Actions.USER_INFO - ) { - const connectOpennebula = connect() - const dataUser = {} - // empty positions for validate... - dataUser[fromData.resource] = {} - dataUser[fromData.query] = {} - dataUser[fromData.postBody] = {} - dataUser[fromData.resource].id = userId - const getOpennebulaMethod = checkOpennebulaCommand( - Actions.USER_INFO, - GET - ) - connectOpennebula( - Actions.USER_INFO, - getOpennebulaMethod(dataUser), - (err, value) => { - responseOpennebula( - () => undefined, - err, - value, - info => { - if (info !== undefined && info !== null) { - callback(info) - } else { - next() - } - }, - next - ) - } - ) - } -} - -const generateQR = (req, res, next, connect, userId) => { - const secret = speakeasy.generateSecret({ - length: 10, - name: twoFactorAuthIssuer - }) - if (secret && secret.otpauth_url && secret.base32) { - const { otpauth_url: otpURL, base32 } = secret - qrcode.toDataURL(otpURL, (err, dataURL) => { - if (err) { - res.locals.httpCode = httpResponse(internalServerError) - next() - } else { - const connectOpennebula = connect() - getUserInfoAuthenticated( - connect, - userId, - info => { - if (info && info.USER && info.USER.TEMPLATE && req) { - const dataUser = Map(req).toObject() - const emptyTemplate = {} - emptyTemplate[default2FAOpennebulaTmpVar] = base32 - - dataUser[fromData.resource].id = userId - dataUser[fromData.postBody].template = generateNewTemplate( - info.USER.TEMPLATE.SUNSTONE || {}, - emptyTemplate, - [default2FAOpennebulaVar] - ) - const getOpennebulaMethod = checkOpennebulaCommand( - Actions.USER_UPDATE, - POST - ) - connectOpennebula( - Actions.USER_UPDATE, - getOpennebulaMethod(dataUser), - (error, value) => { - responseOpennebula( - () => undefined, - error, - value, - pass => { - if (pass !== undefined && pass !== null) { - res.locals.httpCode = httpResponse(ok, { - img: dataURL - }) - next() - } else { - next() - } - }, - next - ) - } - ) - } else { - next() - } - }, - next - ) - } - }) - } else { - next() - } -} - -const twoFactorSetup = (req, res, next, connect, userId) => { - const connectOpennebula = connect() - getUserInfoAuthenticated( - connect, - userId, - info => { - if ( - info && - info.USER && - info.USER.TEMPLATE && - info.USER.TEMPLATE.SUNSTONE && - info.USER.TEMPLATE.SUNSTONE[default2FAOpennebulaTmpVar] && - fromData && - fromData.postBody && - req && - req[fromData.postBody] && - req[fromData.postBody].token - ) { - const sunstone = info.USER.TEMPLATE.SUNSTONE - const token = req[fromData.postBody].token - const secret = sunstone[default2FAOpennebulaTmpVar] - if (check2Fa(secret, token)) { - const emptyTemplate = {} - emptyTemplate[default2FAOpennebulaVar] = secret - - const dataUser = Map(req).toObject() - dataUser[fromData.resource].id = userId - dataUser[fromData.postBody].template = generateNewTemplate( - sunstone || {}, - emptyTemplate, - [default2FAOpennebulaTmpVar] - ) - const getOpennebulaMethodUpdate = checkOpennebulaCommand( - Actions.USER_UPDATE, - POST - ) - connectOpennebula( - Actions.USER_UPDATE, - getOpennebulaMethodUpdate(dataUser), - (err, value) => { - responseOpennebula( - () => undefined, - err, - value, - pass => { - if (pass !== undefined && pass !== null) { - res.locals.httpCode = httpResponse(ok) - } - next() - }, - next - ) - } - ) - } else { - res.locals.httpCode = httpResponse(unauthorized) - next() - } - } else { - next() - } - }, - next - ) -} -const twoFactorDelete = (req, res, next, connect, userId) => { - const connectOpennebula = connect() - getUserInfoAuthenticated( - connect, - userId, - info => { - if ( - info && - info.USER && - info.USER.TEMPLATE && - info.USER.TEMPLATE.SUNSTONE - ) { - const emptyTemplate = {} - const dataUser = Map(req).toObject() - dataUser[fromData.resource].id = userId - dataUser[fromData.postBody].template = generateNewTemplate( - info.USER.TEMPLATE.SUNSTONE || {}, - emptyTemplate, - [default2FAOpennebulaTmpVar, default2FAOpennebulaVar] - ) - const getOpennebulaMethodUpdate = checkOpennebulaCommand( - Actions.USER_UPDATE, - POST - ) - connectOpennebula( - Actions.USER_UPDATE, - getOpennebulaMethodUpdate(dataUser), - (err, value) => { - responseOpennebula( - () => undefined, - err, - value, - pass => { - if (pass !== undefined && pass !== null) { - res.locals.httpCode = httpResponse(ok) - } - next() - }, - next - ) - } - ) - } else { - next() - } - }, - next - ) -} -module.exports = { - getUserInfoAuthenticated, - generateQR, - twoFactorSetup, - twoFactorDelete -} diff --git a/src/fireedge/src/server/routes/api/2fa/index.js b/src/fireedge/src/server/routes/api/2fa/index.js index c5813f2b3c..3ea75e82cd 100644 --- a/src/fireedge/src/server/routes/api/2fa/index.js +++ b/src/fireedge/src/server/routes/api/2fa/index.js @@ -1,50 +1,58 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ -const { generateQR, twoFactorSetup, twoFactorDelete } = require('./functions') -const { httpMethod } = require('server/utils/constants/defaults') // ../../../utils/constants/defaults' -const { - TWO_FACTOR_QR, - TWO_FACTOR_DELETE, - TWO_FACTOR_SETUP -} = require('./string-routes') - -const { POST, DELETE } = httpMethod - -const privateRoutes = [ - { - httpMethod: POST, - endpoint: TWO_FACTOR_QR, - action: generateQR - }, - { - httpMethod: POST, - endpoint: TWO_FACTOR_SETUP, - action: twoFactorSetup - }, - { - httpMethod: DELETE, - endpoint: TWO_FACTOR_DELETE, - action: twoFactorDelete - } -] +const { addFunctionAsRoute, setFunctionRoute } = require('server/utils/server') +const { routes: tfaRoutes } = require('./tfa') +const { TFA } = require('./string-routes') +const privateRoutes = [] const publicRoutes = [] +/** + * Set private routes. + * + * @param {object} routes - object of routes + * @param {string} path - principal route + * @param {Function} action - function of route + */ +const setPrivateRoutes = (routes = {}, path = '', action = () => undefined) => { + if (Object.keys(routes).length > 0 && routes.constructor === Object) { + Object.keys(routes).forEach((route) => { + privateRoutes.push( + setFunctionRoute(route, path, + (req, res, next, connection, userId, user) => { + action(req, res, next, routes[route], user, connection) + }) + ) + }) + } +} + +/** + * Add routes. + * + * @returns {Array} routes + */ +const generatePrivateRoutes = () => { + setPrivateRoutes(tfaRoutes, TFA, addFunctionAsRoute) + return privateRoutes +} + const functionRoutes = { - private: privateRoutes, + private: generatePrivateRoutes(), public: publicRoutes } diff --git a/src/fireedge/src/server/routes/api/2fa/string-routes.js b/src/fireedge/src/server/routes/api/2fa/string-routes.js index 52ba330b0b..b3f68566da 100644 --- a/src/fireedge/src/server/routes/api/2fa/string-routes.js +++ b/src/fireedge/src/server/routes/api/2fa/string-routes.js @@ -1,26 +1,23 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ -const TWO_FACTOR_SETUP = '2fsetup'; -const TWO_FACTOR_QR = '2fqr'; -const TWO_FACTOR_DELETE = '2fdelete'; +const TFA = 'tfa' const Actions = { - TWO_FACTOR_SETUP, - TWO_FACTOR_QR, - TWO_FACTOR_DELETE -}; + TFA +} -module.exports = Actions; +module.exports = Actions diff --git a/src/fireedge/src/server/routes/api/2fa/tfa-functions.js b/src/fireedge/src/server/routes/api/2fa/tfa-functions.js new file mode 100644 index 0000000000..da8057cfd1 --- /dev/null +++ b/src/fireedge/src/server/routes/api/2fa/tfa-functions.js @@ -0,0 +1,270 @@ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ + +const speakeasy = require('speakeasy') +const qrcode = require('qrcode') + +const { + httpMethod, + default2FAIssuer, + defaultEmptyFunction, + default2FAOpennebulaVar, + default2FAOpennebulaTmpVar +} = require('server/utils/constants/defaults') +const { httpResponse } = require('server/utils/server') +const { getConfig } = require('server/utils/yml') +const { check2Fa } = require('server/utils/jwt') +const { Actions } = require('server/utils/constants/commands/user') +const { + responseOpennebula, + getDefaultParamsOfOpennebulaCommand, + generateNewResourceTemplate +} = require('server/utils/opennebula') + +// user config +const appConfig = getConfig() +const twoFactorAuthIssuer = + appConfig.TWO_FACTOR_AUTH_ISSUER || default2FAIssuer + +const { GET } = httpMethod + +const { + ok, + unauthorized, + internalServerError +} = require('server/utils/constants/http-codes') + +/** + * Get information for opennebula authenticated user. + * + * @param {Function} connect - xmlrpc function + * @param {Function} next - express stepper + * @param {Function} callback - run if have user information + */ +const getUserInfoAuthenticated = (connect = defaultEmptyFunction, next = defaultEmptyFunction, callback = defaultEmptyFunction) => { + connect( + Actions.USER_INFO, + getDefaultParamsOfOpennebulaCommand(Actions.USER_INFO, GET), + (err, value) => { + responseOpennebula( + () => undefined, + err, + value, + info => { + if (info !== undefined && info !== null) { + callback(info) + } else { + next() + } + }, + next + ) + } + ) +} + +/** + * Add 2FA user. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {object} userData - user of http request + * @param {Function} oneConnection - function of xmlrpc + */ +const setup = (res = {}, next = defaultEmptyFunction, params = {}, userData = {}, oneConnection = defaultEmptyFunction) => { + const { token } = params + const oneConnect = oneConnection() + getUserInfoAuthenticated( + oneConnect, + next, + userData => { + if ( + userData && + userData.USER && + userData.USER.ID && + userData.USER.TEMPLATE && + userData.USER.TEMPLATE.SUNSTONE && + userData.USER.TEMPLATE.SUNSTONE[default2FAOpennebulaTmpVar] && + token + ) { + const sunstone = userData.USER.TEMPLATE.SUNSTONE + const secret = sunstone[default2FAOpennebulaTmpVar] + if (check2Fa(secret, token)) { + oneConnect( + Actions.USER_UPDATE, + [ + parseInt(userData.USER.ID, 10), + generateNewResourceTemplate( + userData.USER.TEMPLATE.SUNSTONE || {}, + { [default2FAOpennebulaVar]: secret }, + [default2FAOpennebulaTmpVar] + ), + 1 + ], + (error, value) => { + responseOpennebula( + () => undefined, + error, + value, + pass => { + if (pass !== undefined && pass !== null) { + res.locals.httpCode = httpResponse(ok) + } + next() + }, + next + ) + } + ) + } else { + res.locals.httpCode = httpResponse(unauthorized) + next() + } + } else { + next() + } + } + ) +} + +/** + * Generate QR. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {object} userData - user of http request + * @param {Function} oneConnection - function of xmlrpc + */ +const qr = (res = {}, next = defaultEmptyFunction, params = {}, userData = {}, oneConnection = defaultEmptyFunction) => { + const secret = speakeasy.generateSecret({ + length: 10, + name: twoFactorAuthIssuer + }) + if (secret && secret.otpauth_url && secret.base32) { + const { otpauth_url: otpURL, base32 } = secret + qrcode.toDataURL(otpURL, (err, dataURL) => { + if (err) { + res.locals.httpCode = httpResponse(internalServerError) + next() + } else { + const oneConnect = oneConnection() + getUserInfoAuthenticated( + oneConnect, + next, + userData => { + if (userData && userData.USER && userData.USER.ID && userData.USER.TEMPLATE) { + oneConnect( + Actions.USER_UPDATE, + [ + parseInt(userData.USER.ID, 10), + generateNewResourceTemplate( + userData.USER.TEMPLATE.SUNSTONE || {}, + { [default2FAOpennebulaTmpVar]: base32 }, + [default2FAOpennebulaVar] + ), + 1 + ], + (error, value) => { + responseOpennebula( + () => undefined, + error, + value, + pass => { + if (pass !== undefined && pass !== null) { + res.locals.httpCode = httpResponse(ok, { + img: dataURL + }) + next() + } else { + next() + } + }, + next + ) + } + ) + } else { + next() + } + } + ) + } + }) + } else { + next() + } +} + +/** + * Delete 2fa. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {object} userData - user of http request + * @param {Function} oneConnection - function of xmlrpc + */ +const del = (res = {}, next = defaultEmptyFunction, params = {}, userData = {}, oneConnection = defaultEmptyFunction) => { + const oneConnect = oneConnection() + getUserInfoAuthenticated( + oneConnect, + next, + userData => { + if ( + userData && + userData.USER && + userData.USER.ID && + userData.USER.TEMPLATE && + userData.USER.TEMPLATE.SUNSTONE + ) { + oneConnect( + Actions.USER_UPDATE, + [ + parseInt(userData.USER.ID, 10), + generateNewResourceTemplate( + userData.USER.TEMPLATE.SUNSTONE || {}, + {}, + [default2FAOpennebulaTmpVar, default2FAOpennebulaVar] + ), + 1 + ], + (err, value) => { + responseOpennebula( + () => undefined, + err, + value, + pass => { + if (pass !== undefined && pass !== null) { + res.locals.httpCode = httpResponse(ok) + } + next() + }, + next + ) + } + ) + } + } + ) +} +const tfaApi = { + setup, + qr, + del +} +module.exports = tfaApi diff --git a/src/fireedge/src/server/routes/api/2fa/tfa.js b/src/fireedge/src/server/routes/api/2fa/tfa.js new file mode 100644 index 0000000000..d8984bef4d --- /dev/null +++ b/src/fireedge/src/server/routes/api/2fa/tfa.js @@ -0,0 +1,50 @@ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ + +const { httpMethod, from: fromData } = require('server/utils/constants/defaults') +const { setup, qr, del } = require('./tfa-functions') +const { POST, DELETE, GET } = httpMethod + +const routes = { + [POST]: { + null: { + action: setup, + params: { + token: { + from: fromData.postBody, + name: 'token' + } + } + } + }, + [GET]: { + null: { + action: qr, + params: {} + } + }, + [DELETE]: { + null: { + action: del, + params: {} + } + } +} + +const authApi = { + routes +} +module.exports = authApi diff --git a/src/fireedge/src/server/routes/api/auth/auth-functions.js b/src/fireedge/src/server/routes/api/auth/auth-functions.js new file mode 100644 index 0000000000..87861d5ffb --- /dev/null +++ b/src/fireedge/src/server/routes/api/auth/auth-functions.js @@ -0,0 +1,117 @@ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ + +const { Map } = require('immutable') +const { + login, + setUser, + setPass, + setType, + setTfaToken, + setExtended, + setNext, + setRes, + setNodeConnect, + connectOpennebula, + updaterResponse +} = require('./functions') + +const { internalServerError, unauthorized } = require('server/utils/constants/http-codes') +const { Actions } = require('server/utils/constants/commands/user') +const { + httpMethod, + defaultEmptyFunction +} = require('server/utils/constants/defaults') + +const { GET } = httpMethod + +const { + getDefaultParamsOfOpennebulaCommand +} = require('server/utils/opennebula') + +/** + * Login user. + * + * @param {error} err - run if no have user data + * @param {string} value - opennebula information + * @param {Function} success - success + * @param {Function} error - error + */ +const loginUser = (err = '', value = '', success = defaultEmptyFunction, error = defaultEmptyFunction) => { + if (value && value.USER && !err) { + success(value) + } else { + error() + } +} + +/** + * Fireedge user auth. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {object} userData - user of http request + * @param {Function} oneConnection - function of xmlrpc + */ +const auth = (res = {}, next = defaultEmptyFunction, params = {}, userData = {}, oneConnection = defaultEmptyFunction) => { + const { user, token, type, token2fa, extended } = params + setRes(res) + setNext(next) + setNodeConnect(oneConnection) + updaterResponse(new Map(internalServerError).toObject()) + if (user && token) { + const oneConnect = connectOpennebula(user, token) + + /** + * Run if have information. + * + * @param {object} oneValue - opennebula value + */ + const success = oneValue => { + setUser(user || '') + setPass(token || '') + setType(type || '') + setTfaToken(token2fa || '') + setExtended(extended || '') + login(oneValue) + } + + /** + * Run if no have information. + */ + const error = () => { + updaterResponse(new Map(unauthorized).toObject()) + next() + } + + oneConnect( + Actions.USER_INFO, + getDefaultParamsOfOpennebulaCommand(Actions.USER_INFO, GET), + (err, value) => { + loginUser(err, value, success, error) + }, + false + ) + } else { + next() + } +} + +const authApi = { + auth +} +module.exports = authApi diff --git a/src/fireedge/src/server/routes/api/auth/auth.js b/src/fireedge/src/server/routes/api/auth/auth.js index b74a02feb7..085e40fd9f 100644 --- a/src/fireedge/src/server/routes/api/auth/auth.js +++ b/src/fireedge/src/server/routes/api/auth/auth.js @@ -1,108 +1,54 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ -const { Map } = require('immutable') -const { - login, - getUser, - getPass, - setUser, - setPass, - setType, - setTfaToken, - setExtended, - setNext, - setReq, - setRes, - setNodeConnect, - connectOpennebula, - updaterResponse -} = require('./functions') -const { internalServerError } = require('server/utils/constants/http-codes') -const { Actions } = require('server/utils/constants/commands/user') -const { - httpMethod -} = require('server/utils/constants/defaults') -const { - paramsDefaultByCommandOpennebula, - responseOpennebula, - checkOpennebulaCommand -} = require('server/utils/opennebula') -const { from } = require('server/utils/constants/defaults') +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ -const { POST, GET } = httpMethod +const { httpMethod, from: fromData } = require('server/utils/constants/defaults') +const { auth } = require('./auth-functions') +const { POST } = httpMethod -const auth = (req, res, next, connect) => { - if (req && res && connect) { - setReq(req) - setRes(res) - setNext(next) - updaterResponse(new Map(internalServerError).toObject()) - const getOpennebulaMethod = checkOpennebulaCommand( - Actions.USER_LOGIN, - POST - ) - if (getOpennebulaMethod) { - setUser( - (req && - from.postBody && - req[from.postBody] && - req[from.postBody].user) || - '' - ) - setPass( - (req && - from.postBody && - req[from.postBody] && - req[from.postBody].token) || - '' - ) - setType( - (req && - from.postBody && - req[from.postBody] && - req[from.postBody].type && - req[from.postBody].type.toLowerCase()) || - '' - ) - setTfaToken( - (req && req[from.postBody] && req[from.postBody].token2fa) || '' - ) - setExtended( - (req && req[from.postBody] && req[from.postBody].extended) || '' - ) - setNodeConnect(connect) - if (getUser() && getPass()) { - const oneConnect = connectOpennebula() - oneConnect( - Actions.USER_INFO, - paramsDefaultByCommandOpennebula(Actions.USER_INFO, GET), - (err, value) => { - responseOpennebula(updaterResponse, err, value, login, next) - }, - false - ) +const routes = { + [POST]: { + null: { + action: auth, + params: { + user: { + from: fromData.postBody, + name: 'user' + }, + token: { + from: fromData.postBody, + name: 'token' + }, + type: { + from: fromData.postBody, + name: 'type' + }, + token2fa: { + from: fromData.postBody, + name: 'token2fa' + }, + extended: { + from: fromData.postBody, + name: 'extended' + } } - } else { - next() } - } else { - next() } } const authApi = { - auth + routes } module.exports = authApi diff --git a/src/fireedge/src/server/routes/api/auth/functions.js b/src/fireedge/src/server/routes/api/auth/functions.js index f1ff8fc8c9..aad7dc3c5b 100644 --- a/src/fireedge/src/server/routes/api/auth/functions.js +++ b/src/fireedge/src/server/routes/api/auth/functions.js @@ -1,19 +1,20 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ + const { DateTime } = require('luxon') -const { Map } = require('immutable') // eslint-disable-next-line node/no-deprecated-api const { parse } = require('url') const { global, Array } = require('window-or-global') @@ -23,7 +24,8 @@ const { httpMethod, defaultOpennebulaExpiration, default2FAOpennebulaVar, - defaultNamespace + defaultNamespace, + defaultEmptyFunction } = require('server/utils/constants/defaults') const { getConfig } = require('server/utils/yml') const { @@ -32,21 +34,18 @@ const { accepted, internalServerError } = require('server/utils/constants/http-codes') -const { createToken } = require('server/utils/jwt') +const { createJWT, check2Fa } = require('server/utils/jwt') const { httpResponse, encrypt, getSunstoneAuth } = require('server/utils/server') const { responseOpennebula, - checkOpennebulaCommand, - check2Fa + getDefaultParamsOfOpennebulaCommand } = require('server/utils/opennebula') const appConfig = getConfig() const namespace = appConfig.namespace || defaultNamespace -const { POST } = httpMethod - -const getOpennebulaMethod = checkOpennebulaCommand(ActionUsers.USER_LOGIN, POST) +const { GET } = httpMethod let user = '' let key = '' @@ -55,75 +54,174 @@ let pass = '' let type = '' let tfatoken = '' let extended = '' -let next = () => undefined +let next = defaultEmptyFunction let req = {} let res = {} -let nodeConnect = () => undefined +let nodeConnect = defaultEmptyFunction let now = '' let nowUnix = '' let nowWithMinutes = '' let relativeTime = '' -const dataSourceWithExpirateDate = () => Map(req).toObject() - +/** + * Get key opennebula. + * + * @returns {string} get key + */ const getKey = () => key + +/** + * Get initialization vector. + * + * @returns {string} get initialization vector + */ const getIV = () => iv + +/** + * Get user opennebula. + * + * @returns {string} user opennebula + */ const getUser = () => user + +/** + * Get user password openebula. + * + * @returns {string} get password user opennebula + */ const getPass = () => pass + +/** + * Get relative time. + * + * @returns {string} date + */ const getRelativeTime = () => relativeTime +/** + * Opennebula encode-decode key. + * + * @param {string} newKey - new key + * @returns {string} get key + */ const setKey = newKey => { key = newKey return key } +/** + * Initialization vector (encrypt). + * + * @param {string} newIV - //16 characters + * @returns {string} get IV + */ const setIV = newIV => { iv = newIV return iv } +/** + * Username opennebula. + * + * @param {string} newUser - new user data + * @returns {string} get user + */ const setUser = newUser => { user = newUser return user } +/** + * User password opennebula. + * + * @param {string} newPass - set new opennebula password user + * @returns {string} password user + */ const setPass = newPass => { pass = newPass return pass } +/** + * Type app. + * + * @param {string} newtype - new type (application) + * @returns {string} get type + */ const setType = newtype => { type = newtype return type } +/** + * Set 2FA token. + * + * @param {string} newTfaToken - new TFA token + * @returns {string} get TFA token + */ const setTfaToken = newTfaToken => { tfatoken = newTfaToken return tfatoken } +/** + * Set extended. + * + * @param {boolean} newExtended - new extended + * @returns {boolean} extended + */ const setExtended = newExtended => { extended = newExtended return extended } -const setNext = newNext => { + +/** + * Set express stepper. + * + * @param {Function} newNext - new stepper + * @returns {Function} http response + */ +const setNext = (newNext = defaultEmptyFunction) => { next = newNext return next } -const setReq = newReq => { + +/** + * Set http resquest. + * + * @param {object} newReq - new request + * @returns {object} http resquest + */ +const setReq = (newReq = {}) => { req = newReq return req } -const setNodeConnect = newConnect => { + +/** + * Set xlmrpc connection function. + * + * @param {Function} newConnect - new connect opennebula + * @returns {Function} xmlrpc function + */ +const setNodeConnect = (newConnect = defaultEmptyFunction) => { nodeConnect = newConnect return nodeConnect } -const setRes = newRes => { +/** + * Set http response. + * + * @param {object} newRes - new response + * @returns {object} http response + */ +const setRes = (newRes = {}) => { res = newRes return res } +/** + * Set dates. + */ const setDates = () => { const limitToken = appConfig.opennebula_expiration || defaultOpennebulaExpiration now = DateTime.local() @@ -133,12 +231,24 @@ const setDates = () => { relativeTime = diff.seconds } +/** + * Connect to function xmlrpc. + * + * @param {string} usr - user + * @param {string} pss - password + * @returns {Function} xmlrpc function + */ const connectOpennebula = (usr = '', pss = '') => { const connectUser = usr || user const connectPass = pss || pass return nodeConnect(connectUser, connectPass) } +/** + * Updater http request. + * + * @param {string} code - http code + */ const updaterResponse = code => { if ( 'id' in code && @@ -151,6 +261,12 @@ const updaterResponse = code => { } } +/** + * Validate 2FA. + * + * @param {object} informationUser - user data + * @returns {boolean} return if data is valid + */ const validate2faAuthentication = informationUser => { let rtn = false if ( @@ -182,12 +298,18 @@ const validate2faAuthentication = informationUser => { return rtn } +/** + * Generate a JWT. + * + * @param {string} token - opennebula token + * @param {object} informationUser - user data + */ const genJWT = (token, informationUser) => { if (token && token.token && informationUser && informationUser.ID && informationUser.NAME) { const { ID: id, TEMPLATE: userTemplate, NAME: user } = informationUser const dataJWT = { id, user, token: token.token } const addTime = token.expiration_time || nowWithMinutes.toSeconds() - const jwt = createToken(dataJWT, nowUnix, addTime) + const jwt = createJWT(dataJWT, nowUnix, addTime) if (jwt) { if (!global.users) { global.users = {} @@ -202,13 +324,15 @@ const genJWT = (token, informationUser) => { } } +/** + * Get zones. + */ const setZones = () => { if (global && !global.zones) { const oneConnect = connectOpennebula() - const dataSource = dataSourceWithExpirateDate() oneConnect( ActionZones.ZONEPOOL_INFO, - getOpennebulaMethod(dataSource), + getDefaultParamsOfOpennebulaCommand(ActionZones.ZONEPOOL_INFO, GET), (err, value) => { // res, err, value, response, next responseOpennebula( @@ -245,6 +369,13 @@ const setZones = () => { } } +/** + * Create token server admin. + * + * @param {string} serverAdmin - serveradmin name + * @param {string} username - user name + * @returns {string} data encrypted serveradmin + */ const createTokenServerAdmin = (serverAdmin = '', username = '') => { let rtn const key = getKey() @@ -259,6 +390,12 @@ const createTokenServerAdmin = (serverAdmin = '', username = '') => { return rtn } +/** + * Wrap user with serveradmin. + * + * @param {object} serverAdminData - opennebula serveradmin data + * @param {object} userData - opennebula user data + */ const wrapUserWithServerAdmin = (serverAdminData = {}, userData = {}) => { const relativeTime = getRelativeTime() let serverAdminName = '' @@ -303,6 +440,11 @@ const wrapUserWithServerAdmin = (serverAdminData = {}, userData = {}) => { } } +/** + * Get server admin and wrap user. + * + * @param {object} userData - opennebula user data + */ const getServerAdminAndWrapUser = (userData = {}) => { const serverAdminData = getSunstoneAuth() if (serverAdminData && @@ -316,7 +458,7 @@ const getServerAdminAndWrapUser = (userData = {}) => { const oneConnect = connectOpennebula(`${serverAdminData.username}:${serverAdminData.username}`, tokenWithServerAdmin) oneConnect( ActionUsers.USER_INFO, - [-1, false], + getDefaultParamsOfOpennebulaCommand(ActionUsers.USER_INFO, GET), (err, value) => { responseOpennebula( updaterResponse, @@ -330,6 +472,11 @@ const getServerAdminAndWrapUser = (userData = {}) => { } } +/** + * Login route function. + * + * @param {object} userData - opennebula user data + */ const login = userData => { let rtn = false if (userData) { diff --git a/src/fireedge/src/server/routes/api/auth/index.js b/src/fireedge/src/server/routes/api/auth/index.js index 5f57ad3645..b1723da303 100644 --- a/src/fireedge/src/server/routes/api/auth/index.js +++ b/src/fireedge/src/server/routes/api/auth/index.js @@ -1,36 +1,59 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ -const { AUTH } = require('./string-routes') -const { httpMethod } = require('server/utils/constants/defaults') -const { auth } = require('./auth') +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ -const { POST } = httpMethod +const { addFunctionAsRoute, setFunctionRoute } = require('server/utils/server') +const { routes: authRoutes } = require('./auth') +const { AUTH } = require('./string-routes') const privateRoutes = [] +const publicRoutes = [] -const publicRoutes = [ - { - httpMethod: POST, - endpoint: AUTH, - action: auth +/** + * Set private routes. + * + * @param {object} routes - object of routes + * @param {string} path - principal route + * @param {Function} action - function of route + */ +const setPublicRoutes = (routes = {}, path = '', action = () => undefined) => { + if (Object.keys(routes).length > 0 && routes.constructor === Object) { + Object.keys(routes).forEach((route) => { + publicRoutes.push( + setFunctionRoute(route, path, + (req, res, next, connection, userId, user) => { + action(req, res, next, routes[route], user, connection) + }) + ) + }) } -] +} + +/** + * Add routes. + * + * @returns {Array} routes + */ +const generatePublicRoutes = () => { + setPublicRoutes(authRoutes, AUTH, addFunctionAsRoute) + return publicRoutes +} const functionRoutes = { private: privateRoutes, - public: publicRoutes + public: generatePublicRoutes() } module.exports = functionRoutes diff --git a/src/fireedge/src/server/routes/api/auth/string-routes.js b/src/fireedge/src/server/routes/api/auth/string-routes.js index dc46f7319d..f209c20655 100644 --- a/src/fireedge/src/server/routes/api/auth/string-routes.js +++ b/src/fireedge/src/server/routes/api/auth/string-routes.js @@ -1,22 +1,23 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ -const AUTH = 'auth'; +const AUTH = 'auth' const Actions = { AUTH -}; +} -module.exports = Actions; +module.exports = Actions diff --git a/src/fireedge/src/server/routes/api/index.js b/src/fireedge/src/server/routes/api/index.js index 65e1ae0315..3ae33cece9 100644 --- a/src/fireedge/src/server/routes/api/index.js +++ b/src/fireedge/src/server/routes/api/index.js @@ -1,18 +1,18 @@ -/* eslint-disable import/no-dynamic-require */ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { messageTerminal } = require('server/utils/general') const { getRouteForOpennebulaCommand } = require('server/utils/opennebula') diff --git a/src/fireedge/src/server/routes/api/oneflow/functions.js b/src/fireedge/src/server/routes/api/oneflow/functions.js index 63ca6bc4e7..40d908478c 100644 --- a/src/fireedge/src/server/routes/api/oneflow/functions.js +++ b/src/fireedge/src/server/routes/api/oneflow/functions.js @@ -1,17 +1,19 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ + const { request: axios } = require('axios') const btoa = require('btoa') @@ -24,16 +26,35 @@ const { GET, DELETE } = httpMethod const appConfig = getConfig() +/** + * Return schema error. + * + * @param {string} error - get error for schema + * @returns {string} schema + */ const returnSchemaError = (error = []) => error .map(element => (element && element.stack ? element.stack : '')) .toString() +/** + * Parse number to int. + * + * @param {string} validate - number to validate + * @returns {number}number to int + */ const parseToNumber = validate => isNaN(parseInt(validate, 10)) ? validate : parseInt(validate, 10) +/** + * Connection to one flow server. + * + * @param {object} requestData - data for request + * @param {Function} success - callback success function + * @param {Function} error - callback error function + */ const oneFlowConection = (requestData = {}, success = () => undefined, error = () => undefined) => { const { method, path, user, password, request, post } = requestData const optionMethod = method || GET diff --git a/src/fireedge/src/server/routes/api/oneflow/index.js b/src/fireedge/src/server/routes/api/oneflow/index.js index 9be0eaa0bb..631e4fd77f 100644 --- a/src/fireedge/src/server/routes/api/oneflow/index.js +++ b/src/fireedge/src/server/routes/api/oneflow/index.js @@ -1,19 +1,20 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ -const { addFunctionToRoute, fillFunctionRoute } = require('server/utils/server') +const { addFunctionAsRoute, setFunctionRoute } = require('server/utils/server') const { routes: serviceRoutes } = require('./service') const { routes: serviceTemplateRoutes } = require('./service_template') @@ -22,11 +23,18 @@ const { SERVICE, SERVICE_TEMPLATE } = require('./string-routes') const privateRoutes = [] const publicRoutes = [] -const fillPrivateRoutes = (methods = {}, path = '', action = () => undefined) => { +/** + * Set private routes. + * + * @param {object} methods - object of routes + * @param {string} path - principal route + * @param {Function} action - function of route + */ +const setPrivateRoutes = (methods = {}, path = '', action = () => undefined) => { if (Object.keys(methods).length > 0 && methods.constructor === Object) { Object.keys(methods).forEach((method) => { privateRoutes.push( - fillFunctionRoute(method, path, + setFunctionRoute(method, path, (req, res, next, connection, userId, user) => { action(req, res, next, methods[method], user, connection) }) @@ -35,9 +43,14 @@ const fillPrivateRoutes = (methods = {}, path = '', action = () => undefined) => } } +/** + * Add routes. + * + * @returns {Array} routes + */ const generatePrivateRoutes = () => { - fillPrivateRoutes(serviceRoutes, SERVICE, addFunctionToRoute) - fillPrivateRoutes(serviceTemplateRoutes, SERVICE_TEMPLATE, addFunctionToRoute) + setPrivateRoutes(serviceRoutes, SERVICE, addFunctionAsRoute) + setPrivateRoutes(serviceTemplateRoutes, SERVICE_TEMPLATE, addFunctionAsRoute) return privateRoutes } diff --git a/src/fireedge/src/server/routes/api/oneflow/schemas.js b/src/fireedge/src/server/routes/api/oneflow/schemas.js index e976f854db..d9ee80881b 100644 --- a/src/fireedge/src/server/routes/api/oneflow/schemas.js +++ b/src/fireedge/src/server/routes/api/oneflow/schemas.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const action = { id: '/Action', diff --git a/src/fireedge/src/server/routes/api/oneflow/service-functions.js b/src/fireedge/src/server/routes/api/oneflow/service-functions.js index f13213df70..fb48597a5a 100644 --- a/src/fireedge/src/server/routes/api/oneflow/service-functions.js +++ b/src/fireedge/src/server/routes/api/oneflow/service-functions.js @@ -1,22 +1,23 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { Validator } = require('jsonschema') const { action } = require('./schemas') const { oneFlowConection } = require('./functions') -const { httpMethod } = require('server/utils/constants/defaults') +const { httpMethod, defaultEmptyFunction } = require('server/utils/constants/defaults') const { httpResponse, parsePostData } = require('server/utils/server') const { ok, @@ -26,41 +27,69 @@ const { const { returnSchemaError } = require('./functions') const { GET, POST, DELETE } = httpMethod -const service = (res = {}, next = () => undefined, params = {}, userData = {}) => { - const { user, password } = userData - const success = data => { +/** + * Response http when have information. + * + * @param {Function} next - express stepper + * @param {object} res - response http + * @param {string} data - data response http + */ +const success = (next = defaultEmptyFunction, res = {}, data = '') => { + if ((next && typeof next === 'function') && (res && res.locals && res.locals.httpCode)) { res.locals.httpCode = httpResponse(ok, data) next() } - const error = data => { +} + +/** + * Response http when no have information. + * + * @param {Function} next - express stepper + * @param {object} res - response http + * @param {string} data - data response http + */ +const error = (next = defaultEmptyFunction, res = {}, data = '') => { + if ((next && typeof next === 'function') && (res && res.locals && res.locals.httpCode)) { res.locals.httpCode = httpResponse(internalServerError, data && data.message) next() } +} + +/** + * Get service. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params + * @param {object} userData - user data + */ +const service = (res = {}, next = defaultEmptyFunction, params = {}, userData = {}) => { + const { user, password } = userData if (user && password) { const config = { method: GET, path: '/service', user, password } if (params && params.id) { config.path = '/service/{0}' config.request = params.id - oneFlowConection(config, success, error) + oneFlowConection(config, data => success(next, res, data), data => error(next, res, data)) } else { - oneFlowConection(config, success, error) + oneFlowConection(config, data => success(next, res, data), data => error(next, res, data)) } } } -const serviceDelete = (res = {}, next = () => undefined, params = {}, userData = {}) => { +/** + * Delete service. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params + * @param {object} userData - user data + */ +const serviceDelete = (res = {}, next = defaultEmptyFunction, params = {}, userData = {}) => { const { user, password } = userData - const success = data => { - res.locals.httpCode = httpResponse(ok, data) - next() - } - const error = data => { - res.locals.httpCode = httpResponse(internalServerError, data && data.message) - next() - } if (params && params.id && user && password) { const config = { method: DELETE, path: '/service/{0}', user, password, request: params.id } - oneFlowConection(config, success, error) + oneFlowConection(config, data => success(next, res, data), data => error(next, res, data)) } else { res.locals.httpCode = httpResponse( methodNotAllowed, @@ -71,16 +100,16 @@ const serviceDelete = (res = {}, next = () => undefined, params = {}, userData = } } -const serviceAddAction = (res = {}, next = () => undefined, params = {}, userData = {}) => { +/** + * Add action service. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params + * @param {object} userData - user data + */ +const serviceAddAction = (res = {}, next = defaultEmptyFunction, params = {}, userData = {}) => { const { user, password } = userData - const success = data => { - res.locals.httpCode = httpResponse(ok, data) - next() - } - const error = data => { - res.locals.httpCode = httpResponse(internalServerError, data && data.message) - next() - } if (params && params.id && params.action && user && password) { const v = new Validator() const postAction = parsePostData(params.action) @@ -94,7 +123,7 @@ const serviceAddAction = (res = {}, next = () => undefined, params = {}, userDat request: params.id, post: postAction } - oneFlowConection(config, success, error) + oneFlowConection(config, data => success(next, res, data), data => error(next, res, data)) } else { res.locals.httpCode = httpResponse( internalServerError, @@ -113,16 +142,16 @@ const serviceAddAction = (res = {}, next = () => undefined, params = {}, userDat } } -const serviceAddScale = (res = {}, next = () => undefined, params = {}, userData = {}) => { +/** + * Add scale service. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params + * @param {object} userData - user data + */ +const serviceAddScale = (res = {}, next = defaultEmptyFunction, params = {}, userData = {}) => { const { user, password } = userData - const success = data => { - res.locals.httpCode = httpResponse(ok, data) - next() - } - const error = data => { - res.locals.httpCode = httpResponse(internalServerError, data && data.message) - next() - } if (params && params.id && params.action && user && password) { const v = new Validator() const postAction = parsePostData(params.action) @@ -136,7 +165,7 @@ const serviceAddScale = (res = {}, next = () => undefined, params = {}, userData request: params.id, post: postAction } - oneFlowConection(config, success, error) + oneFlowConection(config, data => success(next, res, data), data => error(next, res, data)) } else { res.locals.httpCode = httpResponse( internalServerError, @@ -155,16 +184,16 @@ const serviceAddScale = (res = {}, next = () => undefined, params = {}, userData } } -const serviceAddRoleAction = (res = {}, next = () => undefined, params = {}, userData = {}) => { +/** + * Add service role action. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params + * @param {object} userData - user data + */ +const serviceAddRoleAction = (res = {}, next = defaultEmptyFunction, params = {}, userData = {}) => { const { user, password } = userData - const success = data => { - res.locals.httpCode = httpResponse(ok, data) - next() - } - const error = data => { - res.locals.httpCode = httpResponse(internalServerError, data && data.message) - next() - } if (params && params.role && params.id && params.action && user && password) { const v = new Validator() const postAction = parsePostData(params.action) @@ -178,7 +207,7 @@ const serviceAddRoleAction = (res = {}, next = () => undefined, params = {}, use request: [params.role, params.id], post: postAction } - oneFlowConection(config, success, error) + oneFlowConection(config, data => success(next, res, data), data => error(next, res, data)) } else { res.locals.httpCode = httpResponse( internalServerError, diff --git a/src/fireedge/src/server/routes/api/oneflow/service.js b/src/fireedge/src/server/routes/api/oneflow/service.js index 3ec3a08446..4fa91e4c27 100644 --- a/src/fireedge/src/server/routes/api/oneflow/service.js +++ b/src/fireedge/src/server/routes/api/oneflow/service.js @@ -1,20 +1,20 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { httpMethod, from: fromData } = require('server/utils/constants/defaults') -const { getParamsForObject } = require('server/utils/server') const { service, serviceDelete, diff --git a/src/fireedge/src/server/routes/api/oneflow/service_template-functions.js b/src/fireedge/src/server/routes/api/oneflow/service_template-functions.js index f99865479f..98897f3afb 100644 --- a/src/fireedge/src/server/routes/api/oneflow/service_template-functions.js +++ b/src/fireedge/src/server/routes/api/oneflow/service_template-functions.js @@ -1,21 +1,23 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ + const { Validator } = require('jsonschema') const { role, service, action } = require('./schemas') const { oneFlowConection } = require('./functions') -const { httpMethod } = require('server/utils/constants/defaults') +const { httpMethod, defaultEmptyFunction } = require('server/utils/constants/defaults') const { httpResponse, parsePostData } = require('server/utils/server') const { ok, @@ -25,41 +27,71 @@ const { const { returnSchemaError } = require('./functions') const { GET, POST, DELETE, PUT } = httpMethod -const serviceTemplate = (res = {}, next = () => undefined, params = {}, userData = {}) => { - const { user, password } = userData - const success = data => { +/** + * Response http when have information. + * + * @param {Function} next - express stepper + * @param {object} res - response http + * @param {string} data - data response http + */ +const success = (next = defaultEmptyFunction, res = {}, data = '') => { + if ((next && typeof next === 'function') && (res && res.locals && res.locals.httpCode)) { res.locals.httpCode = httpResponse(ok, data) next() } - const error = data => { +} + +/** + * Response http when no have information. + * + * @param {Function} next - express stepper + * @param {object} res - response http + * @param {string} data - data response http + */ +const error = (next = defaultEmptyFunction, res = {}, data = '') => { + if ((next && typeof next === 'function') && (res && res.locals && res.locals.httpCode)) { res.locals.httpCode = httpResponse(internalServerError, data && data.message) next() } +} + +/** + * Get service template. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params + * @param {object} userData - user data + */ +const serviceTemplate = (res = {}, next = () => undefined, params = {}, userData = {}) => { + const { user, password } = userData + if (user && password) { const config = { method: GET, path: '/service_template', user, password } if (params && params.id) { config.path = '/service_template/{0}' config.request = params.id - oneFlowConection(config, success, error) + oneFlowConection(config, data => success(next, res, data), data => error(next, res, data)) } else { - oneFlowConection(config, success, error) + oneFlowConection(config, data => success(next, res, data), data => error(next, res, data)) } } } +/** + * Delete service template. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params + * @param {object} userData - user data + */ const serviceTemplateDelete = (res = {}, next = () => undefined, params = {}, userData = {}) => { const { user, password } = userData - const success = data => { - res.locals.httpCode = httpResponse(ok, data) - next() - } - const error = data => { - res.locals.httpCode = httpResponse(internalServerError, data && data.message) - next() - } + if (params && params.id && user && password) { const config = { method: DELETE, path: '/service_template/{0}', user, password, request: params.id } - oneFlowConection(config, success, error) + oneFlowConection(config, data => success(next, res, data), data => error(next, res, data)) } else { res.locals.httpCode = httpResponse( methodNotAllowed, @@ -70,16 +102,16 @@ const serviceTemplateDelete = (res = {}, next = () => undefined, params = {}, us } } +/** + * Create service template. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params + * @param {object} userData - user data + */ const serviceTemplateCreate = (res = {}, next = () => undefined, params = {}, userData = {}) => { const { user, password } = userData - const success = data => { - res.locals.httpCode = httpResponse(ok, data) - next() - } - const error = data => { - res.locals.httpCode = httpResponse(internalServerError, data && data.message) - next() - } if (params && params.template && user && password) { const v = new Validator() const template = parsePostData(params.template) @@ -93,7 +125,7 @@ const serviceTemplateCreate = (res = {}, next = () => undefined, params = {}, us password, post: template } - oneFlowConection(config, success, error) + oneFlowConection(config, data => success(next, res, data), data => error(next, res, data)) } else { res.locals.httpCode = httpResponse( internalServerError, @@ -111,16 +143,16 @@ const serviceTemplateCreate = (res = {}, next = () => undefined, params = {}, us } } +/** + * Update service template. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params + * @param {object} userData - user data + */ const serviceTemplateUpdate = (res = {}, next = () => undefined, params = {}, userData = {}) => { const { user, password } = userData - const success = data => { - res.locals.httpCode = httpResponse(ok, data) - next() - } - const error = data => { - res.locals.httpCode = httpResponse(internalServerError, data && data.message) - next() - } if (params && params.id && params.template && user && password) { const v = new Validator() v.addSchema(role, '/Role') @@ -135,7 +167,7 @@ const serviceTemplateUpdate = (res = {}, next = () => undefined, params = {}, us request: params.id, post: template } - oneFlowConection(config, success, error) + oneFlowConection(config, data => success(next, res, data), data => error(next, res, data)) } else { res.locals.httpCode = httpResponse( internalServerError, @@ -154,16 +186,16 @@ const serviceTemplateUpdate = (res = {}, next = () => undefined, params = {}, us } } +/** + * Add service action. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params + * @param {object} userData - user data + */ const serviceTemplateAction = (res = {}, next = () => undefined, params = {}, userData = {}) => { const { user, password } = userData - const success = data => { - res.locals.httpCode = httpResponse(ok, data) - next() - } - const error = data => { - res.locals.httpCode = httpResponse(internalServerError, data && data.message) - next() - } if (params && params.id && params.template && user && password) { const v = new Validator() const template = parsePostData(params.template) @@ -177,7 +209,7 @@ const serviceTemplateAction = (res = {}, next = () => undefined, params = {}, us request: params.id, post: template } - oneFlowConection(config, success, error) + oneFlowConection(config, data => success(next, res, data), data => error(next, res, data)) } else { res.locals.httpCode = httpResponse( internalServerError, diff --git a/src/fireedge/src/server/routes/api/oneflow/service_template.js b/src/fireedge/src/server/routes/api/oneflow/service_template.js index be374a1291..1a3e80a844 100644 --- a/src/fireedge/src/server/routes/api/oneflow/service_template.js +++ b/src/fireedge/src/server/routes/api/oneflow/service_template.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { httpMethod, from: fromData } = require('server/utils/constants/defaults') const { diff --git a/src/fireedge/src/server/routes/api/oneflow/string-routes.js b/src/fireedge/src/server/routes/api/oneflow/string-routes.js index 8e31f43200..3eed3f8fff 100644 --- a/src/fireedge/src/server/routes/api/oneflow/string-routes.js +++ b/src/fireedge/src/server/routes/api/oneflow/string-routes.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const SERVICE = 'service' const SERVICE_TEMPLATE = 'service_template' diff --git a/src/fireedge/src/server/routes/api/provision/functions.js b/src/fireedge/src/server/routes/api/provision/functions.js index 97840cb3f1..bb3c309cde 100644 --- a/src/fireedge/src/server/routes/api/provision/functions.js +++ b/src/fireedge/src/server/routes/api/provision/functions.js @@ -1,17 +1,19 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ + const { v4 } = require('uuid') const { dirname, basename } = require('path') // eslint-disable-next-line node/no-deprecated-api @@ -31,16 +33,28 @@ const { const { getConfig } = require('server/utils/yml') const { spawnSync, spawn } = require('child_process') const { messageTerminal } = require('server/utils/general') -const { defaultError, getDirectories } = require('server/utils/server') +const { defaultError } = require('server/utils/server') const eventsEmitter = new events.EventEmitter() +/** + * Create a event emiter. + * + * @param {string} eventName - name event + * @param {object} message - object message + */ const publish = (eventName = '', message = {}) => { if (eventName && message) { eventsEmitter.emit(eventName, message) } } +/** + * Subscriber to event emitter. + * + * @param {string} eventName - event name + * @param {Function} callback - function executed when event is emited + */ const subscriber = (eventName = '', callback = () => undefined) => { if (eventName && callback && @@ -56,6 +70,14 @@ const subscriber = (eventName = '', callback = () => undefined) => { } } +/** + * Create folder with files. + * + * @param {string} path - path to create files + * @param {Array} files - files to create + * @param {string} filename - return name + * @returns {object} object with files created + */ const createFolderWithFiles = (path = '', files = [], filename = '') => { const rtn = { name: '', files: [] } const name = filename || v4().replace(/-/g, '').toUpperCase() @@ -68,9 +90,8 @@ const createFolderWithFiles = (path = '', files = [], filename = '') => { if (files && Array.isArray(files)) { files.forEach(file => { if (file && file.name && file.ext) { - const filePath = `${internalPath}/${file.name}.${file.ext}` - rtn.files.push({ name: file.name, ext: file.ext, path: filePath }) - writeFileSync(filePath, (file && file.content) || '') + rtn.files.push({ name: file.name, ext: file.ext, path: `${internalPath}/${file.name}.${file.ext}` }) + createTemporalFile(internalPath, file.ext, (file && file.content) || '', file.name) } }) } @@ -80,6 +101,15 @@ const createFolderWithFiles = (path = '', files = [], filename = '') => { return rtn } +/** + * Create a temporal file. + * + * @param {string} path - path of temporal file + * @param {string} ext - extension of the temporal file + * @param {string} content - content of the temporal file + * @param {string} filename - name of the temporal file + * @returns {object} if file is created + */ const createTemporalFile = (path = '', ext = '', content = '', filename = '') => { let rtn const name = filename || v4().replace(/-/g, '').toUpperCase() @@ -96,6 +126,12 @@ const createTemporalFile = (path = '', ext = '', content = '', filename = '') => return rtn } +/** + * Parse content of yaml to json. + * + * @param {string} content - content yaml + * @returns {string} data yaml in json + */ const createYMLContent = (content = '') => { let rtn try { @@ -114,6 +150,11 @@ const createYMLContent = (content = '') => { return rtn } +/** + * Delete file. + * + * @param {string} path - the path for delete + */ const removeFile = (path = '') => { if (path) { try { @@ -124,6 +165,15 @@ const removeFile = (path = '') => { } } +/** + * Rename folder. + * + * @param {string} path - path of folder + * @param {string} name - new name of folder + * @param {string} type - must be "prepend", "append" or "replace" + * @param {Function} callback - function that runs before renaming the folder + * @returns {string} if the folder name was changed + */ const renameFolder = (path = '', name = '', type = 'replace', callback) => { let rtn = false if (path) { @@ -158,6 +208,13 @@ const renameFolder = (path = '', name = '', type = 'replace', callback) => { return rtn } +/** + * Move file to path. + * + * @param {string} path - path of file + * @param {string} relative - relative path + * @returns {boolean} check if file is moved + */ const moveToFolder = (path = '', relative = '/../') => { let rtn = false if (path && relative) { @@ -171,6 +228,13 @@ const moveToFolder = (path = '', relative = '/../') => { return rtn } +/** + * Add prepend of command example: 'ssh xxxx:'". + * + * @param {string} command - cli command + * @param {string} resource - resource by command + * @returns {object} command and resource + */ const addPrependCommand = (command = '', resource = '') => { const appConfig = getConfig() const prependCommand = appConfig.oneprovision_prepend_command || '' @@ -197,12 +261,24 @@ const addPrependCommand = (command = '', resource = '') => { } } +/** + * Add command optional params from fireedge server config. + * + * @returns {Array} command optional params + */ const addOptionalCreateCommand = () => { const appConfig = getConfig() const optionalCreateCommand = appConfig.oneprovision_optional_create_command || '' - return [optionalCreateCommand].filter(Boolean) + return [optionalCreateCommand] } +/** + * Run Asynchronous commands for CLI. + * + * @param {string} command - command to execute + * @param {string} resource - params for the command to execute + * @param {object} callbacks - the functions in case the command emits by the stderr(err), stdout(out) and when it finishes(close) + */ const executeCommandAsync = ( command = '', resource = '', @@ -241,6 +317,14 @@ const executeCommandAsync = ( } } +/** + * Run Synchronous commands for CLI. + * + * @param {string} command - command to execute + * @param {string} resource - params for the command to execute + * @param {object} options - optional params for the command + * @returns {object} CLI output + */ const executeCommand = (command = '', resource = '', options = {}) => { let rtn = { success: false, data: null } const { cmd, rsc } = addPrependCommand(command, resource) @@ -256,6 +340,14 @@ const executeCommand = (command = '', resource = '', options = {}) => { return rtn } +/** + * Get recursive folder from a directory. + * + * @param {string} path - path to create files + * @param {string} finder - finder path + * @param {boolean} rtn - return of function (recursive) + * @returns {string} paths + */ const findRecursiveFolder = (path = '', finder = '', rtn = false) => { if (path && finder) { try { @@ -277,6 +369,11 @@ const findRecursiveFolder = (path = '', finder = '', rtn = false) => { return rtn } +/** + * Get the xmlrpc connection endpoint. + * + * @returns {Array} array endpoint xmlrpc connection + */ const getEndpoint = () => { let rtn = [] const appConfig = getConfig() diff --git a/src/fireedge/src/server/routes/api/provision/index.js b/src/fireedge/src/server/routes/api/provision/index.js index db41613347..7085df3627 100644 --- a/src/fireedge/src/server/routes/api/provision/index.js +++ b/src/fireedge/src/server/routes/api/provision/index.js @@ -1,18 +1,20 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ -const { addFunctionToRoute, fillFunctionRoute } = require('server/utils/server') +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ + +const { addFunctionAsRoute, setFunctionRoute } = require('server/utils/server') const { routes: provisionRoutes } = require('./provision') const { routes: provisionTemplateRoutes } = require('./provision_template') const { routes: providerRoutes } = require('./provider') @@ -22,23 +24,35 @@ const { PROVIDER, PROVISION, PROVISION_TEMPLATE } = require('./string-routes') const privateRoutes = [] const publicRoutes = [] -const fillPrivateRoutes = (methods = {}, path = '', action = () => undefined) => { - if (Object.keys(methods).length > 0 && methods.constructor === Object) { - Object.keys(methods).forEach((method) => { +/** + * Set private routes. + * + * @param {object} routes - object of routes + * @param {string} path - principal route + * @param {Function} action - function of route + */ +const setPrivateRoutes = (routes = {}, path = '', action = () => undefined) => { + if (Object.keys(routes).length > 0 && routes.constructor === Object) { + Object.keys(routes).forEach((route) => { privateRoutes.push( - fillFunctionRoute(method, path, + setFunctionRoute(route, path, (req, res, next, connection, userId, user) => { - action(req, res, next, methods[method], user, connection) + action(req, res, next, routes[route], user, connection) }) ) }) } } +/** + * Add routes. + * + * @returns {Array} routes + */ const generatePrivateRoutes = () => { - fillPrivateRoutes(provisionRoutes, PROVISION, addFunctionToRoute) - fillPrivateRoutes(provisionTemplateRoutes, PROVISION_TEMPLATE, addFunctionToRoute) - fillPrivateRoutes(providerRoutes, PROVIDER, addFunctionToRoute) + setPrivateRoutes(provisionRoutes, PROVISION, addFunctionAsRoute) + setPrivateRoutes(provisionTemplateRoutes, PROVISION_TEMPLATE, addFunctionAsRoute) + setPrivateRoutes(providerRoutes, PROVIDER, addFunctionAsRoute) return privateRoutes } diff --git a/src/fireedge/src/server/routes/api/provision/provider-functions.js b/src/fireedge/src/server/routes/api/provision/provider-functions.js index 745745aec4..b08f79fc0a 100644 --- a/src/fireedge/src/server/routes/api/provision/provider-functions.js +++ b/src/fireedge/src/server/routes/api/provision/provider-functions.js @@ -1,24 +1,26 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { Validator } = require('jsonschema') const { defaultFolderTmpProvision, defaultCommandProvider, defaultHideCredentials, - defaultHideCredentialReplacer + defaultHideCredentialReplacer, + defaultEmptyFunction } = require('server/utils/constants/defaults') const { @@ -31,7 +33,15 @@ const { provider, providerUpdate } = require('./schemas') const httpInternalError = httpResponse(internalServerError, '', '') -const getConnectionProviders = (res = {}, next = () => undefined, params = {}, userData = {}) => { +/** + * Get connection providers. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {object} userData - user of http request + */ +const getConnectionProviders = (res = {}, next = defaultEmptyFunction, params = {}, userData = {}) => { const { user, password } = userData let rtn = httpInternalError if (user && password) { @@ -63,7 +73,15 @@ const getConnectionProviders = (res = {}, next = () => undefined, params = {}, u next() } -const getListProviders = (res = {}, next = () => undefined, params = {}, userData = {}) => { +/** + * Get list providers. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {object} userData - user of http request + */ +const getListProviders = (res = {}, next = defaultEmptyFunction, params = {}, userData = {}) => { const { user, password } = userData let rtn = httpInternalError if (user && password) { @@ -77,12 +95,20 @@ const getListProviders = (res = {}, next = () => undefined, params = {}, userDat try { const response = executedCommand.success ? ok : internalServerError const data = JSON.parse(executedCommand.data) + + /** + * Parse provision.TEMPLATE.PLAIN to JSON. + * + * @param {object} provision - provision + * @returns {object} provision with TEMPLATE.PLAIN in JSON + */ const parseTemplatePlain = provision => { if (provision && provision.TEMPLATE && provision.TEMPLATE.PLAIN) { provision.TEMPLATE.PLAIN = JSON.parse(provision.TEMPLATE.PLAIN) } return provision } + if (data && data.DOCUMENT_POOL && data.DOCUMENT_POOL.DOCUMENT && Array.isArray(data.DOCUMENT_POOL.DOCUMENT)) { data.DOCUMENT_POOL.DOCUMENT = data.DOCUMENT_POOL.DOCUMENT.map(parseTemplatePlain) } @@ -119,7 +145,15 @@ const getListProviders = (res = {}, next = () => undefined, params = {}, userDat next() } -const createProviders = (res = {}, next = () => undefined, params = {}, userData = {}) => { +/** + * Create provider. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {object} userData - user of http request + */ +const createProviders = (res = {}, next = defaultEmptyFunction, params = {}, userData = {}) => { const { user, password } = userData let rtn = httpInternalError if (params && params.resource && user && password) { @@ -164,7 +198,15 @@ const createProviders = (res = {}, next = () => undefined, params = {}, userData next() } -const updateProviders = (res = {}, next = () => undefined, params = {}, userData = {}) => { +/** + * Update provision. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {object} userData - user of http request + */ +const updateProviders = (res = {}, next = defaultEmptyFunction, params = {}, userData = {}) => { const { user, password } = userData let rtn = httpInternalError if (params && params.resource && params.id && user && password) { @@ -200,7 +242,15 @@ const updateProviders = (res = {}, next = () => undefined, params = {}, userData next() } -const deleteProvider = (res = {}, next = () => undefined, params = {}, userData = {}) => { +/** + * Delete provider. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {object} userData - user of http request + */ +const deleteProvider = (res = {}, next = defaultEmptyFunction, params = {}, userData = {}) => { const { user, password } = userData let rtn = httpInternalError if (params && params.id && user && password) { diff --git a/src/fireedge/src/server/routes/api/provision/provider.js b/src/fireedge/src/server/routes/api/provision/provider.js index 299c2d4c61..e36efdb213 100644 --- a/src/fireedge/src/server/routes/api/provision/provider.js +++ b/src/fireedge/src/server/routes/api/provision/provider.js @@ -1,19 +1,20 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ -const { from: fromData, defaultEmptyFunction } = require('server/utils/constants/defaults') -const { getParamsForObject } = require('server/utils/server') +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ + +const { from: fromData } = require('server/utils/constants/defaults') const { getListProviders, getConnectionProviders, diff --git a/src/fireedge/src/server/routes/api/provision/provision-functions.js b/src/fireedge/src/server/routes/api/provision/provision-functions.js index 41fc63731a..46b968285f 100644 --- a/src/fireedge/src/server/routes/api/provision/provision-functions.js +++ b/src/fireedge/src/server/routes/api/provision/provision-functions.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { parse } = require('yaml') const { v4 } = require('uuid') @@ -65,7 +66,7 @@ const logFile = { name: 'stdouterr', ext: 'log' } -const configFile = { +const provisionFile = { name: 'provision', ext: 'yaml' } @@ -77,6 +78,14 @@ const relName = 'provision-mapping' const ext = 'yml' const appendError = '.ERROR' +/** + * Execute command Async and emit in WS. + * + * @param {string} command - command to execute + * @param {object} actions - external functions when command emit in stderr, stdout and finalize + * @param {*} dataForLog - data + * @returns {boolean} check if emmit data + */ const executeWithEmit = (command = [], actions = {}, dataForLog = {}) => { let rtn = false if ( @@ -99,12 +108,23 @@ const executeWithEmit = (command = [], actions = {}, dataForLog = {}) => { let pendingMessages = '' - // send data of command + /** + * Emit data of command. + * + * @param {string} message - line of command CLI + * @param {Function} callback - function when recieve a information + */ const emit = (message, callback = defaultEmptyFunction) => { + /** + * Publisher data to WS. + * + * @param {string} line - command CLI line + */ const publisher = (line = '') => { const resposeData = callback(line, uuid) || { id, data: line, command: commandName, commandId: uuid } publish(defaultCommandProvision, resposeData) } + message.toString().split(regexpSplitLine).map(line => { if (line) { if ( @@ -147,6 +167,13 @@ const executeWithEmit = (command = [], actions = {}, dataForLog = {}) => { return rtn } +/** + * Find log data. + * + * @param {string} id - id of provision + * @param {boolean} fullPath - if need return the path of log + * @returns {object} data of log + */ const logData = (id, fullPath = false) => { let rtn = false if (typeof id !== 'undefined') { @@ -154,9 +181,20 @@ const logData = (id, fullPath = false) => { const relFile = `${basePath}/${relName}` const relFileYML = `${relFile}.${ext}` const find = findRecursiveFolder(basePath, id) + + /** + * Not found log. + */ const rtnNotFound = () => { rtn = false } + + /** + * Found log. + * + * @param {string} path - path of log + * @param {string} uuid - uuid of log + */ const rtnFound = (path = '', uuid) => { if (path) { const stringPath = `${path}/${logFile.name}.${logFile.ext}` @@ -203,6 +241,14 @@ const logData = (id, fullPath = false) => { return rtn } +/** + * Get default provisions. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {object} userData - user of http request + */ const getProvisionDefaults = (res = {}, next = defaultEmptyFunction, params = {}, userData = {}) => { const extFiles = 'yml' const { user, password } = userData @@ -217,11 +263,22 @@ const getProvisionDefaults = (res = {}, next = defaultEmptyFunction, params = {} let description = '' let providers = {} let provisions = {} + /** + * Fill description of provision. + * + * @param {string} content - content of description provision + */ const fillDescription = (content = '') => { if (content) { description = content } } + /** + * Fill providers. + * + * @param {string} content - content of provider + * @param {string} name - name of provider + */ const fillProviders = (content = '', name = '') => { if (content && name) { if (!providers[name]) { @@ -233,6 +290,13 @@ const getProvisionDefaults = (res = {}, next = defaultEmptyFunction, params = {} } } } + /** + * Fill provisions. + * + * @param {string} content - content of provision + * @param {string} filePath - path of provision yamls + * @param {string} path - path for command + */ const fillProvisions = (content = '', filePath = '', path = '') => { if (content && filePath && path) { const name = basename(filePath).replace(`.${extFiles}`, '') @@ -307,7 +371,15 @@ const getProvisionDefaults = (res = {}, next = defaultEmptyFunction, params = {} next() } -const getList = (res = {}, next = defaultEmptyFunction, params = {}, userData = {}) => { +/** + * Get list for resource provisions. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {object} userData - user of http request + */ +const getListResourceProvision = (res = {}, next = defaultEmptyFunction, params = {}, userData = {}) => { const { user, password } = userData let rtn = httpInternalError if (params && params.resource && user && password) { @@ -327,6 +399,14 @@ const getList = (res = {}, next = defaultEmptyFunction, params = {}, userData = next() } +/** + * Get list provisions. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {object} userData - user of http request + */ const getListProvisions = (res = {}, next = defaultEmptyFunction, params = {}, userData = {}) => { const { user, password } = userData let rtn = httpInternalError @@ -360,6 +440,14 @@ const getListProvisions = (res = {}, next = defaultEmptyFunction, params = {}, u next() } +/** + * Delete resource provisions. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {object} userData - user of http request + */ const deleteResource = (res = {}, next = defaultEmptyFunction, params = {}, userData = {}) => { const { user, password } = userData let rtn = httpInternalError @@ -379,6 +467,15 @@ const deleteResource = (res = {}, next = defaultEmptyFunction, params = {}, user next() } +/** + * Delete provision. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {object} userData - user of http request + * @param {Function} oneConnection - function xmlrpc + */ const deleteProvision = (res = {}, next = defaultEmptyFunction, params = {}, userData = {}, oneConnection = defaultEmptyFunction) => { const basePath = `${global.CPI}/provision` const relFile = `${basePath}/${relName}` @@ -398,13 +495,23 @@ const deleteProvision = (res = {}, next = defaultEmptyFunction, params = {}, use // create stream for write into file const stream = dataLog && dataLog.fullPath && createWriteStream(dataLog.fullPath, { flags: 'a' }) - // This function is performed for each command line response + /** + * This function is performed for each command line response. + * + * @param {string} lastLine - last line command + * @param {string} uuid - uuid commnand + */ const emit = (lastLine, uuid) => { const renderLine = { id: params.id, data: lastLine, command: command, commandId: uuid } stream && stream.write && stream.write(`${JSON.stringify(renderLine)}\n`) } - // This function is only executed if the command is completed + /** + * This function is only executed if the command is completed. + * + * @param {boolean} success - check in command complete succefully + * @param {string} lastLine - last line command + */ const close = (success, lastLine) => { if (success) { stream && stream.end && stream.end() @@ -463,6 +570,14 @@ const deleteProvision = (res = {}, next = defaultEmptyFunction, params = {}, use next() } +/** + * Execute command of host into provision. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {object} userData - user of http request + */ const hostCommand = (res = {}, next = defaultEmptyFunction, params = {}, userData = {}) => { const { user, password } = userData let rtn = httpInternalError @@ -484,6 +599,14 @@ const hostCommand = (res = {}, next = defaultEmptyFunction, params = {}, userDat next() } +/** + * SSH command of host into provision. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {object} userData - user of http request + */ const hostCommandSSH = (res = {}, next = defaultEmptyFunction, params = {}, userData = {}) => { const { user, password } = userData let rtn = httpInternalError @@ -505,6 +628,15 @@ const hostCommandSSH = (res = {}, next = defaultEmptyFunction, params = {}, user next() } +/** + * Create a provision. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {object} userData - user of http request + * @param {Function} oneConnection - function of xmlrpc + */ const createProvision = (res = {}, next = defaultEmptyFunction, params = {}, userData = {}, oneConnection = defaultEmptyFunction) => { const basePath = `${global.CPI}/provision` const relFile = `${basePath}/${relName}` @@ -520,19 +652,39 @@ const createProvision = (res = {}, next = defaultEmptyFunction, params = {}, use const command = 'create' const authCommand = ['--user', user, '--password', password] const endpoint = getEndpoint() - const files = createFolderWithFiles(`${global.CPI}/provision/${id}/tmp`, [{ name: logFile.name, ext: logFile.ext }, { name: configFile.name, ext: configFile.ext, content }]) + const files = createFolderWithFiles(`${global.CPI}/provision/${id}/tmp`, [{ name: logFile.name, ext: logFile.ext }, { name: provisionFile.name, ext: provisionFile.ext, content }]) if (files && files.name && files.files) { + /** + * Find file in created files. + * + * @param {string} val - filename + * @param {string} ext - file extension + * @param {Array} arr - array of files + * @returns {Array} path file + */ const find = (val = '', ext = '', arr = files.files) => arr.find(e => e && e.path && e.ext && e.name && e.name === val && e.ext === ext) - const config = find(configFile.name, configFile.ext) + + const config = find(provisionFile.name, provisionFile.ext) const log = find(logFile.name, logFile.ext) if (config && log) { + /** + * Create provision. + * + * @param {string} filedata - provision data + */ const create = (filedata = '') => { const paramsCommand = [command, config.path, '--batch', '--debug', '--json', ...optionalCommand, ...authCommand, ...endpoint] // stream file log var stream = createWriteStream(log.path, { flags: 'a' }) - // This function is performed for each command line response + /** + * This function is performed for each command line response. + * + * @param {string} lastLine - last line command + * @param {string} uuid - UUID command + * @returns {object} string line of command + */ const emit = (lastLine, uuid) => { if (lastLine && uuid) { if (regexp.test(lastLine) && !checkSync(relFileLOCK)) { @@ -552,7 +704,12 @@ const createProvision = (res = {}, next = defaultEmptyFunction, params = {}, use } } - // This function is only executed if the command is completed + /** + * This function is only executed if the command is completed. + * + * @param {boolean} success - check if command finish successfully + * @param {string} lastLine - last line command finish + */ const close = (success, lastLine) => { stream.end() if (success && regexp.test(lastLine)) { @@ -618,6 +775,14 @@ const createProvision = (res = {}, next = defaultEmptyFunction, params = {}, use next() } +/** + * Configure provision. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {object} userData - user of http request + */ const configureProvision = (res = {}, next = defaultEmptyFunction, params = {}, userData = {}) => { const { user, password } = userData const rtn = httpInternalError @@ -633,12 +798,23 @@ const configureProvision = (res = {}, next = defaultEmptyFunction, params = {}, // create stream for write into file const stream = dataLog && dataLog.fullPath && createWriteStream(dataLog.fullPath, { flags: 'a' }) - // This function is performed for each command line response + /** + * This function is performed for each command line response. + * + * @param {string} lastLine - last line command + * @param {string} uuid - UUID command + */ const emit = (lastLine, uuid) => { const renderLine = { id: params.id, data: lastLine, command: command, commandId: uuid } stream && stream.write && stream.write(`${JSON.stringify(renderLine)}\n`) } + /** + * This function is only executed if the command is completed. + * + * @param {boolean} success - check if command complete without errors + * @param {string} lastLine - last line command + */ const close = (success, lastLine) => { stream && stream.end && stream.end() } @@ -659,6 +835,14 @@ const configureProvision = (res = {}, next = defaultEmptyFunction, params = {}, next() } +/** + * Configure host provision. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {object} userData - user of http request + */ const configureHost = (res = {}, next = defaultEmptyFunction, params = {}, userData = {}) => { const { user, password } = userData const rtn = httpInternalError @@ -674,12 +858,23 @@ const configureHost = (res = {}, next = defaultEmptyFunction, params = {}, userD // create stream for write into file const stream = dataLog && dataLog.fullPath && createWriteStream(dataLog.fullPath, { flags: 'a' }) - // This function is performed for each command line response + /** + * This function is performed for each command line response. + * + * @param {string} lastLine - last line command + * @param {string} uuid - uuid command + */ const emit = (lastLine, uuid) => { const renderLine = { id: params.id, data: lastLine, command: `host ${command}`, commandId: uuid } stream && stream.write && stream.write(`${JSON.stringify(renderLine)}\n`) } + /** + * This function is only executed if the command is completed. + * + * @param {boolean} success - check if command complete without error + * @param {string} lastLine - last line command + */ const close = (success, lastLine) => { stream && stream.end && stream.end() } @@ -700,6 +895,14 @@ const configureHost = (res = {}, next = defaultEmptyFunction, params = {}, userD next() } +/** + * Validate provision file. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {object} userData - user of http request + */ const validate = (res = {}, next = defaultEmptyFunction, params = {}, userData = {}) => { const { user, password } = userData let rtn = httpInternalError @@ -740,6 +943,13 @@ const validate = (res = {}, next = defaultEmptyFunction, params = {}, userData = next() } +/** + * Get provision log. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + */ const getLogProvisions = (res = {}, next = defaultEmptyFunction, params = {}) => { let rtn = httpInternalError if (params && params.id) { @@ -757,7 +967,7 @@ const getLogProvisions = (res = {}, next = defaultEmptyFunction, params = {}) => const provisionFunctionsApi = { getProvisionDefaults, getLogProvisions, - getList, + getListResourceProvision, getListProvisions, deleteResource, deleteProvision, diff --git a/src/fireedge/src/server/routes/api/provision/provision.js b/src/fireedge/src/server/routes/api/provision/provision.js index 0888d98dd9..44a6d53408 100644 --- a/src/fireedge/src/server/routes/api/provision/provision.js +++ b/src/fireedge/src/server/routes/api/provision/provision.js @@ -1,20 +1,22 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ + const { httpMethod, from: fromData } = require('server/utils/constants/defaults') const { - getList, + getListResourceProvision, getListProvisions, getLogProvisions, deleteResource, @@ -38,43 +40,43 @@ const routes = { } }, cluster: { - action: getList, + action: getListResourceProvision, params: { resource: { from: fromData.resource, name: 'method' } } }, datastore: { - action: getList, + action: getListResourceProvision, params: { resource: { from: fromData.resource, name: 'method' } } }, host: { - action: getList, + action: getListResourceProvision, params: { resource: { from: fromData.resource, name: 'method' } } }, image: { - action: getList, + action: getListResourceProvision, params: { resource: { from: fromData.resource, name: 'method' } } }, network: { - action: getList, + action: getListResourceProvision, params: { resource: { from: fromData.resource, name: 'method' } } }, template: { - action: getList, + action: getListResourceProvision, params: { resource: { from: fromData.resource, name: 'method' } } }, vntemplate: { - action: getList, + action: getListResourceProvision, params: { resource: { from: fromData.resource, name: 'method' } } diff --git a/src/fireedge/src/server/routes/api/provision/provision_template-functions.js b/src/fireedge/src/server/routes/api/provision/provision_template-functions.js index 4ab9b6b1d2..24984a0a64 100644 --- a/src/fireedge/src/server/routes/api/provision/provision_template-functions.js +++ b/src/fireedge/src/server/routes/api/provision/provision_template-functions.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { Validator } = require('jsonschema') const { defaultFolderTmpProvision, defaultCommandProvisionTemplate } = require('server/utils/constants/defaults') @@ -26,6 +27,14 @@ const { provider } = require('./schemas') const httpInternalError = httpResponse(internalServerError, '', '') +/** + * Get provision list template. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {object} userData - user of http request + */ const getListProvisionTemplates = (res = {}, next = () => undefined, params = {}, userData = {}) => { const { user, password } = userData let rtn = httpInternalError @@ -50,6 +59,14 @@ const getListProvisionTemplates = (res = {}, next = () => undefined, params = {} next() } +/** + * Create provision template. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {object} userData - user of http request + */ const createProvisionTemplate = (res = {}, next = () => undefined, params = {}, userData = {}) => { const { user, password } = userData let rtn = httpInternalError @@ -89,6 +106,14 @@ const createProvisionTemplate = (res = {}, next = () => undefined, params = {}, next() } +/** + * Instantiate provision template. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {object} userData - user of http request + */ const instantiateProvisionTemplate = (res = {}, next = () => undefined, params = {}, userData = {}) => { const { user, password } = userData let rtn = httpInternalError @@ -110,6 +135,14 @@ const instantiateProvisionTemplate = (res = {}, next = () => undefined, params = next() } +/** + * Update provision template. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {object} userData - user of http request + */ const updateProvisionTemplate = (res = {}, next = () => undefined, params = {}, userData = {}) => { const { user, password } = userData let rtn = httpInternalError @@ -149,6 +182,14 @@ const updateProvisionTemplate = (res = {}, next = () => undefined, params = {}, next() } +/** + * Delete provision. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {object} userData - user of http request + */ const deleteProvisionTemplate = (res = {}, next = () => undefined, params = {}, userData = {}) => { const { user, password } = userData let rtn = httpInternalError diff --git a/src/fireedge/src/server/routes/api/provision/provision_template.js b/src/fireedge/src/server/routes/api/provision/provision_template.js index b2d46b085e..9c6258734c 100644 --- a/src/fireedge/src/server/routes/api/provision/provision_template.js +++ b/src/fireedge/src/server/routes/api/provision/provision_template.js @@ -1,17 +1,19 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ + const { from: fromData } = require('server/utils/constants/defaults') const { getListProvisionTemplates, diff --git a/src/fireedge/src/server/routes/api/provision/schemas.js b/src/fireedge/src/server/routes/api/provision/schemas.js index 7d2a6310b5..c302e2e528 100644 --- a/src/fireedge/src/server/routes/api/provision/schemas.js +++ b/src/fireedge/src/server/routes/api/provision/schemas.js @@ -1,17 +1,19 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ + const providers = [ 'aws', 'packet', diff --git a/src/fireedge/src/server/routes/api/provision/string-routes.js b/src/fireedge/src/server/routes/api/provision/string-routes.js index 63a805b673..fa13dd1674 100644 --- a/src/fireedge/src/server/routes/api/provision/string-routes.js +++ b/src/fireedge/src/server/routes/api/provision/string-routes.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const PROVIDER = 'provider' const PROVISION = 'provision' diff --git a/src/fireedge/src/server/routes/api/sunstone/index.js b/src/fireedge/src/server/routes/api/sunstone/index.js index adf602c895..6aa2d06e3a 100644 --- a/src/fireedge/src/server/routes/api/sunstone/index.js +++ b/src/fireedge/src/server/routes/api/sunstone/index.js @@ -1,18 +1,20 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ -const { addFunctionToRoute, fillFunctionRoute } = require('server/utils/server') +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ + +const { addFunctionAsRoute, setFunctionRoute } = require('server/utils/server') const { routes: sunstoneRoutes } = require('./sunstone') const { SUNSTONE } = require('./string-routes') @@ -20,11 +22,18 @@ const { SUNSTONE } = require('./string-routes') const privateRoutes = [] const publicRoutes = [] -const fillPrivateRoutes = (methods = {}, path = '', action = () => undefined) => { +/** + * Set private routes. + * + * @param {object} methods - object of routes + * @param {string} path - principal route + * @param {Function} action - function of route + */ +const setPrivateRoutes = (methods = {}, path = '', action = () => undefined) => { if (Object.keys(methods).length > 0 && methods.constructor === Object) { Object.keys(methods).forEach((method) => { privateRoutes.push( - fillFunctionRoute(method, path, + setFunctionRoute(method, path, (req, res, next, connection, userId, user) => { action(req, res, next, methods[method], user, connection) }) @@ -33,8 +42,13 @@ const fillPrivateRoutes = (methods = {}, path = '', action = () => undefined) => } } +/** + * Add routes. + * + * @returns {Array} routes + */ const generatePrivateRoutes = () => { - fillPrivateRoutes(sunstoneRoutes, SUNSTONE, addFunctionToRoute) + setPrivateRoutes(sunstoneRoutes, SUNSTONE, addFunctionAsRoute) return privateRoutes } diff --git a/src/fireedge/src/server/routes/api/sunstone/string-routes.js b/src/fireedge/src/server/routes/api/sunstone/string-routes.js index d06cdb0659..e393b03bdd 100644 --- a/src/fireedge/src/server/routes/api/sunstone/string-routes.js +++ b/src/fireedge/src/server/routes/api/sunstone/string-routes.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const SUNSTONE = 'sunstone' diff --git a/src/fireedge/src/server/routes/api/sunstone/sunstone-functions.js b/src/fireedge/src/server/routes/api/sunstone/sunstone-functions.js index 61811f28af..e2bbdcff55 100644 --- a/src/fireedge/src/server/routes/api/sunstone/sunstone-functions.js +++ b/src/fireedge/src/server/routes/api/sunstone/sunstone-functions.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { parse } = require('yaml') const { defaultEmptyFunction } = require('server/utils/constants/defaults') @@ -27,15 +28,29 @@ const { const httpInternalError = httpResponse(internalServerError, '', '') -const getInfoZone = (connect = defaultEmptyFunction, idZone, callback = defaultEmptyFunction) => { +/** + * Get information of opennebula group. + * + * @param {Function} connect - xmlrpc function + * @param {string} idGroup - id of group + * @param {Function} callback - run function when have group information + */ +const getInfoGroup = (connect = defaultEmptyFunction, idGroup, callback = defaultEmptyFunction) => { connect( ActionsGroup.GROUP_INFO, - [parseInt(idZone, 10), false], + [parseInt(idGroup, 10), false], callback, false ) } +/** + * Response http request. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} httpCode - object http code + */ const responseHttp = (res = {}, next = defaultEmptyFunction, httpCode) => { if (res && res.locals && res.locals.httpCode && httpCode) { res.locals.httpCode = httpCode @@ -43,6 +58,15 @@ const responseHttp = (res = {}, next = defaultEmptyFunction, httpCode) => { } } +/** + * Get sunstone-server views. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {object} userData - user of http request + * @param {Function} oneConnection - xmlrpc function + */ const getViews = (res = {}, next = () => undefined, params = {}, userData = {}, oneConnection = defaultEmptyFunction) => { const { user, password } = userData if (user && password && global && global.SUNSTONE_VIEWS && global.SUNSTONE_PATH) { @@ -52,7 +76,7 @@ const getViews = (res = {}, next = () => undefined, params = {}, userData = {}, [-1, false], (err = {}, userData = {}) => { if (userData && userData.USER && userData.USER.GID) { - getInfoZone( + getInfoGroup( connect, userData.USER.GID, (err = {}, vmgroupData = {}) => { @@ -109,6 +133,14 @@ const getViews = (res = {}, next = () => undefined, params = {}, userData = {}, } } +/** + * Get sunstone-server config. + * + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} params - params of http request + * @param {object} userData - user of http request + */ const getConfig = (res = {}, next = () => undefined, params = {}, userData = {}) => { if (global && global.SUNSTONE_CONFIG) { existsFile( diff --git a/src/fireedge/src/server/routes/api/sunstone/sunstone.js b/src/fireedge/src/server/routes/api/sunstone/sunstone.js index 1de3dc4ef4..e5072ed9b1 100644 --- a/src/fireedge/src/server/routes/api/sunstone/sunstone.js +++ b/src/fireedge/src/server/routes/api/sunstone/sunstone.js @@ -1,17 +1,19 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ + const { httpMethod } = require('server/utils/constants/defaults') const { getConfig, diff --git a/src/fireedge/src/server/routes/api/support/index.js b/src/fireedge/src/server/routes/api/support/index.js index 5029c0df29..1f041f5df1 100644 --- a/src/fireedge/src/server/routes/api/support/index.js +++ b/src/fireedge/src/server/routes/api/support/index.js @@ -1,25 +1,26 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ -const privateRoutes = []; +const privateRoutes = [] -const publicRoutes = []; +const publicRoutes = [] const functionRoutes = { private: privateRoutes, public: publicRoutes -}; +} -module.exports = functionRoutes; +module.exports = functionRoutes diff --git a/src/fireedge/src/server/routes/api/vcenter/index.js b/src/fireedge/src/server/routes/api/vcenter/index.js index 5029c0df29..1f041f5df1 100644 --- a/src/fireedge/src/server/routes/api/vcenter/index.js +++ b/src/fireedge/src/server/routes/api/vcenter/index.js @@ -1,25 +1,26 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ -const privateRoutes = []; +const privateRoutes = [] -const publicRoutes = []; +const publicRoutes = [] const functionRoutes = { private: privateRoutes, public: publicRoutes -}; +} -module.exports = functionRoutes; +module.exports = functionRoutes diff --git a/src/fireedge/src/server/routes/api/zendesk/index.js b/src/fireedge/src/server/routes/api/zendesk/index.js index 5029c0df29..1f041f5df1 100644 --- a/src/fireedge/src/server/routes/api/zendesk/index.js +++ b/src/fireedge/src/server/routes/api/zendesk/index.js @@ -1,25 +1,26 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ -const privateRoutes = []; +const privateRoutes = [] -const publicRoutes = []; +const publicRoutes = [] const functionRoutes = { private: privateRoutes, public: publicRoutes -}; +} -module.exports = functionRoutes; +module.exports = functionRoutes diff --git a/src/fireedge/src/server/routes/entrypoints/404.js b/src/fireedge/src/server/routes/entrypoints/404.js index decaf34297..50c490f7d9 100644 --- a/src/fireedge/src/server/routes/entrypoints/404.js +++ b/src/fireedge/src/server/routes/entrypoints/404.js @@ -1,28 +1,29 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ -const express = require('express'); +const express = require('express') -const router = express.Router(); +const router = express.Router() router.get('*', (req, res) => res .status(404) .send( - `

404 - Not Found

` + '

404 - Not Found

' ) -); +) -module.exports = router; +module.exports = router diff --git a/src/fireedge/src/server/routes/entrypoints/Api.js b/src/fireedge/src/server/routes/entrypoints/Api.js index 19966837de..4d5a4d18bb 100644 --- a/src/fireedge/src/server/routes/entrypoints/Api.js +++ b/src/fireedge/src/server/routes/entrypoints/Api.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const express = require('express') const { defaults, httpCodes, params } = require('server/utils/constants') @@ -20,7 +21,7 @@ const { getConfig } = require('server/utils/yml') const { opennebulaConnect, checkIfIsARouteFunction, - commandXML, + commandXMLRPC, checkOpennebulaCommand, checkMethodRouteFunction, responseOpennebula, @@ -30,8 +31,8 @@ const { const { validateResourceAndSession, - optionalParameters, - optionalQueries, + setOptionalParameters, + setOptionalQueries, clearStates, getParamsState, getQueriesState, @@ -52,15 +53,20 @@ const router = express.Router() express() -const paramsToRoutes = () => +/** + * Get route parameters. + * + * @returns {Array} valid express route + */ +const routeParameters = () => Object.keys(params).reduce( (resources, param) => String(resources).concat(`/:${params[param]}?`), '/:resource?' ) router.all( - paramsToRoutes(), - [validateResourceAndSession, optionalParameters, optionalQueries], + routeParameters(), + [validateResourceAndSession, setOptionalParameters, setOptionalQueries], (req, res, next) => { const { internalServerError, ok, methodNotAllowed, notFound } = httpCodes const { method: httpMethod } = req @@ -79,6 +85,13 @@ router.all( const zoneData = getDataZone(zone, defaultOpennebulaZones) if (zoneData) { const { rpc } = zoneData + /** + * Instance of connection to opennebula. + * + * @param {string} user - opennegula user + * @param {string} pass - opennebula pass + * @returns {Function} opennebula executer calls to XMLRPC + */ const connectOpennebula = ( user = getUserOpennebula(), pass = getPassOpennebula() @@ -120,13 +133,18 @@ router.all( *********************************************************/ const { method } = getParamsState() - const command = commandXML( + const command = commandXMLRPC( resource, method, httpMethod === httpMethods.GET && defaultGetMethod ) const getOpennebulaMethod = checkOpennebulaCommand(command, httpMethod) if (getOpennebulaMethod) { + /** + * Http response. + * + * @param {object} val - response http code + */ const response = (val = {}) => { switch (typeof val) { case 'string': @@ -144,6 +162,11 @@ router.all( next() } + /** + * Updater http response. + * + * @param {object} code - http code + */ const updaterResponse = code => { if ('id' in code && 'message' in code) { res.locals.httpCode = code diff --git a/src/fireedge/src/server/routes/entrypoints/App.js b/src/fireedge/src/server/routes/entrypoints/App.js index 264cf36604..eefa7661e7 100644 --- a/src/fireedge/src/server/routes/entrypoints/App.js +++ b/src/fireedge/src/server/routes/entrypoints/App.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const React = require('react') const { Router } = require('express') diff --git a/src/fireedge/src/server/routes/entrypoints/index.js b/src/fireedge/src/server/routes/entrypoints/index.js index acdf0e4566..84a4b4cc32 100644 --- a/src/fireedge/src/server/routes/entrypoints/index.js +++ b/src/fireedge/src/server/routes/entrypoints/index.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const entrypoint404 = require('./404') const entrypointApi = require('./Api') diff --git a/src/fireedge/src/server/routes/entrypoints/middlewares/api/index.js b/src/fireedge/src/server/routes/entrypoints/middlewares/api/index.js index 5cf1a9f5c8..8ffc1b3028 100644 --- a/src/fireedge/src/server/routes/entrypoints/middlewares/api/index.js +++ b/src/fireedge/src/server/routes/entrypoints/middlewares/api/index.js @@ -1,17 +1,19 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ + const { Map } = require('immutable') const { env } = require('process') const { global } = require('window-or-global') @@ -37,14 +39,50 @@ let queriesState = defaultQueries.toObject() let idUserOpennebula = '' let userOpennebula = '' let passOpennebula = '' - +/** + * Get params state. + * + * @returns {object} params state + */ const getParamsState = () => paramsState + +/** + * Get query state. + * + * @returns {object} query state + */ const getQueriesState = () => queriesState + +/** + * Get id opennebula user. + * + * @returns {number} id opennebula user + */ const getIdUserOpennebula = () => idUserOpennebula + +/** + * Get user opennebula. + * + * @returns {string} opennebula username + */ const getUserOpennebula = () => userOpennebula + +/** + * Get pass opennebula. + * + * @returns {string} opennebula user password + */ const getPassOpennebula = () => passOpennebula -const passUserValidation = (user = '', token = '') => { +/** + * Validate user in global state. + * + * @param {string} user - username + * @param {string} token - token of user + * @returns {boolean} user valid data + */ +const userValidation = (user = '', token = '') => { + let rtn = false if (user && token && global && @@ -52,16 +90,31 @@ const passUserValidation = (user = '', token = '') => { global.users[user] && global.users[user].token && global.users[user].token === token) { - return true + rtn = true } + return rtn } +/** + * MIDDLEWARE validate resource and session. + * + * @param {object} req - http request + * @param {object} res - http response + * @param {Function} next - express stepper + */ const validateResourceAndSession = (req, res, next) => { const { badRequest, unauthorized, serviceUnavailable } = httpCodes let status = badRequest if (req && req.params && req.params.resource) { const resource = req.params.resource status = serviceUnavailable + + /** + * Finder command. + * + * @param {object} rtnCommand - command + * @returns {object} command + */ const finderCommand = rtnCommand => rtnCommand && rtnCommand.endpoint && rtnCommand.endpoint === resource if (authenticated.some(finderCommand)) { @@ -75,7 +128,7 @@ const validateResourceAndSession = (req, res, next) => { * Validate user in production mode *********************************************************/ - if (passUserValidation(userOpennebula, passOpennebula)) { + if (userValidation(userOpennebula, passOpennebula)) { next() return } @@ -90,7 +143,7 @@ const validateResourceAndSession = (req, res, next) => { if (!global.users[userOpennebula]) { global.users[userOpennebula] = { token: passOpennebula } } - if (passUserValidation(userOpennebula, passOpennebula)) { + if (userValidation(userOpennebula, passOpennebula)) { next() return } @@ -105,8 +158,14 @@ const validateResourceAndSession = (req, res, next) => { } res.status(status.id).json(status) } - -const optionalParameters = (req, res, next) => { +/** + * MIDDLEWARE set optional parameters. + * + * @param {object} req - http request + * @param {object} res - http response + * @param {Function} next - express stepper + */ +const setOptionalParameters = (req, res, next) => { if (req && req.params) { let start = true const keys = Object.keys(req.params) @@ -141,8 +200,14 @@ const optionalParameters = (req, res, next) => { } next() } - -const optionalQueries = (req, res, next) => { +/** + * MIDDLEWARE set optional queries. + * + * @param {object} req - http request + * @param {object} res - http response + * @param {Function} next - express stepper + */ +const setOptionalQueries = (req, res, next) => { if (req && req.query) { const keys = Object.keys(req.query) const queries = getAllowedQueryParams() @@ -155,6 +220,9 @@ const optionalQueries = (req, res, next) => { next() } +/** + * Clear states. + */ const clearStates = () => { paramsState = defaultParams.toObject() queriesState = defaultQueries.toObject() @@ -165,8 +233,8 @@ const clearStates = () => { module.exports = { validateResourceAndSession, - optionalParameters, - optionalQueries, + setOptionalParameters, + setOptionalQueries, clearStates, getParamsState, getQueriesState, diff --git a/src/fireedge/src/server/routes/entrypoints/middlewares/index.js b/src/fireedge/src/server/routes/entrypoints/middlewares/index.js index 7a67ecc8a5..27300847d9 100644 --- a/src/fireedge/src/server/routes/entrypoints/middlewares/index.js +++ b/src/fireedge/src/server/routes/entrypoints/middlewares/index.js @@ -1,21 +1,23 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ + const { validateResourceAndSession, - optionalParameters, - optionalQueries, + setOptionalParameters, + setOptionalQueries, clearStates, getParamsState, getQueriesState, @@ -26,8 +28,8 @@ const { module.exports = { validateResourceAndSession, - optionalParameters, - optionalQueries, + setOptionalParameters, + setOptionalQueries, clearStates, getParamsState, getQueriesState, diff --git a/src/fireedge/src/server/routes/websockets/guacamole/index.js b/src/fireedge/src/server/routes/websockets/guacamole/index.js index fc07182852..f816d239c7 100644 --- a/src/fireedge/src/server/routes/websockets/guacamole/index.js +++ b/src/fireedge/src/server/routes/websockets/guacamole/index.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const GuacamoleOpennebula = require('opennebula-guacamole') const { getConfig } = require('server/utils/yml') @@ -22,12 +23,19 @@ const { endpointGuacamole } = require('server/utils/constants/defaults') // set fireedge_key genFireedgeKey() +/** + * Object http error. + * + * @param {object} error - error message + * @returns {object} error for terminalMessage function + */ const configError = (error) => ({ color: 'red', message: 'Error: %s', error: error && error.message }) +// guacamole client options const clientOptions = { crypt: { cypher: 'AES-256-CBC', @@ -58,6 +66,11 @@ const guacd = appConfig.guacd || {} const guacdPort = guacd.port || 4822 const guacdHost = guacd.host || 'localhost' +/** + * Add guacamole server to node app. + * + * @param {object} appServer - express app + */ const guacamole = appServer => { if ( appServer && diff --git a/src/fireedge/src/server/routes/websockets/hooks/index.js b/src/fireedge/src/server/routes/websockets/hooks/index.js index 17a034a2a0..a36884b172 100644 --- a/src/fireedge/src/server/routes/websockets/hooks/index.js +++ b/src/fireedge/src/server/routes/websockets/hooks/index.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const atob = require('atob') const { socket: socketZeroMQ } = require('zeromq') @@ -20,25 +21,31 @@ const { messageTerminal } = require('server/utils/general') const { middlewareValidateAuthWebsocket, middlewareValidateResourceForHookConnection, - getResourceForHookConnection, + getResourceDataForRequest, getDataZone, - returnQueryData + getQueryData } = require('server/utils/server') +/** + * Route of websocket HOOKS. + * + * @param {object} app - express app + * @param {string} type - type WS + */ const main = (app = {}, type = '') => { try { app .use(middlewareValidateAuthWebsocket) .use(middlewareValidateResourceForHookConnection) .on('connection', (server = {}) => { - const { id, resource } = getResourceForHookConnection(server) - const { zone: queryZone } = returnQueryData(server) + const { id, resource } = getResourceDataForRequest(server) + const { zone: queryZone } = getQueryData(server) const zone = queryZone && queryZone !== 'undefined' ? queryZone : '0' const dataZone = getDataZone(zone) if (dataZone && dataZone.zeromq) { const zeromqSock = socketZeroMQ('sub') zeromqSock.connect(dataZone.zeromq) - zeromqSock.subscribe(`EVENT ${resource.toUpperCase()} ${id}/`) + zeromqSock.subscribe(`EVENT ${resource.toUpperCase()} ${id}/`) // state server.on('disconnect', function () { zeromqSock.close() }) diff --git a/src/fireedge/src/server/routes/websockets/index.js b/src/fireedge/src/server/routes/websockets/index.js index 442e7f3dc6..6faa1c76fa 100644 --- a/src/fireedge/src/server/routes/websockets/index.js +++ b/src/fireedge/src/server/routes/websockets/index.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const socketIO = require('socket.io') const { messageTerminal, checkEmptyObject } = require('server/utils/general') @@ -20,8 +21,12 @@ const { defaultConfigErrorMessage } = require('server/utils/constants/defaults') -// user config - +/** + * Add websockets to express app. + * + * @param {object} appServer - express app + * @returns {Array} sockets + */ const websockets = (appServer = {}) => { const sockets = [] if ( diff --git a/src/fireedge/src/server/routes/websockets/provision/index.js b/src/fireedge/src/server/routes/websockets/provision/index.js index 68c2ecd1f0..b576aff9d7 100644 --- a/src/fireedge/src/server/routes/websockets/provision/index.js +++ b/src/fireedge/src/server/routes/websockets/provision/index.js @@ -1,31 +1,42 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { middlewareValidateAuthWebsocket } = require('server/utils/server') const { messageTerminal } = require('server/utils/general') const { subscriber } = require('server/routes/api/provision/functions') -const main = (app = {}, type = '') => { - const configErrorProvision = (error = '') => { - return { - color: 'red', - error, - message: '%s' - } - } +/** + * Object http error. + * + * @param {object} error - error message + * @returns {object} param of terminalMessage function + */ +const configErrorProvision = (error = '') => ({ + color: 'red', + error, + message: '%s' +}) +/** + * Route of websocket Provisions. + * + * @param {object} app - express app + * @param {string} type - type WS + */ +const main = (app = {}, type = '') => { try { app .use(middlewareValidateAuthWebsocket) diff --git a/src/fireedge/src/server/routes/websockets/vmrc/index.js b/src/fireedge/src/server/routes/websockets/vmrc/index.js index c8623822ac..2ca1f6bef2 100644 --- a/src/fireedge/src/server/routes/websockets/vmrc/index.js +++ b/src/fireedge/src/server/routes/websockets/vmrc/index.js @@ -1,17 +1,19 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ + // eslint-disable-next-line node/no-deprecated-api const { parse } = require('url') const { createProxyMiddleware } = require('http-proxy-middleware') @@ -63,7 +65,12 @@ const vmrcProxy = createProxyMiddleware(endpointVmrc, { } }) -const vmrcUpgrade = appServer => { +/** + * VMRC Proxy. + * + * @param {object} appServer - express app + */ +const vmrc = appServer => { if ( appServer && appServer.on && @@ -76,5 +83,5 @@ const vmrcUpgrade = appServer => { } module.exports = { - vmrcUpgrade + vmrc } diff --git a/src/fireedge/src/server/utils/constants/commands/acl.js b/src/fireedge/src/server/utils/constants/commands/acl.js index 4e7dd53071..f39e334c61 100644 --- a/src/fireedge/src/server/utils/constants/commands/acl.js +++ b/src/fireedge/src/server/utils/constants/commands/acl.js @@ -1,33 +1,32 @@ -/* -------------------------------------------------------------------------- */ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ - +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { from: { resource, postBody }, httpMethod: { GET, POST, DELETE } -} = require('../defaults'); +} = require('../defaults') -const ACL_ADDRULE = 'acl.addrule'; -const ACL_DELRULE = 'acl.delrule'; -const ACL_INFO = 'acl.info'; +const ACL_ADDRULE = 'acl.addrule' +const ACL_DELRULE = 'acl.delrule' +const ACL_INFO = 'acl.info' const Actions = { ACL_ADDRULE, ACL_DELRULE, ACL_INFO -}; +} module.exports = { Actions, @@ -66,4 +65,4 @@ module.exports = { params: {} } } -}; +} diff --git a/src/fireedge/src/server/utils/constants/commands/cluster.js b/src/fireedge/src/server/utils/constants/commands/cluster.js index 6d89f1d84d..1425b62a47 100644 --- a/src/fireedge/src/server/utils/constants/commands/cluster.js +++ b/src/fireedge/src/server/utils/constants/commands/cluster.js @@ -1,35 +1,35 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ - +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { from: { resource, postBody, query }, httpMethod: { GET, POST, PUT, DELETE } -} = require('../defaults'); +} = require('../defaults') -const CLUSTER_ALLOCATE = 'cluster.allocate'; -const CLUSTER_DELETE = 'cluster.delete'; -const CLUSTER_UPDATE = 'cluster.update'; -const CLUSTER_ADDHOST = 'cluster.addhost'; -const CLUSTER_DELHOST = 'cluster.delhost'; -const CLUSTER_ADDDATASTORE = 'cluster.adddatastore'; -const CLUSTER_DELDATASTORE = 'cluster.deldatastore'; -const CLUSTER_ADDVNET = 'cluster.addvnet'; -const CLUSTER_DELVNET = 'cluster.delvnet'; -const CLUSTER_RENAME = 'cluster.rename'; -const CLUSTER_INFO = 'cluster.info'; -const CLUSTER_POOL_INFO = 'clusterpool.info'; +const CLUSTER_ALLOCATE = 'cluster.allocate' +const CLUSTER_DELETE = 'cluster.delete' +const CLUSTER_UPDATE = 'cluster.update' +const CLUSTER_ADDHOST = 'cluster.addhost' +const CLUSTER_DELHOST = 'cluster.delhost' +const CLUSTER_ADDDATASTORE = 'cluster.adddatastore' +const CLUSTER_DELDATASTORE = 'cluster.deldatastore' +const CLUSTER_ADDVNET = 'cluster.addvnet' +const CLUSTER_DELVNET = 'cluster.delvnet' +const CLUSTER_RENAME = 'cluster.rename' +const CLUSTER_INFO = 'cluster.info' +const CLUSTER_POOL_INFO = 'clusterpool.info' const Actions = { CLUSTER_ALLOCATE, @@ -44,7 +44,7 @@ const Actions = { CLUSTER_RENAME, CLUSTER_INFO, CLUSTER_POOL_INFO -}; +} module.exports = { Actions, @@ -205,4 +205,4 @@ module.exports = { params: {} } } -}; +} diff --git a/src/fireedge/src/server/utils/constants/commands/datastore.js b/src/fireedge/src/server/utils/constants/commands/datastore.js index 04b449882d..986fc26b36 100644 --- a/src/fireedge/src/server/utils/constants/commands/datastore.js +++ b/src/fireedge/src/server/utils/constants/commands/datastore.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { from: { resource, postBody, query }, diff --git a/src/fireedge/src/server/utils/constants/commands/document.js b/src/fireedge/src/server/utils/constants/commands/document.js index 2b518b0827..02457688ce 100644 --- a/src/fireedge/src/server/utils/constants/commands/document.js +++ b/src/fireedge/src/server/utils/constants/commands/document.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { from: { resource, postBody, query }, diff --git a/src/fireedge/src/server/utils/constants/commands/group.js b/src/fireedge/src/server/utils/constants/commands/group.js index 52a9226443..5719821f22 100644 --- a/src/fireedge/src/server/utils/constants/commands/group.js +++ b/src/fireedge/src/server/utils/constants/commands/group.js @@ -1,33 +1,34 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { from: { resource, postBody, query }, httpMethod: { GET, POST, PUT, DELETE } -} = require('../defaults'); +} = require('../defaults') -const GROUP_ALLOCATE = 'group.allocate'; -const GROUP_DELETE = 'group.delete'; -const GROUP_INFO = 'group.info'; -const GROUP_UPDATE = 'group.update'; -const GROUP_ADDADMIN = 'group.addadmin'; -const GROUP_DELADMIN = 'group.deladmin'; -const GROUP_QUOTA = 'group.quota'; -const GROUP_POOL_INFO = 'grouppool.info'; -const GROUP_QUOTA_INFO = 'groupquota.info'; -const GROUP_QUOTA_UPDATE = 'groupquota.update'; +const GROUP_ALLOCATE = 'group.allocate' +const GROUP_DELETE = 'group.delete' +const GROUP_INFO = 'group.info' +const GROUP_UPDATE = 'group.update' +const GROUP_ADDADMIN = 'group.addadmin' +const GROUP_DELADMIN = 'group.deladmin' +const GROUP_QUOTA = 'group.quota' +const GROUP_POOL_INFO = 'grouppool.info' +const GROUP_QUOTA_INFO = 'groupquota.info' +const GROUP_QUOTA_UPDATE = 'groupquota.update' const Actions = { GROUP_ALLOCATE, @@ -40,7 +41,7 @@ const Actions = { GROUP_POOL_INFO, GROUP_QUOTA_INFO, GROUP_QUOTA_UPDATE -}; +} module.exports = { Actions, @@ -160,4 +161,4 @@ module.exports = { } } } -}; +} diff --git a/src/fireedge/src/server/utils/constants/commands/hook.js b/src/fireedge/src/server/utils/constants/commands/hook.js index eb140c7a9d..b5d434e71e 100644 --- a/src/fireedge/src/server/utils/constants/commands/hook.js +++ b/src/fireedge/src/server/utils/constants/commands/hook.js @@ -1,32 +1,33 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { from: { resource, postBody, query }, httpMethod: { GET, POST, PUT, DELETE } -} = require('../defaults'); +} = require('../defaults') -const HOOK_ALLOCATE = 'hook.allocate'; -const HOOK_DELETE = 'hook.delete'; -const HOOK_INFO = 'hook.info'; -const HOOK_RENAME = 'hook.rename'; -const HOOK_LOCK = 'hook.lock'; -const HOOK_UNLOCK = 'hook.unlock'; -const HOOK_RETRY = 'hook.retry'; -const HOOK_POOL_INFO = 'hookpool.info'; -const HOOK_LOG_INFO = 'hooklog.info'; +const HOOK_ALLOCATE = 'hook.allocate' +const HOOK_DELETE = 'hook.delete' +const HOOK_INFO = 'hook.info' +const HOOK_RENAME = 'hook.rename' +const HOOK_LOCK = 'hook.lock' +const HOOK_UNLOCK = 'hook.unlock' +const HOOK_RETRY = 'hook.retry' +const HOOK_POOL_INFO = 'hookpool.info' +const HOOK_LOG_INFO = 'hooklog.info' const Actions = { HOOK_ALLOCATE, @@ -38,7 +39,7 @@ const Actions = { HOOK_RETRY, HOOK_POOL_INFO, HOOK_LOG_INFO -}; +} module.exports = { Actions, @@ -188,4 +189,4 @@ module.exports = { } } } -}; +} diff --git a/src/fireedge/src/server/utils/constants/commands/host.js b/src/fireedge/src/server/utils/constants/commands/host.js index 160849dc46..14723be61c 100644 --- a/src/fireedge/src/server/utils/constants/commands/host.js +++ b/src/fireedge/src/server/utils/constants/commands/host.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { from: { resource, postBody, query }, diff --git a/src/fireedge/src/server/utils/constants/commands/image.js b/src/fireedge/src/server/utils/constants/commands/image.js index d3bfd2e5f0..2db275c886 100644 --- a/src/fireedge/src/server/utils/constants/commands/image.js +++ b/src/fireedge/src/server/utils/constants/commands/image.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { from: { resource, postBody, query }, diff --git a/src/fireedge/src/server/utils/constants/commands/index.js b/src/fireedge/src/server/utils/constants/commands/index.js index 326fc6eb2d..d7b310bce1 100644 --- a/src/fireedge/src/server/utils/constants/commands/index.js +++ b/src/fireedge/src/server/utils/constants/commands/index.js @@ -1,39 +1,40 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ -const { Commands: acl } = require('./acl'); -const { Commands: cluster } = require('./cluster'); -const { Commands: datastore } = require('./datastore'); -const { Commands: document } = require('./document'); -const { Commands: group } = require('./group'); -const { Commands: secgroup } = require('./secgroup'); -const { Commands: hook } = require('./hook'); -const { Commands: host } = require('./host'); -const { Commands: image } = require('./image'); -const { Commands: market } = require('./market'); -const { Commands: marketapp } = require('./marketapp'); -const { Commands: system } = require('./system'); -const { Commands: template } = require('./template'); -const { Commands: user } = require('./user'); -const { Commands: vdc } = require('./vdc'); -const { Commands: vm } = require('./vm'); -const { Commands: vmgroup } = require('./vmgroup'); -const { Commands: vn } = require('./vn'); -const { Commands: vntemplate } = require('./vntemplate'); -const { Commands: vrouter } = require('./vrouter'); -const { Commands: zone } = require('./zone'); +const { Commands: acl } = require('./acl') +const { Commands: cluster } = require('./cluster') +const { Commands: datastore } = require('./datastore') +const { Commands: document } = require('./document') +const { Commands: group } = require('./group') +const { Commands: secgroup } = require('./secgroup') +const { Commands: hook } = require('./hook') +const { Commands: host } = require('./host') +const { Commands: image } = require('./image') +const { Commands: market } = require('./market') +const { Commands: marketapp } = require('./marketapp') +const { Commands: system } = require('./system') +const { Commands: template } = require('./template') +const { Commands: user } = require('./user') +const { Commands: vdc } = require('./vdc') +const { Commands: vm } = require('./vm') +const { Commands: vmgroup } = require('./vmgroup') +const { Commands: vn } = require('./vn') +const { Commands: vntemplate } = require('./vntemplate') +const { Commands: vrouter } = require('./vrouter') +const { Commands: zone } = require('./zone') module.exports = { ...acl, @@ -57,4 +58,4 @@ module.exports = { ...vntemplate, ...vrouter, ...zone -}; +} diff --git a/src/fireedge/src/server/utils/constants/commands/market.js b/src/fireedge/src/server/utils/constants/commands/market.js index 9fe9bab760..84e0dde3e9 100644 --- a/src/fireedge/src/server/utils/constants/commands/market.js +++ b/src/fireedge/src/server/utils/constants/commands/market.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { from: { resource, postBody, query }, diff --git a/src/fireedge/src/server/utils/constants/commands/marketapp.js b/src/fireedge/src/server/utils/constants/commands/marketapp.js index eb0be53bd2..affdcfcd6c 100644 --- a/src/fireedge/src/server/utils/constants/commands/marketapp.js +++ b/src/fireedge/src/server/utils/constants/commands/marketapp.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { from: { resource, postBody, query }, diff --git a/src/fireedge/src/server/utils/constants/commands/secgroup.js b/src/fireedge/src/server/utils/constants/commands/secgroup.js index 37ef155dea..93adfbf29e 100644 --- a/src/fireedge/src/server/utils/constants/commands/secgroup.js +++ b/src/fireedge/src/server/utils/constants/commands/secgroup.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { from: { resource, postBody, query }, diff --git a/src/fireedge/src/server/utils/constants/commands/system.js b/src/fireedge/src/server/utils/constants/commands/system.js index a1a83ea762..4de622f662 100644 --- a/src/fireedge/src/server/utils/constants/commands/system.js +++ b/src/fireedge/src/server/utils/constants/commands/system.js @@ -1,29 +1,30 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { httpMethod: { GET } -} = require('../defaults'); +} = require('../defaults') -const SYSTEM_VERSION = 'system.version'; -const SYSTEM_CONFIG = 'system.config'; +const SYSTEM_VERSION = 'system.version' +const SYSTEM_CONFIG = 'system.config' const Actions = { SYSTEM_VERSION, SYSTEM_CONFIG -}; +} module.exports = { Actions, @@ -39,4 +40,4 @@ module.exports = { params: {} } } -}; +} diff --git a/src/fireedge/src/server/utils/constants/commands/template.js b/src/fireedge/src/server/utils/constants/commands/template.js index a683dd2983..1a443185b8 100644 --- a/src/fireedge/src/server/utils/constants/commands/template.js +++ b/src/fireedge/src/server/utils/constants/commands/template.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { from: { resource, postBody, query }, diff --git a/src/fireedge/src/server/utils/constants/commands/user.js b/src/fireedge/src/server/utils/constants/commands/user.js index 1005f3a33a..6397a50517 100644 --- a/src/fireedge/src/server/utils/constants/commands/user.js +++ b/src/fireedge/src/server/utils/constants/commands/user.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { from: { resource, postBody, query }, diff --git a/src/fireedge/src/server/utils/constants/commands/vdc.js b/src/fireedge/src/server/utils/constants/commands/vdc.js index ba4daea7a1..faa8e66080 100644 --- a/src/fireedge/src/server/utils/constants/commands/vdc.js +++ b/src/fireedge/src/server/utils/constants/commands/vdc.js @@ -1,39 +1,40 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { from: { resource, postBody, query }, httpMethod: { GET, POST, PUT, DELETE } -} = require('../defaults'); +} = require('../defaults') -const VDC_ALLOCATE = 'vdc.allocate'; -const VDC_DELETE = 'vdc.delete'; -const VDC_UPDATE = 'vdc.update'; -const VDC_RENAME = 'vdc.rename'; -const VDC_ADDGROUP = 'vdc.addgroup'; -const VDC_DELGROUP = 'vdc.delgroup'; -const VDC_ADDCLUSTER = 'vdc.addcluster'; -const VDC_DELCLUSTER = 'vdc.delcluster'; -const VDC_ADDHOST = 'vdc.addhost'; -const VDC_DELHOST = 'vdc.delhost'; -const VDC_ADDDATASTORE = 'vdc.adddatastore'; -const VDC_DELDATASTORE = 'vdc.deldatastore'; -const VDC_ADDVNET = 'vdc.addvnet'; -const VDC_DELVNET = 'vdc.delvnet'; -const VDC_INFO = 'vdc.info'; -const VDC_POOL_INFO = 'vdcpool.info'; +const VDC_ALLOCATE = 'vdc.allocate' +const VDC_DELETE = 'vdc.delete' +const VDC_UPDATE = 'vdc.update' +const VDC_RENAME = 'vdc.rename' +const VDC_ADDGROUP = 'vdc.addgroup' +const VDC_DELGROUP = 'vdc.delgroup' +const VDC_ADDCLUSTER = 'vdc.addcluster' +const VDC_DELCLUSTER = 'vdc.delcluster' +const VDC_ADDHOST = 'vdc.addhost' +const VDC_DELHOST = 'vdc.delhost' +const VDC_ADDDATASTORE = 'vdc.adddatastore' +const VDC_DELDATASTORE = 'vdc.deldatastore' +const VDC_ADDVNET = 'vdc.addvnet' +const VDC_DELVNET = 'vdc.delvnet' +const VDC_INFO = 'vdc.info' +const VDC_POOL_INFO = 'vdcpool.info' const Actions = { VDC_ALLOCATE, @@ -52,7 +53,7 @@ const Actions = { VDC_DELVNET, VDC_INFO, VDC_POOL_INFO -}; +} module.exports = { Actions, @@ -305,4 +306,4 @@ module.exports = { } } } -}; +} diff --git a/src/fireedge/src/server/utils/constants/commands/vm.js b/src/fireedge/src/server/utils/constants/commands/vm.js index 0c1b3a5ea6..f121b6cfac 100644 --- a/src/fireedge/src/server/utils/constants/commands/vm.js +++ b/src/fireedge/src/server/utils/constants/commands/vm.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { from: { resource, postBody, query }, diff --git a/src/fireedge/src/server/utils/constants/commands/vmgroup.js b/src/fireedge/src/server/utils/constants/commands/vmgroup.js index 9ef058ff10..8e54be4a52 100644 --- a/src/fireedge/src/server/utils/constants/commands/vmgroup.js +++ b/src/fireedge/src/server/utils/constants/commands/vmgroup.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { from: { resource, postBody, query }, diff --git a/src/fireedge/src/server/utils/constants/commands/vn.js b/src/fireedge/src/server/utils/constants/commands/vn.js index f0db78c220..95a344dea7 100644 --- a/src/fireedge/src/server/utils/constants/commands/vn.js +++ b/src/fireedge/src/server/utils/constants/commands/vn.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { from: { resource, postBody, query }, diff --git a/src/fireedge/src/server/utils/constants/commands/vntemplate.js b/src/fireedge/src/server/utils/constants/commands/vntemplate.js index 71a365f304..7e4108457a 100644 --- a/src/fireedge/src/server/utils/constants/commands/vntemplate.js +++ b/src/fireedge/src/server/utils/constants/commands/vntemplate.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { from: { resource, postBody, query }, diff --git a/src/fireedge/src/server/utils/constants/commands/vrouter.js b/src/fireedge/src/server/utils/constants/commands/vrouter.js index 9d1e346e8a..ae6aad3528 100644 --- a/src/fireedge/src/server/utils/constants/commands/vrouter.js +++ b/src/fireedge/src/server/utils/constants/commands/vrouter.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { from: { resource, postBody, query }, diff --git a/src/fireedge/src/server/utils/constants/commands/zone.js b/src/fireedge/src/server/utils/constants/commands/zone.js index a5728b6cc5..5bb5447774 100644 --- a/src/fireedge/src/server/utils/constants/commands/zone.js +++ b/src/fireedge/src/server/utils/constants/commands/zone.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { from: { resource, postBody, query }, diff --git a/src/fireedge/src/server/utils/constants/defaults.js b/src/fireedge/src/server/utils/constants/defaults.js index 045e7bf1fa..3a25755aea 100644 --- a/src/fireedge/src/server/utils/constants/defaults.js +++ b/src/fireedge/src/server/utils/constants/defaults.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const appName = 'fireedge' const appNameSunstone = 'sunstone' @@ -34,6 +35,11 @@ const defaultIp = 'localhost' const protocol = 'http' const defaults = { defaultTypeCrypto: 'aes-256-cbc', + /** + * Empty function. + * + * @returns {undefined} undefined data + */ defaultEmptyFunction: () => undefined, defaultErrorTemplate: 'ERROR_FIREEDGE="%1$s"', defaultOpennebulaExpiration: 180, diff --git a/src/fireedge/src/server/utils/constants/http-codes.js b/src/fireedge/src/server/utils/constants/http-codes.js index 61a154cd45..d9955a7721 100644 --- a/src/fireedge/src/server/utils/constants/http-codes.js +++ b/src/fireedge/src/server/utils/constants/http-codes.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const httpCodes = { badRequest: { @@ -46,6 +47,6 @@ const httpCodes = { id: 200, message: 'OK' } -}; +} -module.exports = httpCodes; +module.exports = httpCodes diff --git a/src/fireedge/src/server/utils/constants/index.js b/src/fireedge/src/server/utils/constants/index.js index 3ddc57dfd1..abd0a3f952 100644 --- a/src/fireedge/src/server/utils/constants/index.js +++ b/src/fireedge/src/server/utils/constants/index.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const defaults = require('./defaults') const httpCodes = require('./http-codes') diff --git a/src/fireedge/src/server/utils/constants/params.js b/src/fireedge/src/server/utils/constants/params.js index ed8ada857a..ad06a7c6f4 100644 --- a/src/fireedge/src/server/utils/constants/params.js +++ b/src/fireedge/src/server/utils/constants/params.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const params = ['method', 'id', 'id2'] module.exports = params diff --git a/src/fireedge/src/server/utils/general.js b/src/fireedge/src/server/utils/general.js index 495d03c95b..568585acc6 100644 --- a/src/fireedge/src/server/utils/general.js +++ b/src/fireedge/src/server/utils/general.js @@ -1,21 +1,30 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ // const colors = require('colors') const { sprintf } = require('sprintf-js') +/** + * Message in CLI (console.log). + * + * @param {object} config - message config: {color,error, message} + * @param {string} config.color - color + * @param {string} config.error - error mesage + * @param {string} config.message - formar error + */ const messageTerminal = ( { color = 'red', @@ -39,6 +48,13 @@ const messageTerminal = ( console.log(consoleColor, sprintf(message, error), reset) } +/** + * Printf. + * + * @param {string} string - text string + * @param {Array} args - values to replace + * @returns {string} string with values resplaced + */ const addPrintf = (string = '', args = '') => { let rtn = string if (string && args) { @@ -50,6 +66,12 @@ const addPrintf = (string = '', args = '') => { return rtn } +/** + * Check of object is empty. + * + * @param {object} obj - object to evaluate + * @returns {boolean} check if object is empty + */ const checkEmptyObject = (obj = {}) => Object.keys(obj).length === 0 && obj.constructor === Object diff --git a/src/fireedge/src/server/utils/index.js b/src/fireedge/src/server/utils/index.js index e409e52fb1..ea5930b680 100644 --- a/src/fireedge/src/server/utils/index.js +++ b/src/fireedge/src/server/utils/index.js @@ -1,42 +1,41 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ -const fs = require('fs-extra') const params = require('./constants/params') -const { defaultTypeLog } = require('./constants/defaults') const functionRoutes = require('../routes/api') const { validateAuth } = require('./jwt') const { httpResponse, getDataZone } = require('./server') const { messageTerminal, addPrintf, checkEmptyObject } = require('./general') const { getConfig } = require('./yml') -// user config -const appConfig = getConfig() - -const mode = appConfig.log || defaultTypeLog - const { responseOpennebula, opennebulaConnect, getMethodForOpennebulaCommand, - commandXML, + commandXMLRPC, getAllowedQueryParams, getRouteForOpennebulaCommand, checkOpennebulaCommand } = require('./opennebula') +/** + * Create params http request state. + * + * @returns {object} params state + */ const createParamsState = () => { const rtn = {} if (params && Array.isArray(params)) { @@ -47,6 +46,11 @@ const createParamsState = () => { return rtn } +/** + * Create Queries http request state. + * + * @returns {object} queries state + */ const createQueriesState = () => { const rtn = {} const queries = getAllowedQueryParams() @@ -58,38 +62,13 @@ const createQueriesState = () => { return rtn } -const includeMAPSJSbyHTML = (path = '') => { - let scripts = '' - if (mode === defaultTypeLog) { - fs.readdirSync(path).forEach(file => { - if (file.match(/\w*\.js\.map+$\b/gi)) { - scripts += `` - } - }) - } - return scripts -} - -const includeJSbyHTML = (path = '') => { - let scripts = '' - fs.readdirSync(path).forEach(file => { - if (file.match(/\w*\.js+$\b/gi)) { - scripts += `` - } - }) - return scripts -} - -const includeCSSbyHTML = (path = '') => { - let scripts = '' - fs.readdirSync(path).forEach(file => { - if (file.match(/\w*\.css+$\b/gi)) { - scripts += `` - } - }) - return scripts -} - +/** + * Check method in route function. + * + * @param {object} routeFunction - data function route + * @param {string} httpMethod - http method + * @returns {Function} function route + */ const checkMethodRouteFunction = (routeFunction, httpMethod = '') => { let rtn if ( @@ -104,11 +83,24 @@ const checkMethodRouteFunction = (routeFunction, httpMethod = '') => { return rtn } +/** + * Check if route id a function. + * + * @param {string} route - route + * @param {string} httpMethod - http method + * @returns {object} route function + */ const checkIfIsARouteFunction = (route, httpMethod) => { let rtn = false if (route && route.length) { const { private: functionPrivate, public: functionPublic } = functionRoutes const functions = [...functionPrivate, ...functionPublic] + /** + * Finder command. + * + * @param {object} rtnCommand - command to validate + * @returns {object} command + */ const finderCommand = rtnCommand => rtnCommand && rtnCommand.endpoint && @@ -131,14 +123,11 @@ module.exports = { getAllowedQueryParams, createQueriesState, opennebulaConnect, - includeMAPSJSbyHTML, - includeJSbyHTML, - includeCSSbyHTML, messageTerminal, addPrintf, getRouteForOpennebulaCommand, getMethodForOpennebulaCommand, - commandXML, + commandXMLRPC, checkIfIsARouteFunction, checkOpennebulaCommand, checkMethodRouteFunction, diff --git a/src/fireedge/src/server/utils/jwt.js b/src/fireedge/src/server/utils/jwt.js index bda1d61129..90073351c5 100644 --- a/src/fireedge/src/server/utils/jwt.js +++ b/src/fireedge/src/server/utils/jwt.js @@ -1,22 +1,35 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const jwt = require('jwt-simple') const { messageTerminal } = require('./general') +const speakeasy = require('speakeasy') -const createToken = ( +/** + * Create a JWT. + * + * @param {object} param0 - object of data to JWT {ID, USER, Opennebula_token} + * @param {string} param0.id - user ID + * @param {string} param0.user - username + * @param {string} param0.token - token opennebula + * @param {number} iat - epoch create time (now) + * @param {number} exp - epoch expire time + * @returns {string} JWT + */ +const createJWT = ( { id: iss, user: aud, token: jti }, iat = '', exp = '' @@ -35,7 +48,13 @@ const createToken = ( return rtn } -const validateAuth = req => { +/** + * Validate auth (JWT). + * + * @param {object} req - http request + * @returns {object} return data of JWT + */ +const validateAuth = (req = {}) => { let rtn = false if (req && req.headers && req.headers.authorization) { const authorization = req.headers.authorization @@ -78,7 +97,27 @@ const validateAuth = req => { return rtn } -module.exports = { - createToken, - validateAuth +/** + * Check 2FA. + * + * @param {string} secret - secret key + * @param {string} token - token JWT + * @returns {string} data decoded + */ +const check2Fa = (secret = '', token = '') => { + let rtn = false + if (secret && token) { + rtn = speakeasy.totp.verify({ + secret, + encoding: 'base32', + token + }) + } + return rtn +} + +module.exports = { + createJWT, + validateAuth, + check2Fa } diff --git a/src/fireedge/src/server/utils/opennebula.js b/src/fireedge/src/server/utils/opennebula.js index 199db22f77..00e3eca713 100644 --- a/src/fireedge/src/server/utils/opennebula.js +++ b/src/fireedge/src/server/utils/opennebula.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const upcast = require('upcast') // eslint-disable-next-line node/no-deprecated-api @@ -20,12 +21,12 @@ const rpc = require('xmlrpc') const xml2js = require('xml2js') const { Map } = require('immutable') const { sprintf } = require('sprintf-js') -const speakeasy = require('speakeasy') const { global } = require('window-or-global') const httpCodes = require('./constants/http-codes') const commandsParams = require('./constants/commands') const { from, + defaultEmptyFunction, defaultNamespace, defaultMessageProblemOpennebula } = require('./constants/defaults') @@ -39,6 +40,13 @@ const { getConfig } = require('./yml') const appConfig = getConfig() const namespace = appConfig.namespace || defaultNamespace +/** + * Authorizes if the user has access to the resource, for their connection to the HOOK. + * + * @param {string} username - username + * @param {string} action - action (only valid i the action terminate in .info) + * @param {Array} parameters - parameters of xmlrpc. If parameters[0] >=0 is a id of resource + */ const fillResourceforHookConection = (username = '', action = '', parameters = '') => { let match // parameters[0] is the resource ID @@ -56,16 +64,24 @@ const fillResourceforHookConection = (username = '', action = '', parameters = ' } } -const opennebulaConnect = (username = '', password = '', path = '') => { - let rtn = () => null - if (username && password && path) { +/** + * Opennebula XMLRPC function. + * + * @param {string} username - opennebula user + * @param {string} password - opennebula password + * @param {string} zoneURL - opennebula zone URL + * @returns {Function} executer XMLRPC function + */ +const opennebulaConnect = (username = '', password = '', zoneURL = '') => { + let rtn = defaultEmptyFunction + if (username && password && zoneURL) { let xmlClient = null - const parsedHostname = parse(path) + const parsedHostname = parse(zoneURL) const protocol = parsedHostname.protocol if (protocol === 'https:') { - xmlClient = rpc.createSecureClient(path) + xmlClient = rpc.createSecureClient(zoneURL) } else { - xmlClient = rpc.createClient(path) + xmlClient = rpc.createClient(zoneURL) } if (xmlClient && xmlClient.methodCall) { rtn = (action = '', parameters = [], callback = () => undefined, fillHookResource = true) => { @@ -160,6 +176,15 @@ const opennebulaConnect = (username = '', password = '', path = '') => { return rtn } +/** + * Set http response. + * + * @param {object} res - http response + * @param {object} err - error + * @param {object} value - data if no has a error + * @param {Function} response - callback function, run when has value + * @param {Function} next - express stepper + */ const responseOpennebula = (res, err, value, response, next) => { if (err && res && typeof res === 'function') { const { internalServerError } = httpCodes @@ -172,6 +197,11 @@ const responseOpennebula = (res, err, value, response, next) => { } } +/** + * Get the method valid by the opennebula command. + * + * @returns {Array} opennebula command + */ const getMethodForOpennebulaCommand = () => { const rtn = [] if (commandsParams) { @@ -188,7 +218,15 @@ const getMethodForOpennebulaCommand = () => { return rtn } -const commandXML = (resource = '', method = '', defaultMethod = '') => { +/** + * Get a command to XMLRPC. + * + * @param {string} resource - resource + * @param {string} method - method + * @param {string} defaultMethod - default method + * @returns {string} command to XMLRPC + */ +const commandXMLRPC = (resource = '', method = '', defaultMethod = '') => { let command = '' const allowedActions = getMethodForOpennebulaCommand() if (resource && resource.length) { @@ -205,6 +243,11 @@ const commandXML = (resource = '', method = '', defaultMethod = '') => { return command } +/** + * Get allowed query parameters. + * + * @returns {Array} query parameters + */ const getAllowedQueryParams = () => { const rtn = [] const allowedQuerys = Object.keys(commandsParams) @@ -230,6 +273,11 @@ const getAllowedQueryParams = () => { return rtn } +/** + * Get route for opennebula command. + * + * @returns {Array} command + */ const getRouteForOpennebulaCommand = () => { const rtn = [] if (commandsParams) { @@ -238,6 +286,13 @@ const getRouteForOpennebulaCommand = () => { if (command && command.length) { let commandString = command.split('.') commandString = commandString[0] + + /** + * Finder command. + * + * @param {object} rtnCommand - command to validate + * @returns {string} - command + */ const finderCommand = rtnCommand => rtnCommand && rtnCommand.endpoint && @@ -251,6 +306,12 @@ const getRouteForOpennebulaCommand = () => { return rtn } +/** + * Check position in data source. + * + * @param {object} dataSource - source data + * @returns {boolean} check if exists + */ const checkPositionInDataSource = dataSource => { let rtn = true if (dataSource && from) { @@ -263,7 +324,14 @@ const checkPositionInDataSource = dataSource => { } return rtn } - +/** + * Check if http command exist in commands.js. + * + * @param {string} command - openenbula command + * @param {string} method - method of opennebula command + * @param {boolean} commandParams - commands + * @returns {object} command opennebula + */ const checkOpennebulaCommand = ( command = '', method = '', @@ -315,7 +383,14 @@ const checkOpennebulaCommand = ( return rtn } -const paramsDefaultByCommandOpennebula = (command = '', httpCode = '') => { +/** + * Get default params of opennebula command. + * + * @param {string} command - opennebula command + * @param {object} httpCode - http code + * @returns {Array} array defaults of command + */ +const getDefaultParamsOfOpennebulaCommand = (command = '', httpCode = '') => { const rtn = [] if (command && httpCode) { const getDefaultValues = checkOpennebulaCommand(command, httpCode, false) @@ -336,7 +411,16 @@ const paramsDefaultByCommandOpennebula = (command = '', httpCode = '') => { return rtn } -const generateNewTemplate = ( +/** + * Generate a new resource template opennebula. + * + * @param {object} current - actual resource template + * @param {object} addPositions - new positions of resource template + * @param {object} removedPositions - delete positions of resource template + * @param {string} wrapper - wrapper data of template + * @returns {string} opennebula template + */ +const generateNewResourceTemplate = ( current = {}, addPositions = {}, removedPositions = [], @@ -355,28 +439,15 @@ const generateNewTemplate = ( return sprintf(wrapper, positions) } -const check2Fa = (secret = '', token = '') => { - let rtn = false - if (secret && token) { - rtn = speakeasy.totp.verify({ - secret, - encoding: 'base32', - token - }) - } - return rtn -} - module.exports = { opennebulaConnect, responseOpennebula, getMethodForOpennebulaCommand, - commandXML, + commandXMLRPC, getAllowedQueryParams, getRouteForOpennebulaCommand, checkPositionInDataSource, checkOpennebulaCommand, - paramsDefaultByCommandOpennebula, - generateNewTemplate, - check2Fa + getDefaultParamsOfOpennebulaCommand, + generateNewResourceTemplate } diff --git a/src/fireedge/src/server/utils/server.js b/src/fireedge/src/server/utils/server.js index 3d83f6b54c..5c1b03c16a 100644 --- a/src/fireedge/src/server/utils/server.js +++ b/src/fireedge/src/server/utils/server.js @@ -1,21 +1,23 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ + const { env } = require('process') const { Map } = require('immutable') const { global } = require('window-or-global') -const { dirname, resolve } = require('path') +const { resolve } = require('path') // eslint-disable-next-line node/no-deprecated-api const { createCipheriv, createCipher, createDecipheriv, createDecipher, createHash } = require('crypto') const { existsSync, readFileSync, createWriteStream, readdirSync, statSync } = require('fs-extra') @@ -47,23 +49,42 @@ const { let cert = '' let key = '' -const fillFunctionRoute = (method, endpoint, action) => ({ +/** + * Set functions as routes. + * + * @param {string} method - http methof + * @param {string} endpoint - http endpoint + * @param {string} action - opennebula action + * @returns {object} object function + */ +const setFunctionRoute = (method, endpoint, action) => ({ httpMethod: method, endpoint, action }) -const addFunctionToRoute = (req = {}, res = {}, next = defaultEmptyFunction, routes = {}, user = {}, oneConnection = defaultEmptyFunction, index = 0) => { +/** + * Add function as express route. + * + * @param {object} req - http request + * @param {object} res - http response + * @param {Function} next - express stepper + * @param {object} routes - new routes + * @param {object} user - user Data + * @param {Function} oneConnection - function one XMLRPC + * @param {string} index - resource index + */ +const addFunctionAsRoute = (req = {}, res = {}, next = defaultEmptyFunction, routes = {}, user = {}, oneConnection = defaultEmptyFunction, index = 0) => { const resources = Object.keys(req[fromData.resource]) if (req && res && next && routes) { const route = routes[`${req[fromData.resource][resources[index]]}`.toLowerCase()] if (req && fromData && fromData.resource && req[fromData.resource] && route) { if (Object.keys(route).length > 0 && route.constructor === Object) { if (route.action && route.params && typeof route.action === 'function') { - const params = getParamsForObject(route.params, req) + const params = getRequestParameters(route.params, req) route.action(res, next, params, user, oneConnection) } else { - addFunctionToRoute(req, res, next, route, user, oneConnection, index + 1) + addFunctionAsRoute(req, res, next, route, user, oneConnection, index + 1) } } else { next() @@ -76,6 +97,11 @@ const addFunctionToRoute = (req = {}, res = {}, next = defaultEmptyFunction, rou } } +/** + * Validate if server app have certs. + * + * @returns {boolean} file certs + */ const validateServerIsSecure = () => { const folder = 'cert/' const dirCerts = env && env.NODE_ENV === defaultWebpackMode ? ['../', '../', '../', folder] : ['../', folder] @@ -84,11 +110,29 @@ const validateServerIsSecure = () => { key = `${pathfile}key.pem` return existsSync && key && cert && existsSync(key) && existsSync(cert) } - +/** + * Get certificate SSL. + * + * @returns {string} ssl path + */ const getCert = () => cert + +/** + * Get key of certificate SSL. + * + * @returns {string} key ssl path + */ const getKey = () => key -const httpResponse = (response, data, message) => { +/** + * Response http. + * + * @param {object} response - response http + * @param {string} data - data for response http + * @param {string} message - message + * @returns {object} {data, message, id} + */ +const httpResponse = (response = null, data = '', message = '') => { let rtn = Map(internalServerError).toObject() rtn.data = data if (response) { @@ -102,7 +146,14 @@ const httpResponse = (response, data, message) => { } return rtn } -const returnQueryData = (server = {}) => { + +/** + * Get Query data for websockets. + * + * @param {object} server - express app + * @returns {object} queries http + */ +const getQueryData = (server = {}) => { let rtn = {} if ( server && @@ -114,9 +165,15 @@ const returnQueryData = (server = {}) => { return rtn } +/** + * Validate Authentication for websocket. + * + * @param {object} server - express app + * @returns {boolean} if token is valid + */ const validateAuthWebsocket = (server = {}) => { let rtn - const { token } = returnQueryData(server) + const { token } = getQueryData(server) if (token) { rtn = validateAuth({ headers: { authorization: token } @@ -125,14 +182,26 @@ const validateAuthWebsocket = (server = {}) => { return rtn } -const getResourceForHookConnection = (server = {}) => { - const { id, resource } = returnQueryData(server) +/** + * Get resource data for http request. + * + * @param {object} server - express app + * @returns {object} request data + */ +const getResourceDataForRequest = (server = {}) => { + const { id, resource } = getQueryData(server) const { aud: username } = validateAuthWebsocket(server) return { id, resource, username } } +/** + * MIDDLEWARE Websockets, validates if the user has permissions for the resource hook. + * + * @param {object} server - express app + * @param {Function} next - express stepper + */ const middlewareValidateResourceForHookConnection = (server = {}, next = () => undefined) => { - const { id, resource, username } = getResourceForHookConnection(server) + const { id, resource, username } = getResourceDataForRequest(server) if ( id && resource && @@ -150,6 +219,12 @@ const middlewareValidateResourceForHookConnection = (server = {}, next = () => u } } +/** + * MIDDLEWARE Websockets, authenticate user. + * + * @param {object} server - express app + * @param {Function} next - express stepper + */ const middlewareValidateAuthWebsocket = (server = {}, next = () => undefined) => { if (validateAuthWebsocket(server)) { next() @@ -158,6 +233,14 @@ const middlewareValidateAuthWebsocket = (server = {}, next = () => undefined) => } } +/** + * Encrypt. + * + * @param {string} data - data to encrypt + * @param {string} key - key to encrypt data + * @param {string} iv - initialization vector to encrypt data + * @returns {string} data encrypt + */ const encrypt = (data = '', key = '', iv = '') => { let rtn if (data && key) { @@ -178,6 +261,14 @@ const encrypt = (data = '', key = '', iv = '') => { return rtn } +/** + * Decrypt. + * + * @param {string} data - data to decrypt + * @param {string} key - key to decrypt data + * @param {string} iv - initialization vector to decrypt data + * @returns {string} data decrypt + */ const decrypt = (data = '', key = '', iv = '') => { let rtn if (data && key) { @@ -198,7 +289,15 @@ const decrypt = (data = '', key = '', iv = '') => { return rtn } -const existsFile = (path = '', success = () => undefined, error = () => undefined) => { +/** + * Check if file exist. + * + * @param {string} path - path of file + * @param {Function} success - function executed when file exist + * @param {Function} error - function executed when file no exists + * @returns {boolean} validate if file exists + */ +const existsFile = (path = '', success = defaultEmptyFunction, error = defaultEmptyFunction) => { let rtn = false let file let errorData @@ -224,6 +323,15 @@ const existsFile = (path = '', success = () => undefined, error = () => undefine return rtn } +/** + * Create a file. + * + * @param {string} path - path of file + * @param {string} data - content of file + * @param {Function} callback - run function when file is created + * @param {Function} error - run function when file creation failed + * @returns {boolean} check if file is created + */ const createFile = (path = '', data = '', callback = () => undefined, error = () => undefined) => { let rtn = false try { @@ -237,6 +345,9 @@ const createFile = (path = '', data = '', callback = () => undefined, error = () return rtn } +/** + * Generate fireedge key file. + */ const genFireedgeKey = () => { if (global && !global.FIREEDGE_KEY) { const { v4 } = require('uuid') @@ -266,6 +377,12 @@ const genFireedgeKey = () => { } } +/** + * Replace escape sequence. + * + * @param {string} text - string to clean + * @returns {string} clean string + */ const replaceEscapeSequence = (text = '') => { let rtn = text if (text) { @@ -274,6 +391,11 @@ const replaceEscapeSequence = (text = '') => { return rtn } +/** + * Get sunstone auth. + * + * @returns {object} credentials of serveradmin + */ const getSunstoneAuth = () => { let rtn if (global && global.SUNSTONE_AUTH_PATH) { @@ -302,6 +424,13 @@ const getSunstoneAuth = () => { return rtn } +/** + * Get data of zone. + * + * @param {string} zone - zone id + * @param {string} configuredZones - default zones + * @returns {object} data zone + */ const getDataZone = (zone = '0', configuredZones) => { let rtn const zones = (global && global.zones) || configuredZones || defaultOpennebulaZones @@ -316,6 +445,9 @@ const getDataZone = (zone = '0', configuredZones) => { return rtn } +/** + * Generate a resource paths. + */ const genPathResources = () => { const ONE_LOCATION = env && env.ONE_LOCATION const LOG_LOCATION = !ONE_LOCATION ? defaultLogPath : `${ONE_LOCATION}/var` @@ -361,7 +493,14 @@ const genPathResources = () => { } } -const getParamsForObject = (params = {}, req = {}) => { +/** + * Get http params. + * + * @param {object} params - finder params + * @param {object} req - http request + * @returns {object} params by functions + */ +const getRequestParameters = (params = {}, req = {}) => { const rtn = {} if (params && Object.keys(params).length > 0 && params.constructor === Object) { Object.entries(params).forEach(([param, value]) => { @@ -373,12 +512,27 @@ const getParamsForObject = (params = {}, req = {}) => { return rtn } +/** + * Error format template (console.log). + * + * @param {string} err - error message + * @param {string} message - format + * @returns {object} {color, message, error} format for the messageTerminal function + */ const defaultError = (err = '', message = 'Error: %s') => ({ color: 'red', message, error: err || '' }) +/** + * Get files by path. + * + * @param {string} path - path of files + * @param {boolean} recursive - find all files into path + * @param {Array} files - for recursion + * @returns {Array} files + */ const getFiles = (path = '', recursive = false, files = []) => { if (path) { try { @@ -403,7 +557,15 @@ const getFiles = (path = '', recursive = false, files = []) => { return files } -const getFilesbyEXT = (dir = '', ext = '', errorCallback = () => undefined) => { +/** + * Get files by ext. + * + * @param {string} dir - path + * @param {string} ext - ext + * @param {Function} errorCallback - run this function if it cant read directory + * @returns {Array} array of pathfiles + */ +const getFilesbyEXT = (dir = '', ext = '', errorCallback = defaultEmptyFunction) => { const pathFiles = [] if (dir && ext) { const exp = new RegExp('\\w*\\.' + ext + '+$\\b', 'gi') @@ -428,6 +590,13 @@ const getFilesbyEXT = (dir = '', ext = '', errorCallback = () => undefined) => { return pathFiles } +/** + * Get directories for path. + * + * @param {string} dir - path + * @param {Function} errorCallback - run this function if it cant read directory + * @returns {Array} directories + */ const getDirectories = (dir = '', errorCallback = () => undefined) => { const directories = [] if (dir) { @@ -448,6 +617,12 @@ const getDirectories = (dir = '', errorCallback = () => undefined) => { return directories } +/** + * Parse post data. + * + * @param {object} postData - port data + * @returns {object} data parsed + */ const parsePostData = (postData = {}) => { const rtn = {} Object.entries(postData).forEach(([key, value]) => { @@ -466,8 +641,8 @@ const parsePostData = (postData = {}) => { return rtn } module.exports = { - fillFunctionRoute, - addFunctionToRoute, + setFunctionRoute, + addFunctionAsRoute, encrypt, decrypt, getDataZone, @@ -482,9 +657,9 @@ module.exports = { getCert, getKey, parsePostData, - getParamsForObject, - returnQueryData, - getResourceForHookConnection, + getRequestParameters, + getQueryData, + getResourceDataForRequest, middlewareValidateAuthWebsocket, middlewareValidateResourceForHookConnection, defaultError, diff --git a/src/fireedge/src/server/utils/yml.js b/src/fireedge/src/server/utils/yml.js index 38b8e1a591..e7f558fded 100644 --- a/src/fireedge/src/server/utils/yml.js +++ b/src/fireedge/src/server/utils/yml.js @@ -1,17 +1,18 @@ -/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */ -/* */ -/* 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. */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * 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. * + * ------------------------------------------------------------------------- */ const { resolve } = require('path') const { parse } = require('yaml') @@ -20,6 +21,11 @@ const { existsFile } = require('server/utils/server') const { messageTerminal } = require('server/utils/general') const { global } = require('window-or-global') +/** + * Get fireedge configurations. + * + * @returns {object} fireedge configurations + */ const getConfig = () => { let rtn = {} const pathfile = global.FIREEDGE_CONFIG || resolve(__dirname, '../', '../', '../', 'etc', defaultConfigFile) diff --git a/src/fireedge/webpack.config.prod.client.js b/src/fireedge/webpack.config.prod.client.js index 533d6f8d01..1758acab77 100644 --- a/src/fireedge/webpack.config.prod.client.js +++ b/src/fireedge/webpack.config.prod.client.js @@ -26,6 +26,11 @@ const js = { include: path.resolve(__dirname, 'src') } +/** + * bundle app + * @param {object} {assets, appname} + * @returns {object} webpack config + */ const bundle = ({ assets = false, name = 'flow' }) => { const plugins = [ new TimeFixPlugin(),