Skip to content

Commit

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

# Backport

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

<!--- Backport version: 9.4.3 -->

### 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
(elastic#201051)\n\n## Summary\r\n\r\nResolves elastic#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"],"title":"[UII]
Allow to create integration policy with no agent
policies","number":201051,"url":"https://github.com/elastic/kibana/pull/201051","mergeCommit":{"message":"[UII]
Allow to create integration policy with no agent policies
(elastic#201051)\n\n## Summary\r\n\r\nResolves elastic#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","branchLabelMappingKey":"^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
(elastic#201051)\n\n## Summary\r\n\r\nResolves elastic#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"}}]}]
BACKPORT-->

Co-authored-by: Jen Huang <[email protected]>
  • Loading branch information
kibanamachine and jen-huang authored Nov 22, 2024
1 parent 1dfae0f commit 8eb67bd
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 56 deletions.
87 changes: 59 additions & 28 deletions x-pack/plugins/fleet/cypress/e2e/package_policy.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,46 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { EXISTING_HOSTS_TAB } from '../screens/fleet';
import {
ADD_INTEGRATION_POLICY_BTN,
CREATE_PACKAGE_POLICY_SAVE_BTN,
POLICY_EDITOR,
} from '../screens/integrations';
import { CONFIRM_MODAL } from '../screens/navigation';
import { login } from '../tasks/login';

describe('Edit package policy', () => {
const policyConfig = {
id: 'policy-1',
name: 'fleet_server-1',
namespace: 'default',
package: { name: 'fleet_server', title: 'Fleet Server', version: '1.1.0' },
enabled: true,
policy_id: 'fleet-server-policy',
policy_ids: ['fleet-server-policy'],
output_id: 'fleet-default-output',
inputs: [
{
type: 'fleet-server',
policy_template: 'fleet_server',
enabled: true,
streams: [],
vars: {
host: { value: ['0.0.0.0'], type: 'text' },
port: { value: [8220], type: 'integer' },
max_connections: { type: 'integer' },
custom: { value: '', type: 'yaml' },
},
compiled_input: { server: { port: 8220, host: '0.0.0.0' } },
},
],
};
describe('Package policy', () => {
beforeEach(() => {
login();
});

it('should edit package policy', () => {
const policyConfig = {
id: 'policy-1',
name: 'fleet_server-1',
namespace: 'default',
package: { name: 'fleet_server', title: 'Fleet Server', version: '1.1.0' },
enabled: true,
policy_id: 'fleet-server-policy',
policy_ids: ['fleet-server-policy'],
output_id: 'fleet-default-output',
inputs: [
{
type: 'fleet-server',
policy_template: 'fleet_server',
enabled: true,
streams: [],
vars: {
host: { value: ['0.0.0.0'], type: 'text' },
port: { value: [8220], type: 'integer' },
max_connections: { type: 'integer' },
custom: { value: '', type: 'yaml' },
},
compiled_input: { server: { port: 8220, host: '0.0.0.0' } },
},
],
};

cy.intercept('/api/fleet/package_policies/policy-1', {
item: policyConfig,
Expand Down Expand Up @@ -111,9 +121,7 @@ describe('Edit package policy', () => {
status: 'not_installed',
},
});
});

it('should edit package policy', () => {
cy.visit('/app/fleet/policies/fleet-server-policy/edit-integration/policy-1');
cy.getBySel('packagePolicyDescriptionInput').clear().type('desc');

Expand All @@ -128,4 +136,27 @@ describe('Edit package policy', () => {
expect(interception.request.body.description).to.equal('desc');
});
});

it('should create a new orphaned package policy', () => {
cy.visit('/app/integrations/detail/system');
cy.getBySel(ADD_INTEGRATION_POLICY_BTN).click();
cy.getBySel(EXISTING_HOSTS_TAB).click();
cy.getBySel(POLICY_EDITOR.AGENT_POLICY_SELECT).should('exist');
cy.getBySel(POLICY_EDITOR.AGENT_POLICY_CLEAR).should('not.exist');
cy.getBySel(POLICY_EDITOR.POLICY_NAME_INPUT).clear().type('system-orphaned-test');

cy.intercept({
method: 'POST',
url: '/api/fleet/package_policies',
}).as('createPackagePolicy');

cy.getBySel(CREATE_PACKAGE_POLICY_SAVE_BTN).should('be.enabled').click();
cy.getBySel(CONFIRM_MODAL.CONFIRM_BUTTON).click();

cy.wait('@createPackagePolicy').then((interception) => {
expect(interception.request.body.name).to.equal('system-orphaned-test');
expect(interception.request.body.policy_id).to.equal(undefined);
expect(interception.request.body.policy_ids).to.deep.equal([]);
});
});
});
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/cypress/screens/integrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const POLICY_EDITOR = {
POLICY_NAME_INPUT: 'packagePolicyNameInput',
DATASET_SELECT: 'datasetComboBox',
AGENT_POLICY_SELECT: 'agentPolicyMultiSelect',
AGENT_POLICY_CLEAR: 'comboBoxClearButton',
INSPECT_PIPELINES_BTN: 'datastreamInspectPipelineBtn',
EDIT_MAPPINGS_BTN: 'datastreamEditMappingsBtn',
CREATE_MAPPINGS_BTN: 'datastreamAddCustomComponentTemplateBtn',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ describe('StepSelectHosts', () => {
await waitFor(() => {
expect(renderResult.getByText('New agent policy name')).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', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,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 @@ -117,13 +117,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 @@ -280,7 +280,7 @@ export function useOnSubmit({

useEffect(() => {
if (
agentPolicies.length > 0 &&
(canUseMultipleAgentPolicies || agentPolicies.length > 0) &&
!isEqual(
agentPolicies.map((policy) => policy.id),
packagePolicy.policy_ids
Expand All @@ -290,7 +290,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 @@ -124,8 +124,9 @@ describe('StepEditHosts', () => {

render();

expect(renderResult.getByText('New agent policy name')).toBeInTheDocument();
expect(renderResult.queryByRole('tablist')).not.toBeInTheDocument();
expect(renderResult.getByText('For existing hosts:')).toBeInTheDocument();
expect(renderResult.getByText('For a new host:')).toBeInTheDocument();
});

it('should display new policy button and existing policies when agent policies exist', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const StepEditHosts: React.FunctionComponent<Props> = ({
}
}, [existingAgentPolicies.length]); // eslint-disable-line react-hooks/exhaustive-deps

return existingAgentPolicies.length > 0 ? (
return (
<EuiFlexGroup direction="column" alignItems="flexStart">
<EuiFlexItem>
<EuiTitle size="xs">
Expand Down Expand Up @@ -141,13 +141,5 @@ export const StepEditHosts: React.FunctionComponent<Props> = ({
</>
)}
</EuiFlexGroup>
) : (
<AgentPolicyIntegrationForm
agentPolicy={newAgentPolicy}
updateAgentPolicy={updateNewAgentPolicy}
withSysMonitoring={withSysMonitoring}
updateSysMonitoring={updateSysMonitoring}
validation={validation}
/>
);
};
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 @@ -8,15 +8,16 @@
import { LICENCE_FOR_MULTIPLE_AGENT_POLICIES } from '../../common/constants';
import { ExperimentalFeaturesService } from '../services';

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

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 8eb67bd

Please sign in to comment.