-
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] Adds diff algorithm and unit tests for multi-line…
… string fields (#188022) ## Summary Related ticket: #180159 Adds diff algorithm for multi-line string fields and unit tests to cover all use cases. Also adds the [`node-diff3`](https://www.npmjs.com/package/node-diff3#3-way-diff-and-merging) package to utilize in the diffing logic to both determine if conflicts exist and merge the 3 rule versions together to create the returned merged version. ### Checklist Delete any items that are not applicable to this PR. - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios ### 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
5 changed files
with
323 additions
and
3 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
174 changes: 174 additions & 0 deletions
174
...prebuilt_rules/logic/diff/calculation/algorithms/multi_line_string_diff_algorithm.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,174 @@ | ||
/* | ||
* 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 { | ||
ThreeWayDiffOutcome, | ||
ThreeWayMergeOutcome, | ||
MissingVersion, | ||
} from '../../../../../../../../common/api/detection_engine'; | ||
import { multiLineStringDiffAlgorithm } from './multi_line_string_diff_algorithm'; | ||
|
||
describe('multiLineStringDiffAlgorithm', () => { | ||
it('returns current_version as merged output if there is no update - scenario AAA', () => { | ||
const mockVersions: ThreeVersionsOf<string> = { | ||
base_version: 'My description.\nThis is a second line.', | ||
current_version: 'My description.\nThis is a second line.', | ||
target_version: 'My description.\nThis is a second line.', | ||
}; | ||
|
||
const result = multiLineStringDiffAlgorithm(mockVersions); | ||
|
||
expect(result).toEqual( | ||
expect.objectContaining({ | ||
merged_version: mockVersions.current_version, | ||
diff_outcome: ThreeWayDiffOutcome.StockValueNoUpdate, | ||
merge_outcome: ThreeWayMergeOutcome.Current, | ||
has_conflict: false, | ||
}) | ||
); | ||
}); | ||
|
||
it('returns current_version as merged output if current_version is different and there is no update - scenario ABA', () => { | ||
const mockVersions: ThreeVersionsOf<string> = { | ||
base_version: 'My description.\nThis is a second line.', | ||
current_version: 'My GREAT description.\nThis is a second line.', | ||
target_version: 'My description.\nThis is a second line.', | ||
}; | ||
|
||
const result = multiLineStringDiffAlgorithm(mockVersions); | ||
|
||
expect(result).toEqual( | ||
expect.objectContaining({ | ||
merged_version: mockVersions.current_version, | ||
diff_outcome: ThreeWayDiffOutcome.CustomizedValueNoUpdate, | ||
merge_outcome: ThreeWayMergeOutcome.Current, | ||
has_conflict: false, | ||
}) | ||
); | ||
}); | ||
|
||
it('returns target_version as merged output if current_version is the same and there is an update - scenario AAB', () => { | ||
const mockVersions: ThreeVersionsOf<string> = { | ||
base_version: 'My description.\nThis is a second line.', | ||
current_version: 'My description.\nThis is a second line.', | ||
target_version: 'My GREAT description.\nThis is a second line.', | ||
}; | ||
|
||
const result = multiLineStringDiffAlgorithm(mockVersions); | ||
|
||
expect(result).toEqual( | ||
expect.objectContaining({ | ||
merged_version: mockVersions.target_version, | ||
diff_outcome: ThreeWayDiffOutcome.StockValueCanUpdate, | ||
merge_outcome: ThreeWayMergeOutcome.Target, | ||
has_conflict: false, | ||
}) | ||
); | ||
}); | ||
|
||
it('returns current_version as merged output if current version is different but it matches the update - scenario ABB', () => { | ||
const mockVersions: ThreeVersionsOf<string> = { | ||
base_version: 'My description.\nThis is a second line.', | ||
current_version: 'My GREAT description.\nThis is a second line.', | ||
target_version: 'My GREAT description.\nThis is a second line.', | ||
}; | ||
|
||
const result = multiLineStringDiffAlgorithm(mockVersions); | ||
|
||
expect(result).toEqual( | ||
expect.objectContaining({ | ||
merged_version: mockVersions.current_version, | ||
diff_outcome: ThreeWayDiffOutcome.CustomizedValueSameUpdate, | ||
merge_outcome: ThreeWayMergeOutcome.Current, | ||
has_conflict: false, | ||
}) | ||
); | ||
}); | ||
|
||
describe('if all three versions are different - scenario ABC', () => { | ||
it('returns a computated merged version without a conflict if 3 way merge is possible', () => { | ||
const mockVersions: ThreeVersionsOf<string> = { | ||
base_version: `My description.\f\nThis is a second\u2001 line.\f\nThis is a third line.`, | ||
current_version: `My GREAT description.\f\nThis is a second\u2001 line.\f\nThis is a third line.`, | ||
target_version: `My description.\f\nThis is a second\u2001 line.\f\nThis is a GREAT line.`, | ||
}; | ||
|
||
const expectedMergedVersion = `My GREAT description.\f\nThis is a second\u2001 line.\f\nThis is a GREAT line.`; | ||
|
||
const result = multiLineStringDiffAlgorithm(mockVersions); | ||
|
||
expect(result).toEqual( | ||
expect.objectContaining({ | ||
merged_version: expectedMergedVersion, | ||
diff_outcome: ThreeWayDiffOutcome.CustomizedValueCanUpdate, | ||
merge_outcome: ThreeWayMergeOutcome.Merged, | ||
has_conflict: false, | ||
}) | ||
); | ||
}); | ||
|
||
it('returns the current_version with a conflict if 3 way merge is not possible', () => { | ||
const mockVersions: ThreeVersionsOf<string> = { | ||
base_version: 'My description.\nThis is a second line.', | ||
current_version: 'My GREAT description.\nThis is a third line.', | ||
target_version: 'My EXCELLENT description.\nThis is a fourth.', | ||
}; | ||
|
||
const result = multiLineStringDiffAlgorithm(mockVersions); | ||
|
||
expect(result).toEqual( | ||
expect.objectContaining({ | ||
merged_version: mockVersions.current_version, | ||
diff_outcome: ThreeWayDiffOutcome.CustomizedValueCanUpdate, | ||
merge_outcome: ThreeWayMergeOutcome.Conflict, | ||
has_conflict: true, | ||
}) | ||
); | ||
}); | ||
}); | ||
|
||
describe('if base_version is missing', () => { | ||
it('returns current_version as merged output if current_version and target_version are the same - scenario -AA', () => { | ||
const mockVersions: ThreeVersionsOf<string> = { | ||
base_version: MissingVersion, | ||
current_version: 'My description.\nThis is a second line.', | ||
target_version: 'My description.\nThis is a second line.', | ||
}; | ||
|
||
const result = multiLineStringDiffAlgorithm(mockVersions); | ||
|
||
expect(result).toEqual( | ||
expect.objectContaining({ | ||
merged_version: mockVersions.current_version, | ||
diff_outcome: ThreeWayDiffOutcome.StockValueNoUpdate, | ||
merge_outcome: ThreeWayMergeOutcome.Current, | ||
has_conflict: false, | ||
}) | ||
); | ||
}); | ||
|
||
it('returns target_version as merged output if current_version and target_version are different - scenario -AB', () => { | ||
const mockVersions: ThreeVersionsOf<string> = { | ||
base_version: MissingVersion, | ||
current_version: `My GREAT description.\nThis is a second line.`, | ||
target_version: `My description.\nThis is a second line, now longer.`, | ||
}; | ||
|
||
const result = multiLineStringDiffAlgorithm(mockVersions); | ||
|
||
expect(result).toEqual( | ||
expect.objectContaining({ | ||
merged_version: mockVersions.target_version, | ||
diff_outcome: ThreeWayDiffOutcome.StockValueCanUpdate, | ||
merge_outcome: ThreeWayMergeOutcome.Target, | ||
has_conflict: false, | ||
}) | ||
); | ||
}); | ||
}); | ||
}); |
114 changes: 114 additions & 0 deletions
114
...gine/prebuilt_rules/logic/diff/calculation/algorithms/multi_line_string_diff_algorithm.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,114 @@ | ||
/* | ||
* 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 { merge } from 'node-diff3'; | ||
import { assertUnreachable } from '../../../../../../../../common/utility_types'; | ||
import type { | ||
ThreeVersionsOf, | ||
ThreeWayDiff, | ||
} from '../../../../../../../../common/api/detection_engine/prebuilt_rules'; | ||
import { | ||
determineDiffOutcome, | ||
determineIfValueCanUpdate, | ||
ThreeWayDiffOutcome, | ||
ThreeWayMergeOutcome, | ||
MissingVersion, | ||
} from '../../../../../../../../common/api/detection_engine/prebuilt_rules'; | ||
|
||
/** | ||
* Diff algorithm used for string fields that contain multiple lines | ||
*/ | ||
export const multiLineStringDiffAlgorithm = ( | ||
versions: ThreeVersionsOf<string> | ||
): ThreeWayDiff<string> => { | ||
const { | ||
base_version: baseVersion, | ||
current_version: currentVersion, | ||
target_version: targetVersion, | ||
} = versions; | ||
|
||
const diffOutcome = determineDiffOutcome(baseVersion, currentVersion, targetVersion); | ||
const valueCanUpdate = determineIfValueCanUpdate(diffOutcome); | ||
|
||
const { mergeOutcome, mergedVersion } = mergeVersions({ | ||
baseVersion, | ||
currentVersion, | ||
targetVersion, | ||
diffOutcome, | ||
}); | ||
|
||
return { | ||
base_version: baseVersion, | ||
current_version: currentVersion, | ||
target_version: targetVersion, | ||
merged_version: mergedVersion, | ||
|
||
diff_outcome: diffOutcome, | ||
merge_outcome: mergeOutcome, | ||
has_update: valueCanUpdate, | ||
has_conflict: mergeOutcome === ThreeWayMergeOutcome.Conflict, | ||
}; | ||
}; | ||
|
||
interface MergeResult { | ||
mergeOutcome: ThreeWayMergeOutcome; | ||
mergedVersion: string; | ||
} | ||
|
||
interface MergeArgs { | ||
baseVersion: string | MissingVersion; | ||
currentVersion: string; | ||
targetVersion: string; | ||
diffOutcome: ThreeWayDiffOutcome; | ||
} | ||
|
||
const mergeVersions = ({ | ||
baseVersion, | ||
currentVersion, | ||
targetVersion, | ||
diffOutcome, | ||
}: MergeArgs): MergeResult => { | ||
switch (diffOutcome) { | ||
case ThreeWayDiffOutcome.StockValueNoUpdate: | ||
case ThreeWayDiffOutcome.CustomizedValueNoUpdate: | ||
case ThreeWayDiffOutcome.CustomizedValueSameUpdate: { | ||
return { | ||
mergeOutcome: ThreeWayMergeOutcome.Current, | ||
mergedVersion: currentVersion, | ||
}; | ||
} | ||
case ThreeWayDiffOutcome.StockValueCanUpdate: { | ||
return { | ||
mergeOutcome: ThreeWayMergeOutcome.Target, | ||
mergedVersion: targetVersion, | ||
}; | ||
} | ||
case ThreeWayDiffOutcome.CustomizedValueCanUpdate: { | ||
if (baseVersion === MissingVersion) { | ||
return { | ||
mergeOutcome: ThreeWayMergeOutcome.Conflict, | ||
mergedVersion: currentVersion, | ||
}; | ||
} | ||
const mergedVersion = merge(currentVersion, baseVersion, targetVersion, { | ||
stringSeparator: /(\S+|\s+)/g, // Retains all whitespace, which we keep to preserve formatting | ||
}); | ||
|
||
return mergedVersion.conflict | ||
? { | ||
mergeOutcome: ThreeWayMergeOutcome.Conflict, | ||
mergedVersion: currentVersion, | ||
} | ||
: { | ||
mergeOutcome: ThreeWayMergeOutcome.Merged, | ||
mergedVersion: mergedVersion.result.join(''), | ||
}; | ||
} | ||
default: | ||
return assertUnreachable(diffOutcome); | ||
} | ||
}; |
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