diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/prebuilt_rules/logic/diff/calculation/algorithms/force_target_version_diff_algorithm.test.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/prebuilt_rules/logic/diff/calculation/algorithms/force_target_version_diff_algorithm.test.ts new file mode 100644 index 0000000000000..f6035c9e87ca0 --- /dev/null +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/prebuilt_rules/logic/diff/calculation/algorithms/force_target_version_diff_algorithm.test.ts @@ -0,0 +1,78 @@ +/* + * 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 { ThreeVersionsOf } from '../../../../../../../../common/api/detection_engine'; +import { + ThreeWayMergeOutcome, + MissingVersion, + ThreeWayDiffConflict, +} from '../../../../../../../../common/api/detection_engine'; +import { forceTargetVersionDiffAlgorithm } from './force_target_version_diff_algorithm'; + +describe('forceTargetVersionDiffAlgorithm', () => { + describe('when base version exists', () => { + it('returns a NON conflict diff', () => { + const mockVersions: ThreeVersionsOf = { + base_version: 1, + current_version: 1, + target_version: 2, + }; + + const result = forceTargetVersionDiffAlgorithm(mockVersions); + + expect(result).toMatchObject({ + conflict: ThreeWayDiffConflict.NONE, + }); + }); + + it('return merge outcome TARGET', () => { + const mockVersions: ThreeVersionsOf = { + base_version: 1, + current_version: 1, + target_version: 2, + }; + + const result = forceTargetVersionDiffAlgorithm(mockVersions); + + expect(result).toMatchObject({ + has_base_version: true, + merge_outcome: ThreeWayMergeOutcome.Target, + }); + }); + }); + + describe('when base version missing', () => { + it('returns a NON conflict diff', () => { + const mockVersions: ThreeVersionsOf = { + base_version: MissingVersion, + current_version: 1, + target_version: 2, + }; + + const result = forceTargetVersionDiffAlgorithm(mockVersions); + + expect(result).toMatchObject({ + conflict: ThreeWayDiffConflict.NONE, + }); + }); + + it('return merge outcome TARGET', () => { + const mockVersions: ThreeVersionsOf = { + base_version: MissingVersion, + current_version: 1, + target_version: 2, + }; + + const result = forceTargetVersionDiffAlgorithm(mockVersions); + + expect(result).toMatchObject({ + has_base_version: false, + merge_outcome: ThreeWayMergeOutcome.Target, + }); + }); + }); +}); diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/prebuilt_rules/logic/diff/calculation/algorithms/force_target_version_diff_algorithm.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/prebuilt_rules/logic/diff/calculation/algorithms/force_target_version_diff_algorithm.ts new file mode 100644 index 0000000000000..3ae0b62bdb7b1 --- /dev/null +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/prebuilt_rules/logic/diff/calculation/algorithms/force_target_version_diff_algorithm.ts @@ -0,0 +1,44 @@ +/* + * 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 { + ThreeVersionsOf, + ThreeWayDiff, +} from '../../../../../../../../common/api/detection_engine/prebuilt_rules'; +import { + MissingVersion, + ThreeWayDiffConflict, + ThreeWayDiffOutcome, + ThreeWayMergeOutcome, +} from '../../../../../../../../common/api/detection_engine/prebuilt_rules'; + +/** + * Diff algorithm forcing target version. Useful for special fields like `version`. + */ +export const forceTargetVersionDiffAlgorithm = ( + versions: ThreeVersionsOf +): ThreeWayDiff => { + const { + base_version: baseVersion, + current_version: currentVersion, + target_version: targetVersion, + } = versions; + const hasBaseVersion = baseVersion !== MissingVersion; + + return { + has_base_version: hasBaseVersion, + base_version: hasBaseVersion ? baseVersion : undefined, + current_version: currentVersion, + target_version: targetVersion, + merged_version: targetVersion, + merge_outcome: ThreeWayMergeOutcome.Target, + + diff_outcome: ThreeWayDiffOutcome.StockValueCanUpdate, + has_update: true, + conflict: ThreeWayDiffConflict.NONE, + }; +}; diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/prebuilt_rules/logic/diff/calculation/algorithms/index.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/prebuilt_rules/logic/diff/calculation/algorithms/index.ts index c8b55a49edc00..b0f192cf70e42 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/prebuilt_rules/logic/diff/calculation/algorithms/index.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/prebuilt_rules/logic/diff/calculation/algorithms/index.ts @@ -15,3 +15,4 @@ export { kqlQueryDiffAlgorithm } from './kql_query_diff_algorithm'; export { eqlQueryDiffAlgorithm } from './eql_query_diff_algorithm'; export { esqlQueryDiffAlgorithm } from './esql_query_diff_algorithm'; export { ruleTypeDiffAlgorithm } from './rule_type_diff_algorithm'; +export { forceTargetVersionDiffAlgorithm } from './force_target_version_diff_algorithm'; diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/prebuilt_rules/logic/diff/calculation/calculate_rule_fields_diff.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/prebuilt_rules/logic/diff/calculation/calculate_rule_fields_diff.ts index bde52596667d2..550e81d783160 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/prebuilt_rules/logic/diff/calculation/calculate_rule_fields_diff.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/prebuilt_rules/logic/diff/calculation/calculate_rule_fields_diff.ts @@ -48,6 +48,7 @@ import { eqlQueryDiffAlgorithm, esqlQueryDiffAlgorithm, ruleTypeDiffAlgorithm, + forceTargetVersionDiffAlgorithm, } from './algorithms'; const BASE_TYPE_ERROR = `Base version can't be of different rule type`; @@ -179,7 +180,11 @@ const calculateCommonFieldsDiff = ( const commonFieldsDiffAlgorithms: FieldsDiffAlgorithmsFor = { rule_id: simpleDiffAlgorithm, - version: numberDiffAlgorithm, + /** + * `version` shouldn't have a conflict. It always get target value automatically. + * Diff has informational purpose. + */ + version: forceTargetVersionDiffAlgorithm, name: singleLineStringDiffAlgorithm, tags: scalarArrayDiffAlgorithm, description: multiLineStringDiffAlgorithm,