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] Display cardinality for threshold rules #201162

Merged
merged 15 commits into from
Nov 27, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import { FilterBadgeGroup } from '@kbn/unified-search-plugin/public';
import { IntervalAbbrScreenReader } from '../../../../common/components/accessibility';
import type {
RequiredFieldArray,
Threshold,
AlertSuppressionMissingFieldsStrategy,
} from '../../../../../common/api/detection_engine/model/rule_schema';
import { AlertSuppressionMissingFieldsStrategyEnum } from '../../../../../common/api/detection_engine/model/rule_schema';
Expand All @@ -50,6 +49,7 @@ import { defaultToEmptyTag } from '../../../../common/components/empty_value';
import { RequiredFieldIcon } from '../../../rule_management/components/rule_details/required_field_icon';
import { ThreatEuiFlexGroup } from './threat_description';
import { AlertSuppressionLabel } from './alert_suppression_label';
import type { FieldValueThreshold } from '../threshold_input';

const NoteDescriptionContainer = styled(EuiFlexItem)`
height: 105px;
Expand Down Expand Up @@ -490,20 +490,29 @@ export const buildRuleTypeDescription = (label: string, ruleType: Type): ListIte
}
};

export const buildThresholdDescription = (label: string, threshold: Threshold): ListItems[] => [
{
title: label,
description: (
<>
{isEmpty(threshold.field[0])
? `${i18n.THRESHOLD_RESULTS_ALL} >= ${threshold.value}`
: `${i18n.THRESHOLD_RESULTS_AGGREGATED_BY} ${
Array.isArray(threshold.field) ? threshold.field.join(',') : threshold.field
} >= ${threshold.value}`}
</>
),
},
];
export const buildThresholdDescription = (
label: string,
threshold: FieldValueThreshold
): ListItems[] => {
let thresholdDescription = isEmpty(threshold.field[0])
? `${i18n.THRESHOLD_RESULTS_ALL} >= ${threshold.value}`
: `${i18n.THRESHOLD_RESULTS_AGGREGATED_BY} ${threshold.field.join(',')} >= ${threshold.value}`;

if (threshold.cardinality?.value && threshold.cardinality?.field.length > 0) {
thresholdDescription = i18n.THRESHOLD_CARDINALITY(
thresholdDescription,
threshold.cardinality.field[0],
threshold.cardinality.value
);
}
jkelas marked this conversation as resolved.
Show resolved Hide resolved

return [
{
title: label,
description: <>{thresholdDescription}</>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
description: <>{thresholdDescription}</>,
description: thresholdDescription,

No need for fragment in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually it complains when I remove the fragment. That's why I left it there in the first place. I was a bit confused to see this. I need to investigate later why it is like that.

Copy link
Contributor

Choose a reason for hiding this comment

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

Weird. It doesn't complain for me if I remove the fragment. Let's take a look together.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, let's sync

},
];
};

export const buildThreatMappingDescription = (
title: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,62 @@ describe('description_step', () => {

expect(result[0].title).toEqual('Threshold label');
expect(React.isValidElement(result[0].description)).toBeTruthy();
expect(mount(result[0].description as React.ReactElement).html()).toContain(
expect(mount(result[0].description as React.ReactElement).html()).toEqual(
'Results aggregated by user.name >= 100'
);
});

test('returns threshold description when threshold exist, field is set, and cardinality is not set', () => {
const mockThreshold = {
threshold: {
field: ['user.name'],
value: 100,
cardinality: {
field: [],
value: 0,
},
},
};
const result: ListItems[] = getDescriptionItem(
'threshold',
'Threshold label',
mockThreshold,
mockFilterManager,
mockLicenseService
);

expect(result[0].title).toEqual('Threshold label');
expect(React.isValidElement(result[0].description)).toBeTruthy();
expect(mount(result[0].description as React.ReactElement).html()).toEqual(
'Results aggregated by user.name >= 100'
);
});

test('returns threshold description when threshold exist, field is set and cardinality is set', () => {
const mockThreshold = {
threshold: {
field: ['user.name'],
value: 100,
cardinality: {
field: ['host.test_value'],
value: 10,
},
},
};
const result: ListItems[] = getDescriptionItem(
'threshold',
'Threshold label',
mockThreshold,
mockFilterManager,
mockLicenseService
);

expect(result[0].title).toEqual('Threshold label');
expect(React.isValidElement(result[0].description)).toBeTruthy();
expect(mount(result[0].description as React.ReactElement).html()).toContain(
'Results aggregated by user.name >= 100 when unique value count of host.test_value >= 10'
);
});
});

describe('references', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,24 @@ export const THRESHOLD_RESULTS_AGGREGATED_BY = i18n.translate(
}
);

export const THRESHOLD_CARDINALITY = (
thresholdFieldsGroupedBy: string,
cardinalityField: string,
cardinalityValue: string | number
) =>
i18n.translate(
'xpack.securitySolution.detectionEngine.ruleDescription.thresholdResultsCardinalityDescription',
{
defaultMessage:
'{thresholdFieldsGroupedBy} when unique values count of {cardinalityField} >= {cardinalityValue}',
values: {
thresholdFieldsGroupedBy,
cardinalityField,
cardinalityValue,
},
}
);

export const EQL_EVENT_CATEGORY_FIELD_LABEL = i18n.translate(
'xpack.securitySolution.detectionEngine.ruleDescription.eqlEventCategoryFieldLabel',
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,23 @@ interface ThresholdProps {
threshold: ThresholdType;
}

export const Threshold = ({ threshold }: ThresholdProps) => (
<div data-test-subj="thresholdPropertyValue">
{isEmpty(threshold.field[0])
? `${descriptionStepI18n.THRESHOLD_RESULTS_ALL} >= ${threshold.value}`
: `${descriptionStepI18n.THRESHOLD_RESULTS_AGGREGATED_BY} ${
Array.isArray(threshold.field) ? threshold.field.join(',') : threshold.field
} >= ${threshold.value}`}
</div>
);
export const Threshold = ({ threshold }: ThresholdProps) => {
let thresholdDescription = isEmpty(threshold.field[0])
? `${descriptionStepI18n.THRESHOLD_RESULTS_ALL} >= ${threshold.value}`
: `${descriptionStepI18n.THRESHOLD_RESULTS_AGGREGATED_BY} ${
Array.isArray(threshold.field) ? threshold.field.join(',') : threshold.field
} >= ${threshold.value}`;

if (threshold.cardinality && threshold.cardinality.length > 0) {
jkelas marked this conversation as resolved.
Show resolved Hide resolved
thresholdDescription = descriptionStepI18n.THRESHOLD_CARDINALITY(
thresholdDescription,
threshold.cardinality[0].field,
threshold.cardinality[0].value
);
}

return <div data-test-subj="thresholdPropertyValue">{thresholdDescription}</div>;
};

interface AnomalyThresholdProps {
anomalyThreshold: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ describe(
const { threshold } = THRESHOLD_RULE_INDEX_PATTERN['security-rule'] as {
threshold: Threshold;
};
assertThresholdPropertyShown(threshold.value);
assertThresholdPropertyShown(threshold);

const { index } = THRESHOLD_RULE_INDEX_PATTERN['security-rule'] as { index: string[] };
assertIndexPropertyShown(index);
Expand Down Expand Up @@ -952,7 +952,7 @@ describe(
const { threshold } = UPDATED_THRESHOLD_RULE_INDEX_PATTERN['security-rule'] as {
threshold: Threshold;
};
assertThresholdPropertyShown(threshold.value);
assertThresholdPropertyShown(threshold);

const { index } = UPDATED_THRESHOLD_RULE_INDEX_PATTERN['security-rule'] as {
index: string[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import { capitalize } from 'lodash';
import type { ThreatMapping } from '@kbn/securitysolution-io-ts-alerting-types';
import type { Module } from '@kbn/ml-plugin/common/types/modules';
import { AlertSuppression } from '@kbn/security-solution-plugin/common/api/detection_engine/model/rule_schema';
import {
AlertSuppression,
Threshold,
} from '@kbn/security-solution-plugin/common/api/detection_engine/model/rule_schema';
import type { Filter } from '@kbn/es-query';
import type { PrebuiltRuleAsset } from '@kbn/security-solution-plugin/server/lib/detection_engine/prebuilt_rules';
import {
Expand Down Expand Up @@ -312,9 +315,15 @@ export const assertMachineLearningPropertiesShown = (
});
};

export const assertThresholdPropertyShown = (thresholdValue: number) => {
export const assertThresholdPropertyShown = (threshold: Threshold) => {
cy.get(THRESHOLD_TITLE).should('have.text', 'Threshold');
cy.get(THRESHOLD_VALUE).should('contain', thresholdValue);
cy.get(THRESHOLD_VALUE).should('contain', threshold.value);
if (threshold.cardinality) {
cy.get(THRESHOLD_VALUE).should(
'contain',
`when unique value count of ${threshold.cardinality[0].field} >= ${threshold.cardinality[0].value}`
);
}
};

export const assertEqlQueryPropertyShown = (query: string) => {
Expand Down