Skip to content

Commit

Permalink
[Security Solution] Invalidate prebuilt rules status after package up…
Browse files Browse the repository at this point in the history
…grade or installation (elastic#150292)

**Resolves: elastic#150306

## Summary

Fixes the Load Prebuilt rules button not visible when users visit the
rules management page for the first time (no prebuilt detection rules
package installed).

## Steps to test

1. Ensure that the detection engine package is not installed: <img
src="https://user-images.githubusercontent.com/1938181/216949857-a49731f3-535e-4813-95d3-62b2995500bb.png"
width="600" />
2. Navigate to the rules management page.

### Previously

The "Load Elastic Prebuilt Rules" button is not visible, and users
cannot install prebuilt rules.

<img
src="https://user-images.githubusercontent.com/1938181/216950430-4c57a3ad-3146-40a0-82c2-08c4fc2f65c3.png"
width="600" />

### With the fix

Users now see loading animation, indicating that the package
installation happens in the background. Once the package installation
finishes, users see the Load Prebuilt rules button appear.


https://user-images.githubusercontent.com/1938181/217585144-879fe288-0ede-4e01-b585-6aced1d89379.mov
  • Loading branch information
xcrzx authored Feb 13, 2023
1 parent 2749e95 commit 4f1f2a8
Show file tree
Hide file tree
Showing 10 changed files with 304 additions and 186 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/security_solution/public/app/home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import {
getScopeFromPath,
useSourcererDataView,
} from '../../common/containers/sourcerer';
import { useUpgradeSecurityPackages } from '../../common/hooks/use_upgrade_security_packages';
import { GlobalHeader } from './global_header';
import { ConsoleManager } from '../../management/components/console/components/console_manager';

import { TourContextProvider } from '../../common/components/guided_onboarding_tour';

import { useUrlState } from '../../common/hooks/use_url_state';
import { useUpdateBrowserTitle } from '../../common/hooks/use_update_browser_title';
import { useUpgradeSecurityPackages } from '../../detection_engine/rule_management/logic/use_upgrade_security_packages';

interface HomePageProps {
children: React.ReactNode;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import type {
ExceptionListItemSchema,
} from '@kbn/securitysolution-io-ts-list-types';

import type { BulkInstallPackagesResponse } from '@kbn/fleet-plugin/common';
import { epmRouteService } from '@kbn/fleet-plugin/common';
import type { InstallPackageResponse } from '@kbn/fleet-plugin/common/types';
import type { RuleManagementFiltersResponse } from '../../../../common/detection_engine/rule_management/api/rules/filters/response_schema';
import { RULE_MANAGEMENT_FILTERS_URL } from '../../../../common/detection_engine/rule_management/api/urls';
import type { BulkActionsDryRunErrCode } from '../../../../common/constants';
Expand Down Expand Up @@ -481,3 +484,61 @@ export const addRuleExceptions = async ({
signal,
}
);

export interface InstallFleetPackageProps {
packageName: string;
packageVersion: string;
prerelease?: boolean;
force?: boolean;
}

/**
* Install a Fleet package from the registry
*
* @param packageName Name of the package to install
* @param packageVersion Version of the package to install
* @param prerelease Whether to install a prerelease version of the package
* @param force Whether to force install the package. If false, the package will only be installed if it is not already installed
*
* @returns The response from the Fleet API
*/
export const installFleetPackage = ({
packageName,
packageVersion,
prerelease = false,
force = true,
}: InstallFleetPackageProps): Promise<InstallPackageResponse> => {
return KibanaServices.get().http.post<InstallPackageResponse>(
epmRouteService.getInstallPath(packageName, packageVersion),
{
query: { prerelease },
body: JSON.stringify({ force }),
}
);
};

export interface BulkInstallFleetPackagesProps {
packages: string[];
prerelease?: boolean;
}

/**
* Install multiple Fleet packages from the registry
*
* @param packages Array of package names to install
* @param prerelease Whether to install prerelease versions of the packages
*
* @returns The response from the Fleet API
*/
export const bulkInstallFleetPackages = ({
packages,
prerelease = false,
}: BulkInstallFleetPackagesProps): Promise<BulkInstallPackagesResponse> => {
return KibanaServices.get().http.post<BulkInstallPackagesResponse>(
epmRouteService.getBulkInstallPath(),
{
query: { prerelease },
body: JSON.stringify({ packages }),
}
);
};
Original file line number Diff line number Diff line change
@@ -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 { EPM_API_ROUTES } from '@kbn/fleet-plugin/common';
import type { BulkInstallPackagesResponse } from '@kbn/fleet-plugin/common/types';
import type { UseMutationOptions } from '@tanstack/react-query';
import { useMutation } from '@tanstack/react-query';
import { PREBUILT_RULES_PACKAGE_NAME } from '../../../../../common/detection_engine/constants';
import type { BulkInstallFleetPackagesProps } from '../api';
import { bulkInstallFleetPackages } from '../api';
import { useInvalidateFetchPrebuiltRulesStatusQuery } from './use_fetch_prebuilt_rules_status_query';

export const BULK_INSTALL_FLEET_PACKAGES_MUTATION_KEY = [
'POST',
EPM_API_ROUTES.BULK_INSTALL_PATTERN,
];

export const useBulkInstallFleetPackagesMutation = (
options?: UseMutationOptions<BulkInstallPackagesResponse, Error, BulkInstallFleetPackagesProps>
) => {
const invalidatePrePackagedRulesStatus = useInvalidateFetchPrebuiltRulesStatusQuery();

return useMutation((props: BulkInstallFleetPackagesProps) => bulkInstallFleetPackages(props), {
...options,
mutationKey: BULK_INSTALL_FLEET_PACKAGES_MUTATION_KEY,
onSettled: (...args) => {
const response = args[0];
const rulesPackage = response?.items.find(
(item) => item.name === PREBUILT_RULES_PACKAGE_NAME
);
if (rulesPackage && 'result' in rulesPackage && rulesPackage.result.status === 'installed') {
// The rules package was installed/updated, so invalidate the pre-packaged rules status query
invalidatePrePackagedRulesStatus();
}

if (options?.onSettled) {
options.onSettled(...args);
}
},
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 { EPM_API_ROUTES } from '@kbn/fleet-plugin/common';
import type { InstallPackageResponse } from '@kbn/fleet-plugin/common/types';
import type { UseMutationOptions } from '@tanstack/react-query';
import { useMutation } from '@tanstack/react-query';
import { PREBUILT_RULES_PACKAGE_NAME } from '../../../../../common/detection_engine/constants';
import type { InstallFleetPackageProps } from '../api';
import { installFleetPackage } from '../api';
import { useInvalidateFetchPrebuiltRulesStatusQuery } from './use_fetch_prebuilt_rules_status_query';

export const INSTALL_FLEET_PACKAGE_MUTATION_KEY = [
'POST',
EPM_API_ROUTES.INSTALL_FROM_REGISTRY_PATTERN,
];

export const useInstallFleetPackageMutation = (
options?: UseMutationOptions<InstallPackageResponse, Error, InstallFleetPackageProps>
) => {
const invalidatePrePackagedRulesStatus = useInvalidateFetchPrebuiltRulesStatusQuery();

return useMutation((props: InstallFleetPackageProps) => installFleetPackage(props), {
...options,
mutationKey: INSTALL_FLEET_PACKAGE_MUTATION_KEY,
onSettled: (...args) => {
const { packageName } = args[2];
if (packageName === PREBUILT_RULES_PACKAGE_NAME) {
// Invalidate the pre-packaged rules status query as there might be new rules to install
invalidatePrePackagedRulesStatus();
}

if (options?.onSettled) {
options.onSettled(...args);
}
},
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { useIsMutating } from '@tanstack/react-query';
import { useAppToasts } from '../../../common/hooks/use_app_toasts';
import { useCreatePrebuiltRulesMutation } from '../api/hooks/use_create_prebuilt_rules_mutation';
import {
CREATE_PREBUILT_RULES_MUTATION_KEY,
useCreatePrebuiltRulesMutation,
} from '../api/hooks/use_create_prebuilt_rules_mutation';
import * as i18n from './translations';

export const useInstallPrePackagedRules = () => {
Expand All @@ -21,6 +25,11 @@ export const useInstallPrePackagedRules = () => {
});
};

export const useIsInstallingPrePackagedRules = () => {
const mutationsCount = useIsMutating(CREATE_PREBUILT_RULES_MUTATION_KEY);
return mutationsCount > 0;
};

const getSuccessToastMessage = (result: {
rules_installed: number;
rules_updated: number;
Expand Down
Loading

0 comments on commit 4f1f2a8

Please sign in to comment.