Skip to content

Commit

Permalink
[Fleet] handle multiple policies in edit package policy extension view (
Browse files Browse the repository at this point in the history
#187334)

## Summary

Related to #75867

Handling multiple policies in package policy edit extension view, made
changes in CSP.

@elastic/kibana-cloud-security-posture Hey, could you help me how can I
create an agentless policy to test with?

### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
  • Loading branch information
juliaElastic committed Jul 3, 2024
1 parent 307237f commit 260882e
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ describe('<CspPolicyTemplateForm />', () => {
const WrappedComponent = ({
newPolicy,
edit = false,
agentPolicy,
agentPolicies,
packageInfo = {} as PackageInfo,
agentlessPolicy,
}: {
edit?: boolean;
newPolicy: NewPackagePolicy;
agentPolicy?: AgentPolicy;
agentPolicies?: AgentPolicy[];
packageInfo?: PackageInfo;
onChange?: jest.Mock<void, [NewPackagePolicy]>;
agentlessPolicy?: AgentPolicy;
Expand All @@ -136,7 +136,7 @@ describe('<CspPolicyTemplateForm />', () => {
onChange={onChange}
packageInfo={packageInfo}
isEditPage={true}
agentPolicy={agentPolicy}
agentPolicies={agentPolicies}
agentlessPolicy={agentlessPolicy}
/>
)}
Expand All @@ -146,7 +146,7 @@ describe('<CspPolicyTemplateForm />', () => {
onChange={onChange}
packageInfo={packageInfo}
isEditPage={false}
agentPolicy={agentPolicy}
agentPolicies={agentPolicies}
agentlessPolicy={agentlessPolicy}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ const IntegrationSettings = ({ onChange, fields }: IntegrationInfoFieldsProps) =

export const CspPolicyTemplateForm = memo<PackagePolicyReplaceDefineStepExtensionComponentProps>(
({
agentPolicy,
agentPolicies,
newPolicy,
onChange,
validationResults,
Expand All @@ -551,7 +551,7 @@ export const CspPolicyTemplateForm = memo<PackagePolicyReplaceDefineStepExtensio
const input = getSelectedOption(newPolicy.inputs, integration);
const { isAgentlessAvailable, setupTechnology, updateSetupTechnology } = useSetupTechnology({
input,
agentPolicy,
agentPolicies,
agentlessPolicy,
handleSetupTechnologyChange,
isEditPage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ describe('useSetupTechnology', () => {

it('sets to AGENT_BASED when agentPolicyId differs from agentlessPolicyId', () => {
const input = { type: CLOUDBEAT_AWS } as NewPackagePolicyInput;
const agentPolicy = { id: 'agentPolicyId' } as AgentPolicy;
const agentPolicies = [{ id: 'agentPolicyId' } as AgentPolicy];
const agentlessPolicy = { id: 'agentlessPolicyId' } as AgentPolicy;
const { result } = renderHook(() =>
useSetupTechnology({ input, agentPolicy, agentlessPolicy, isEditPage })
useSetupTechnology({ input, agentPolicies, agentlessPolicy, isEditPage })
);
expect(result.current.setupTechnology).toBe(SetupTechnology.AGENT_BASED);
});
Expand Down Expand Up @@ -115,11 +115,11 @@ describe('useSetupTechnology', () => {

it('initializes with AGENTLESS technology if the agent policy id is "agentless"', () => {
const input = { type: CLOUDBEAT_AWS } as NewPackagePolicyInput;
const agentPolicy = { id: 'agentless' } as AgentPolicy;
const agentPolicies = [{ id: 'agentless' } as AgentPolicy];
const { result } = renderHook(() =>
useSetupTechnology({
input,
agentPolicy,
agentPolicies,
isEditPage,
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import { CLOUDBEAT_AWS, CLOUDBEAT_GCP, CLOUDBEAT_AZURE } from '../../../../commo

export const useSetupTechnology = ({
input,
agentPolicy,
agentPolicies,
agentlessPolicy,
handleSetupTechnologyChange,
isEditPage,
}: {
input: NewPackagePolicyInput;
agentPolicy?: AgentPolicy;
agentPolicies?: AgentPolicy[];
agentlessPolicy?: AgentPolicy;
handleSetupTechnologyChange?: (value: SetupTechnology) => void;
isEditPage: boolean;
Expand All @@ -28,10 +28,10 @@ export const useSetupTechnology = ({
const isCspmAzure = input.type === CLOUDBEAT_AZURE;
const isAgentlessSupportedForCloudProvider = isCspmAws || isCspmGcp || isCspmAzure;
const isAgentlessAvailable = Boolean(isAgentlessSupportedForCloudProvider && agentlessPolicy);
const agentPolicyId = agentPolicy?.id;
const agentPolicyIds = (agentPolicies || []).map((policy: AgentPolicy) => policy.id);
const agentlessPolicyId = agentlessPolicy?.id;
const [setupTechnology, setSetupTechnology] = useState<SetupTechnology>(() => {
if (isEditPage && agentPolicyId === SetupTechnology.AGENTLESS) {
if (isEditPage && agentPolicyIds.includes(SetupTechnology.AGENTLESS)) {
return SetupTechnology.AGENTLESS;
}

Expand All @@ -50,7 +50,11 @@ export const useSetupTechnology = ({
return;
}

if (agentPolicyId && agentPolicyId !== agentlessPolicyId) {
const hasAgentPolicies = agentPolicyIds.length > 0;
const agentlessPolicyIsAbsent =
!agentlessPolicyId || !agentPolicyIds.includes(agentlessPolicyId);

if (hasAgentPolicies && agentlessPolicyIsAbsent) {
/*
handle case when agent policy is coming from outside,
e.g. from the get param or when coming to integration from a specific agent policy
Expand All @@ -65,7 +69,7 @@ export const useSetupTechnology = ({
} else {
setSetupTechnology(SetupTechnology.AGENT_BASED);
}
}, [agentPolicyId, agentlessPolicyId, isAgentlessAvailable, isDirty, isEditPage]);
}, [agentPolicyIds, agentlessPolicyId, isAgentlessAvailable, isDirty, isEditPage]);

useEffect(() => {
if (isEditPage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ export const CreatePackagePolicySinglePage: CreatePackagePolicyParams = ({
) : (
<ExtensionWrapper>
<replaceDefineStepView.Component
agentPolicy={agentPolicies[0]}
agentPolicies={agentPolicies}
packageInfo={packageInfo}
newPolicy={packagePolicy}
onChange={handleExtensionViewOnChange}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export const EditPackagePolicyForm = memo<{
} = usePackagePolicyWithRelatedData(packagePolicyId, {
forceUpgrade,
});
const hasAgentlessAgentPolicy = packagePolicy.policy_ids.includes(AGENTLESS_POLICY_ID);

const canWriteIntegrationPolicies = useAuthz().integrations.writeIntegrationPolicies;
useSetIsReadOnly(!canWriteIntegrationPolicies);
Expand Down Expand Up @@ -241,7 +242,7 @@ export const EditPackagePolicyForm = memo<{
}
if (
(agentCount !== 0 || agentPoliciesToAdd.length > 0 || agentPoliciesToRemove.length > 0) &&
!packagePolicy.policy_ids.includes(AGENTLESS_POLICY_ID) &&
!hasAgentlessAgentPolicy &&
formState !== 'CONFIRM'
) {
setFormState('CONFIRM');
Expand Down Expand Up @@ -432,7 +433,7 @@ export const EditPackagePolicyForm = memo<{
const replaceConfigurePackage = replaceDefineStepView && originalPackagePolicy && packageInfo && (
<ExtensionWrapper>
<replaceDefineStepView.Component
agentPolicy={agentPolicies[0]}
agentPolicies={agentPolicies}
packageInfo={packageInfo}
policy={originalPackagePolicy}
newPolicy={packagePolicy}
Expand Down Expand Up @@ -521,7 +522,7 @@ export const EditPackagePolicyForm = memo<{
<EuiSpacer size="xxl" />
</>
)}
{canUseMultipleAgentPolicies ? (
{canUseMultipleAgentPolicies && !hasAgentlessAgentPolicy ? (
<StepsWithLessPadding steps={steps} />
) : (
replaceConfigurePackage || configurePackage
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/public/types/ui_extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export type PackagePolicyReplaceDefineStepExtensionComponentProps = (
| (PackagePolicyCreateExtensionComponentProps & { isEditPage: false })
) & {
validationResults?: PackagePolicyValidationResults;
agentPolicy?: AgentPolicy;
agentPolicies?: AgentPolicy[];
packageInfo: PackageInfo;
agentlessPolicy?: AgentPolicy;
handleSetupTechnologyChange?: (setupTechnology: string) => void;
Expand Down

0 comments on commit 260882e

Please sign in to comment.