-
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.
add warning validation for indicator mapping field names
- Loading branch information
Showing
6 changed files
with
203 additions
and
58 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
11 changes: 11 additions & 0 deletions
11
...ction_engine/rule_creation/components/threat_match_mapping_edit/validators/error_codes.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,11 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export enum THREAT_MATCH_MAPPING_ERROR_CODES { | ||
ERR_FIELD_MISSING = 'ERR_FIELD_MISSING', | ||
ERR_FIELDS_UNKNOWN = 'ERR_FIELDS_UNKNOWN', | ||
} |
51 changes: 51 additions & 0 deletions
51
...ents/threat_match_mapping_edit/validators/get_unknown_threat_match_mapping_field_names.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,51 @@ | ||
/* | ||
* 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 { DataViewBase } from '@kbn/es-query'; | ||
import type { ThreatMapEntries } from '../../../../../common/components/threat_match/types'; | ||
|
||
interface GetUnknownThreatMatchMappingFieldNamesParams { | ||
entries: ThreatMapEntries[]; | ||
indexPatterns: DataViewBase; | ||
threatIndexPatterns: DataViewBase; | ||
} | ||
|
||
interface GetUnknownThreatMatchMappingFieldNamesResult { | ||
unknownSourceIndicesFields: string[]; | ||
unknownThreatMatchIndicesFields: string[]; | ||
} | ||
|
||
export function getUnknownThreatMatchMappingFieldNames({ | ||
entries, | ||
indexPatterns, | ||
threatIndexPatterns, | ||
}: GetUnknownThreatMatchMappingFieldNamesParams): GetUnknownThreatMatchMappingFieldNamesResult { | ||
const knownIndexPatternsFields = new Set(indexPatterns.fields.map(({ name }) => name)); | ||
const knownThreatMatchIndexPatternsFields = new Set( | ||
threatIndexPatterns.fields.map(({ name }) => name) | ||
); | ||
|
||
const unknownSourceIndicesFields: string[] = []; | ||
const unknownThreatMatchIndicesFields: string[] = []; | ||
|
||
for (const { entries: subEntries } of entries) { | ||
for (const subEntry of subEntries) { | ||
if (subEntry.field && !knownIndexPatternsFields.has(subEntry.field)) { | ||
unknownSourceIndicesFields.push(subEntry.field); | ||
} | ||
|
||
if (subEntry.value && !knownThreatMatchIndexPatternsFields.has(subEntry.value)) { | ||
unknownThreatMatchIndicesFields.push(subEntry.value); | ||
} | ||
} | ||
} | ||
|
||
return { | ||
unknownSourceIndicesFields, | ||
unknownThreatMatchIndicesFields, | ||
}; | ||
} |
46 changes: 0 additions & 46 deletions
46
...reation/components/threat_match_mapping_edit/validators/threat_match_mapping_validator.ts
This file was deleted.
Oops, something went wrong.
116 changes: 116 additions & 0 deletions
116
...components/threat_match_mapping_edit/validators/threat_match_mapping_validator_factory.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,116 @@ | ||
/* | ||
* 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 { i18n } from '@kbn/i18n'; | ||
import type { DataViewBase } from '@kbn/es-query'; | ||
import { | ||
containsInvalidItems, | ||
singleEntryThreat, | ||
} from '../../../../../common/components/threat_match/helpers'; | ||
import type { FormData, ValidationFunc } from '../../../../../shared_imports'; | ||
import type { ThreatMapEntries } from '../../../../../common/components/threat_match/types'; | ||
import { THREAT_MATCH_MAPPING_ERROR_CODES } from './error_codes'; | ||
import { getUnknownThreatMatchMappingFieldNames } from './get_unknown_threat_match_mapping_field_names'; | ||
|
||
interface ThreatMatchMappingValidatorFactoryParams { | ||
indexPatterns: DataViewBase; | ||
threatIndexPatterns: DataViewBase; | ||
} | ||
|
||
export function threatMatchMappingValidatorFactory({ | ||
indexPatterns, | ||
threatIndexPatterns, | ||
}: ThreatMatchMappingValidatorFactoryParams): ValidationFunc<FormData, string, ThreatMapEntries[]> { | ||
return (...args) => { | ||
const [{ path, value }] = args; | ||
|
||
if (singleEntryThreat(value)) { | ||
return { | ||
code: THREAT_MATCH_MAPPING_ERROR_CODES.ERR_FIELD_MISSING, | ||
path, | ||
message: i18n.translate( | ||
'xpack.securitySolution.detectionEngine.ruleManagement.createRule.threatMappingField.requiredError', | ||
{ | ||
defaultMessage: 'At least one indicator match is required.', | ||
} | ||
), | ||
}; | ||
} | ||
|
||
if (containsInvalidItems(value)) { | ||
return { | ||
code: THREAT_MATCH_MAPPING_ERROR_CODES.ERR_FIELD_MISSING, | ||
path, | ||
message: i18n.translate( | ||
'xpack.securitySolution.detectionEngine.ruleManagement.threatMappingField.bothFieldNamesRequiredError', | ||
{ | ||
defaultMessage: 'All matches require both a field and threat index field.', | ||
} | ||
), | ||
}; | ||
} | ||
|
||
const { unknownSourceIndicesFields, unknownThreatMatchIndicesFields } = | ||
getUnknownThreatMatchMappingFieldNames({ | ||
entries: value, | ||
indexPatterns, | ||
threatIndexPatterns, | ||
}); | ||
|
||
if (unknownSourceIndicesFields.length > 0 && unknownThreatMatchIndicesFields.length > 0) { | ||
return { | ||
code: THREAT_MATCH_MAPPING_ERROR_CODES.ERR_FIELDS_UNKNOWN, | ||
path, | ||
message: i18n.translate( | ||
'xpack.securitySolution.detectionEngine.ruleManagement.threatMappingField.unknownFields', | ||
{ | ||
defaultMessage: | ||
'Indicator mapping has unknown fields. {unknownSourceIndicesFields} field(s) not found in the source events indices and {unknownThreatMatchIndicesFields} field(s) not found in the indicator indices.', | ||
values: { | ||
unknownSourceIndicesFields: `"${unknownSourceIndicesFields.join('", "')}"`, | ||
unknownThreatMatchIndicesFields: `"${unknownThreatMatchIndicesFields.join('", "')}"`, | ||
}, | ||
} | ||
), | ||
}; | ||
} | ||
|
||
if (unknownSourceIndicesFields.length > 0) { | ||
return { | ||
code: THREAT_MATCH_MAPPING_ERROR_CODES.ERR_FIELDS_UNKNOWN, | ||
path, | ||
message: i18n.translate( | ||
'xpack.securitySolution.detectionEngine.ruleManagement.threatMappingField.unknownSourceIndicesFields', | ||
{ | ||
defaultMessage: | ||
'Indicator mapping has unknown fields. {unknownSourceIndicesFields} field(s) not found in the source events indices.', | ||
values: { | ||
unknownSourceIndicesFields: `"${unknownSourceIndicesFields.join('", "')}"`, | ||
}, | ||
} | ||
), | ||
}; | ||
} | ||
|
||
if (unknownSourceIndicesFields.length > 0) { | ||
return { | ||
code: THREAT_MATCH_MAPPING_ERROR_CODES.ERR_FIELDS_UNKNOWN, | ||
path, | ||
message: i18n.translate( | ||
'xpack.securitySolution.detectionEngine.ruleManagement.threatMappingField.unknownIndicatorIndicesFields', | ||
{ | ||
defaultMessage: | ||
'Indicator mapping has unknown fields. {unknownThreatMatchIndicesFields} field(s) not found in the indicator indices.', | ||
values: { | ||
unknownThreatMatchIndicesFields: `"${unknownThreatMatchIndicesFields.join('", "')}"`, | ||
}, | ||
} | ||
), | ||
}; | ||
} | ||
}; | ||
} |
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