Skip to content

Commit

Permalink
[Security Solution] Fix importing rules referencing preconfigured con…
Browse files Browse the repository at this point in the history
…nectors (elastic#176284)

**Fixes:** elastic#157253

## Summary

This PR fixes rules import with `overwrite_action_connectors` set to true when ndjson contains rules with actions referencing preconfigured action connectors.

## Details

A user can preconfigure action connectors as described [here](https://www.elastic.co/guide/en/kibana/current/pre-configured-connectors.html). At the same time Elastic Could instances have Elastic-cloud-SMTP connector preconfigured. In particular import doesn't work as expected in Elastic Cloud for rules having actions referencing the preconfigured Elastic-cloud-SMTP connector.

This is fixed by filtering out preconfigured connector ids so importing logic only handles custom action connectors.

On top of this functional tests have been added to make sure the problem won't come back.

### Checklist

- [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios
- [x] [Ran](https://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/5139) in Flaky test runner for ESS and Serverless and no flakiness has been revealed
  • Loading branch information
maximpn authored Feb 13, 2024
1 parent 1ec43c9 commit 934a06c
Show file tree
Hide file tree
Showing 16 changed files with 1,020 additions and 400 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@

import type { RuleToImport } from './rule_to_import';

export const getImportRulesSchemaMock = (ruleId = 'rule-1'): RuleToImport => ({
description: 'some description',
name: 'Query with a rule id',
query: 'user.name: root or user.name: admin',
severity: 'high',
type: 'query',
risk_score: 55,
language: 'kuery',
rule_id: ruleId,
immutable: false,
});
export const getImportRulesSchemaMock = (rewrites?: Partial<RuleToImport>): RuleToImport =>
({
description: 'some description',
name: 'Query with a rule id',
query: 'user.name: root or user.name: admin',
severity: 'high',
type: 'query',
risk_score: 55,
language: 'kuery',
rule_id: 'rule-1',
immutable: false,
...rewrites,
} as RuleToImport);

export const getImportRulesWithIdSchemaMock = (ruleId = 'rule-1'): RuleToImport => ({
id: '6afb8ce1-ea94-4790-8653-fd0b021d2113',
Expand Down Expand Up @@ -47,42 +49,46 @@ export const rulesToNdJsonString = (rules: RuleToImport[]) => {
* @param ruleIds Array of ruleIds with which to generate rule JSON
*/
export const ruleIdsToNdJsonString = (ruleIds: string[]) => {
const rules = ruleIds.map((ruleId) => getImportRulesSchemaMock(ruleId));
const rules = ruleIds.map((ruleId) => getImportRulesSchemaMock({ rule_id: ruleId }));
return rulesToNdJsonString(rules);
};

export const getImportThreatMatchRulesSchemaMock = (ruleId = 'rule-1'): RuleToImport => ({
description: 'some description',
name: 'Query with a rule id',
query: 'user.name: root or user.name: admin',
severity: 'high',
type: 'threat_match',
risk_score: 55,
language: 'kuery',
rule_id: ruleId,
threat_index: ['index-123'],
threat_mapping: [{ entries: [{ field: 'host.name', type: 'mapping', value: 'host.name' }] }],
threat_query: '*:*',
threat_filters: [
{
bool: {
must: [
{
query_string: {
query: 'host.name: linux',
analyze_wildcard: true,
time_zone: 'Zulu',
export const getImportThreatMatchRulesSchemaMock = (
rewrites?: Partial<RuleToImport>
): RuleToImport =>
({
description: 'some description',
name: 'Query with a rule id',
query: 'user.name: root or user.name: admin',
severity: 'high',
type: 'threat_match',
risk_score: 55,
language: 'kuery',
rule_id: 'rule-1',
threat_index: ['index-123'],
threat_mapping: [{ entries: [{ field: 'host.name', type: 'mapping', value: 'host.name' }] }],
threat_query: '*:*',
threat_filters: [
{
bool: {
must: [
{
query_string: {
query: 'host.name: linux',
analyze_wildcard: true,
time_zone: 'Zulu',
},
},
},
],
filter: [],
should: [],
must_not: [],
],
filter: [],
should: [],
must_not: [],
},
},
},
],
immutable: false,
});
],
immutable: false,
...rewrites,
} as RuleToImport);

export const webHookConnector = {
id: 'cabc78e0-9031-11ed-b076-53cc4d57aaf1',
Expand All @@ -104,8 +110,7 @@ export const webHookConnector = {

export const ruleWithConnectorNdJSON = (): string => {
const items = [
{
...getImportRulesSchemaMock(),
getImportRulesSchemaMock({
actions: [
{
group: 'default',
Expand All @@ -114,7 +119,7 @@ export const ruleWithConnectorNdJSON = (): string => {
params: {},
},
],
},
}),
webHookConnector,
];
const stringOfExceptions = items.map((item) => JSON.stringify(item));
Expand Down
Loading

0 comments on commit 934a06c

Please sign in to comment.