diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/components/actions_menu.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/components/actions_menu.tsx index f4f519b0a9c95..88dd00546e51f 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/components/actions_menu.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/components/actions_menu.tsx @@ -137,13 +137,22 @@ export const AgentPolicyActionMenu = memo<{ const copyPolicyItem = ( { setIsContextMenuOpen(false); copyAgentPolicyPrompt(agentPolicy, onCopySuccess); }} key="copyPolicy" + toolTipContent={ + hasManagedPackagePolicy ? ( + + ) : undefined + } > { }); }); + describe('copy', () => { + let soClient: ReturnType; + let esClient: ReturnType['asInternalUser']; + + beforeEach(() => { + soClient = getSavedObjectMock({ revision: 1, package_policies: ['package-1'] }); + esClient = elasticsearchServiceMock.createClusterClient().asInternalUser; + }); + + it('should throw error for agent policy which has managed package policy', async () => { + mockedPackagePolicyService.findAllForAgentPolicy.mockReturnValue([ + { + id: 'package-1', + is_managed: true, + }, + ] as any); + try { + await agentPolicyService.copy(soClient, esClient, 'mocked', { + name: 'copy mocked', + }); + } catch (e) { + expect(e.message).toEqual( + new PackagePolicyRestrictionRelatedError( + `Cannot copy an agent policy mocked that contains managed package policies` + ).message + ); + } + }); + }); + describe('deployPolicy', () => { beforeEach(() => { mockedGetFullAgentPolicy.mockReset(); diff --git a/x-pack/plugins/fleet/server/services/agent_policy.ts b/x-pack/plugins/fleet/server/services/agent_policy.ts index f377f95190df0..2ce1c42f0a74d 100644 --- a/x-pack/plugins/fleet/server/services/agent_policy.ts +++ b/x-pack/plugins/fleet/server/services/agent_policy.ts @@ -756,6 +756,17 @@ class AgentPolicyService { if (!baseAgentPolicy) { throw new AgentPolicyNotFoundError('Agent policy not found'); } + if (baseAgentPolicy.package_policies?.length) { + const hasManagedPackagePolicies = baseAgentPolicy.package_policies.some( + (packagePolicy) => packagePolicy.is_managed + ); + if (hasManagedPackagePolicies) { + throw new PackagePolicyRestrictionRelatedError( + `Cannot copy an agent policy ${id} that contains managed package policies` + ); + } + } + const newAgentPolicy = await this.create( soClient, esClient,