Skip to content
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] Integration tests for simple diff algorithm #184889

Merged
merged 14 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -893,8 +893,8 @@ And <field_name> field is customized by the user (current version != base versio
And <field_name> field is updated by Elastic in this upgrade (target version != base version)
And customized <field_name> field is the same as the Elastic update in this upgrade (current version == target version)
Then for <field_name> field the diff algorithm should output the current version as the merged one without a conflict
And <field_name> field should not be returned from the `upgrade/_review` API endpoint
And <field_name> field should not be shown in the upgrade preview UI
And <field_name> field should be returned from the `upgrade/_review` API endpoint
And <field_name> field should be shown in the upgrade preview UI

Examples:
| field_name | base_version | current_version | target_version |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export const schema: FormSchema<AboutStepRule> = {
label: i18n.translate(
'xpack.securitySolution.detectionEngine.createRule.stepAboutRule.fieldMitreThreatLabel',
{
defaultMessage: 'MITRE ATT&CK\\u2122',
defaultMessage: 'MITRE ATT&CK\u2122',
Copy link
Contributor Author

@dplumlee dplumlee Jun 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I was doing some testing, I noticed this was returning with the literal string both in the diffing UI and in the rule forms, probably caused by the big i18n update that got merged in a couple weeks ago. This fixes the unicode to display correctly.

Screenshot 2024-06-10 at 11 20 17 AM

}
),
labelAppend: OptionalFieldLabel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* 2.0.
*/

import type { RuleFieldsDiff, ThreeWayDiff } from '../../../../../common/api/detection_engine';
import { ThreeWayDiffOutcome } from '../../../../../common/api/detection_engine';
import type { FieldsGroupDiff } from '../../model/rule_details/rule_field_diff';
import {
ABOUT_UPGRADE_FIELD_ORDER,
Expand Down Expand Up @@ -36,3 +38,21 @@ export const getSectionedFieldDiffs = (fields: FieldsGroupDiff[]) => {
setupFields,
};
};

/**
* Filters out any fields that have a `diff_outcome` of `CustomizedValueNoUpdate`
* or `CustomizedValueSameUpdate` as they are not supported for display in the
* current per-field rule diff flyout
*/
export const filterUnsupportedDiffOutcomes = (
fields: Partial<RuleFieldsDiff>
): Partial<RuleFieldsDiff> =>
Object.fromEntries(
Object.entries(fields).filter(([key, value]) => {
const diff = value as ThreeWayDiff<unknown>;
return (
diff.diff_outcome !== ThreeWayDiffOutcome.CustomizedValueNoUpdate &&
diff.diff_outcome !== ThreeWayDiffOutcome.CustomizedValueSameUpdate
);
})
);
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { PartialRuleDiff, RuleFieldsDiff } from '../../../../../common/api/
import { getFormattedFieldDiffGroups } from './per_field_diff/get_formatted_field_diff';
import { UPGRADE_FIELD_ORDER } from './constants';
import { RuleDiffHeaderBar, RuleDiffSection } from './diff_components';
import { getSectionedFieldDiffs } from './helpers';
import { filterUnsupportedDiffOutcomes, getSectionedFieldDiffs } from './helpers';
import type { FieldsGroupDiff } from '../../model/rule_details/rule_field_diff';
import * as i18n from './translations';

Expand All @@ -21,9 +21,11 @@ interface PerFieldRuleDiffTabProps {
export const PerFieldRuleDiffTab = ({ ruleDiff }: PerFieldRuleDiffTabProps) => {
const fieldsToRender = useMemo(() => {
const fields: FieldsGroupDiff[] = [];
for (const field of Object.keys(ruleDiff.fields)) {
// Filter out diff outcomes that we don't support displaying in the per-field diff flyout
const filteredFieldDiffs = filterUnsupportedDiffOutcomes(ruleDiff.fields);
for (const field of Object.keys(filteredFieldDiffs)) {
const typedField = field as keyof RuleFieldsDiff;
const formattedDiffs = getFormattedFieldDiffGroups(typedField, ruleDiff.fields);
const formattedDiffs = getFormattedFieldDiffGroups(typedField, filteredFieldDiffs);
fields.push({ formattedDiffs, fieldsGroupName: typedField });
}
const sortedFields = fields.sort(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export const RULE_NAME_OVERRIDE_FIELD_LABEL = i18n.translate(
export const THREAT_FIELD_LABEL = i18n.translate(
'xpack.securitySolution.detectionEngine.ruleDetails.threatFieldLabel',
{
defaultMessage: 'MITRE ATT&CK\\u2122',
defaultMessage: 'MITRE ATT&CK\u2122',
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

import { transformError } from '@kbn/securitysolution-es-utils';
import { pickBy } from 'lodash';
import { REVIEW_RULE_UPGRADE_URL } from '../../../../../../common/api/detection_engine/prebuilt_rules';
import {
REVIEW_RULE_UPGRADE_URL,
ThreeWayDiffOutcome,
} from '../../../../../../common/api/detection_engine/prebuilt_rules';
import type {
ReviewRuleUpgradeResponseBody,
RuleUpgradeInfoForReview,
Expand Down Expand Up @@ -120,7 +123,7 @@ const calculateRuleInfos = (results: CalculateRuleDiffResult[]): RuleUpgradeInfo
diff: {
fields: pickBy<ThreeWayDiff<unknown>>(
ruleDiff.fields,
(fieldDiff) => fieldDiff.has_update || fieldDiff.has_conflict
(fieldDiff) => fieldDiff.diff_outcome !== ThreeWayDiffOutcome.StockValueNoUpdate
),
has_conflict: ruleDiff.has_conflict,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* 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 { numberDiffAlgorithm } from './number_diff_algorithm';
export { singleLineStringDiffAlgorithm } from './single_line_string_diff_algorithm';
export { simpleDiffAlgorithm } from './simple_diff_algorithm';
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* 2.0.
*/

import type { ThreeVersionsOf } from '../../../../../../../../common/api/detection_engine';
import { simpleDiffAlgorithm } from './simple_diff_algorithm';

export const numberDiffAlgorithm = simpleDiffAlgorithm<number>;
export const numberDiffAlgorithm = <TValue extends number | undefined>(
versions: ThreeVersionsOf<TValue>
) => simpleDiffAlgorithm<TValue>(versions);
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* 2.0.
*/

import type { ThreeVersionsOf } from '../../../../../../../../common/api/detection_engine';
import { simpleDiffAlgorithm } from './simple_diff_algorithm';

export const singleLineStringDiffAlgorithm = simpleDiffAlgorithm<string>;
export const singleLineStringDiffAlgorithm = <TValue extends string | undefined>(
versions: ThreeVersionsOf<TValue>
) => simpleDiffAlgorithm<TValue>(versions);
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ import type { FieldsDiffAlgorithmsFor } from '../../../../../../../common/api/de
import type { ThreeVersionsOf } from '../../../../../../../common/api/detection_engine/prebuilt_rules/model/diff/three_way_diff/three_way_diff';
import { MissingVersion } from '../../../../../../../common/api/detection_engine/prebuilt_rules/model/diff/three_way_diff/three_way_diff';
import { calculateFieldsDiffFor } from './diff_calculation_helpers';
import { simpleDiffAlgorithm } from './algorithms/simple_diff_algorithm';
import {
numberDiffAlgorithm,
simpleDiffAlgorithm,
singleLineStringDiffAlgorithm,
} from './algorithms';

const BASE_TYPE_ERROR = `Base version can't be of different rule type`;
const TARGET_TYPE_ERROR = `Target version can't be of different rule type`;
Expand Down Expand Up @@ -168,14 +172,14 @@ const calculateCommonFieldsDiff = (

const commonFieldsDiffAlgorithms: FieldsDiffAlgorithmsFor<DiffableCommonFields> = {
rule_id: simpleDiffAlgorithm,
version: simpleDiffAlgorithm,
version: numberDiffAlgorithm,
meta: simpleDiffAlgorithm,
name: simpleDiffAlgorithm,
name: singleLineStringDiffAlgorithm,
tags: simpleDiffAlgorithm,
description: simpleDiffAlgorithm,
severity: simpleDiffAlgorithm,
severity: singleLineStringDiffAlgorithm,
severity_mapping: simpleDiffAlgorithm,
risk_score: simpleDiffAlgorithm,
risk_score: numberDiffAlgorithm,
risk_score_mapping: simpleDiffAlgorithm,
references: simpleDiffAlgorithm,
false_positives: simpleDiffAlgorithm,
Expand All @@ -185,12 +189,12 @@ const commonFieldsDiffAlgorithms: FieldsDiffAlgorithmsFor<DiffableCommonFields>
related_integrations: simpleDiffAlgorithm,
required_fields: simpleDiffAlgorithm,
author: simpleDiffAlgorithm,
license: simpleDiffAlgorithm,
license: singleLineStringDiffAlgorithm,
rule_schedule: simpleDiffAlgorithm,
actions: simpleDiffAlgorithm,
throttle: simpleDiffAlgorithm,
exceptions_list: simpleDiffAlgorithm,
max_signals: simpleDiffAlgorithm,
max_signals: numberDiffAlgorithm,
rule_name_override: simpleDiffAlgorithm,
timestamp_override: simpleDiffAlgorithm,
timeline_template: simpleDiffAlgorithm,
Expand Down Expand Up @@ -233,9 +237,9 @@ const eqlFieldsDiffAlgorithms: FieldsDiffAlgorithmsFor<DiffableEqlFields> = {
type: simpleDiffAlgorithm,
eql_query: simpleDiffAlgorithm,
data_source: simpleDiffAlgorithm,
event_category_override: simpleDiffAlgorithm,
timestamp_field: simpleDiffAlgorithm,
tiebreaker_field: simpleDiffAlgorithm,
event_category_override: singleLineStringDiffAlgorithm,
timestamp_field: singleLineStringDiffAlgorithm,
tiebreaker_field: singleLineStringDiffAlgorithm,
};

const calculateEsqlFieldsDiff = (
Expand All @@ -262,7 +266,7 @@ const threatMatchFieldsDiffAlgorithms: FieldsDiffAlgorithmsFor<DiffableThreatMat
threat_query: simpleDiffAlgorithm,
threat_index: simpleDiffAlgorithm,
threat_mapping: simpleDiffAlgorithm,
threat_indicator_path: simpleDiffAlgorithm,
threat_indicator_path: singleLineStringDiffAlgorithm,
concurrent_searches: simpleDiffAlgorithm,
items_per_search: simpleDiffAlgorithm,
};
Expand Down Expand Up @@ -290,7 +294,7 @@ const machineLearningFieldsDiffAlgorithms: FieldsDiffAlgorithmsFor<DiffableMachi
{
type: simpleDiffAlgorithm,
machine_learning_job_id: simpleDiffAlgorithm,
anomaly_threshold: simpleDiffAlgorithm,
anomaly_threshold: numberDiffAlgorithm,
};

const calculateNewTermsFieldsDiff = (
Expand All @@ -304,7 +308,7 @@ const newTermsFieldsDiffAlgorithms: FieldsDiffAlgorithmsFor<DiffableNewTermsFiel
kql_query: simpleDiffAlgorithm,
data_source: simpleDiffAlgorithm,
new_terms_fields: simpleDiffAlgorithm,
history_window_start: simpleDiffAlgorithm,
history_window_start: singleLineStringDiffAlgorithm,
};

const calculateAllFieldsDiff = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ export default ({ loadTestFile }: FtrProviderContext): void => {
loadTestFile(require.resolve('./upgrade_prebuilt_rules'));
loadTestFile(require.resolve('./upgrade_prebuilt_rules_with_historical_versions'));
loadTestFile(require.resolve('./fleet_integration'));
loadTestFile(require.resolve('./upgrade_review_prebuilt_rules'));
});
};
Loading