Skip to content

Commit

Permalink
addresses comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dplumlee committed Dec 6, 2021
1 parent 5b25b3f commit 9b3b2f3
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export interface IndexOptions {
* shouldn't exist on an alert index and shouldn't be queried together with
* real alerts in any way, because the rule that created them doesn't exist
*/
additionalPrefix?: '.preview';
additionalPrefix?: string;
}

/**
Expand Down
9 changes: 0 additions & 9 deletions x-pack/plugins/security_solution/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,6 @@ export const DETECTION_ENGINE_PREPACKAGED_RULES_STATUS_URL =
export const DETECTION_ENGINE_RULES_BULK_ACTION =
`${DETECTION_ENGINE_RULES_URL}/_bulk_action` as const;
export const DETECTION_ENGINE_RULES_PREVIEW = `${DETECTION_ENGINE_RULES_URL}/preview` as const;
export const DETECTION_ENGINE_RULES_PREVIEW_INDEX_URL =
`${DETECTION_ENGINE_RULES_PREVIEW}/index` as const;

/**
* Internal detection engine routes
Expand Down Expand Up @@ -382,10 +380,3 @@ export const WARNING_TRANSFORM_STATES = new Set([
TRANSFORM_STATES.STOPPED,
TRANSFORM_STATES.STOPPING,
]);

export enum INVOCATION_COUNT {
HOUR = 20,
DAY = 24,
WEEK = 168,
MONTH = 30,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* 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.
*/

