Skip to content

Commit

Permalink
[Cloud Security] fix agent count to show correct pop-up modal (#181503)
Browse files Browse the repository at this point in the history
## Summary

Summarize your PR. If it involves visual changes include a screenshot or
gif.

This PR fixes agent count to for the following cases:
* If current agent policy is New agent policy, then AWS CloudFormation
when the agent count is 0
* if current agent policy is Existing agent policy with an agent , then
show confirmation agent modal
* If current agent policy is Agentless policy, then navigate to fleet
agent policies page.

`isDirty` and `currentAgentPolicy` states will used be to circumvent the
race condition `useEffect` that sets SetupTechnology when we first
navigate to the add CSPM integration page.
* `currentAgentPolicy` state keep current agentPolicy and reset agent
count when we switch from `agentless` to `agent-based`
* `isDirty` helps exit out
`x-pack/plugins/cloud_security_posture/public/components/fleet_extensions/setup_technology_selector/use_setup_technology.ts`
`useEffect` race condition once we initialize `setupTechnology` state on
page load.
  • Loading branch information
Omolola-Akinleye authored May 2, 2024
1 parent 0c9a323 commit 310bba0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ export const CspPolicyTemplateForm = memo<PackagePolicyReplaceDefineStepExtensio
// Handling validation state
const [isValid, setIsValid] = useState(true);
const input = getSelectedOption(newPolicy.inputs, integration);
const { isAgentlessAvailable, setupTechnology, setSetupTechnology } = useSetupTechnology({
const { isAgentlessAvailable, setupTechnology, updateSetupTechnology } = useSetupTechnology({
input,
agentPolicy,
agentlessPolicy,
Expand Down Expand Up @@ -767,7 +767,7 @@ export const CspPolicyTemplateForm = memo<PackagePolicyReplaceDefineStepExtensio
<SetupTechnologySelector
disabled={isEditPage}
setupTechnology={setupTechnology}
onSetupTechnologyChange={setSetupTechnology}
onSetupTechnologyChange={updateSetupTechnology}
/>
)}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,15 @@ export const useSetupTechnology = ({
return SetupTechnology.AGENT_BASED;
});

const [isDirty, setIsDirty] = useState<boolean>(false);

const updateSetupTechnology = (value: SetupTechnology) => {
setSetupTechnology(value);
setIsDirty(true);
};

useEffect(() => {
if (isEditPage) {
if (isEditPage || isDirty) {
return;
}

Expand All @@ -58,7 +65,7 @@ export const useSetupTechnology = ({
} else {
setSetupTechnology(SetupTechnology.AGENT_BASED);
}
}, [agentPolicyId, agentlessPolicyId, isAgentlessAvailable, isEditPage]);
}, [agentPolicyId, agentlessPolicyId, isAgentlessAvailable, isDirty, isEditPage]);

useEffect(() => {
if (isEditPage) {
Expand All @@ -74,5 +81,6 @@ export const useSetupTechnology = ({
isAgentlessAvailable,
setupTechnology,
setSetupTechnology,
updateSetupTechnology,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function useSetupTechnology({
}: {
updateNewAgentPolicy: (policy: NewAgentPolicy) => void;
newAgentPolicy: NewAgentPolicy;
updateAgentPolicy: (policy: AgentPolicy) => void;
updateAgentPolicy: (policy: AgentPolicy | undefined) => void;
setSelectedPolicyTab: (tab: SelectedPolicyTab) => void;
}) {
const { isAgentlessEnabled } = useAgentlessPolicy();
Expand Down Expand Up @@ -75,8 +75,8 @@ export function useSetupTechnology({
} else if (setupTechnology === SetupTechnology.AGENT_BASED) {
updateNewAgentPolicy(newAgentPolicy);
setSelectedPolicyTab(SelectedPolicyTab.NEW);
updateAgentPolicy(undefined);
}

setSelectedSetupTechnology(setupTechnology);
},
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,15 @@ export const CreatePackagePolicySinglePage: CreatePackagePolicyParams = ({
}
};

if (selectedPolicyTab === SelectedPolicyTab.NEW) {
setAgentCount(0);
return;
}

if (isFleetEnabled && agentPolicyId) {
getAgentCount();
}
}, [agentPolicyId, isFleetEnabled]);
}, [agentPolicyId, selectedPolicyTab, isFleetEnabled]);

const handleExtensionViewOnChange = useCallback<
PackagePolicyEditExtensionComponentProps['onChange']
Expand Down

0 comments on commit 310bba0

Please sign in to comment.