Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add session token to rulest #3257

Merged
merged 14 commits into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ All notable changes to the Wazuh app project will be documented in this file.
### Fixed

- Fixed screen flickers in Cluster visualization [#3159](https://github.com/wazuh/wazuh-kibana-app/pull/3159)
- Fix the broken links when using `server.basePath` Kibana setting [#3161](https://github.com/wazuh/wazuh-kibana-app/pull/3161)
- Fixing filter in reports [#3173](https://github.com/wazuh/wazuh-kibana-app/pull/3173)
- Fix typo error in Settings/Configuration [#3234](https://github.com/wazuh/wazuh-kibana-app/pull/3234)
- Fixed the broken links when using `server.basePath` Kibana setting [#3161](https://github.com/wazuh/wazuh-kibana-app/pull/3161)
- Fixed filter in reports [#3173](https://github.com/wazuh/wazuh-kibana-app/pull/3173)
- Fixed typo error in Settings/Configuration [#3234](https://github.com/wazuh/wazuh-kibana-app/pull/3234)
- Fixed fields overlap in the agent summary screen [#3217](https://github.com/wazuh/wazuh-kibana-app/pull/3217)
- Fixed Ruleset Test, each request is made in a different session instead of all in the same session [#3257](https://github.com/wazuh/wazuh-kibana-app/pull/3257)
- Fixed the `Visualize` button is not displaying when expanding a field in the Events sidebar [#3237](https://github.com/wazuh/wazuh-kibana-app/pull/3237)
- Add error when add sample data fails [#3241] (https://github.com/wazuh/wazuh-kibana-app/pull/3241)
- Fix modules are missing in the agent menu [#3244] (https://github.com/wazuh/wazuh-kibana-app/pull/3244)
- Fixed message of error when add sample data fails [#3241](https://github.com/wazuh/wazuh-kibana-app/pull/3241)
- Fixed modules are missing in the agent menu [#3244](https://github.com/wazuh/wazuh-kibana-app/pull/3244)

## Wazuh v4.2.0 - Kibana 7.10.2 , 7.11.2 - Revision 4201

Expand Down
2 changes: 1 addition & 1 deletion public/controllers/management/management.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ export class ManagementController {

openCloseFlyout() {
this.logtestOpened = !this.logtestOpened;
this.logtestProps.isRuleset = this.tab,
this.logtestProps.isRuleset = this.tab;
this.$scope.$applyAsync();
}

Expand Down
76 changes: 61 additions & 15 deletions public/directives/wz-logtest/components/logtest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import {
import { WzRequest } from '../../../react-services';
import { withReduxProvider, withUserAuthorizationPrompt } from '../../../components/common/hocs';
import { compose } from 'redux';
import { useSelector, useDispatch } from 'react-redux';
import { updateLogtestToken } from '../../../redux/actions/appStateActions';
import { WzButtonPermissionsModalConfirm } from '../../../components/common/buttons';

type LogstestProps = {
openCloseFlyout: () => {};
Expand All @@ -41,12 +44,14 @@ export const Logtest = compose(
withReduxProvider,
withUserAuthorizationPrompt([{ action: 'logtest:run', resource: `*:*:*` }])
)((props: LogstestProps) => {
const [value, setValue] = useState([]);
const [events, setEvents] = useState([]);
const [testing, setTesting] = useState(false);
const [testResult, setTestResult] = useState('');
const dispatch = useDispatch();
const sessionToken = useSelector((state)=> state.appStateReducers.logtestToken);

const onChange = (e) => {
setValue(e.target.value.split('\n').filter((item) => item));
setEvents(e.target.value.split('\n').filter((item) => item));
};

const formatResult = (result, alert) => {
Expand Down Expand Up @@ -84,21 +89,28 @@ export const Logtest = compose(
const runAllTests = async () => {
setTestResult('');
setTesting(true);
let token = sessionToken;
const responses = [];
let gotToken = Boolean(token);

try {
const responsesLogtest = await Promise.all(
value.map(async (event) => {
const body = {
log_format: 'syslog',
location: 'logtest',
event: event,
};
return await WzRequest.apiReq('PUT', '/logtest', body);
})
);
const testResults = responsesLogtest.map((response) =>
for (let event of events) {
const response = await WzRequest.apiReq('PUT', '/logtest', {
log_format: 'syslog',
location: 'logtest',
event,
...(token ? { token }: {})
});
token = response.data.data.token;
!sessionToken && !gotToken && token && dispatch(updateLogtestToken(token));
token && (gotToken = true);
responses.push(response);
};

const testResults = responses.map((response) =>
response.data.data.output.rule || ''
? formatResult(response.data.data.output, response.data.data.alert)
: `No result found for: ${response.data.data.output.full_log} \n\n\n`
: `No result found for: ${response.data.data.output.full_log} \n\n\n`
);
setTestResult(testResults);
} finally {
Expand All @@ -112,6 +124,17 @@ export const Logtest = compose(
}
};

const deleteToken = async() =>{
try {
const response = await WzRequest.apiReq('DELETE', `/logtest/sessions/${sessionToken}`, {});
dispatch(updateLogtestToken(''));
setTestResult('');
}
catch(error) {
this.showToast('danger', 'Error', `Error trying to delete logtest token due to: ${error.message || error}`);
}
}

const buildLogtest = () => {
return (
<Fragment>
Expand All @@ -124,18 +147,41 @@ export const Logtest = compose(
onKeyPress={handleKeyPress}
/>
<EuiSpacer size="m" />
<EuiFlexGroup justifyContent="spaceBetween">
<EuiFlexItem grow={false}>
<EuiButton
style={{ maxWidth: '100px' }}
isLoading={testing}
isDisabled={testing || value.length === 0}
isDisabled={testing || events.length === 0}
iconType="play"
fill
onClick={runAllTests}
>
Test
</EuiButton>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<WzButtonPermissionsModalConfirm
style={{ maxWidth: '150px' }}
tooltip={{position: 'top', content: 'Clear current session'}}
fill
isDisabled={sessionToken === '' ? true : false}
aria-label="Clear current session"
iconType="broom"
onConfirm={async () => {
deleteToken();
}}
color="danger"
modalTitle={`Do you want to clear current session?`}
modalProps={{
buttonColor: 'danger',
children: 'Clearing the session means the logs execution history is removed. This affects to rules that fire an alert when similar logs are executed in a specific range of time.'
}}
>
Clear session
</WzButtonPermissionsModalConfirm>
</EuiFlexItem>
</EuiFlexGroup>
<EuiSpacer size="m" />
<EuiCodeBlock
language="json"
Expand Down
11 changes: 11 additions & 0 deletions public/redux/actions/appStateActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,15 @@ export const updateAllowedAgents = allowedAgents => {
type: 'GET_ALLOWED_AGENTS',
allowedAgents
};
};

/**
* Updates logtestToken in the appState store
* @param logtestToken
*/
export const updateLogtestToken = (logtestToken) => {
return {
type: 'UPDATE_LOGTEST_TOKEN',
logtestToken: logtestToken
};
};
12 changes: 10 additions & 2 deletions public/redux/reducers/appStateReducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ const initialState = {
status: false,
contextConfigServer: 'manager',
},
withUserLogged: false,
allowedAgents: [],
withUserLogged: false,
allowedAgents: [],
logtestToken: '',
};

const appStateReducers = (state = initialState, action) => {
Expand Down Expand Up @@ -147,6 +148,13 @@ const appStateReducers = (state = initialState, action) => {
};
}

if (action.type === 'UPDATE_LOGTEST_TOKEN') {
return {
...state,
logtestToken: action.logtestToken
};
}

return state;
};

Expand Down