export enum RULE_PREVIEW_INVOCATION_COUNT {
HOUR = 20,
DAY = 24,
WEEK = 168,
MONTH = 30,
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const PreviewButton = styled(EuiButton)`
margin-left: 0;
`;

const defaultTimeRange: Unit = 'h';

const RulePreviewComponent: React.FC<RulePreviewProps> = ({
index,
isDisabled,
Expand All @@ -64,7 +66,7 @@ const RulePreviewComponent: React.FC<RulePreviewProps> = ({
}
}, [spaces]);

const [timeFrame, setTimeFrame] = useState<Unit>('h');
const [timeFrame, setTimeFrame] = useState<Unit>(defaultTimeRange);
const {
addNoiseWarning,
createPreview,
Expand All @@ -84,9 +86,9 @@ const RulePreviewComponent: React.FC<RulePreviewProps> = ({
threshold,
});

// Resets the timeFrame to default when rule type is changed
// Resets the timeFrame to default when rule type is changed because not all time frames are supported by all rule types
useEffect(() => {
setTimeFrame('h');
setTimeFrame(defaultTimeRange);
}, [ruleType]);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { useEffect, useState } from 'react';

import { Unit } from '@elastic/datemath';
import { INVOCATION_COUNT } from '../../../../../common/constants';
import { RULE_PREVIEW_INVOCATION_COUNT } from '../../../../../common/detection_engine/constants';
import { useAppToasts } from '../../../../common/hooks/use_app_toasts';
import {
PreviewResponse,
Expand All @@ -30,17 +30,17 @@ export const usePreviewRule = (timeframe: Unit = 'h') => {
const [response, setResponse] = useState<PreviewResponse>(emptyPreviewRule);
const [isLoading, setIsLoading] = useState(false);
const { addError } = useAppToasts();
let invocationCount = INVOCATION_COUNT.HOUR;
let invocationCount = RULE_PREVIEW_INVOCATION_COUNT.HOUR;

switch (timeframe) {
case 'd':
invocationCount = INVOCATION_COUNT.DAY;
invocationCount = RULE_PREVIEW_INVOCATION_COUNT.DAY;
break;
case 'w':
invocationCount = INVOCATION_COUNT.WEEK;
invocationCount = RULE_PREVIEW_INVOCATION_COUNT.WEEK;
break;
case 'M':
invocationCount = INVOCATION_COUNT.MONTH;
invocationCount = RULE_PREVIEW_INVOCATION_COUNT.MONTH;
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { buildRouteValidation } from '../../../../utils/build_validation/route_v
import { SetupPlugins } from '../../../../plugin';
import type { SecuritySolutionPluginRouter } from '../../../../types';
import { createRuleValidateTypeDependents } from '../../../../../common/detection_engine/schemas/request/create_rules_type_dependents';
import { DETECTION_ENGINE_RULES_PREVIEW, INVOCATION_COUNT } from '../../../../../common/constants';
import { DETECTION_ENGINE_RULES_PREVIEW } from '../../../../../common/constants';
import { previewRulesSchema } from '../../../../../common/detection_engine/schemas/request';
import { RuleExecutionStatus } from '../../../../../common/detection_engine/schemas/common/schemas';

Expand All @@ -43,6 +43,7 @@ import {
createThresholdAlertType,
} from '../../rule_types';
import { createSecurityRuleTypeWrapper } from '../../rule_types/create_security_rule_type_wrapper';
import { RULE_PREVIEW_INVOCATION_COUNT } from '../../../../../common/detection_engine/constants';

export const previewRulesRoute = async (
router: SecuritySolutionPluginRouter,
Expand Down Expand Up @@ -79,10 +80,10 @@ export const previewRulesRoute = async (
let invocationCount = request.body.invocationCount;
if (
![
INVOCATION_COUNT.HOUR,
INVOCATION_COUNT.DAY,
INVOCATION_COUNT.WEEK,
INVOCATION_COUNT.MONTH,
RULE_PREVIEW_INVOCATION_COUNT.HOUR,
RULE_PREVIEW_INVOCATION_COUNT.DAY,
RULE_PREVIEW_INVOCATION_COUNT.WEEK,
RULE_PREVIEW_INVOCATION_COUNT.MONTH,
].includes(invocationCount)
) {
return response.ok({ body: { errors: ['Invalid invocation count'] } });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ import expect from '@kbn/expect';

import { DETECTION_ENGINE_RULES_PREVIEW } from '../../../../plugins/security_solution/common/constants';
import { FtrProviderContext } from '../../common/ftr_provider_context';
import {
deleteAllAlerts,
createPreviewSignalsIndex,
deletePreviewSignalsIndex,
getSimplePreviewRule,
getSimpleRulePreviewOutput,
} from '../../utils';
import { deleteAllAlerts, getSimplePreviewRule, getSimpleRulePreviewOutput } from '../../utils';
import { ROLES } from '../../../../plugins/security_solution/common/test';
import { createUserAndRole, deleteUserAndRole } from '../../../common/services/security_solution';

Expand All @@ -36,12 +30,7 @@ export default ({ getService }: FtrProviderContext) => {
await esArchiver.unload('x-pack/test/functional/es_archives/auditbeat/hosts');
});

beforeEach(async () => {
await createPreviewSignalsIndex(supertest, log);
});

afterEach(async () => {
await deletePreviewSignalsIndex(supertest, log);
await deleteAllAlerts(supertest, log);
});

Expand Down
41 changes: 0 additions & 41 deletions x-pack/test/detection_engine_api_integration/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import {
DETECTION_ENGINE_INDEX_URL,
DETECTION_ENGINE_PREPACKAGED_URL,
DETECTION_ENGINE_QUERY_SIGNALS_URL,
DETECTION_ENGINE_RULES_PREVIEW_INDEX_URL,
DETECTION_ENGINE_RULES_URL,
DETECTION_ENGINE_SIGNALS_FINALIZE_MIGRATION_URL,
DETECTION_ENGINE_SIGNALS_MIGRATION_URL,
Expand Down Expand Up @@ -579,25 +578,6 @@ export const createSignalsIndex = async (
);
};

/**
* Creates the preview signals index for use inside of beforeEach blocks of tests
* This will retry 20 times before giving up and hopefully still not interfere with other tests
* @param supertest The supertest client library
*/
export const createPreviewSignalsIndex = async (
supertest: SuperTest.SuperTest<SuperTest.Test>,
log: ToolingLog
): Promise<void> => {
await countDownTest(
async () => {
await supertest.post(DETECTION_ENGINE_RULES_PREVIEW_INDEX_URL).set('kbn-xsrf', 'true').send();
return true;
},
'createPreviewSignalsIndex',
log
);
};

export const createLegacyRuleAction = async (
supertest: SuperTest.SuperTest<SuperTest.Test>,
alertId: string,
Expand Down Expand Up @@ -639,27 +619,6 @@ export const deleteSignalsIndex = async (
);
};

/**
* Deletes the signals index for use inside of afterEach blocks of tests
* @param supertest The supertest client library
*/
export const deletePreviewSignalsIndex = async (
supertest: SuperTest.SuperTest<SuperTest.Test>,
log: ToolingLog
): Promise<void> => {
await countDownTest(
async () => {
await supertest
.delete(DETECTION_ENGINE_RULES_PREVIEW_INDEX_URL)
.set('kbn-xsrf', 'true')
.send();
return true;
},
'deleteSignalsIndex',
log
);
};

/**
* Given an array of rule_id strings this will return a ndjson buffer which is useful
* for testing uploads.
Expand Down

0 comments on commit 9b3b2f3

Please sign in to comment.