Skip to content

Cypress checking on rule YAML content #248

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

Merged
Show file tree
Hide file tree
Changes from 2 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
42 changes: 28 additions & 14 deletions cypress/integration/2_rules.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const uniqueId = Cypress._.random(0, 1e6);
const SAMPLE_RULE = {
name: `Cypress test rule ${uniqueId}`,
logType: 'windows',
description: 'This is a rule used to test the rule creation workflow. Not for production use.',
description: 'This is a rule used to test the rule creation workflow.',
detection:
'selection:\n Provider_Name: Service Control Manager\nEventID: 7045\nServiceName: ZzNetSvc\n{backspace}{backspace}condition: selection',
detectionLine: [
Expand All @@ -28,22 +28,23 @@ const SAMPLE_RULE = {
};

const YAML_RULE_LINES = [
`id:`,
`logsource:`,
`product: ${SAMPLE_RULE.logType}`,
`title: ${SAMPLE_RULE.name}`,
`description:`,
`${SAMPLE_RULE.description}`,
`level: ${SAMPLE_RULE.severity}`,
`description: ${SAMPLE_RULE.description}`,
`tags:`,
`- ${SAMPLE_RULE.tags[0]}`,
`- ${SAMPLE_RULE.tags[1]}`,
`- ${SAMPLE_RULE.tags[2]}`,
`references:`,
`- '${SAMPLE_RULE.references}'`,
`falsepositives:`,
`- ${SAMPLE_RULE.falsePositive}`,
`author: ${SAMPLE_RULE.author}`,
`level: ${SAMPLE_RULE.severity}`,
`status: ${SAMPLE_RULE.status}`,
`logsource:`,
`product: ${SAMPLE_RULE.logType}`,
`references:`,
`- '${SAMPLE_RULE.references}'`,
`author: ${SAMPLE_RULE.author}`,
`detection:`,
...SAMPLE_RULE.detection.replaceAll(' ', '').replaceAll('{backspace}', '').split('\n'),
];

Expand Down Expand Up @@ -222,11 +223,24 @@ describe('Rules', () => {
force: true,
});

YAML_RULE_LINES.forEach((line) =>
cy
.get('[data-test-subj="rule_flyout_yaml_rule"]', TWENTY_SECONDS_TIMEOUT)
.contains(line, TWENTY_SECONDS_TIMEOUT)
);
cy.get('[data-test-subj="rule_flyout_yaml_rule"]')
.get('[class="euiCodeBlock__line"]')
.each((lineElement, lineIndex) => {
if (lineIndex >= YAML_RULE_LINES.length) {
return;
}
let line = lineElement.text().replaceAll('\n', '').trim();
let expectedLine = YAML_RULE_LINES[lineIndex];

// The document ID field is generated when the document is added to the index,
// so this test just checks that the line starts with the ID key.
if (expectedLine.startsWith('id:')) {
expectedLine = 'id:';
expect(line, `Sigma rule line ${lineIndex}`).to.contain(expectedLine);
} else {
expect(line, `Sigma rule line ${lineIndex}`).to.equal(expectedLine);
}
});

// Close the flyout
cy.get('[data-test-subj="close-rule-details-flyout"]', TWENTY_SECONDS_TIMEOUT).click({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const RuleContentYamlViewer: React.FC<RuleContentYamlViewerProps> = ({ ru
const ruleYaml = mapYamlObjectToYamlString(yamlObject);

return (
<EuiCodeBlock language="yaml" data-test-subj={'rule_flyout_yaml_rule'}>
<EuiCodeBlock language="yaml" data-test-subj={'rule_flyout_yaml_rule'} isCopyable={true}>
{ruleYaml}
</EuiCodeBlock>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`<RuleContentYamlViewer /> spec renders the component 1`] = `
<div
class="euiCodeBlock euiCodeBlock--fontSmall euiCodeBlock--paddingLarge prismjs language-yaml"
class="euiCodeBlock euiCodeBlock--fontSmall euiCodeBlock--paddingLarge euiCodeBlock--hasControls prismjs language-yaml"
>
<pre
class="euiCodeBlock__pre euiCodeBlock__pre--whiteSpacePreWrap"
Expand Down Expand Up @@ -403,5 +403,24 @@ exports[`<RuleContentYamlViewer /> spec renders the component 1`] = `
</span>
</code>
</pre>
<div
class="euiCodeBlock__controls"
>
<div
class="euiCodeBlock__copyButton"
>
<span
class="euiToolTipAnchor"
>
<button
aria-label="Copy"
class="euiButtonIcon euiButtonIcon--text euiButtonIcon--empty euiButtonIcon--xSmall"
type="button"
>
EuiIconMock
</button>
</span>
</div>
</div>
</div>
`;