Skip to content

Commit

Permalink
Implement try catch react-services (#3436)
Browse files Browse the repository at this point in the history
* Implement try catch react-services

* Changes requested

* Change severity to show the toast

* Add changelog

* Solve comments

* Changed all context value of all try-catch implementations (#3432)

* refactor(error-orchestrator): Changed all context value of all try-catch implementations

* docs(error-orchestrator): Updated changelog.

* Implement try catch strategy in Groups (#3415)

* Implement try catch groups

* Add test

* test(groups): Added simple snapshot test.

* Add changelog

* Change context

* Change title

Co-authored-by: gabiwassan <gabriel.wassan@wazuh.com>

* Create Cypress Workflow for 4.3-7.10 (#3444)

* Fixed dispatch for updateCurrentAgentData (#3453)

* fix(syscollector): Fixed dispatch for updateCurrentAgentData

* fix(syscollector): Refactor for agents-sections.

* Fixing and updating unit-tests. (#3413)

* test(unit-test): Fixing and updating unit-tests.

* test(scheduler-job): Fixing suite scheduler-job tests.

* test(unit-test): Fixing updating unit-tests.

* test(check-result): Fixing uTest of check-result, added error case.

* test(unit-test): Skipped uTest with dependency of API.

* test(prettier): Applied prettier, redux-mock-store dependency

* test(jest): Jest config without dependencies.

* test(git-actions): Added workflow for uTest.

* test(check-result): Update workflow.

* test(git-actions): Updated workflow

* test(gitactions): Dependencies for unit test.

* test(gitactions): Added coverage-comment

* test(gitactions): Changed text to text-summary

* test(gitactions): Show summary

* test(gitactions): Show summary + added dependencies

* test(gitactions): Typo

* test(gitactions): Porcents

* test(gitactions): Typo

* test(gitactions): Testing node version with yarn

* test(gitactions): Update

* test(gitactions): Update

* test(gitactions): testing coverage summary

* test(gitactions): testing coverage summary

* test(gitactions): fix param github-token

* test(gitactions): update

* test(gitactions): testing report.json

* test(gitactions): final test.

* test(actions): testing kibana dependencies

* test(actions): testing kibana dependencies

* test(actions): testing kibana dependencies

* test(actions): testing kibana dependencies

* test(actions): testing

* test(actions): testing

* test(actions): test with old config jest.

* test(actions): set node version for jest

* test(actions): testing with bootstrap

* test(actions): rollback dependencies

* test(actions): rollback

* test(groups-main): fixing snapshot test

* test(groups-main): added coverage

* test(groups-main): fix path

* test(groups-main): fix path

* test(git-actions): fix script

* test(git-actions): fix checkout

* test(git-actions): fix checkout

* test(git-actions): fix checkout

* test(git-actions): fix checkout

* test(git-actions): fix checkout

* test(git-actions): fix checkout

* test(git-actions): fix checkout

* test(git-actions): fix checkout

* fix(syscollector): Refactor for agents-sections.

* fix(syscollector): Clean files.

* fix(syscollector): Clean files.

* fix(syscollector): test

* fix(syscollector): test

* fix(syscollector): test

* fix(syscollector): test

* fix(syscollector): test

* fix(syscollector): test

* fix(syscollector): test

* fix(syscollector): test

* fix(syscollector): test

* fix(syscollector): test

* fix(syscollector): test

* fix(syscollector): test

* fix(syscollector): test

* fix(syscollector): test

* fix(syscollector): test comment coverage

* fix(syscollector): test comment coverage

* fix(syscollector): test comment coverage

* fix(actions): test comment coverage

* Create Cypress Workflow for 4.3-7.10 (#3444)

* Fixed dispatch for updateCurrentAgentData (#3453)

* fix(syscollector): Fixed dispatch for updateCurrentAgentData

* fix(syscollector): Refactor for agents-sections.

* fix(actions): add const env

Co-authored-by: Matias Ezequiel Moreno <49887871+matiasmoreno876@users.noreply.github.com>

* Added try-catch strategy in Reporting section (#3427)

* Implemented new try-catch strategy

* Updated context

* Added test file and snapshot

* Updated CHANGELOG

* Requested changes

* Removed blank space in message

* Updated error title and message

Co-authored-by: Ibarra Maximiliano <maximiliano.ibarra@wazuh.com>
Co-authored-by: Gabriel Wassan <gabriel.wassan@wazuh.com>

* Implement/try catch in Components >  Overview (#3442)

* Techniques and mitre

* Resources

* Update changelog

* Refactor try catch in Management > Statistics (#3429)

* Added error handling implementation

* Changed wrong var err

* Draft statistics test and snapshot

* Updated CHANGELOG

* Updating title and message error

Co-authored-by: Ibarra Maximiliano <maximiliano.ibarra@wazuh.com>
Co-authored-by: Gabriel Wassan <gabriel.wassan@wazuh.com>

* Refactor try catch in Management > Configuration (#3451)

* Added new try-catch strategy in Management > Configuration

* Updated CHANGELOG

* Requested changes

* doc(changelog): update

Co-authored-by: Ibarra Maximiliano <maximiliano.ibarra@wazuh.com>
Co-authored-by: Gabriel Wassan <gabriel.wassan@wazuh.com>

* Updating test. (#3470)

* test(unittest): Updating test.

* test(unittest): Updating test.

* feat(orchestratorError): Fixed some options for orchestrator and added on app-state

* Xpack and odfe tests

* Fix test

* Add copyright

* fix(discover): fix conflict

Co-authored-by: Gabriel Wassan <gabriel.wassan@wazuh.com>
Co-authored-by: Matias Ezequiel Moreno <49887871+matiasmoreno876@users.noreply.github.com>
Co-authored-by: Maximiliano Ibarra <maximilianoaibarra@gmail.com>
Co-authored-by: Ibarra Maximiliano <maximiliano.ibarra@wazuh.com>
  • Loading branch information
5 people committed Jul 20, 2021
1 parent 63bb06d commit d6f5ae1
Show file tree
Hide file tree
Showing 15 changed files with 662 additions and 363 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -31,6 +31,7 @@ All notable changes to the Wazuh app project will be documented in this file.
[#3469](https://github.com/wazuh/wazuh-kibana-app/pull/3469)
[#3448](https://github.com/wazuh/wazuh-kibana-app/pull/3448)
[#3465](https://github.com/wazuh/wazuh-kibana-app/pull/3465)
[#3464](https://github.com/wazuh/wazuh-kibana-app/pull/3464)

- Added fields status and type in vulnerabilities table [#3196](https://github.com/wazuh/wazuh-kibana-app/pull/3196)
- Added Intelligence tab to Mitre Att&ck module [#3368](https://github.com/wazuh/wazuh-kibana-app/pull/3368) [#3344](https://github.com/wazuh/wazuh-kibana-app/pull/3344)
Expand Down
2 changes: 1 addition & 1 deletion public/components/common/modules/discover/discover.tsx
Expand Up @@ -805,4 +805,4 @@ export const Discover = compose(
{flyout}
</div>);
}
);
});
1 change: 0 additions & 1 deletion public/controllers/agent/agents.js
Expand Up @@ -461,7 +461,6 @@ export class AgentsController {
});
}
this.$scope.tabView = subtab;
return;
} catch (error) {
throw new Error(error);
}
Expand Down
252 changes: 147 additions & 105 deletions public/react-services/action-agents.js
Expand Up @@ -10,176 +10,218 @@
* Find more information about this on the LICENSE file.
*/
import { WzRequest } from './wz-request';
import { getToasts } from '../kibana-services';
import { getToasts } from '../kibana-services';
import { UI_LOGGER_LEVELS } from '../../common/constants';
import { UI_ERROR_SEVERITIES } from './error-orchestrator/types';
import { getErrorOrchestrator } from './common-services';

export class ActionAgents {
static showToast = (color, title, text, time) => {
getToasts().add({
color: color,
title: title,
text: text,
toastLifeTimeMs: time
toastLifeTimeMs: time,
});
};

/**
* Upgrade unique agent to the latest version avaible.
* @param {Number} agentId
*/
static upgradeAgent(agentId) {
WzRequest.apiReq('PUT', `/agents/${agentId}/upgrade`, {
force: 1
})
.then(() => {
console.log('Upgrading');
})
.catch(error => {
error !== 'Wazuh API error: 3021 - Timeout executing API request'
? this.showToast('danger', 'Error upgrading agent', error, 5000)
: false;
static async upgradeAgent(agentId) {
try {
await WzRequest.apiReq('PUT', `/agents/${agentId}/upgrade`, {
force: 1,
});
this.showToast('success', 'Upgrading agent...', '', 5000);
this.showToast('success', 'Upgrading agent...', '', 5000);
} catch (error) {
const options = {
context: `${ActionAgents.name}.upgradeAgent`,
level: UI_LOGGER_LEVELS.ERROR,
severity: UI_ERROR_SEVERITIES.BUSINESS,
store: true,
error: {
error: error,
message: error.message || error,
title: `Error upgrading the agent`,
},
};
getErrorOrchestrator().handleError(options);
}
}

/**
* Upgrade list of agents to the latest version avaible.
* @param {Array} selectedItems
*/
static upgradeAgents(selectedItems) {
static async upgradeAgents(selectedItems) {
for (let item of selectedItems.filter(
item => item.outdated && item.status !== 'Disconnected'
(item) => item.outdated && item.status !== 'Disconnected'
)) {
WzRequest.apiReq('PUT', `/agents/${item.id}/upgrade`, '1')
.then(() => {})
.catch(error => {});
try {
await WzRequest.apiReq('PUT', `/agents/${item.id}/upgrade`, '1');
this.showToast('success', 'Upgrading selected agents...', '', 5000);
} catch (error) {
const options = {
context: `${ActionAgents.name}.upgradeAgents`,
level: UI_LOGGER_LEVELS.ERROR,
severity: UI_ERROR_SEVERITIES.BUSINESS,
store: true,
error: {
error: error,
message: error.message || error,
title: `Error upgrading selected agents`,
},
};
getErrorOrchestrator().handleError(options);
}
}
this.showToast('success', 'Upgrading selected agents...', '', 5000);
}

/**
* Upgrade a list of agents to the latest version avaible.
* @param {Array} selectedItems
* @param {String} managerVersion
*/
static upgradeAllAgents(selectedItems, managerVersion) {
selectedItems.forEach(agent => {
static async upgradeAllAgents(selectedItems, managerVersion) {
selectedItems.forEach(async (agent) => {
if (
agent.id !== '000' &&
this.compareVersions(agent.version, managerVersion) === true &&
agent.status === 'active'
) {
WzRequest.apiReq('PUT', `/agents/${agent.id}/upgrade`, '1')
.then(() => {})
.catch(error => {});
try {
await WzRequest.apiReq('PUT', `/agents/${agent.id}/upgrade`, '1');
this.showToast('success', 'Upgrading all agents...', '', 5000);
} catch (error) {
const options = {
context: `${ActionAgents.name}.upgradeAllAgents`,
level: UI_LOGGER_LEVELS.ERROR,
severity: UI_ERROR_SEVERITIES.BUSINESS,
store: true,
error: {
error: error,
message: error.message || error,
title: `Error upgrading all the agents`,
},
};
getErrorOrchestrator().handleError(options);
}
}
});
this.showToast('success', 'Upgrading all agents...', '', 5000);
}

/**
* Restart an agent
* @param {Number} agentId
*/
static restartAgent(agentId) {
WzRequest.apiReq('PUT', `/agents/restart`, { ids: agentId })
.then(value => {
value.status === 200
? this.showToast('success', 'Restarting agent...', '', 5000)
: this.showToast('warning', 'Error restarting agent', '', 5000);
})
.catch(error => {
this.showToast('danger', 'Error restarting agent', error, 5000);
});
static async restartAgent(agentId) {
try {
await WzRequest.apiReq('PUT', `/agents/restart`, { ids: agentId });
this.showToast('success', 'Restarting agent...', '', 5000);
} catch (error) {
const options = {
context: `${ActionAgents.name}.restartAgent`,
level: UI_LOGGER_LEVELS.ERROR,
severity: UI_ERROR_SEVERITIES.BUSINESS,
store: true,
error: {
error: error,
message: error.message || error,
title: `Error restarting agent`,
},
};
getErrorOrchestrator().handleError(options);
}
}

/**
* Restart a list of agents
* @param {Array} selectedItems
*/
static restartAgents(selectedItems) {
const agentsId = selectedItems.map(item => item.id);

WzRequest.apiReq('PUT', `/agents/restart`, { ids: [...agentsId] })
.then(value => {
value.status === 200
? this.showToast('success', 'Restarting selected agents...', '', 5000)
: this.showToast(
'warning',
'Error restarting selected agents',
'',
5000
);
})
.catch(error => {
this.showToast(
'danger',
'Error restarting selected agents',
error,
5000
);
});
static async restartAgents(selectedItems) {
const agentsId = selectedItems.map((item) => item.id);
try {
await WzRequest.apiReq('PUT', `/agents/restart`, { ids: [...agentsId] });
this.showToast('success', 'Restarting selected agents...', '', 5000);
} catch (error) {
const options = {
context: `${ActionAgents.name}.restartAgents`,
level: UI_LOGGER_LEVELS.ERROR,
severity: UI_ERROR_SEVERITIES.BUSINESS,
store: true,
error: {
error: error,
message: error.message || error,
title: `Error restarting selected agents`,
},
};
getErrorOrchestrator().handleError(options);
}
}

/**
* Restart a list of agents
* @param {Array} selectedItems
*/
static restartAllAgents(selectedItems) {
static async restartAllAgents(selectedItems) {
let idAvaibleAgents = [];
selectedItems.forEach(agent => {
selectedItems.forEach((agent) => {
if (agent.id !== '000' && agent.status === 'active') {
idAvaibleAgents.push(agent.id);
}
});
WzRequest.apiReq('PUT', `/agents/restart`, { ids: [...idAvaibleAgents] })
.then(value => {
value.status === 200
? this.showToast('success', 'Restarting all agents...', '', 5000)
: this.showToast('warning', 'Error restarting all agents.', '', 5000);
})
.catch(error => {
this.showToast('danger', 'Error restarting all agents.', error, 5000);
});
try {
await WzRequest.apiReq('PUT', `/agents/restart`, { ids: [...idAvaibleAgents] });
this.showToast('success', 'Restarting all agents...', '', 5000);
} catch (error) {
const options = {
context: `${ActionAgents.name}.restartAllAgents`,
level: UI_LOGGER_LEVELS.ERROR,
severity: UI_ERROR_SEVERITIES.BUSINESS,
store: true,
error: {
error: error,
message: error.message || error,
title: `Error restarting all agents`,
},
};
getErrorOrchestrator().handleError(options);
}
}

/**
* Delete an agent
* @param {Number} selectedItems
*/
static deleteAgents(selectedItems) {
static async deleteAgents(selectedItems) {
const auxAgents = selectedItems
.map(agent => {
.map((agent) => {
return agent.id !== '000' ? agent.id : null;
})
.filter(agent => agent !== null);
WzRequest.apiReq('DELETE', `/agents`, {
purge: true,
ids: auxAgents,
older_than: '1s'
})
.then(value => {
value.status === 200
? this.showToast(
'success',
`Selected agents were successfully deleted`,
'',
5000
)
: this.showToast(
'warning',
`Failed to delete selected agents`,
'',
5000
);
})
.catch(error => {
this.showToast(
'danger',
`Failed to delete selected agents`,
error,
5000
);
.filter((agent) => agent !== null);
try {
await WzRequest.apiReq('DELETE', `/agents`, {
purge: true,
ids: auxAgents,
older_than: '1s',
});
this.showToast('success', `Selected agents were successfully deleted`, '', 5000);
} catch (error) {
const options = {
context: `${ActionAgents.name}.deleteAgents`,
level: UI_LOGGER_LEVELS.ERROR,
severity: UI_ERROR_SEVERITIES.BUSINESS,
store: true,
error: {
error: error,
message: error.message || error,
title: `Failed to delete selected agents`,
},
};
getErrorOrchestrator().handleError(options);
}
}

/**
Expand All @@ -190,12 +232,12 @@ export class ActionAgents {
* @returns {Boolean}
*/
static compareVersions(managerVersion, agentVersion) {
let agentMatch = new RegExp(
/[.+]?v(?<version>\d+)\.(?<minor>\d+)\.(?<path>\d+)/
).exec(agentVersion);
let managerMatch = new RegExp(
/[.+]?v(?<version>\d+)\.(?<minor>\d+)\.(?<path>\d+)/
).exec(managerVersion);
let agentMatch = new RegExp(/[.+]?v(?<version>\d+)\.(?<minor>\d+)\.(?<path>\d+)/).exec(
agentVersion
);
let managerMatch = new RegExp(/[.+]?v(?<version>\d+)\.(?<minor>\d+)\.(?<path>\d+)/).exec(
managerVersion
);
if (agentMatch === null || managerMatch === null) return;
return managerMatch[1] <= agentMatch[1]
? managerMatch[2] <= agentMatch[2]
Expand Down

0 comments on commit d6f5ae1

Please sign in to comment.