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

[Security Solution] Adds extra cypress and integration tests for setup guide field #180638

Merged
merged 13 commits into from
May 2, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import * as z from 'zod';
import {
RelatedIntegrationArray,
RequiredFieldArray,
SetupGuide,
RuleSignatureId,
RuleVersion,
BaseCreateProps,
Expand Down Expand Up @@ -37,6 +36,5 @@ export const PrebuiltRuleAsset = BaseCreateProps.and(TypeSpecificCreateProps).an
version: RuleVersion,
related_integrations: RelatedIntegrationArray.optional(),
required_fields: RequiredFieldArray.optional(),
setup: SetupGuide.optional(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dplumlee @jpdjere Can you folks explain this change to me? Should related_integrations and required_fields get the same treatment?

Copy link
Contributor Author

@dplumlee dplumlee Apr 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in reference to this comment, so yes if you're adding those fields to BaseDefaultableFields, they should respectively be removed in this type.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Removed "required_fields" in my required fields PR.

})
);
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import expect from 'expect';

import { DETECTION_ENGINE_RULES_URL } from '@kbn/security-solution-plugin/common/constants';
import { BaseDefaultableFields } from '@kbn/security-solution-plugin/common/api/detection_engine';
import { FtrProviderContext } from '../../../../../ftr_provider_context';
import { binaryToString, getCustomQueryRuleParams } from '../../../utils';
import {
Expand All @@ -20,6 +21,7 @@ export default ({ getService }: FtrProviderContext): void => {
const supertest = getService('supertest');
const log = getService('log');
const es = getService('es');
const securitySolutionApi = getService('securitySolutionApi');

describe('@ess @serverless export_rules', () => {
describe('exporting rules', () => {
Expand Down Expand Up @@ -63,6 +65,24 @@ export default ({ getService }: FtrProviderContext): void => {
expect(exportedRule).toMatchObject(ruleToExport);
});

it('should export defaultable fields when values are set', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

Suggested change
it('should export defaultable fields when values are set', async () => {
it('exports defaultable fields when non-default values are set', async () => {

const defaultableFields: BaseDefaultableFields = {
setup: '# some setup markdown',
};
const ruleToExport = getCustomQueryRuleParams(defaultableFields);

await securitySolutionApi.createRule({ body: ruleToExport });

const { body } = await securitySolutionApi
dplumlee marked this conversation as resolved.
Show resolved Hide resolved
.exportRules({ query: {}, body: null })
.expect(200)
.parse(binaryToString);

const exportedRule = JSON.parse(body.toString().split(/\n/)[0]);

expect(exportedRule).toMatchObject(defaultableFields);
});

it('should have export summary reflecting a number of rules', async () => {
await createRule(supertest, log, getCustomQueryRuleParams());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import expect from 'expect';

import { DETECTION_ENGINE_RULES_URL } from '@kbn/security-solution-plugin/common/constants';
import { BaseDefaultableFields } from '@kbn/security-solution-plugin/common/api/detection_engine';
import { FtrProviderContext } from '../../../../../ftr_provider_context';
import { getCustomQueryRuleParams, combineToNdJson, fetchRule } from '../../../utils';
import {
Expand All @@ -21,6 +22,7 @@ export default ({ getService }: FtrProviderContext): void => {
const supertest = getService('supertest');
const log = getService('log');
const es = getService('es');
const securitySolutionApi = getService('securitySolutionApi');

describe('@ess @serverless import_rules', () => {
describe('importing rules with an index', () => {
Expand Down Expand Up @@ -135,6 +137,30 @@ export default ({ getService }: FtrProviderContext): void => {
expect(body.errors[0].error.message).toBe('from: Failed to parse date-math expression');
});

it('should be able to import rules with defaultable fields', async () => {
const defaultableFields: BaseDefaultableFields = {
setup: '# some setup markdown',
};
const ruleToImport = getCustomQueryRuleParams({
...defaultableFields,
rule_id: 'rule-1',
});
const ndjson = combineToNdJson(ruleToImport);

await securitySolutionApi
dplumlee marked this conversation as resolved.
Show resolved Hide resolved
.importRules({ query: {} })
.attach('file', Buffer.from(ndjson), 'rules.ndjson')
.expect(200);

const { body: importedRule } = await securitySolutionApi
.readRule({
query: { rule_id: 'rule-1' },
})
.expect(200);

expect(importedRule).toMatchObject(ruleToImport);
});

it('should be able to import two rules', async () => {
const ndjson = combineToNdJson(
getCustomQueryRuleParams({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type {
RuleName,
RuleReferenceArray,
RuleTagArray,
SetupGuide,
} from '@kbn/security-solution-plugin/common/api/detection_engine';

interface RuleFields {
Expand All @@ -44,6 +45,7 @@ interface RuleFields {
threat: Threat;
threatSubtechnique: ThreatSubtechnique;
threatTechnique: ThreatTechnique;
setup: SetupGuide;
}

export const ruleFields: RuleFields = {
Expand All @@ -60,6 +62,7 @@ export const ruleFields: RuleFields = {
],
falsePositives: ['False1', 'False2'],
investigationGuide: '# test markdown',
setup: '# test setup markdown',
investigationFields: {
field_names: ['agent.hostname'],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import {
RULE_NAME_INPUT,
SCHEDULE_CONTINUE_BUTTON,
} from '../../../../screens/create_new_rule';
import { RULE_NAME_HEADER } from '../../../../screens/rule_details';
import {
DESCRIPTION_SETUP_GUIDE_BUTTON,
DESCRIPTION_SETUP_GUIDE_CONTENT,
RULE_NAME_HEADER,
} from '../../../../screens/rule_details';
import { createTimeline } from '../../../../tasks/api_calls/timelines';
import { deleteAlertsAndRules } from '../../../../tasks/api_calls/common';
import {
Expand All @@ -30,6 +34,7 @@ import {
fillRiskScore,
fillRuleName,
fillRuleTags,
fillSetup,
fillSeverity,
fillThreat,
fillThreatSubtechnique,
Expand Down Expand Up @@ -75,6 +80,7 @@ describe('Common rule creation flows', { tags: ['@ess', '@serverless'] }, () =>
fillThreatSubtechnique();
fillCustomInvestigationFields();
fillNote();
fillSetup();
cy.get(ABOUT_CONTINUE_BTN).click();

cy.log('Filling schedule section');
Expand All @@ -95,5 +101,8 @@ describe('Common rule creation flows', { tags: ['@ess', '@serverless'] }, () =>

// UI redirects to rule creation page of a created rule
cy.get(RULE_NAME_HEADER).should('contain', ruleFields.ruleName);

cy.get(DESCRIPTION_SETUP_GUIDE_BUTTON).click();
cy.get(DESCRIPTION_SETUP_GUIDE_CONTENT).should('contain', ruleFields.setup);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ export const INPUT = '[data-test-subj="input"]';
export const INVESTIGATION_NOTES_TEXTAREA =
'[data-test-subj="detectionEngineStepAboutRuleNote"] textarea';

export const SETUP_GUIDE_TEXTAREA = '[data-test-subj="detectionEngineStepAboutRuleSetup"] textarea';

export const FALSE_POSITIVES_INPUT =
'[data-test-subj="detectionEngineStepAboutRuleFalsePositives"] input';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,7 @@ export const ALERT_SUPPRESSION_INSUFFICIENT_LICENSING_ICON =

export const HIGHLIGHTED_ROWS_IN_TABLE =
'[data-test-subj="euiDataGridBody"] .alertsTableHighlightedRow';

export const DESCRIPTION_SETUP_GUIDE_BUTTON = '[data-test-subj="stepAboutDetailsToggle-setup"]';

export const DESCRIPTION_SETUP_GUIDE_CONTENT = '[data-test-subj="stepAboutDetailsSetupContent"]';
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ import {
ALERTS_INDEX_BUTTON,
INVESTIGATIONS_INPUT,
QUERY_BAR_ADD_FILTER,
SETUP_GUIDE_TEXTAREA,
} from '../screens/create_new_rule';
import {
INDEX_SELECTOR,
Expand Down Expand Up @@ -203,6 +204,13 @@ export const fillNote = (note: string = ruleFields.investigationGuide) => {
return note;
};

export const fillSetup = (setup: string = ruleFields.setup) => {
cy.get(SETUP_GUIDE_TEXTAREA).clear({ force: true });
cy.get(SETUP_GUIDE_TEXTAREA).type(setup);

return setup;
};

export const fillMitre = (mitreAttacks: Threat[]) => {
let techniqueIndex = 0;
let subtechniqueInputIndex = 0;
Expand Down