Skip to content

Commit

Permalink
[8.15] [UII] Allow to create integration policy with no agent policies (
Browse files Browse the repository at this point in the history
#201051) (#201684)

# Backport

This will backport the following commits from `main` to `8.15`:
- [[UII] Allow to create integration policy with no agent policies
(#201051)](#201051)

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

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

<!--BACKPORT [{"author":{"name":"Jen
Huang","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-11-22T00:57:06Z","message":"[UII]
Allow to create integration policy with no agent policies
(#201051)\n\n## Summary\r\n\r\nResolves #198165. This PR fixes an issue
where `policy_ids` array was\r\nnot able to be cleared behind the scenes
to create an orphaned\r\nintegration policy, even when cluster is able
to use the multiple agent\r\npolicies
feature.","sha":"3952dea3962c52dbd8d0cf01f7966670a64639d4","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:Fleet","v9.0.0","backport:prev-major","v8.17.0","v8.18.0"],"number":201051,"url":"https://github.com/elastic/kibana/pull/201051","mergeCommit":{"message":"[UII]
Allow to create integration policy with no agent policies
(#201051)\n\n## Summary\r\n\r\nResolves #198165. This PR fixes an issue
where `policy_ids` array was\r\nnot able to be cleared behind the scenes
to create an orphaned\r\nintegration policy, even when cluster is able
to use the multiple agent\r\npolicies
feature.","sha":"3952dea3962c52dbd8d0cf01f7966670a64639d4"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","labelRegex":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/201051","number":201051,"mergeCommit":{"message":"[UII]
Allow to create integration policy with no agent policies
(#201051)\n\n## Summary\r\n\r\nResolves #198165. This PR fixes an issue
where `policy_ids` array was\r\nnot able to be cleared behind the scenes
to create an orphaned\r\nintegration policy, even when cluster is able
to use the multiple agent\r\npolicies
feature.","sha":"3952dea3962c52dbd8d0cf01f7966670a64639d4"}},{"branch":"8.17","label":"v8.17.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"url":"https://github.com/elastic/kibana/pull/201305","number":201305,"state":"MERGED","mergeCommit":{"sha":"8eb67bd1883aad804d5c33df61d065f01f4398ca","message":"[8.17]
[UII] Allow to create integration policy with no agent policies
(#201051) (#201305)\n\n# Backport\n\nThis will backport the following
commits from `main` to `8.17`:\n- [[UII] Allow to create integration
policy with no agent
policies\n(#201051)](https://github.com/elastic/kibana/pull/201051)\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
[{\"author\":{\"name\":\"Jen\nHuang\",\"email\":\"[email protected]\"},\"sourceCommit\":{\"committedDate\":\"2024-11-22T00:57:06Z\",\"message\":\"[UII]\nAllow
to create integration policy with no agent policies\n(#201051)\\n\\n##
Summary\\r\\n\\r\\nResolves #198165. This PR fixes an issue\nwhere
`policy_ids` array was\\r\\nnot able to be cleared behind the scenes\nto
create an orphaned\\r\\nintegration policy, even when cluster is
able\nto use the multiple
agent\\r\\npolicies\nfeature.\",\"sha\":\"3952dea3962c52dbd8d0cf01f7966670a64639d4\",\"branchLabelMapping\":{\"^v9.0.0$\":\"main\",\"^v8.18.0$\":\"8.x\",\"^v(\\\\d+).(\\\\d+).\\\\d+$\":\"$1.$2\"}},\"sourcePullRequest\":{\"labels\":[\"release_note:fix\",\"Team:Fleet\",\"v9.0.0\",\"backport:prev-major\"],\"title\":\"[UII]\nAllow
to create integration policy with no
agent\npolicies\",\"number\":201051,\"url\":\"https://github.com/elastic/kibana/pull/201051\",\"mergeCommit\":{\"message\":\"[UII]\nAllow
to create integration policy with no agent policies\n(#201051)\\n\\n##
Summary\\r\\n\\r\\nResolves #198165. This PR fixes an issue\nwhere
`policy_ids` array was\\r\\nnot able to be cleared behind the scenes\nto
create an orphaned\\r\\nintegration policy, even when cluster is
able\nto use the multiple
agent\\r\\npolicies\nfeature.\",\"sha\":\"3952dea3962c52dbd8d0cf01f7966670a64639d4\"}},\"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/201051\",\"number\":201051,\"mergeCommit\":{\"message\":\"[UII]\nAllow
to create integration policy with no agent policies\n(#201051)\\n\\n##
Summary\\r\\n\\r\\nResolves #198165. This PR fixes an issue\nwhere
`policy_ids` array was\\r\\nnot able to be cleared behind the scenes\nto
create an orphaned\\r\\nintegration policy, even when cluster is
able\nto use the multiple
agent\\r\\npolicies\nfeature.\",\"sha\":\"3952dea3962c52dbd8d0cf01f7966670a64639d4\"}}]}]\nBACKPORT-->\n\nCo-authored-by:
Jen Huang
<[email protected]>"}},{"branch":"8.x","label":"v8.18.0","labelRegex":"^v8.18.0$","isSourceBranch":false,"url":"https://github.com/elastic/kibana/pull/201306","number":201306,"state":"MERGED","mergeCommit":{"sha":"19059ee9c2d90d1da7cc21ce05c7c34e6a5b0578","message":"[8.x]
[UII] Allow to create integration policy with no agent policies
(#201051) (#201306)\n\n# Backport\n\nThis will backport the following
commits from `main` to `8.x`:\n- [[UII] Allow to create integration
policy with no agent
policies\n(#201051)](https://github.com/elastic/kibana/pull/201051)\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
[{\"author\":{\"name\":\"Jen\nHuang\",\"email\":\"[email protected]\"},\"sourceCommit\":{\"committedDate\":\"2024-11-22T00:57:06Z\",\"message\":\"[UII]\nAllow
to create integration policy with no agent policies\n(#201051)\\n\\n##
Summary\\r\\n\\r\\nResolves #198165. This PR fixes an issue\nwhere
`policy_ids` array was\\r\\nnot able to be cleared behind the scenes\nto
create an orphaned\\r\\nintegration policy, even when cluster is
able\nto use the multiple
agent\\r\\npolicies\nfeature.\",\"sha\":\"3952dea3962c52dbd8d0cf01f7966670a64639d4\",\"branchLabelMapping\":{\"^v9.0.0$\":\"main\",\"^v8.18.0$\":\"8.x\",\"^v(\\\\d+).(\\\\d+).\\\\d+$\":\"$1.$2\"}},\"sourcePullRequest\":{\"labels\":[\"release_note:fix\",\"Team:Fleet\",\"v9.0.0\",\"backport:prev-major\"],\"title\":\"[UII]\nAllow
to create integration policy with no
agent\npolicies\",\"number\":201051,\"url\":\"https://github.com/elastic/kibana/pull/201051\",\"mergeCommit\":{\"message\":\"[UII]\nAllow
to create integration policy with no agent policies\n(#201051)\\n\\n##
Summary\\r\\n\\r\\nResolves #198165. This PR fixes an issue\nwhere
`policy_ids` array was\\r\\nnot able to be cleared behind the scenes\nto
create an orphaned\\r\\nintegration policy, even when cluster is
able\nto use the multiple
agent\\r\\npolicies\nfeature.\",\"sha\":\"3952dea3962c52dbd8d0cf01f7966670a64639d4\"}},\"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/201051\",\"number\":201051,\"mergeCommit\":{\"message\":\"[UII]\nAllow
to create integration policy with no agent policies\n(#201051)\\n\\n##
Summary\\r\\n\\r\\nResolves #198165. This PR fixes an issue\nwhere
`policy_ids` array was\\r\\nnot able to be cleared behind the scenes\nto
create an orphaned\\r\\nintegration policy, even when cluster is
able\nto use the multiple
agent\\r\\npolicies\nfeature.\",\"sha\":\"3952dea3962c52dbd8d0cf01f7966670a64639d4\"}}]}]\nBACKPORT-->\n\nCo-authored-by:
Jen Huang <[email protected]>"}}]}] BACKPORT-->
  • Loading branch information
jen-huang authored Nov 25, 2024
1 parent 373dd0e commit 61cba7f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ describe('StepSelectHosts', () => {
waitFor(() => {
expect(renderResult.getByText('Agent policy 1')).toBeInTheDocument();
});
expect(renderResult.queryByRole('tablist')).not.toBeInTheDocument();
expect(renderResult.queryByRole('tablist')).toBeInTheDocument();
expect(renderResult.getByText('Create agent policy')).toBeInTheDocument();
});

it('should display tabs with New hosts selected when agent policies exist', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export const StepSelectHosts: React.FunctionComponent<Props> = ({
const handleOnTabClick = (tab: EuiTabbedContentTab) =>
updateSelectedTab(tab.id as SelectedPolicyTab);

return existingAgentPolicies.length > 0 ? (
return (
<StyledEuiTabbedContent
initialSelectedTab={
initialSelectedTabIndex
Expand All @@ -133,13 +133,5 @@ export const StepSelectHosts: React.FunctionComponent<Props> = ({
tabs={tabs}
onTabClick={handleOnTabClick}
/>
) : (
<AgentPolicyIntegrationForm
agentPolicy={newAgentPolicy}
updateAgentPolicy={updateNewAgentPolicy}
withSysMonitoring={withSysMonitoring}
updateSysMonitoring={updateSysMonitoring}
validation={validation}
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
sendCreatePackagePolicy,
sendBulkInstallPackages,
sendGetPackagePolicies,
useMultipleAgentPolicies,
} from '../../../../../hooks';
import { isVerificationError, packageToPackagePolicy } from '../../../../../services';
import {
Expand Down Expand Up @@ -152,6 +153,8 @@ export function useOnSubmit({
}) {
const { notifications } = useStartServices();
const confirmForceInstall = useConfirmForceInstall();
const { canUseMultipleAgentPolicies } = useMultipleAgentPolicies();

// only used to store the resulting package policy once saved
const [savedPackagePolicy, setSavedPackagePolicy] = useState<PackagePolicy>();
// Form state
Expand Down Expand Up @@ -279,7 +282,7 @@ export function useOnSubmit({

useEffect(() => {
if (
agentPolicies.length > 0 &&
(canUseMultipleAgentPolicies || agentPolicies.length > 0) &&
!isEqual(
agentPolicies.map((policy) => policy.id),
packagePolicy.policy_ids
Expand All @@ -289,7 +292,7 @@ export function useOnSubmit({
policy_ids: agentPolicies.map((policy) => policy.id),
});
}
}, [packagePolicy, agentPolicies, updatePackagePolicy]);
}, [packagePolicy, agentPolicies, updatePackagePolicy, canUseMultipleAgentPolicies]);

const onSaveNavigate = useOnSaveNavigate({
packagePolicy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
UpdatePackagePolicy,
UpdateAgentPolicyRequest,
} from '../../../types';
import { canUseMultipleAgentPolicies } from '../../../hooks';

function generateKibanaDevToolsRequest(method: string, path: string, body: any) {
return `${method} kbn:${path}\n${JSON.stringify(body, null, 2)}\n`;
Expand Down Expand Up @@ -49,9 +50,13 @@ export function generateCreateAgentPolicyDevToolsRequest(
export function generateCreatePackagePolicyDevToolsRequest(
packagePolicy: NewPackagePolicy & { force?: boolean }
) {
const canHaveNoAgentPolicies = canUseMultipleAgentPolicies();

return generateKibanaDevToolsRequest('POST', packagePolicyRouteService.getCreatePath(), {
policy_ids:
packagePolicy.policy_ids.length > 0 ? packagePolicy.policy_ids : ['<agent_policy_id>'],
packagePolicy.policy_ids.length > 0 || canHaveNoAgentPolicies
? packagePolicy.policy_ids
: ['<agent_policy_id>'],
package: formatPackage(packagePolicy.package),
...omit(packagePolicy, 'policy_ids', 'package', 'enabled'),
inputs: formatInputs(packagePolicy.inputs),
Expand Down
11 changes: 6 additions & 5 deletions x-pack/plugins/fleet/public/hooks/use_multiple_agent_policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@

import { ExperimentalFeaturesService } from '../services';

import { useLicense } from './use_license';
import { licenseService } from './use_license';

export const LICENCE_FOR_MULTIPLE_AGENT_POLICIES = 'enterprise';

export function useMultipleAgentPolicies() {
const licenseService = useLicense();
return { canUseMultipleAgentPolicies: canUseMultipleAgentPolicies() };
}

export function canUseMultipleAgentPolicies() {
const { enableReusableIntegrationPolicies } = ExperimentalFeaturesService.get();

const hasEnterpriseLicence = licenseService.hasAtLeast(LICENCE_FOR_MULTIPLE_AGENT_POLICIES);

const canUseMultipleAgentPolicies = enableReusableIntegrationPolicies && hasEnterpriseLicence;

return { canUseMultipleAgentPolicies };
return Boolean(enableReusableIntegrationPolicies && hasEnterpriseLicence);
}

0 comments on commit 61cba7f

Please sign in to comment.