-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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] Implement normalization of ruleSource for API responses #188631
Changes from 1 commit
11c7ebc
b087bf4
c13c9e2
3f0cc97
12960bd
8a96a75
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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_source'; | ||
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', | ||
is_customized: false, | ||
}); | ||
}); | ||
|
||
it('should return snake_case version of rule_source 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, | ||
is_customized: externalRuleSource.isCustomized, | ||
}); | ||
|
||
const internalRuleSource: BaseRuleParams['ruleSource'] = { | ||
type: 'internal', | ||
}; | ||
const internalResult = normalizeRuleSource({ | ||
immutable: false, | ||
ruleSource: internalRuleSource, | ||
}); | ||
expect(internalResult).toEqual({ | ||
type: internalRuleSource.type, | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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 { RuleSource } from '../../../../../../../common/api/detection_engine'; | ||
import { convertObjectKeysToSnakeCase } from '../../../../../../utils/object_case_converters'; | ||
import type { BaseRuleParams } from '../../../../rule_schema'; | ||
|
||
interface NormalizeRuleSourceParams { | ||
immutable: BaseRuleParams['immutable']; | ||
ruleSource: BaseRuleParams['ruleSource']; | ||
} | ||
export const normalizeRuleSource = ({ | ||
immutable, | ||
ruleSource, | ||
}: NormalizeRuleSourceParams): RuleSource => { | ||
if (!ruleSource) { | ||
const normalizedRuleSource = immutable | ||
? { | ||
type: 'external', | ||
isCustomized: false, | ||
} | ||
: { | ||
type: 'internal', | ||
}; | ||
|
||
return convertObjectKeysToSnakeCase(normalizedRuleSource) as RuleSource; | ||
} | ||
return convertObjectKeysToSnakeCase(ruleSource) as RuleSource; | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @xcrzx I remember you mentioning handling possible data inconsistencies. For example, data in ES being for whatever reason:
It's hard to me to think where these inconsitencies might arise from, but do you think it makes sense to rely always on In the case above, calculating
Or if in ES data looks like:
calculating
WDYT? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think what you've implemented is correct. Relying on |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should not be normalizing data in case converters, as these functions are solely responsible for converting from camel case to snake case and vice versa. I think what you need is a converter from the alerting rule type to the rule response type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, thanks.
Created a normalization function to all params, applied in
internalRuleToAPIResponse
.