1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-21 14:50:08 +03:00

B #3951: add support api same route (#197)

Signed-off-by: Jorge Lobo <jlobo@opennebula.io>
This commit is contained in:
Jorge Miguel Lobo Escalona 2020-09-10 10:05:36 +02:00 committed by GitHub
parent 5ce49ed404
commit 8ba93bc902
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 124 additions and 60 deletions

View File

@ -16,7 +16,7 @@
import React, { useState, useEffect } from 'react';
import io from 'socket.io-client';
import { findStorageData } from '../../../utils';
import constants from '../../../constants';
import constants from 'client/constants';
const { jwtName } = constants;

View File

@ -14,7 +14,7 @@
/* -------------------------------------------------------------------------- */
import React from 'react';
import { hydrate } from 'react-dom';
import { hydrate, render } from 'react-dom';
import { createStore } from 'redux';
import root from 'window-or-global';
@ -38,5 +38,7 @@ const element = document.getElementById('preloadState');
if (element) {
element.remove();
}
const mainDiv = document.getElementById('root');
const renderMethod = mainDiv && mainDiv.innerHTML !== '' ? hydrate : render;
hydrate(<App store={store} />, document.getElementById('root'));
renderMethod(<App store={store} />, document.getElementById('root'));

View File

@ -40,9 +40,8 @@ const {
} = require('./routes/zendesk');
const opennebulaActions = getRouteForOpennebulaCommand();
const routes = {
private: {
private: [
...opennebulaActions,
...functions2faPrivate,
...functionsAuthPrivate,
@ -50,14 +49,14 @@ const routes = {
...functionsSupportPrivate,
...functionsVcenterPrivate,
...functionsZendeskPrivate
},
public: {
],
public: [
...functions2faPublic,
...functionsAuthPublic,
...functionsOneflowPublic,
...functionsSupportPublic,
...functionsVcenterPublic,
...functionsZendeskPublic
}
]
};
module.exports = routes;

View File

@ -25,6 +25,11 @@ const {
default2FAOpennebulaVar,
default2FAOpennebulaTmpVar
} = require('../../../../utils/constants/defaults');
const {
TWO_FACTOR_QR,
TWO_FACTOR_DELETE,
TWO_FACTOR_SETUP
} = require('./string-routes');
const {
ok,
unauthorized,
@ -91,9 +96,10 @@ const getUserInfoAuthenticated = (connect, userId, callback, next) => {
}
};
const privateRoutes = {
'2fqr': {
const privateRoutes = [
{
httpMethod: POST,
endpoint: TWO_FACTOR_QR,
action: (req, res, next, connect, userId) => {
const secret = speakeasy.generateSecret({
length: 10,
@ -163,8 +169,9 @@ const privateRoutes = {
}
}
},
'2fsetup': {
{
httpMethod: POST,
endpoint: TWO_FACTOR_SETUP,
action: (req, res, next, connect, userId) => {
const connectOpennebula = connect();
getUserInfoAuthenticated(
@ -231,8 +238,9 @@ const privateRoutes = {
);
}
},
'2fdelete': {
{
httpMethod: DELETE,
endpoint: TWO_FACTOR_DELETE,
action: (req, res, next, connect, userId) => {
const connectOpennebula = connect();
getUserInfoAuthenticated(
@ -283,9 +291,9 @@ const privateRoutes = {
);
}
}
};
];
const publicRoutes = {};
const publicRoutes = [];
const functionRoutes = {
private: privateRoutes,

View File

@ -0,0 +1,11 @@
const TWO_FACTOR_SETUP = '2fsetup';
const TWO_FACTOR_QR = '2fqr';
const TWO_FACTOR_DELETE = '2fdelete';
const Actions = {
TWO_FACTOR_SETUP,
TWO_FACTOR_QR,
TWO_FACTOR_DELETE
};
module.exports = Actions;

View File

@ -107,7 +107,7 @@ const setDates = () => {
const { MIN, MAX } = limitToken;
now = DateTime.local();
nowUnix = now.toSeconds();
nowWithDays = now.plus({"days": extended ? MAX : MIN});
nowWithDays = now.plus({ days: extended ? MAX : MIN });
const diff = nowWithDays.diff(now, 'seconds');
relativeTime = diff.seconds;
};

View File

@ -14,6 +14,9 @@
/* -------------------------------------------------------------------------- */
const { Map } = require('immutable');
const {
AUTH,
} = require('./string-routes');
const {
httpMethod,
defaultMethodLogin
@ -48,11 +51,12 @@ const {
const { POST } = httpMethod;
const privateRoutes = {};
const privateRoutes = [];
const publicRoutes = {
auth: {
const publicRoutes = [
{
httpMethod: POST,
endpoint: AUTH,
action: (req, res, next, connect) => {
if (req && res && connect) {
setReq(req);
@ -119,7 +123,7 @@ const publicRoutes = {
}
}
}
};
];
const functionRoutes = {
private: privateRoutes,

View File

@ -0,0 +1,7 @@
const AUTH = 'auth';
const Actions = {
AUTH
};
module.exports = Actions;

View File

@ -42,62 +42,74 @@ const {
SERVICE_TEMPLATE_CREATE,
SERVICE_TEMPLATE_UPDATE,
SERVICE_TEMPLATE_ACTION
} = require('./oneflow-routes');
} = require('./string-routes');
const { GET, POST, DELETE, PUT } = httpMethod;
const privateRoutes = {
[SERVICE_ALL]: {
const privateRoutes = [
{
httpMethod: GET,
endpoint: SERVICE_ALL,
action: serviceAll
},
[SERVICE]: {
{
httpMethod: GET,
endpoint: SERVICE,
action: service
},
[SERVICE_DELETE]: {
{
httpMethod: DELETE,
endpoint: SERVICE_DELETE,
action: serviceDelete
},
[SERVICE_ADD_ACTION]: {
{
httpMethod: POST,
endpoint: SERVICE_ADD_ACTION,
action: serviceAddAction
},
[SERVICE_ADD_SCALE]: {
{
httpMethod: POST,
endpoint: SERVICE_ADD_SCALE,
action: serviceAddScale
},
[SERVICE_ADD_ROLE_ACTION]: {
{
httpMethod: POST,
endpoint: SERVICE_ADD_ROLE_ACTION,
action: serviceAddRoleAction
},
[SERVICE_TEMPLATE_ALL]: {
{
httpMethod: GET,
endpoint: SERVICE_TEMPLATE_ALL,
action: serviceTemplateAll
},
[SERVICE_TEMPLATE]: {
{
httpMethod: GET,
endpoint: SERVICE_TEMPLATE,
action: serviceTemplate
},
[SERVICE_TEMPLATE_DELETE]: {
{
httpMethod: DELETE,
endpoint: SERVICE_TEMPLATE_DELETE,
action: serviceTemplateDelete
},
[SERVICE_TEMPLATE_CREATE]: {
{
httpMethod: POST,
endpoint: SERVICE_TEMPLATE_CREATE,
action: serviceTemplateCreate
},
[SERVICE_TEMPLATE_UPDATE]: {
{
httpMethod: PUT,
endpoint: SERVICE_TEMPLATE_UPDATE,
action: serviceTemplateUpdate
},
[SERVICE_TEMPLATE_ACTION]: {
{
httpMethod: POST,
endpoint: SERVICE_TEMPLATE_ACTION,
action: serviceTemplateAction
}
};
];
const publicRoutes = {};
const publicRoutes = [];
const functionRoutes = {
private: privateRoutes,

View File

@ -13,9 +13,9 @@
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
const privateRoutes = {};
const privateRoutes = [];
const publicRoutes = {};
const publicRoutes = [];
const functionRoutes = {
private: privateRoutes,

View File

@ -13,9 +13,9 @@
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
const privateRoutes = {};
const privateRoutes = [];
const publicRoutes = {};
const publicRoutes = [];
const functionRoutes = {
private: privateRoutes,

View File

@ -13,9 +13,9 @@
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
const privateRoutes = {};
const privateRoutes = [];
const publicRoutes = {};
const publicRoutes = [];
const functionRoutes = {
private: privateRoutes,

View File

@ -17,10 +17,10 @@ const express = require('express');
const { defaults, httpCodes, params } = require('../../utils/constants');
const {
opennebulaConnect,
checkRouteFunction,
checkIfIsARouteFunction,
commandXML,
checkOpennebulaCommand,
validateRouteFunction,
checkMethodRouteFunction,
responseOpennebula,
httpResponse
} = require('../../utils');
@ -85,7 +85,7 @@ router.all(
pass = getPassOpennebula()
) => opennebulaConnect(user, pass, RPC);
const { resource } = req.params;
const routeFunction = checkRouteFunction(resource);
const routeFunction = checkIfIsARouteFunction(resource, httpMethod);
res.locals.httpCode = httpResponse(methodNotAllowed);
const dataSources = {
[fromData.resource]: getParamsState(),
@ -93,7 +93,7 @@ router.all(
[fromData.postBody]: req.body
};
if (routeFunction) {
const valRouteFunction = validateRouteFunction(
const valRouteFunction = checkMethodRouteFunction(
routeFunction,
httpMethod
);

View File

@ -46,7 +46,9 @@ const validateResource = (req, res, next) => {
if (req && req.params && req.params.resource) {
const resource = req.params.resource;
status = serviceUnavailable;
if (resource in authenticated) {
const finderCommand = rtnCommand =>
rtnCommand && rtnCommand.endpoint && rtnCommand.endpoint === resource;
if (authenticated.some(finderCommand)) {
const session = validateAuth(req);
if (
session &&
@ -76,7 +78,7 @@ const validateResource = (req, res, next) => {
}
status = unauthorized;
}
if (resource in nonAuthenticated) {
if (nonAuthenticated.some(finderCommand)) {
next();
return;
}

View File

@ -91,7 +91,7 @@ const includeCSSbyHTML = (path = '') => {
return scripts;
};
const validateRouteFunction = (routeFunction, httpMethod = '') => {
const checkMethodRouteFunction = (routeFunction, httpMethod = '') => {
let rtn;
if (
routeFunction &&
@ -105,12 +105,23 @@ const validateRouteFunction = (routeFunction, httpMethod = '') => {
return rtn;
};
const checkRouteFunction = route => {
const checkIfIsARouteFunction = (route, httpMethod) => {
let rtn = false;
const { private: functionPrivate, public: functionPublic } = functionRoutes;
const functions = { ...functionPrivate, ...functionPublic };
if (route in functions) {
rtn = functions[route];
if (route && route.length) {
const { private: functionPrivate, public: functionPublic } = functionRoutes;
const functions = [...functionPrivate, ...functionPublic];
const finderCommand = rtnCommand =>
rtnCommand &&
rtnCommand.endpoint &&
rtnCommand.endpoint === route &&
rtnCommand.httpMethod &&
rtnCommand.httpMethod === httpMethod &&
rtnCommand.action &&
typeof rtnCommand.action === 'function';
const find = functions.find(finderCommand);
if (find) {
rtn = find;
}
}
return rtn;
};
@ -130,9 +141,9 @@ module.exports = {
getRouteForOpennebulaCommand,
getMethodForOpennebulaCommand,
commandXML,
checkRouteFunction,
checkIfIsARouteFunction,
checkOpennebulaCommand,
validateRouteFunction,
checkMethodRouteFunction,
responseOpennebula,
getConfig,
httpResponse

View File

@ -118,8 +118,11 @@ const getMethodForOpennebulaCommand = () => {
};
const commandXML = (resource = '', method = '', defaultMethod = '') => {
let command = '';
const allowedActions = getMethodForOpennebulaCommand();
let command = `${resource}`;
if (resource && resource.length) {
command = `${resource}`;
}
const commandWithDefault = defaultMethod
? `${command}.${defaultMethod}`
: command;
@ -157,14 +160,19 @@ const getAllowedQueryParams = () => {
};
const getRouteForOpennebulaCommand = () => {
const rtn = {};
const rtn = [];
if (commandsParams) {
const commands = Object.keys(commandsParams);
commands.forEach(command => {
if (command && command.length) {
const commandString = command.split('.');
if (!(commandString[0] in rtn)) {
rtn[commandString[0]] = false; // false is a opennebula command
let commandString = command.split('.');
commandString = commandString[0];
const finderCommand = rtnCommand =>
rtnCommand &&
rtnCommand.endpoint &&
rtnCommand.endpoint === commandString;
if (!rtn.some(finderCommand)) {
rtn.push({ endpoint: commandString });
}
}
});