Skip to content

Commit

Permalink
[Security Solution][Endpoint] Ensure fleet setup is done prior to att…
Browse files Browse the repository at this point in the history
…empting to install/upgrade the Endpoint package (#107929) (#108045)

* Ensure install/upgrade of endpoint package first checks to see that fleet is setup
* Delete un-used `<Setup />` component
* Test cases for `useUpgradeSecurityPackages()` hook

Co-authored-by: Paul Tavares <[email protected]>
  • Loading branch information
kibanamachine and paul-tavares authored Aug 10, 2021
1 parent 3dfcdf8 commit ed4c3fe
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 52 deletions.
41 changes: 0 additions & 41 deletions x-pack/plugins/security_solution/public/app/home/setup.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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 React, { memo } from 'react';
import { useKibana } from '../lib/kibana';
import { renderHook as _renderHook, RenderHookResult } from '@testing-library/react-hooks';
import { useUpgradeSecurityPackages } from './use_upgrade_security_packages';

jest.mock('../components/user_privileges', () => {
return {
useUserPrivileges: jest.fn().mockReturnValue({
endpointPrivileges: {
canAccessFleet: true,
},
}),
};
});
jest.mock('../lib/kibana');

describe('When using the `useUpgradeSecurityPackages()` hook', () => {
let renderResult: RenderHookResult<object, void>;
let renderHook: () => RenderHookResult<object, void>;
let kibana: ReturnType<typeof useKibana>;

// eslint-disable-next-line react/display-name
const Wrapper = memo(({ children }) => {
kibana = useKibana();
return <>{children}</>;
});

beforeEach(() => {
renderHook = () => {
renderResult = _renderHook(() => useUpgradeSecurityPackages(), { wrapper: Wrapper });
return renderResult;
};
});

afterEach(() => {
if (renderResult) {
renderResult.unmount();
}
});

it('should call fleet setup first via `isInitialized()` and then send upgrade request', async () => {
renderHook();

expect(kibana.services.fleet?.isInitialized).toHaveBeenCalled();
expect(kibana.services.http.post).not.toHaveBeenCalled();

await renderResult.waitFor(
() => (kibana.services.http.post as jest.Mock).mock.calls.length > 0
);

expect(kibana.services.http.post).toHaveBeenCalledWith(
'/api/fleet/epm/packages/_bulk',
expect.objectContaining({
body: '{"packages":["endpoint","security_detection_engine"]}',
})
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@

import { useEffect } from 'react';
import { HttpFetchOptions, HttpStart } from 'kibana/public';
import { useKibana } from '../../../../../../src/plugins/kibana_react/public';
import { useKibana } from '../lib/kibana';
import { epmRouteService, BulkInstallPackagesResponse } from '../../../../fleet/common';
import { StartServices } from '../../types';
import { useUserPrivileges } from '../components/user_privileges';

/**
Expand All @@ -31,7 +30,7 @@ const sendUpgradeSecurityPackages = async (
};

export const useUpgradeSecurityPackages = () => {
const context = useKibana<StartServices>();
const context = useKibana();
const canAccessFleet = useUserPrivileges().endpointPrivileges.canAccessFleet;

useEffect(() => {
Expand All @@ -47,20 +46,23 @@ export const useUpgradeSecurityPackages = () => {

(async () => {
try {
// Make sure fleet is initialized first
await context.services.fleet?.isInitialized();

// ignore the response for now since we aren't notifying the user
await sendUpgradeSecurityPackages(context.services.http, { signal });
} catch (error) {
// Ignore Errors, since this should not hinder the user's ability to use the UI

// ignore the error that occurs from aborting a request
// log to console, except if the error occurred due to aborting a request
if (!abortController.signal.aborted) {
// eslint-disable-next-line no-console
console.error(error);
}
}

return abortRequests;
})();

return abortRequests;
}
}, [canAccessFleet, context.services.http]);
}, [canAccessFleet, context.services.fleet, context.services.http]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { createSecuritySolutionStorageMock } from '../../mock/mock_local_storage
import { MlLocatorDefinition } from '../../../../../ml/public';
import { EuiTheme } from '../../../../../../../src/plugins/kibana_react/common';
import { MockUrlService } from 'src/plugins/share/common/mocks';
import { fleetMock } from '../../../../../fleet/public/mocks';

const mockUiSettings: Record<string, unknown> = {
[DEFAULT_TIME_RANGE]: { from: 'now-15m', to: 'now', mode: 'quick' },
Expand Down Expand Up @@ -96,6 +97,7 @@ export const createStartServicesMock = (): StartServices => {
const security = securityMock.createSetup();
const urlService = new MockUrlService();
const locator = urlService.locators.create(new MlLocatorDefinition());
const fleet = fleetMock.createStartMock();

return ({
...core,
Expand Down Expand Up @@ -141,6 +143,7 @@ export const createStartServicesMock = (): StartServices => {
},
security,
storage,
fleet,
ml: {
locator,
},
Expand Down
2 changes: 0 additions & 2 deletions x-pack/plugins/translations/translations/ja-JP.json
Original file line number Diff line number Diff line change
Expand Up @@ -21618,8 +21618,6 @@
"xpack.securitySolution.endpoint.hostIsolation.unIsolateThisHost": "現在{hostName}は{isolated}です。このホストを{unisolate}しますか?",
"xpack.securitySolution.endpoint.hostIsolationStatus.isolated": "分離済み",
"xpack.securitySolution.endpoint.ingestManager.createPackagePolicy.endpointConfiguration": "推奨のデフォルト値で統合が保存されます。後からこれを変更するには、エージェントポリシー内で Endpoint Security 統合を編集します。",
"xpack.securitySolution.endpoint.ingestToastMessage": "Fleetが設定中に失敗しました。",
"xpack.securitySolution.endpoint.ingestToastTitle": "アプリを初期化できませんでした",
"xpack.securitySolution.endpoint.list.actionmenu": "開く",
"xpack.securitySolution.endpoint.list.actions": "アクション",
"xpack.securitySolution.endpoint.list.endpointsEnrolling": "エンドポイントを登録しています。進行状況を追跡するには、{agentsLink}してください。",
Expand Down
2 changes: 0 additions & 2 deletions x-pack/plugins/translations/translations/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -21929,8 +21929,6 @@
"xpack.securitySolution.endpoint.hostIsolation.unIsolateThisHost": "{hostName} 当前{isolated}。是否确定要{unisolate}此主机?",
"xpack.securitySolution.endpoint.hostIsolationStatus.isolated": "已隔离",
"xpack.securitySolution.endpoint.ingestManager.createPackagePolicy.endpointConfiguration": "我们将使用建议的默认值保存您的集成。稍后,您可以通过在代理策略中编辑 Endpoint Security 集成对其进行更改。",
"xpack.securitySolution.endpoint.ingestToastMessage": "Fleet 在设置期间失败。",
"xpack.securitySolution.endpoint.ingestToastTitle": "应用无法初始化",
"xpack.securitySolution.endpoint.list.actionmenu": "未结",
"xpack.securitySolution.endpoint.list.actions": "操作",
"xpack.securitySolution.endpoint.list.endpointsEnrolling": "正在注册终端。{agentsLink}以跟踪进度。",
Expand Down

0 comments on commit ed4c3fe

Please sign in to comment.