-
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.
- Loading branch information
Showing
62 changed files
with
2,552 additions
and
2,040 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
14 changes: 14 additions & 0 deletions
14
...etection_engine/prebuilt_rules/logic/rule_assets/__mocks__/prebuilt_rule_assets_client.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,14 @@ | ||
/* | ||
* 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 const createPrebuiltRuleAssetsClient = () => { | ||
return { | ||
fetchLatestAssets: jest.fn(), | ||
fetchLatestVersions: jest.fn(), | ||
fetchAssetsByVersion: jest.fn(), | ||
}; | ||
}; |
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
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
29 changes: 29 additions & 0 deletions
29
...e_management/logic/detection_rules_client/converters/common_params_camel_to_snake.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,29 @@ | ||
/* | ||
* 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 { getBaseRuleParams } from '../../../../rule_schema/mocks'; | ||
import { commonParamsCamelToSnake } from './common_params_camel_to_snake'; | ||
|
||
describe('commonParamsCamelToSnake', () => { | ||
test('should convert rule_source params to snake case', () => { | ||
const transformedParams = commonParamsCamelToSnake({ | ||
...getBaseRuleParams(), | ||
ruleSource: { | ||
type: 'external', | ||
isCustomized: false, | ||
}, | ||
}); | ||
expect(transformedParams).toEqual( | ||
expect.objectContaining({ | ||
rule_source: { | ||
type: 'external', | ||
is_customized: false, | ||
}, | ||
}) | ||
); | ||
}); | ||
}); |
47 changes: 47 additions & 0 deletions
47
...e/rule_management/logic/detection_rules_client/converters/common_params_camel_to_snake.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,47 @@ | ||
/* | ||
* 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 { convertObjectKeysToSnakeCase } from '../../../../../../utils/object_case_converters'; | ||
import type { BaseRuleParams } from '../../../../rule_schema'; | ||
import { migrateLegacyInvestigationFields } from '../../../utils/utils'; | ||
|
||
export const commonParamsCamelToSnake = (params: BaseRuleParams) => { | ||
return { | ||
description: params.description, | ||
risk_score: params.riskScore, | ||
severity: params.severity, | ||
building_block_type: params.buildingBlockType, | ||
namespace: params.namespace, | ||
note: params.note, | ||
license: params.license, | ||
output_index: params.outputIndex, | ||
timeline_id: params.timelineId, | ||
timeline_title: params.timelineTitle, | ||
meta: params.meta, | ||
rule_name_override: params.ruleNameOverride, | ||
timestamp_override: params.timestampOverride, | ||
timestamp_override_fallback_disabled: params.timestampOverrideFallbackDisabled, | ||
investigation_fields: migrateLegacyInvestigationFields(params.investigationFields), | ||
author: params.author, | ||
false_positives: params.falsePositives, | ||
from: params.from, | ||
rule_id: params.ruleId, | ||
max_signals: params.maxSignals, | ||
risk_score_mapping: params.riskScoreMapping, | ||
severity_mapping: params.severityMapping, | ||
threat: params.threat, | ||
to: params.to, | ||
references: params.references, | ||
version: params.version, | ||
exceptions_list: params.exceptionsList, | ||
immutable: params.immutable, | ||
rule_source: convertObjectKeysToSnakeCase(params.ruleSource), | ||
related_integrations: params.relatedIntegrations ?? [], | ||
required_fields: params.requiredFields ?? [], | ||
setup: params.setup ?? '', | ||
}; | ||
}; |
26 changes: 26 additions & 0 deletions
26
...agement/logic/detection_rules_client/converters/convert_alerting_rule_to_rule_response.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,26 @@ | ||
/* | ||
* 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 { SanitizedRule } from '@kbn/alerting-plugin/common'; | ||
import { stringifyZodError } from '@kbn/zod-helpers'; | ||
import { RuleResponse } from '../../../../../../../common/api/detection_engine/model/rule_schema'; | ||
import type { RuleParams } from '../../../../rule_schema'; | ||
import { internalRuleToAPIResponse } from './internal_rule_to_api_response'; | ||
import { RuleResponseValidationError } from '../utils'; | ||
|
||
export function convertAlertingRuleToRuleResponse(rule: SanitizedRule<RuleParams>): RuleResponse { | ||
const parseResult = RuleResponse.safeParse(internalRuleToAPIResponse(rule)); | ||
|
||
if (!parseResult.success) { | ||
throw new RuleResponseValidationError({ | ||
message: stringifyZodError(parseResult.error), | ||
ruleId: rule.params.ruleId, | ||
}); | ||
} | ||
|
||
return parseResult.data; | ||
} |
39 changes: 39 additions & 0 deletions
39
...t/logic/detection_rules_client/converters/convert_prebuilt_rule_asset_to_rule_response.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,39 @@ | ||
/* | ||
* 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 { v4 as uuidv4 } from 'uuid'; | ||
import { RuleResponse } from '../../../../../../../common/api/detection_engine/model/rule_schema'; | ||
import { addEcsToRequiredFields } from '../../../utils/utils'; | ||
import type { PrebuiltRuleAsset } from '../../../../prebuilt_rules'; | ||
import { RULE_DEFAULTS } from '../mergers/apply_rule_defaults'; | ||
|
||
export const convertPrebuiltRuleAssetToRuleResponse = ( | ||
prebuiltRuleAsset: PrebuiltRuleAsset | ||
): RuleResponse => { | ||
const immutable = true; | ||
|
||
const ruleResponseSpecificFields = { | ||
id: uuidv4(), | ||
updated_at: new Date().toISOString(), | ||
updated_by: '', | ||
created_at: new Date().toISOString(), | ||
created_by: '', | ||
immutable, | ||
rule_source: { | ||
type: 'external', | ||
is_customized: false, | ||
}, | ||
revision: 1, | ||
}; | ||
|
||
return RuleResponse.parse({ | ||
...RULE_DEFAULTS, | ||
...prebuiltRuleAsset, | ||
required_fields: addEcsToRequiredFields(prebuiltRuleAsset.required_fields), | ||
...ruleResponseSpecificFields, | ||
}); | ||
}; |
Oops, something went wrong.