Skip to content
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
76 changes: 76 additions & 0 deletions x-pack/test/cases_api_integration/common/lib/alerts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type SuperTest from 'supertest';
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { ToolingLog } from '@kbn/tooling-log';
import { DETECTION_ENGINE_QUERY_SIGNALS_URL } from '@kbn/security-solution-plugin/common/constants';
import { DetectionAlert } from '@kbn/security-solution-plugin/common/detection_engine/schemas/alerts';
import { RiskEnrichmentFields } from '@kbn/security-solution-plugin/server/lib/detection_engine/signals/enrichments/types';
import {
getRuleForSignalTesting,
createRule,
waitForRuleSuccess,
waitForSignalsToBePresent,
getSignalsByIds,
getQuerySignalIds,
} from '../../../detection_engine_api_integration/utils';
import { superUser } from './authentication/users';
import { User } from './authentication/types';
import { getSpaceUrlPrefix } from './api/helpers';

export const createSecuritySolutionAlerts = async (
supertest: SuperTest.SuperTest<SuperTest.Test>,
log: ToolingLog
): Promise<estypes.SearchResponse<DetectionAlert & RiskEnrichmentFields>> => {
const rule = getRuleForSignalTesting(['auditbeat-*']);
const { id } = await createRule(supertest, log, rule);
await waitForRuleSuccess({ supertest, log, id });
await waitForSignalsToBePresent(supertest, log, 1, [id]);
const signals = await getSignalsByIds(supertest, log, [id]);

return signals;
};

export const getSecuritySolutionAlerts = async (
supertest: SuperTest.SuperTest<SuperTest.Test>,
alertIds: string[]
): Promise<estypes.SearchResponse<DetectionAlert & RiskEnrichmentFields>> => {
const { body: updatedAlert } = await supertest
.post(DETECTION_ENGINE_QUERY_SIGNALS_URL)
.set('kbn-xsrf', 'true')
.send(getQuerySignalIds(alertIds))
.expect(200);

return updatedAlert;
};

interface AlertResponse {
'kibana.alert.case_ids'?: string[];
}

export const getAlertById = async ({
supertest,
id,
index,
expectedHttpCode = 200,
auth = { user: superUser, space: null },
}: {
supertest: SuperTest.SuperTest<SuperTest.Test>;
id: string;
index: string;
expectedHttpCode?: number;
auth?: { user: User; space: string | null };
}): Promise<AlertResponse> => {
const { body: alert } = await supertest
.get(`${getSpaceUrlPrefix(auth?.space)}/internal/rac/alerts?id=${id}&index=${index}`)
.auth(auth.user.username, auth.user.password)
.set('kbn-xsrf', 'true')
.expect(expectedHttpCode);

return alert;
};
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import {
deleteSignalsIndex,
deleteAllRules,
getRuleForSignalTesting,
waitForRuleSuccessOrStatus,
waitForRuleSuccess,
waitForSignalsToBePresent,
getSignalsByIds,
createRule,
Expand Down Expand Up @@ -804,7 +804,7 @@ export default ({ getService }: FtrProviderContext): void => {
const postedCase = await createCase(supertest, postCaseReq);

const { id } = await createRule(supertest, log, rule);
await waitForRuleSuccessOrStatus(supertest, log, id);
await waitForRuleSuccess({ supertest, log, id });
await waitForSignalsToBePresent(supertest, log, 1, [id]);
const signals = await getSignalsByIds(supertest, log, [id]);

Expand Down Expand Up @@ -864,7 +864,7 @@ export default ({ getService }: FtrProviderContext): void => {
});

const { id } = await createRule(supertest, log, rule);
await waitForRuleSuccessOrStatus(supertest, log, id);
await waitForRuleSuccess({ supertest, log, id });
await waitForSignalsToBePresent(supertest, log, 1, [id]);
const signals = await getSignalsByIds(supertest, log, [id]);

Expand Down Expand Up @@ -917,7 +917,7 @@ export default ({ getService }: FtrProviderContext): void => {
});

