From 2d93bcb08579921cfa1770a3b96645656517a312 Mon Sep 17 00:00:00 2001 From: Julia Bardi <90178898+juliaElastic@users.noreply.github.com> Date: Fri, 11 Feb 2022 14:46:06 +0100 Subject: [PATCH] fixed bug where fleet server instructions were incorrectly shown (#125357) --- .../agent_enrollment_flyout.test.mocks.ts | 1 + .../agent_enrollment_flyout.test.tsx | 24 +++++++++++++++++++ .../managed_instructions.tsx | 22 ++++++++++++++--- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/agent_enrollment_flyout.test.mocks.ts b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/agent_enrollment_flyout.test.mocks.ts index 79cfb7ff2b2d1..acb9b198fdcba 100644 --- a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/agent_enrollment_flyout.test.mocks.ts +++ b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/agent_enrollment_flyout.test.mocks.ts @@ -13,6 +13,7 @@ jest.mock('../../hooks/use_request', () => { sendGetFleetStatus: jest.fn(), sendGetOneAgentPolicy: jest.fn(), useGetAgents: jest.fn(), + useGetAgentPolicies: jest.fn(), }; }); diff --git a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/agent_enrollment_flyout.test.tsx b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/agent_enrollment_flyout.test.tsx index 617bd95e18a6d..b46996ef164bd 100644 --- a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/agent_enrollment_flyout.test.tsx +++ b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/agent_enrollment_flyout.test.tsx @@ -21,6 +21,7 @@ import { sendGetFleetStatus, sendGetOneAgentPolicy, useGetAgents, + useGetAgentPolicies, } from '../../hooks/use_request'; import { FleetStatusProvider, ConfigContext } from '../../hooks'; @@ -101,6 +102,10 @@ describe('', () => { data: { items: [{ policy_id: 'fleet-server-policy' }] }, }); + (useGetAgentPolicies as jest.Mock).mockReturnValue?.({ + data: { items: [{ id: 'fleet-server-policy' }] }, + }); + await act(async () => { testBed = await setup({ agentPolicies: [{ id: 'fleet-server-policy' } as AgentPolicy], @@ -143,6 +148,25 @@ describe('', () => { }); }); + describe('with a specific policy when no agentPolicies set', () => { + beforeEach(async () => { + jest.clearAllMocks(); + await act(async () => { + testBed = await setup({ + agentPolicy: testAgentPolicy, + onClose: jest.fn(), + }); + testBed.component.update(); + }); + }); + + it('should not show fleet server instructions', () => { + const { exists } = testBed; + expect(exists('agentEnrollmentFlyout')).toBe(true); + expect(AgentEnrollmentKeySelectionStep).toHaveBeenCalled(); + }); + }); + // Skipped due to implementation details in the step components. See https://github.com/elastic/kibana/issues/103894 describe.skip('"View data" extension point', () => { it('shows the "View data" step when UI extension is provided', async () => { diff --git a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/managed_instructions.tsx b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/managed_instructions.tsx index 6d65476e3641f..d3294692c9e55 100644 --- a/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/managed_instructions.tsx +++ b/x-pack/plugins/fleet/public/components/agent_enrollment_flyout/managed_instructions.tsx @@ -11,7 +11,13 @@ import type { EuiContainedStepProps } from '@elastic/eui/src/components/steps/st import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n-react'; -import { useGetOneEnrollmentAPIKey, useLink, useFleetStatus, useGetAgents } from '../../hooks'; +import { + useGetOneEnrollmentAPIKey, + useLink, + useFleetStatus, + useGetAgents, + useGetAgentPolicies, +} from '../../hooks'; import { ManualInstructions } from '../../components/enrollment_instructions'; import { @@ -81,14 +87,24 @@ export const ManagedInstructions = React.memo( showInactive: false, }); + const { data: agentPoliciesData, isLoading: isLoadingAgentPolicies } = useGetAgentPolicies({ + page: 1, + perPage: 1000, + full: true, + }); + const fleetServers = useMemo(() => { - const fleetServerAgentPolicies: string[] = (agentPolicies ?? []) + let policies = agentPolicies; + if (!agentPolicies && !isLoadingAgentPolicies) { + policies = agentPoliciesData?.items; + } + const fleetServerAgentPolicies: string[] = (policies ?? []) .filter((pol) => policyHasFleetServer(pol)) .map((pol) => pol.id); return (agents?.items ?? []).filter((agent) => fleetServerAgentPolicies.includes(agent.policy_id ?? '') ); - }, [agents, agentPolicies]); + }, [agents, agentPolicies, agentPoliciesData, isLoadingAgentPolicies]); const fleetServerSteps = useMemo(() => { const {