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

[Response Ops][Alerting] Refactoring rule SO const and partiallyUpdateAlert -> partiallyUpdateRule #172859

Merged
merged 4 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { fromKueryExpression, nodeTypes } from '@kbn/es-query';
import { RecoveredActionGroup } from '../../../../../common';
import { DefaultRuleAggregationResult } from '../../../../routes/rule/apis/aggregate/types';
import { defaultRuleAggregationFactory } from '.';
import { RULE_SAVED_OBJECT_TYPE } from '../../../../saved_objects';

const taskManager = taskManagerMock.createStart();
const ruleTypeRegistry = ruleTypeRegistryMock.create();
Expand Down Expand Up @@ -289,7 +290,7 @@ describe('aggregate()', () => {
filter: undefined,
page: 1,
perPage: 0,
type: 'alert',
type: RULE_SAVED_OBJECT_TYPE,
aggs: {
status: {
terms: { field: 'alert.attributes.executionStatus.status' },
Expand Down Expand Up @@ -350,7 +351,7 @@ describe('aggregate()', () => {
]),
page: 1,
perPage: 0,
type: 'alert',
type: RULE_SAVED_OBJECT_TYPE,
aggs: {
status: {
terms: { field: 'alert.attributes.executionStatus.status' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
siemRuleForBulkOps1,
} from '../../../../rules_client/tests/test_helpers';
import { migrateLegacyActions } from '../../../../rules_client/lib';
import { RULE_SAVED_OBJECT_TYPE } from '../../../../saved_objects';

jest.mock('../../../../rules_client/lib/siem_legacy_actions/migrate_legacy_actions', () => {
return {
Expand Down Expand Up @@ -85,7 +86,7 @@ const rulesClientParams: jest.Mocked<ConstructorOptions> = {

const getBulkOperationStatusErrorResponse = (statusCode: number) => ({
id: 'id2',
type: 'alert',
type: RULE_SAVED_OBJECT_TYPE,
success: false,
error: {
error: '',
Expand Down Expand Up @@ -164,9 +165,9 @@ describe('bulkDelete', () => {
test('should try to delete rules, two successful and one with 500 error', async () => {
unsecuredSavedObjectsClient.bulkDelete.mockResolvedValue({
statuses: [
{ id: 'id1', type: 'alert', success: true },
{ id: 'id1', type: RULE_SAVED_OBJECT_TYPE, success: true },
getBulkOperationStatusErrorResponse(500),
{ id: 'id3', type: 'alert', success: true },
{ id: 'id3', type: RULE_SAVED_OBJECT_TYPE, success: true },
],
});

Expand All @@ -176,7 +177,7 @@ describe('bulkDelete', () => {
expect(unsecuredSavedObjectsClient.bulkDelete).toHaveBeenCalledWith(
[enabledRuleForBulkOps1, enabledRuleForBulkOps2, enabledRuleForBulkOps3].map(({ id }) => ({
id,
type: 'alert',
type: RULE_SAVED_OBJECT_TYPE,
})),
undefined
);
Expand All @@ -201,7 +202,7 @@ describe('bulkDelete', () => {
unsecuredSavedObjectsClient.bulkDelete
.mockResolvedValueOnce({
statuses: [
{ id: 'id1', type: 'alert', success: true },
{ id: 'id1', type: RULE_SAVED_OBJECT_TYPE, success: true },
getBulkOperationStatusErrorResponse(409),
],
})
Expand Down Expand Up @@ -265,15 +266,15 @@ describe('bulkDelete', () => {
unsecuredSavedObjectsClient.bulkDelete
.mockResolvedValueOnce({
statuses: [
{ id: 'id1', type: 'alert', success: true },
{ id: 'id1', type: RULE_SAVED_OBJECT_TYPE, success: true },
getBulkOperationStatusErrorResponse(409),
],
})
.mockResolvedValueOnce({
statuses: [
{
id: 'id2',
type: 'alert',
type: RULE_SAVED_OBJECT_TYPE,
success: true,
},
],
Expand Down Expand Up @@ -356,9 +357,9 @@ describe('bulkDelete', () => {
test('should not mark API keys for invalidation if the user is authenticated using an api key', async () => {
unsecuredSavedObjectsClient.bulkDelete.mockResolvedValue({
statuses: [
{ id: 'id3', type: 'alert', success: true },
{ id: 'id1', type: 'alert', success: true },
{ id: 'id2', type: 'alert', success: true },
{ id: 'id3', type: RULE_SAVED_OBJECT_TYPE, success: true },
{ id: 'id1', type: RULE_SAVED_OBJECT_TYPE, success: true },
{ id: 'id2', type: RULE_SAVED_OBJECT_TYPE, success: true },
],
});

Expand All @@ -376,15 +377,15 @@ describe('bulkDelete', () => {
test('should return task id if deleting task failed', async () => {
unsecuredSavedObjectsClient.bulkDelete.mockResolvedValue({
statuses: [
{ id: 'id1', type: 'alert', success: true },
{ id: 'id2', type: 'alert', success: true },
{ id: 'id1', type: RULE_SAVED_OBJECT_TYPE, success: true },
{ id: 'id2', type: RULE_SAVED_OBJECT_TYPE, success: true },
],
});
taskManager.bulkRemove.mockImplementation(async () => ({
statuses: [
{
id: 'id1',
type: 'alert',
type: RULE_SAVED_OBJECT_TYPE,
success: true,
},
getBulkOperationStatusErrorResponse(500),
Expand All @@ -404,8 +405,8 @@ describe('bulkDelete', () => {
test('should not throw an error if taskManager throw an error', async () => {
unsecuredSavedObjectsClient.bulkDelete.mockResolvedValue({
statuses: [
{ id: 'id1', type: 'alert', success: true },
{ id: 'id2', type: 'alert', success: true },
{ id: 'id1', type: RULE_SAVED_OBJECT_TYPE, success: true },
{ id: 'id2', type: RULE_SAVED_OBJECT_TYPE, success: true },
],
});
taskManager.bulkRemove.mockImplementation(() => {
Expand All @@ -424,20 +425,20 @@ describe('bulkDelete', () => {
mockCreatePointInTimeFinderAsInternalUser();
unsecuredSavedObjectsClient.bulkDelete.mockResolvedValue({
statuses: [
{ id: 'id1', type: 'alert', success: true },
{ id: 'id2', type: 'alert', success: true },
{ id: 'id1', type: RULE_SAVED_OBJECT_TYPE, success: true },
{ id: 'id2', type: RULE_SAVED_OBJECT_TYPE, success: true },
],
});
taskManager.bulkRemove.mockImplementation(async () => ({
statuses: [
{
id: 'id1',
type: 'alert',
type: RULE_SAVED_OBJECT_TYPE,
success: true,
},
{
id: 'id2',
type: 'alert',
type: RULE_SAVED_OBJECT_TYPE,
success: true,
},
],
Expand Down Expand Up @@ -468,9 +469,9 @@ describe('bulkDelete', () => {

unsecuredSavedObjectsClient.bulkDelete.mockResolvedValue({
statuses: [
{ id: enabledRuleForBulkOps1.id, type: 'alert', success: true },
{ id: enabledRuleForBulkOps2.id, type: 'alert', success: true },
{ id: siemRuleForBulkOps1.id, type: 'alert', success: true },
{ id: enabledRuleForBulkOps1.id, type: RULE_SAVED_OBJECT_TYPE, success: true },
{ id: enabledRuleForBulkOps2.id, type: RULE_SAVED_OBJECT_TYPE, success: true },
{ id: siemRuleForBulkOps1.id, type: RULE_SAVED_OBJECT_TYPE, success: true },
],
});

Expand Down Expand Up @@ -501,8 +502,8 @@ describe('bulkDelete', () => {
test('logs audit event when deleting rules', async () => {
unsecuredSavedObjectsClient.bulkDelete.mockResolvedValue({
statuses: [
{ id: 'id1', type: 'alert', success: true },
{ id: 'id2', type: 'alert', success: true },
{ id: 'id1', type: RULE_SAVED_OBJECT_TYPE, success: true },
{ id: 'id2', type: RULE_SAVED_OBJECT_TYPE, success: true },
],
});

Expand All @@ -511,12 +512,12 @@ describe('bulkDelete', () => {
expect(auditLogger.log.mock.calls[0][0]?.event?.action).toEqual('rule_delete');
expect(auditLogger.log.mock.calls[0][0]?.event?.outcome).toEqual('unknown');
expect(auditLogger.log.mock.calls[0][0]?.kibana).toEqual({
saved_object: { id: 'id1', type: 'alert' },
saved_object: { id: 'id1', type: RULE_SAVED_OBJECT_TYPE },
});
expect(auditLogger.log.mock.calls[1][0]?.event?.action).toEqual('rule_delete');
expect(auditLogger.log.mock.calls[1][0]?.event?.outcome).toEqual('unknown');
expect(auditLogger.log.mock.calls[1][0]?.kibana).toEqual({
saved_object: { id: 'id2', type: 'alert' },
saved_object: { id: 'id2', type: RULE_SAVED_OBJECT_TYPE },
});
});

Expand All @@ -525,7 +526,7 @@ describe('bulkDelete', () => {
throw new Error('Unauthorized');
});
unsecuredSavedObjectsClient.bulkDelete.mockResolvedValue({
statuses: [{ id: 'id1', type: 'alert', success: true }],
statuses: [{ id: 'id1', type: RULE_SAVED_OBJECT_TYPE, success: true }],
});

await expect(rulesClient.bulkDeleteRules({ filter: 'fake_filter' })).rejects.toThrowError(
Expand All @@ -541,7 +542,7 @@ describe('bulkDelete', () => {
throw new Error('Error');
});
unsecuredSavedObjectsClient.bulkDelete.mockResolvedValue({
statuses: [{ id: 'id1', type: 'alert', success: true }],
statuses: [{ id: 'id1', type: RULE_SAVED_OBJECT_TYPE, success: true }],
});

await expect(rulesClient.bulkDeleteRules({ filter: 'fake_filter' })).rejects.toThrowError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Boom from '@hapi/boom';
import { KueryNode, nodeBuilder } from '@kbn/es-query';
import { SavedObjectsBulkUpdateObject } from '@kbn/core/server';
import { withSpan } from '@kbn/apm-utils';
import { RULE_SAVED_OBJECT_TYPE } from '../../../../saved_objects';
import { convertRuleIdsToKueryNode } from '../../../../lib';
import { bulkMarkApiKeysForInvalidation } from '../../../../invalidate_pending_api_keys/bulk_mark_api_keys_for_invalidation';
import { ruleAuditEvent, RuleAuditAction } from '../../../../rules_client/common/audit_events';
Expand Down Expand Up @@ -136,7 +137,7 @@ const bulkDeleteWithOCC = async (
context.encryptedSavedObjectsClient.createPointInTimeFinderDecryptedAsInternalUser<RuleAttributes>(
{
filter,
type: 'alert',
type: RULE_SAVED_OBJECT_TYPE,
perPage: 100,
...(context.namespace ? { namespaces: [context.namespace] } : undefined),
}
Expand Down Expand Up @@ -168,7 +169,7 @@ const bulkDeleteWithOCC = async (
ruleAuditEvent({
action: RuleAuditAction.DELETE,
outcome: 'unknown',
savedObject: { type: 'alert', id: rule.id },
savedObject: { type: RULE_SAVED_OBJECT_TYPE, id: rule.id },
})
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
siemRuleForBulkOps2,
} from '../../../../rules_client/tests/test_helpers';
import { migrateLegacyActions } from '../../../../rules_client/lib';
import { RULE_SAVED_OBJECT_TYPE } from '../../../../saved_objects';

jest.mock('../../../../task_runner/alert_task_instance', () => ({
taskInstanceToAlertTaskInstance: jest.fn(),
Expand Down Expand Up @@ -428,8 +429,8 @@ describe('bulkDisableRules', () => {

taskManager.bulkRemove.mockResolvedValue({
statuses: [
{ id: 'id1', type: 'alert', success: true },
{ id: 'id2', type: 'alert', success: false },
{ id: 'id1', type: RULE_SAVED_OBJECT_TYPE, success: true },
{ id: 'id2', type: RULE_SAVED_OBJECT_TYPE, success: false },
],
});

Expand Down Expand Up @@ -543,12 +544,12 @@ describe('bulkDisableRules', () => {
expect(auditLogger.log.mock.calls[0][0]?.event?.action).toEqual('rule_disable');
expect(auditLogger.log.mock.calls[0][0]?.event?.outcome).toEqual('unknown');
expect(auditLogger.log.mock.calls[0][0]?.kibana).toEqual({
saved_object: { id: 'id1', type: 'alert' },
saved_object: { id: 'id1', type: RULE_SAVED_OBJECT_TYPE },
});
expect(auditLogger.log.mock.calls[1][0]?.event?.action).toEqual('rule_disable');
expect(auditLogger.log.mock.calls[1][0]?.event?.outcome).toEqual('unknown');
expect(auditLogger.log.mock.calls[1][0]?.kibana).toEqual({
saved_object: { id: 'id2', type: 'alert' },
saved_object: { id: 'id2', type: RULE_SAVED_OBJECT_TYPE },
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { withSpan } from '@kbn/apm-utils';
import pMap from 'p-map';
import { Logger } from '@kbn/core/server';
import { TaskManagerStartContract } from '@kbn/task-manager-plugin/server';
import { RULE_SAVED_OBJECT_TYPE } from '../../../../saved_objects';
import type { RawRule, SanitizedRule, RawRuleAction } from '../../../../types';
import { convertRuleIdsToKueryNode } from '../../../../lib';
import { ruleAuditEvent, RuleAuditAction } from '../../../../rules_client/common/audit_events';
Expand Down Expand Up @@ -132,7 +133,7 @@ const bulkDisableRulesWithOCC = async (
context.encryptedSavedObjectsClient.createPointInTimeFinderDecryptedAsInternalUser<RuleAttributes>(
{
filter: filter ? nodeBuilder.and([filter, additionalFilter]) : additionalFilter,
type: 'alert',
type: RULE_SAVED_OBJECT_TYPE,
perPage: 100,
...(context.namespace ? { namespaces: [context.namespace] } : undefined),
}
Expand Down Expand Up @@ -200,7 +201,7 @@ const bulkDisableRulesWithOCC = async (
ruleAuditEvent({
action: RuleAuditAction.DISABLE,
outcome: 'unknown',
savedObject: { type: 'alert', id: rule.id },
savedObject: { type: RULE_SAVED_OBJECT_TYPE, id: rule.id },
})
);
} catch (error) {
Expand Down
Loading
Loading