Skip to content

Commit aff9a2e

Browse files
committed
Alert Rule : Delete with id
1 parent 0407268 commit aff9a2e

File tree

4 files changed

+36
-36
lines changed

4 files changed

+36
-36
lines changed

src/main/java/com/airbus_cyber_security/graylog/wizard/alert/rest/AlertRuleResource.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public class AlertRuleResource extends RestResource implements PluginRestResourc
105105
private static final Logger LOG = LoggerFactory.getLogger(AlertRuleResource.class);
106106

107107
private static final String ENCODING = "UTF-8";
108+
private static final String ID = "id";
108109
private static final String TITLE = "title";
109110

110111
private static final String DEFAULT_SORT_FIELD = "title";
@@ -305,8 +306,9 @@ private String checkImportPolicyAndGetTitle(String title, UserContext userContex
305306
alertTitle = newAlertTitle;
306307
} else if (importPolicy != null && importPolicy.equals(ImportPolicyType.REPLACE)) {
307308
try {
308-
this.delete(alertTitle, userContext);
309-
} catch (MongoException | UnsupportedEncodingException e) {
309+
AlertRule alert = this.alertRuleService.load(alertTitle);
310+
this.delete(alert.id(), userContext);
311+
} catch (MongoException | UnsupportedEncodingException | NotFoundException e) {
310312
LOG.error("Failed to replace alert rule");
311313
throw new BadRequestException("Failed to replace alert rule.");
312314
}
@@ -531,7 +533,7 @@ private void deleteAlertPattern(AlertPattern alertPattern) {
531533
}
532534

533535
@DELETE
534-
@Path("/{title}")
536+
@Path("/{id}")
535537
@RequiresAuthentication
536538
@RequiresPermissions(AlertRuleRestPermissions.WIZARD_ALERTS_RULES_DELETE)
537539
@ApiOperation(value = "Delete a alert")
@@ -540,25 +542,25 @@ private void deleteAlertPattern(AlertPattern alertPattern) {
540542
@ApiResponse(code = 400, message = "Invalid ObjectId.")
541543
})
542544
@AuditEvent(type = AlertWizardAuditEventTypes.WIZARD_ALERTS_RULES_DELETE)
543-
public void delete(@ApiParam(name = TITLE, required = true)
544-
@PathParam(TITLE) String title,
545+
public void delete(@ApiParam(name = ID, required = true)
546+
@PathParam(ID) String id,
545547
@Context UserContext userContext
546548
) throws MongoException, UnsupportedEncodingException {
547-
String alertTitle = java.net.URLDecoder.decode(title, ENCODING);
549+
Optional<AlertRule> alertRuleOptional = this.alertRuleService.get(id);
548550

549-
try {
550-
AlertRule alertRule = this.alertRuleService.load(alertTitle);
551+
if (alertRuleOptional.isPresent()) {
552+
AlertRule alertRule = alertRuleOptional.get();
551553

552554
deleteAlertPattern(alertRule.pattern());
553555
if (alertRule.getNotificationID() != null && !alertRule.getNotificationID().isEmpty()) {
554556
// TODO move this down into AlertRuleUtilsService and remove the use for eventNotificationsResource
555557
this.eventNotificationsResource.delete(alertRule.getNotificationID(), userContext);
556558
}
557-
} catch (NotFoundException e) {
558-
LOG.error("Cannot find alert " + alertTitle, e);
559-
}
560559

561-
this.alertRuleService.destroy(alertTitle);
560+
this.alertRuleService.delete(id);
561+
} else {
562+
LOG.error("Cannot find alert {}", id);
563+
}
562564
}
563565

564566
@POST

src/web/wizard/actions/AlertRuleActions.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const AlertRuleActions = Reflux.createActions({
2222
get: {asyncResult: true},
2323
create: {asyncResult: true},
2424
clone: {asyncResult: true},
25-
deleteByName: {asyncResult: true},
25+
delete: {asyncResult: true},
2626
update: {asyncResult: true},
2727
searchPaginated: { asyncResult: true },
2828
});

src/web/wizard/components/rules/AlertRulesContainer.jsx

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@
1717

1818
import React from 'react';
1919
import { useState, useCallback } from 'react';
20+
import { useIntl, FormattedMessage } from 'react-intl';
21+
import { useQueryClient } from '@tanstack/react-query';
22+
import { toDateObject } from 'util/DateTime';
23+
import StreamsStore from 'stores/streams/StreamsStore';
2024
import { PaginatedEntityTable, Timestamp } from 'components/common';
25+
import ButtonToEventDefinition from 'wizard/components/buttons/ButtonToEventDefinition';
26+
import ButtonToNotification from 'wizard/components/buttons/ButtonToNotification';
27+
import ButtonToSearch from 'wizard/components/buttons/ButtonToSearch';
28+
import ButtonToUpdateRule from 'wizard/components/buttons/ButtonToUpdateRule';
29+
import EventDefinitionResources from 'wizard/resources/EventDefinitionResource';
2130
import AlertRuleActions from 'wizard/actions/AlertRuleActions';
22-
import { useQueryClient } from '@tanstack/react-query';
23-
import { keyFn, fetchAlertRules, KEY_PREFIX } from './hooks/useAlertRules';
24-
import { useIntl, FormattedMessage } from 'react-intl';
31+
import AlertValidation from 'wizard/logic/AlertValidation';
32+
import { keyFn, fetchAlertRules } from './hooks/useAlertRules';
2533
import AlertRuleBulkActions from './AlertRuleBulkActions';
26-
import { toDateObject } from 'util/DateTime';
2734
import AlertRuleText from './AlertRuleText';
28-
import ButtonToEventDefinition from '../buttons/ButtonToEventDefinition';
29-
import ButtonToNotification from '../buttons/ButtonToNotification';
3035
import AlertRuleCloneForm from './AlertRuleCloneForm';
31-
import EventDefinitionResources from '../../resources/EventDefinitionResource';
32-
import StreamsStore from 'stores/streams/StreamsStore';
33-
import ButtonToSearch from '../buttons/ButtonToSearch';
34-
import AlertValidation from '../../logic/AlertValidation';
35-
import ButtonToUpdateRule from '../buttons/ButtonToUpdateRule';
3636
import {DEFAULT_LAYOUT} from "./Constants";
3737

3838
function _convertAlertToElement(alert) {
@@ -84,7 +84,7 @@ const AlertRulesContainer = ({ fieldOrder }) => {
8484
const intl = useIntl();
8585
const queryClient = useQueryClient();
8686

87-
const _loadAlertRules = () => queryClient.invalidateQueries(KEY_PREFIX);
87+
const _loadAlertRules = () => queryClient.invalidateQueries(keyFn());
8888
const fieldsTitle = [
8989
{key: 'title', label: intl.formatMessage({id: 'wizard.title', defaultMessage: 'Title'}), config: 'title', sortable: true},
9090
{key: 'priority', label: intl.formatMessage({id: 'wizard.priority', defaultMessage: 'Priority'}), config: 'Priority', sortable: true},
@@ -169,7 +169,6 @@ const AlertRulesContainer = ({ fieldOrder }) => {
169169
disableAlertRulesFunction={disableAlertRules}
170170
enableAlertRulesFunction={enableAlertRules} />
171171
);
172-
const onSortChange = useCallback(() => {}, []);
173172
const renderAlertRuleActions = useCallback((alert) => {
174173
const element = _convertAlertToElement(alert);
175174

@@ -182,22 +181,21 @@ const AlertRulesContainer = ({ fieldOrder }) => {
182181
</div>);
183182
}, []);
184183

185-
const deleteAlertRules = (alertRulesTitles) => {
186-
const promises = alertRulesTitles.map(name => AlertRuleActions.deleteByName(name));
184+
const deleteAlertRules = (alertRulesIds) => {
185+
const promises = alertRulesIds.map(id => AlertRuleActions.delete(id));
187186
Promise.all(promises).then(() => _loadAlertRules());
188187
}
189188

190-
const disableAlertRules = (alertRulesTitles) => {
191-
console.log(alertRulesTitles);
192-
const tempElements = alertRulesTitles.map(name => elements.find(x => x.id === name));
189+
const disableAlertRules = (alertRulesIds) => {
190+
const tempElements = alertRulesIds.map(name => elements.find(x => x.id === name));
193191

194192
for(const elt of tempElements) {
195193
_onPause(elt.title, elt.condition, elt.streamId, elt.secondEventDefinition, elt.streamId2);
196194
}
197195
}
198196

199-
const enableAlertRules = (alertRulesTitles) => {
200-
const tempElements = alertRulesTitles.map(name => elements.find(x => x.id === name));
197+
const enableAlertRules = (alertRulesIds) => {
198+
const tempElements = alertRulesIds.map(name => elements.find(x => x.id === name));
201199

202200
for(const elt of tempElements) {
203201
_onResume(elt.condition, elt.streamId, elt.secondEventDefinition, elt.streamId2);

src/web/wizard/stores/AlertRuleStore.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ const AlertRuleStore = Reflux.createStore({
199199
AlertRuleActions.update.promise(promise);
200200
},
201201

202-
deleteByName(alertName) {
203-
const url = URLUtils.qualifyUrl(SOURCE_URL + '/' + encodeURIComponent(alertName));
202+
delete(id) {
203+
const url = URLUtils.qualifyUrl(SOURCE_URL + '/' + encodeURIComponent(id));
204204
const method = 'DELETE';
205205

206206
const promise = fetch(method, url)
@@ -211,7 +211,7 @@ const AlertRuleStore = Reflux.createStore({
211211
UserNotification.error(`Deleting alert rule failed with status: ${error.message}`,
212212
'Could not delete alert rule');
213213
});
214-
AlertRuleActions.deleteByName.promise(promise);
214+
AlertRuleActions.delete.promise(promise);
215215
},
216216
});
217217

0 commit comments

Comments
 (0)