Skip to content

Commit

Permalink
[8.16] [Fleet] Prevent duplication of managed policy !! (elastic#197575
Browse files Browse the repository at this point in the history
…) (elastic#198282)

# Backport

This will backport the following commits from `main` to `8.16`:
- [[Fleet] Prevent duplication of managed policy !!
(elastic#197575)](elastic#197575)

<!--- Backport version: 8.9.8 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT
[{"author":{"name":"Shahzad","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-10-29T20:00:40Z","message":"[Fleet]
Prevent duplication of managed policy !! (elastic#197575)\n\n##
Summary\r\n\r\nFixes
https://github.com/elastic/kibana/issues/194149\r\n\r\nPrevent
duplication of managed policy !!\r\n\r\n<img width=\"1594\"
alt=\"image\"\r\nsrc=\"https://github.com/user-attachments/assets/f386a287-4f9e-4307-ba84-98f3ea807ef9\">","sha":"81856bc8431daf83c972a65c6b8b0e312f8477a6","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:Fleet","v9.0.0","v8.16.0","backport:version","v8.17.0"],"number":197575,"url":"https://github.com/elastic/kibana/pull/197575","mergeCommit":{"message":"[Fleet]
Prevent duplication of managed policy !! (elastic#197575)\n\n##
Summary\r\n\r\nFixes
https://github.com/elastic/kibana/issues/194149\r\n\r\nPrevent
duplication of managed policy !!\r\n\r\n<img width=\"1594\"
alt=\"image\"\r\nsrc=\"https://github.com/user-attachments/assets/f386a287-4f9e-4307-ba84-98f3ea807ef9\">","sha":"81856bc8431daf83c972a65c6b8b0e312f8477a6"}},"sourceBranch":"main","suggestedTargetBranches":["8.16"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","labelRegex":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/197575","number":197575,"mergeCommit":{"message":"[Fleet]
Prevent duplication of managed policy !! (elastic#197575)\n\n##
Summary\r\n\r\nFixes
https://github.com/elastic/kibana/issues/194149\r\n\r\nPrevent
duplication of managed policy !!\r\n\r\n<img width=\"1594\"
alt=\"image\"\r\nsrc=\"https://github.com/user-attachments/assets/f386a287-4f9e-4307-ba84-98f3ea807ef9\">","sha":"81856bc8431daf83c972a65c6b8b0e312f8477a6"}},{"branch":"8.16","label":"v8.16.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.x","label":"v8.17.0","labelRegex":"^v8.17.0$","isSourceBranch":false,"url":"https://github.com/elastic/kibana/pull/198201","number":198201,"state":"MERGED","mergeCommit":{"sha":"15aaea8d02b9c6f214b426f23dfd2f7ab415af5a","message":"[8.x]
[Fleet] Prevent duplication of managed policy !! (elastic#197575)
(elastic#198201)\n\n# Backport\n\nThis will backport the following commits from
`main` to `8.x`:\n- [[Fleet] Prevent duplication of managed policy
!!\n(elastic#197575)](https://github.com/elastic/kibana/pull/197575)\n\n<!---
Backport version: 9.4.3 -->\n\n### Questions ?\nPlease refer to the
[Backport
tool\ndocumentation](https://github.com/sqren/backport)\n\n<!--BACKPORT\n[{\"author\":{\"name\":\"Shahzad\",\"email\":\"[email protected]\"},\"sourceCommit\":{\"committedDate\":\"2024-10-29T20:00:40Z\",\"message\":\"[Fleet]\nPrevent
duplication of managed policy !!
(elastic#197575)\\n\\n##\nSummary\\r\\n\\r\\nFixes\nhttps://github.com/elastic/issues/194149\\r\\n\\r\\nPrevent\nduplication
of managed policy !!\\r\\n\\r\\n<img
width=\\\"1594\\\"\nalt=\\\"image\\\"\\r\\nsrc=\\\"https://github.com/user-attachments/assets/f386a287-4f9e-4307-ba84-98f3ea807ef9\\\">\",\"sha\":\"81856bc8431daf83c972a65c6b8b0e312f8477a6\",\"branchLabelMapping\":{\"^v9.0.0$\":\"main\",\"^v8.17.0$\":\"8.x\",\"^v(\\\\d+).(\\\\d+).\\\\d+$\":\"$1.$2\"}},\"sourcePullRequest\":{\"labels\":[\"release_note:skip\",\"Team:Fleet\",\"v9.0.0\",\"backport:prev-minor\"],\"title\":\"[Fleet]\nPrevent
duplication of managed
policy\n!!\",\"number\":197575,\"url\":\"https://github.com/elastic/kibana/pull/197575\",\"mergeCommit\":{\"message\":\"[Fleet]\nPrevent
duplication of managed policy !!
(elastic#197575)\\n\\n##\nSummary\\r\\n\\r\\nFixes\nhttps://github.com/elastic/issues/194149\\r\\n\\r\\nPrevent\nduplication
of managed policy !!\\r\\n\\r\\n<img
width=\\\"1594\\\"\nalt=\\\"image\\\"\\r\\nsrc=\\\"https://github.com/user-attachments/assets/f386a287-4f9e-4307-ba84-98f3ea807ef9\\\">\",\"sha\":\"81856bc8431daf83c972a65c6b8b0e312f8477a6\"}},\"sourceBranch\":\"main\",\"suggestedTargetBranches\":[],\"targetPullRequestStates\":[{\"branch\":\"main\",\"label\":\"v9.0.0\",\"branchLabelMappingKey\":\"^v9.0.0$\",\"isSourceBranch\":true,\"state\":\"MERGED\",\"url\":\"https://github.com/elastic/kibana/pull/197575\",\"number\":197575,\"mergeCommit\":{\"message\":\"[Fleet]\nPrevent
duplication of managed policy !!
(elastic#197575)\\n\\n##\nSummary\\r\\n\\r\\nFixes\nhttps://github.com/elastic/issues/194149\\r\\n\\r\\nPrevent\nduplication
of managed policy !!\\r\\n\\r\\n<img
width=\\\"1594\\\"\nalt=\\\"image\\\"\\r\\nsrc=\\\"https://github.com/user-attachments/assets/f386a287-4f9e-4307-ba84-98f3ea807ef9\\\">\",\"sha\":\"81856bc8431daf83c972a65c6b8b0e312f8477a6\"}}]}]\nBACKPORT-->\n\nCo-authored-by:
Shahzad <[email protected]>"}}]}] BACKPORT-->
  • Loading branch information
shahzad31 authored Nov 1, 2024
1 parent 75b2332 commit e07a8f4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,22 @@ export const AgentPolicyActionMenu = memo<{
const copyPolicyItem = (
<EuiContextMenuItem
data-test-subj="agentPolicyActionMenuCopyButton"
disabled={!authz.integrations.writeIntegrationPolicies}
disabled={!authz.integrations.writeIntegrationPolicies || hasManagedPackagePolicy}
icon="copy"
onClick={() => {
setIsContextMenuOpen(false);
copyAgentPolicyPrompt(agentPolicy, onCopySuccess);
}}
key="copyPolicy"
toolTipContent={
hasManagedPackagePolicy ? (
<FormattedMessage
id="xpack.fleet.policyForm.copyPolicyActionText.disabled"
defaultMessage="Agent policy with managed package policies cannot be copied."
data-test-subj="agentPolicyActionMenuCopyButtonDisabledTooltip"
/>
) : undefined
}
>
<FormattedMessage
id="xpack.fleet.agentPolicyActionMenu.copyPolicyActionText"
Expand Down
30 changes: 30 additions & 0 deletions x-pack/plugins/fleet/server/services/agent_policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,36 @@ describe('Agent policy', () => {
});
});

describe('copy', () => {
let soClient: ReturnType<typeof savedObjectsClientMock.create>;
let esClient: ReturnType<typeof elasticsearchServiceMock.createClusterClient>['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();
Expand Down
11 changes: 11 additions & 0 deletions x-pack/plugins/fleet/server/services/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e07a8f4

Please sign in to comment.