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] Reintroduce ML Jobs warning popover on rule upgrade #159868

Merged
merged 5 commits into from
Jun 20, 2023
Merged
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 @@ -5,9 +5,11 @@
* 2.0.
*/

// import { isEqual } from 'lodash';
import type { Dispatch, SetStateAction } from 'react';
import React, { createContext, useCallback, useContext, useMemo, useState } from 'react';
import { useInstalledSecurityJobs } from '../../../../../common/components/ml/hooks/use_installed_security_jobs';
import { useBoolState } from '../../../../../common/hooks/use_bool_state';
import { affectedJobIds } from '../../../../../detections/components/callouts/ml_job_compatibility_callout/affected_job_ids';
import type { RuleUpgradeInfoForReview } from '../../../../../../common/detection_engine/prebuilt_rules/api/review_rule_upgrade/response_schema';
import type { RuleSignatureId } from '../../../../../../common/detection_engine/rule_schema';
import { invariant } from '../../../../../../common/utils/invariant';
Expand All @@ -18,6 +20,9 @@ import {
import { usePrebuiltRulesUpgradeReview } from '../../../../rule_management/logic/prebuilt_rules/use_prebuilt_rules_upgrade_review';
import type { UpgradePrebuiltRulesTableFilterOptions } from './use_filter_prebuilt_rules_to_upgrade';
import { useFilterPrebuiltRulesToUpgrade } from './use_filter_prebuilt_rules_to_upgrade';
import { useAsyncConfirmation } from '../rules_table/use_async_confirmation';

import { MlJobUpgradeModal } from '../../../../../detections/components/modals/ml_job_upgrade_modal';

export interface UpgradePrebuiltRulesTableState {
/**
Expand Down Expand Up @@ -90,7 +95,6 @@ export const UpgradePrebuiltRulesTableContextProvider = ({
}: UpgradePrebuiltRulesTableContextProviderProps) => {
const [loadingRules, setLoadingRules] = useState<RuleSignatureId[]>([]);
const [selectedRules, setSelectedRules] = useState<RuleUpgradeInfoForReview[]>([]);

const [filterOptions, setFilterOptions] = useState<UpgradePrebuiltRulesTableFilterOptions>({
filter: '',
tags: [],
Expand All @@ -114,13 +118,29 @@ export const UpgradePrebuiltRulesTableContextProvider = ({
const { mutateAsync: upgradeAllRulesRequest } = usePerformUpgradeAllRules();
const { mutateAsync: upgradeSpecificRulesRequest } = usePerformUpgradeSpecificRules();

// Wrapper to add confirmation modal for users who may be running older ML Jobs that would
// be overridden by updating their rules. For details, see: /~https://github.com/elastic/kibana/issues/128121
const [isUpgradeModalVisible, showUpgradeModal, hideUpgradeModal] = useBoolState(false);
const { loading: loadingJobs, jobs } = useInstalledSecurityJobs();
const legacyJobsInstalled = jobs.filter((job) => affectedJobIds.includes(job.id));

const [confirmUpgrade, handleUpgradeConfirm, handleUpgradeCancel] = useAsyncConfirmation({
onInit: showUpgradeModal,
onFinish: hideUpgradeModal,
});

const shouldConfirmUpgrade = legacyJobsInstalled.length > 0;

const upgradeOneRule = useCallback(
async (ruleId: RuleSignatureId) => {
const rule = rules.find((r) => r.rule_id === ruleId);
invariant(rule, `Rule with id ${ruleId} not found`);

setLoadingRules((prev) => [...prev, ruleId]);
try {
if (shouldConfirmUpgrade && !(await confirmUpgrade())) {
return;
}
await upgradeSpecificRulesRequest([
{
rule_id: ruleId,
Expand All @@ -132,7 +152,7 @@ export const UpgradePrebuiltRulesTableContextProvider = ({
setLoadingRules((prev) => prev.filter((id) => id !== ruleId));
}
},
[rules, upgradeSpecificRulesRequest]
[confirmUpgrade, rules, shouldConfirmUpgrade, upgradeSpecificRulesRequest]
);

const upgradeSelectedRules = useCallback(async () => {
Expand All @@ -143,23 +163,29 @@ export const UpgradePrebuiltRulesTableContextProvider = ({
}));
setLoadingRules((prev) => [...prev, ...rulesToUpgrade.map((r) => r.rule_id)]);
try {
if (shouldConfirmUpgrade && !(await confirmUpgrade())) {
return;
}
await upgradeSpecificRulesRequest(rulesToUpgrade);
} finally {
setLoadingRules((prev) => prev.filter((id) => !rulesToUpgrade.some((r) => r.rule_id === id)));
setSelectedRules([]);
}
}, [selectedRules, upgradeSpecificRulesRequest]);
}, [confirmUpgrade, selectedRules, shouldConfirmUpgrade, upgradeSpecificRulesRequest]);

const upgradeAllRules = useCallback(async () => {
// Unselect all rules so that the table doesn't show the "bulk actions" bar
setLoadingRules((prev) => [...prev, ...rules.map((r) => r.rule_id)]);
try {
if (shouldConfirmUpgrade && !(await confirmUpgrade())) {
return;
}
await upgradeAllRulesRequest();
} finally {
setLoadingRules([]);
setSelectedRules([]);
}
}, [rules, upgradeAllRulesRequest]);
}, [confirmUpgrade, rules, shouldConfirmUpgrade, upgradeAllRulesRequest]);

const actions = useMemo<UpgradePrebuiltRulesTableActions>(
() => ({
Expand All @@ -170,7 +196,7 @@ export const UpgradePrebuiltRulesTableContextProvider = ({
setFilterOptions,
selectRules: setSelectedRules,
}),
[refetch, upgradeAllRules, upgradeOneRule, upgradeSelectedRules]
[refetch, upgradeOneRule, upgradeSelectedRules, upgradeAllRules]
);

const filteredRules = useFilterPrebuiltRulesToUpgrade({ filterOptions, rules });
Expand All @@ -183,11 +209,13 @@ export const UpgradePrebuiltRulesTableContextProvider = ({
filterOptions,
tags,
isFetched,
isLoading,
isLoading: isLoading && loadingJobs,
isRefetching,
selectedRules,
loadingRules,
lastUpdated: dataUpdatedAt,
legacyJobsInstalled,
isUpgradeModalVisible,
},
actions,
};
Expand All @@ -198,15 +226,25 @@ export const UpgradePrebuiltRulesTableContextProvider = ({
tags,
isFetched,
isLoading,
loadingJobs,
isRefetching,
selectedRules,
loadingRules,
dataUpdatedAt,
legacyJobsInstalled,
isUpgradeModalVisible,
actions,
]);

return (
<UpgradePrebuiltRulesTableContext.Provider value={providerValue}>
{isUpgradeModalVisible && (
<MlJobUpgradeModal
jobs={legacyJobsInstalled}
onCancel={handleUpgradeCancel}
onConfirm={handleUpgradeConfirm}
/>
)}
{children}
</UpgradePrebuiltRulesTableContext.Provider>
);
Expand Down