Skip to content

Commit

Permalink
Add centralized request service (#4831)
Browse files Browse the repository at this point in the history
* create request handler

* Add interceptor initialization to app mount

* Implement request handling service

* Add request disabling to server

* Modify changelog

* Use core plugin as parameter

* Move services to public

* Remove service from backend

* Modify imports of the service

* Add validation for unauthorized requests

* Improve code quality

* Remove duplicated logic

* Add reload on unauthorized requests

* Change when check-wazuh is executed

* Remove unnecessary verifications

* Improve code quality

* fix: condition on request service and replace http status code by constant

* fix: remove unused import

* fix: default value of parameter in request service

Co-authored-by: Federico Rodriguez <[email protected]>
Co-authored-by: Antonio David Gutiérrez <[email protected]>
(cherry picked from commit 99fbab1)
  • Loading branch information
Tostti authored and github-actions[bot] committed Nov 14, 2022
1 parent 8d54cfb commit f365a37
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 57 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ All notable changes to the Wazuh app project will be documented in this file.
- Added validation to the plugin settings in the form of `Settings/Configuration` and the endpoint to update the plugin configuration [#4503](https:/wazuh/wazuh-kibana-app/pull/4503)[#4785](https:/wazuh/wazuh-kibana-app/pull/4785)
- Added new plugin settings to customize the header and footer on the PDF reports [#4505](https:/wazuh/wazuh-kibana-app/pull/4505)[#4798](https:/wazuh/wazuh-kibana-app/pull/4798)[#4805](https:/wazuh/wazuh-kibana-app/pull/4805)
- Add a new plugin setting to enable or disable the customization [#4507](https:/wazuh/wazuh-kibana-app/pull/4507)
- Added a centralized service to handle the requests [#4831](https:/wazuh/wazuh-kibana-app/pull/4831)

### Changed

Expand Down
26 changes: 14 additions & 12 deletions public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import { AppState } from './react-services/app-state';
import { setErrorOrchestrator } from './react-services/common-services';
import { ErrorOrchestratorService } from './react-services/error-orchestrator/error-orchestrator.service';
import { getThemeAssetURL, getAssetURL } from './utils/assets';
import { WzRequest } from './react-services/wz-request';
import store from './redux/store';
import { updateAppConfig } from './redux/actions/appConfigActions';
import { initializeInterceptor } from './services/request-handler';

const SIDEBAR_LOGO = 'customization.logo.sidebar';
const innerAngularName = 'app/wazuh';
Expand All @@ -53,6 +53,16 @@ export class WazuhPlugin implements Plugin<WazuhSetup, WazuhStart, WazuhSetupPlu
console.error('plugin.ts: Error getting logos configuration', error);
}

//Check if user has wazuh disabled and avoid registering the application if not
let response={isWazuhDisabled:1};
try{
response = await core.http.get('/api/check-wazuh');
}
catch(error){
console.error('plugin.ts: Error checking if Wazuh is enabled', error);
}

if (!response.isWazuhDisabled){
core.application.register({
id: `wazuh`,
title: 'Wazuh',
Expand All @@ -62,6 +72,7 @@ export class WazuhPlugin implements Plugin<WazuhSetup, WazuhStart, WazuhSetupPlu
getThemeAssetURL('icon.svg', UI_THEME)),
mount: async (params: AppMountParameters) => {
try {
initializeInterceptor(core);
if (!this.initializeInnerAngular) {
throw Error('Wazuh plugin method initializeInnerAngular is undefined');
}
Expand All @@ -85,21 +96,11 @@ export class WazuhPlugin implements Plugin<WazuhSetup, WazuhStart, WazuhSetupPlu
window.location.reload();
}
await this.initializeInnerAngular();
//Check is user has Wazuh disabled
const response = await WzRequest.genericReq(
'GET',
`/api/check-wazuh`,
)

params.element.classList.add('dscAppWrapper', 'wz-app');
const unmount = await renderApp(innerAngularName, params.element);
//Update if user has Wazuh disabled
this.stateUpdater.next(() => {
if (response.data.isWazuhDisabled) {
unmount();
}
return {
status: response.data.isWazuhDisabled,
status: response.isWazuhDisabled,
category: {
id: 'wazuh',
label: 'Wazuh',
Expand All @@ -122,6 +123,7 @@ export class WazuhPlugin implements Plugin<WazuhSetup, WazuhStart, WazuhSetupPlu
},
updater$: this.stateUpdater
});
}
return {};
}
public start(core: CoreStart, plugins: AppPluginStartDependencies): WazuhStart {
Expand Down
6 changes: 2 additions & 4 deletions public/react-services/generic-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
* Find more information about this on the LICENSE file.
*/

import axios from 'axios';
import { AppState } from './app-state';
import { WazuhConfig } from './wazuh-config';
import { ApiCheck } from './wz-api-check';
import { WzMisc } from '../factories/misc';
import { OdfeUtils } from '../utils';
import { getHttp, getDataPlugin } from '../kibana-services';
import { PLUGIN_PLATFORM_REQUEST_HEADERS } from '../../common/constants';
import { request } from '../services/request-handler';

export class GenericRequest {
static async request(method, path, payload = null, returnError = false) {
Expand Down Expand Up @@ -81,7 +80,7 @@ export class GenericRequest {
};
}

Object.assign(data, await axios(options));
Object.assign(data, await request(options));
if (!data) {
throw new Error(
`Error doing a request to ${tmpUrl}, method: ${method}.`
Expand All @@ -90,7 +89,6 @@ export class GenericRequest {

return data;
} catch (err) {
OdfeUtils.checkOdfeSessionExpired(err);
//if the requests fails, we need to check if the API is down
const currentApi = JSON.parse(AppState.getCurrentAPI() || '{}');
if (currentApi && currentApi.id) {
Expand Down
6 changes: 3 additions & 3 deletions public/react-services/wz-api-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
* Find more information about this on the LICENSE file.
*/
import { WazuhConfig } from './wazuh-config';
import axios from 'axios';
import { AppState } from './app-state';
import { WzMisc } from '../factories/misc';
import { getHttp } from '../kibana-services';
import { PLUGIN_PLATFORM_REQUEST_HEADERS } from '../../common/constants';
import { request } from '../services/request-handler';

export class ApiCheck {
static async checkStored(data, idChanged = false) {
Expand All @@ -40,7 +40,7 @@ export class ApiCheck {
AppState.setPatternSelector(configuration['ip.selector']);
}

const response = await axios(options);
const response = await request(options);

if (response.error) {
return Promise.reject(response);
Expand Down Expand Up @@ -79,7 +79,7 @@ export class ApiCheck {
timeout: timeout || 20000
};

const response = await axios(options);
const response = await request(options);

if (response.error) {
return Promise.reject(response);
Expand Down
7 changes: 3 additions & 4 deletions public/react-services/wz-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
*
* Find more information about this on the LICENSE file.
*/
import axios from 'axios';
import { AppState } from './app-state';
import { ApiCheck } from './wz-api-check';
import { WzAuthentication } from './wz-authentication';
import { WzMisc } from '../factories/misc';
import { WazuhConfig } from './wazuh-config';
import { OdfeUtils } from '../utils';
import IApiResponse from './interfaces/api-response.interface';
import { getHttp } from '../kibana-services';
import { PLUGIN_PLATFORM_REQUEST_HEADERS } from '../../common/constants';
import { request } from '../services/request-handler';

export class WzRequest {
static wazuhConfig: any;

Expand Down Expand Up @@ -58,15 +58,14 @@ export class WzRequest {
timeout: timeout,
};

const data = await axios(options);
const data = await request(options);

if (data['error']) {
throw new Error(data['error']);
}

return Promise.resolve(data);
} catch (error) {
OdfeUtils.checkOdfeSessionExpired(error);
//if the requests fails, we need to check if the API is down
if(checkCurrentApiIsUp){
const currentApi = JSON.parse(AppState.getCurrentAPI() || '{}');
Expand Down
49 changes: 49 additions & 0 deletions public/services/request-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import axios from 'axios';
import { HTTP_STATUS_CODES } from '../../common/constants';

let allow = true;
const source = axios.CancelToken.source();

const disableRequests = () => {
allow = false;
source.cancel('Requests cancelled');
return;
}

export const initializeInterceptor = (core) => {
core.http.intercept({
responseError: (httpErrorResponse, controller) => {
if (
httpErrorResponse.response?.status === HTTP_STATUS_CODES.UNAUTHORIZED
) {
disableRequests();
}
},
});
}

export const request = async (options = {}) => {
if (!allow) {
return Promise.reject('Requests are disabled');
};
if (!options.method || !options.url) {
return Promise.reject("Missing parameters");
};
options = {
...options, cancelToken: source.token
};

if (allow) {
try {
const requestData = await axios(options);
return Promise.resolve(requestData);
}
catch (e) {
if (e.response?.data?.message === 'Unauthorized' || e.response?.data?.message === 'Authentication required') {
disableRequests();
window.location.reload();
}
return Promise.reject(e);
}
}
}
2 changes: 0 additions & 2 deletions public/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export * as OdfeUtils from './odfe-utils';

export { checkPluginVersion } from './check-plugin-version';

export { addHelpMenuToAppChrome } from './add_help_menu_to_app';
32 changes: 0 additions & 32 deletions public/utils/odfe-utils.ts

This file was deleted.

0 comments on commit f365a37

Please sign in to comment.