const { id } = await createRule(supertest, log, rule);
await waitForRuleSuccessOrStatus(supertest, log, id);
await waitForRuleSuccess({ supertest, log, id });
await waitForSignalsToBePresent(supertest, log, 1, [id]);
const signals = await getSignalsByIds(supertest, log, [id]);

Expand Down Expand Up @@ -986,7 +986,7 @@ export default ({ getService }: FtrProviderContext): void => {

const postedCase = await createCase(supertest, postCaseReq);
const { id } = await createRule(supertest, log, rule);
await waitForRuleSuccessOrStatus(supertest, log, id);
await waitForRuleSuccess({ supertest, log, id });
await waitForSignalsToBePresent(supertest, log, 1, [id]);
const signals = await getSignalsByIds(supertest, log, [id]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {
deleteSignalsIndex,
deleteAllRules,
getRuleForSignalTesting,
waitForRuleSuccessOrStatus,
waitForRuleSuccess,
waitForSignalsToBePresent,
getSignalsByIds,
createRule,
Expand Down Expand Up @@ -386,7 +386,7 @@ export default ({ getService }: FtrProviderContext): void => {
});

const { id } = await createRule(supertest, log, rule);
await waitForRuleSuccessOrStatus(supertest, log, id);
await waitForRuleSuccess({ supertest, log, id });
await waitForSignalsToBePresent(supertest, log, 1, [id]);
const signals = await getSignalsByIds(supertest, log, [id]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
deleteSignalsIndex,
deleteAllRules,
getRuleForSignalTesting,
waitForRuleSuccessOrStatus,
waitForRuleSuccess,
waitForSignalsToBePresent,
getSignalsByIds,
createRule,
Expand Down Expand Up @@ -503,7 +503,7 @@ export default ({ getService }: FtrProviderContext): void => {
});

const { id } = await createRule(supertest, log, rule);
await waitForRuleSuccessOrStatus(supertest, log, id);
await waitForRuleSuccess({ supertest, log, id });
await waitForSignalsToBePresent(supertest, log, 1, [id]);
const signals = await getSignalsByIds(supertest, log, [id]);
const attachments: CommentRequest[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
createRule,
waitForSignalsToBePresent,
getSignalsByIds,
waitForRuleSuccessOrStatus,
waitForRuleSuccess,
getRuleForSignalTesting,
} from '../../utils';

Expand Down Expand Up @@ -57,7 +57,7 @@ export default ({ getService }: FtrProviderContext) => {
it('should be able to execute and get 10 signals', async () => {
const rule = getRuleForSignalTesting(['auditbeat-*']);
const { id } = await createRule(supertest, log, rule);
await waitForRuleSuccessOrStatus(supertest, log, id);
await waitForRuleSuccess({ supertest, log, id });
await waitForSignalsToBePresent(supertest, log, 10, [id]);
const signalsOpen = await getSignalsByIds(supertest, log, [id]);
expect(signalsOpen.hits.hits.length).equal(10);
Expand All @@ -66,7 +66,7 @@ export default ({ getService }: FtrProviderContext) => {
it('should be have set the signals in an open state initially', async () => {
const rule = getRuleForSignalTesting(['auditbeat-*']);
const { id } = await createRule(supertest, log, rule);
await waitForRuleSuccessOrStatus(supertest, log, id);
await waitForRuleSuccess({ supertest, log, id });
await waitForSignalsToBePresent(supertest, log, 10, [id]);
const signalsOpen = await getSignalsByIds(supertest, log, [id]);
const everySignalOpen = signalsOpen.hits.hits.every(
Expand All @@ -78,7 +78,7 @@ export default ({ getService }: FtrProviderContext) => {
it('should be able to get a count of 10 closed signals when closing 10', async () => {
const rule = getRuleForSignalTesting(['auditbeat-*']);
const { id } = await createRule(supertest, log, rule);
await waitForRuleSuccessOrStatus(supertest, log, id);
await waitForRuleSuccess({ supertest, log, id });
await waitForSignalsToBePresent(supertest, log, 10, [id]);
const signalsOpen = await getSignalsByIds(supertest, log, [id]);
const signalIds = signalsOpen.hits.hits.map((signal) => signal._id);
Expand All @@ -104,7 +104,7 @@ export default ({ getService }: FtrProviderContext) => {
it('should be able close 10 signals immediately and they all should be closed', async () => {
const rule = getRuleForSignalTesting(['auditbeat-*']);
const { id } = await createRule(supertest, log, rule);
await waitForRuleSuccessOrStatus(supertest, log, id);
await waitForRuleSuccess({ supertest, log, id });
await waitForSignalsToBePresent(supertest, log, 10, [id]);
const signalsOpen = await getSignalsByIds(supertest, log, [id]);
const signalIds = signalsOpen.hits.hits.map((signal) => signal._id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
getWebHookAction,
getRuleWithWebHookAction,
getSimpleRuleOutputWithWebHookAction,
waitForRuleSuccessOrStatus,
waitForRuleSuccess,
createRule,
} from '../../utils';

Expand Down Expand Up @@ -74,7 +74,7 @@ export default ({ getService }: FtrProviderContext) => {
log,
getRuleWithWebHookAction(hookAction.id, true)
);
await waitForRuleSuccessOrStatus(supertest, log, rule.id);
await waitForRuleSuccess({ supertest, log, id: rule.id });
});

it('should be able to create a new webhook action and attach it to a rule with a meta field and run it correctly', async () => {
Expand All @@ -92,7 +92,7 @@ export default ({ getService }: FtrProviderContext) => {
};

const rule = await createRule(supertest, log, ruleWithAction);
await waitForRuleSuccessOrStatus(supertest, log, rule.id);
await waitForRuleSuccess({ supertest, log, id: rule.id });
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
deleteSignalsIndex,
getRuleForSignalTesting,
getSignalsById,
waitForRuleSuccessOrStatus,
waitForRuleSuccess,
waitForSignalsToBePresent,
} from '../../utils';

Expand Down Expand Up @@ -50,7 +50,7 @@ export default ({ getService }: FtrProviderContext) => {
it('should keep the original alias value such as "host_alias" from a source index when the value is indexed', async () => {
const rule = getRuleForSignalTesting(['host_alias']);
const { id } = await createRule(supertest, log, rule);
await waitForRuleSuccessOrStatus(supertest, log, id);
await waitForRuleSuccess({ supertest, log, id });
await waitForSignalsToBePresent(supertest, log, 4, [id]);
const signalsOpen = await getSignalsById(supertest, log, id);
const hits = signalsOpen.hits.hits
Expand All @@ -62,7 +62,7 @@ export default ({ getService }: FtrProviderContext) => {
it('should copy alias data from a source index into the signals index in the same position when the target is ECS compatible', async () => {
const rule = getRuleForSignalTesting(['host_alias']);
const { id } = await createRule(supertest, log, rule);
await waitForRuleSuccessOrStatus(supertest, log, id);
await waitForRuleSuccess({ supertest, log, id });
await waitForSignalsToBePresent(supertest, log, 4, [id]);
const signalsOpen = await getSignalsById(supertest, log, id);
const hits = signalsOpen.hits.hits
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
import expect from '@kbn/expect';
import { DETECTION_ENGINE_RULES_URL } from '@kbn/security-solution-plugin/common/constants';
import { ROLES } from '@kbn/security-solution-plugin/common/test';
import { RuleExecutionStatus } from '@kbn/security-solution-plugin/common/detection_engine/rule_monitoring';
import { ThresholdRuleCreateProps } from '@kbn/security-solution-plugin/common/detection_engine/rule_schema';
import { FtrProviderContext } from '../../common/ftr_provider_context';
import {
createSignalsIndex,
deleteSignalsIndex,
deleteAllRules,
waitForRuleSuccessOrStatus,
waitForRulePartialFailure,
getRuleForSignalTesting,
createRuleWithAuth,
getThresholdRuleForSignalTesting,
Expand Down Expand Up @@ -65,12 +64,11 @@ export default ({ getService }: FtrProviderContext) => {
user: ROLES.detections_admin,
pass: 'changeme',
});
await waitForRuleSuccessOrStatus(
await waitForRulePartialFailure({
supertest,
log,
id,
RuleExecutionStatus['partial failure']
);
});
const { body } = await supertest
.get(DETECTION_ENGINE_RULES_URL)
.set('kbn-xsrf', 'true')
Expand Down Expand Up @@ -104,12 +102,11 @@ export default ({ getService }: FtrProviderContext) => {
user: ROLES.detections_admin,
pass: 'changeme',
});
await waitForRuleSuccessOrStatus(
await waitForRulePartialFailure({
supertest,
log,
id,
RuleExecutionStatus['partial failure']
);
});
const { body } = await supertest
.get(DETECTION_ENGINE_RULES_URL)
.set('kbn-xsrf', 'true')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import expect from '@kbn/expect';

import { DETECTION_ENGINE_RULES_URL } from '@kbn/security-solution-plugin/common/constants';
import { RuleExecutionStatus } from '@kbn/security-solution-plugin/common/detection_engine/rule_monitoring';
import { RuleCreateProps } from '@kbn/security-solution-plugin/common/detection_engine/rule_schema';
import { ExceptionListTypeEnum } from '@kbn/securitysolution-io-ts-list-types';
import { ROLES } from '@kbn/security-solution-plugin/common/test';
Expand All @@ -26,12 +25,13 @@ import {
removeServerGeneratedPropertiesIncludingRuleId,
getSimpleMlRule,
getSimpleMlRuleOutput,
waitForRuleSuccessOrStatus,
waitForRuleSuccess,
getRuleForSignalTesting,
getRuleForSignalTestingWithTimestampOverride,
waitForAlertToComplete,
waitForSignalsToBePresent,
getThresholdRuleForSignalTesting,
waitForRulePartialFailure,
} from '../../utils';
import { createUserAndRole, deleteUserAndRole } from '../../../common/services/security_solution';

Expand Down Expand Up @@ -118,7 +118,7 @@ export default ({ getService }: FtrProviderContext) => {
.send(simpleRule)
.expect(200);

await waitForRuleSuccessOrStatus(supertest, log, body.id);
await waitForRuleSuccess({ supertest, log, id: body.id });
});

it('should create a single rule with a rule_id and an index pattern that does not match anything available and partial failure for the rule', async () => {
Expand All @@ -129,12 +129,11 @@ export default ({ getService }: FtrProviderContext) => {
.send(simpleRule)
.expect(200);

await waitForRuleSuccessOrStatus(
await waitForRulePartialFailure({
supertest,
log,
body.id,
RuleExecutionStatus['partial failure']
);
id: body.id,
});

const { body: rule } = await supertest
.get(DETECTION_ENGINE_RULES_URL)
Expand All @@ -157,7 +156,7 @@ export default ({ getService }: FtrProviderContext) => {
.send(simpleRule)
.expect(200);

await waitForRuleSuccessOrStatus(supertest, log, body.id, RuleExecutionStatus.succeeded);
await waitForRuleSuccess({ supertest, log, id: body.id });
});

it('should create a single rule without an input index', async () => {
Expand Down Expand Up @@ -518,12 +517,11 @@ export default ({ getService }: FtrProviderContext) => {
const bodyId = body.id;

await waitForAlertToComplete(supertest, log, bodyId);
await waitForRuleSuccessOrStatus(
await waitForRulePartialFailure({
supertest,
log,
bodyId,
RuleExecutionStatus['partial failure']
);
id: bodyId,
});

const { body: rule } = await supertest
.get(DETECTION_ENGINE_RULES_URL)
Expand All @@ -550,12 +548,11 @@ export default ({ getService }: FtrProviderContext) => {
.expect(200);
const bodyId = body.id;

await waitForRuleSuccessOrStatus(
await waitForRulePartialFailure({
supertest,
log,
bodyId,
RuleExecutionStatus['partial failure']
);
id: bodyId,
});
await waitForSignalsToBePresent(supertest, log, 2, [bodyId]);

const { body: rule } = await supertest
Expand Down
Loading