-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Security Solution] Implement normalization of ruleSource for API res…
…ponses (#188631) Fixes: #180140 ## Summary - Implements normalization of`rule_source` for API responses - `rule_source` field in API responses is calculated out of the `immutable` and `ruleSource` fields. ### For maintainers - [ ] This was checked for breaking API changes and was [labeled appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
- Loading branch information
Showing
9 changed files
with
129 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...ine/rule_management/logic/detection_rules_client/converters/normalize_rule_params.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* 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 { normalizeRuleSource } from './normalize_rule_params'; | ||
import type { BaseRuleParams } from '../../../../rule_schema'; | ||
|
||
describe('normalizeRuleSource', () => { | ||
it('should return rule_source of type `internal` when immutable is false and ruleSource is undefined', () => { | ||
const result = normalizeRuleSource({ | ||
immutable: false, | ||
ruleSource: undefined, | ||
}); | ||
expect(result).toEqual({ | ||
type: 'internal', | ||
}); | ||
}); | ||
|
||
it('should return rule_source of type `external` and `isCustomized: false` when immutable is true and ruleSource is undefined', () => { | ||
const result = normalizeRuleSource({ | ||
immutable: true, | ||
ruleSource: undefined, | ||
}); | ||
expect(result).toEqual({ | ||
type: 'external', | ||
isCustomized: false, | ||
}); | ||
}); | ||
|
||
it('should return existing value when ruleSource is present', () => { | ||
const externalRuleSource: BaseRuleParams['ruleSource'] = { | ||
type: 'external', | ||
isCustomized: true, | ||
}; | ||
const externalResult = normalizeRuleSource({ immutable: true, ruleSource: externalRuleSource }); | ||
expect(externalResult).toEqual({ | ||
type: externalRuleSource.type, | ||
isCustomized: externalRuleSource.isCustomized, | ||
}); | ||
|
||
const internalRuleSource: BaseRuleParams['ruleSource'] = { | ||
type: 'internal', | ||
}; | ||
const internalResult = normalizeRuleSource({ | ||
immutable: false, | ||
ruleSource: internalRuleSource, | ||
}); | ||
expect(internalResult).toEqual({ | ||
type: internalRuleSource.type, | ||
}); | ||
}); | ||
}); |
48 changes: 48 additions & 0 deletions
48
...n_engine/rule_management/logic/detection_rules_client/converters/normalize_rule_params.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* 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 { BaseRuleParams, RuleSourceCamelCased } from '../../../../rule_schema'; | ||
|
||
interface NormalizeRuleSourceParams { | ||
immutable: BaseRuleParams['immutable']; | ||
ruleSource: BaseRuleParams['ruleSource']; | ||
} | ||
|
||
/* | ||
* Since there's no mechanism to migrate all rules at the same time, | ||
* we cannot guarantee that the ruleSource params is present in all rules. | ||
* This function will normalize the ruleSource param, creating it if does | ||
* not exist in ES, based on the immutable param. | ||
*/ | ||
export const normalizeRuleSource = ({ | ||
immutable, | ||
ruleSource, | ||
}: NormalizeRuleSourceParams): RuleSourceCamelCased => { | ||
if (!ruleSource) { | ||
const normalizedRuleSource: RuleSourceCamelCased = immutable | ||
? { | ||
type: 'external', | ||
isCustomized: false, | ||
} | ||
: { | ||
type: 'internal', | ||
}; | ||
|
||
return normalizedRuleSource; | ||
} | ||
return ruleSource; | ||
}; | ||
|
||
export const normalizeRuleParams = (params: BaseRuleParams) => { | ||
return { | ||
...params, | ||
// Fields to normalize | ||
ruleSource: normalizeRuleSource({ | ||
immutable: params.immutable, | ||
ruleSource: params.ruleSource, | ||
}), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters