Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cloud Security] fix agent count to show correct pop-up modal #181503

Merged
merged 10 commits into from
May 2, 2024

Conversation

Omolola-Akinleye
Copy link
Contributor

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.

@Omolola-Akinleye Omolola-Akinleye added bug Fixes for quality problems that affect the customer experience Team:Cloud Security Cloud Security team related v8.15.0 labels Apr 23, 2024
@Omolola-Akinleye Omolola-Akinleye self-assigned this Apr 23, 2024
@Omolola-Akinleye Omolola-Akinleye requested review from a team as code owners April 23, 2024 22:10
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-cloud-security-posture (Team:Cloud Security)

@botelastic botelastic bot added the Team:Fleet Team label for Observability Data Collection Fleet team label Apr 23, 2024
@elasticmachine
Copy link
Contributor

Pinging @elastic/fleet (Team:Fleet)

@Omolola-Akinleye Omolola-Akinleye added the release_note:skip Skip the PR/issue when compiling release notes label Apr 23, 2024
@Omolola-Akinleye Omolola-Akinleye changed the title [Cloud Security] Show AWS Cloud Formation Pop up with 0 agents [Cloud Security] fix agent count to show correct pop-up modal Apr 23, 2024
setSelectedPolicyTab: (tab: SelectedPolicyTab) => void;
}) {
const { isAgentlessEnabled } = useAgentlessPolicy();
const [selectedSetupTechnology, setSelectedSetupTechnology] = useState<SetupTechnology>(
SetupTechnology.AGENT_BASED
);
const [agentlessPolicy, setAgentlessPolicy] = useState<AgentPolicy | undefined>();
const [currentAgentPolicy, setCurrentAgentPolicy] = useState<AgentPolicy | undefined>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this state? What confuses me :

  1. we set it in the effect that fetches the agentless policy. But this is incorrect logically, as there is no connection between this effect and setting up the actual agent policy. It doesn't do any updateAgentPolicy
  2. in the handleSetupTechnologyChange the usage of currentAgentPolicy is quite confusing.
setCurrentAgentPolicy(agentlessPolicy);
updateAgentPolicy(currentAgentPolicy || agentlessPolicy);

first of all it leaves the impression that as currentAgentPolicy is set to agentlessPolicy right before calling updateAgentPolicy it will always be agentlessPolicy anyway. But mind that setState is async in nature, so the previous value of the currentAgentPolicy will be used. But as the two possible values for currentAgentPolicy are agentlessPolicy or undefined, this code is just equal to updateAgentPolicy(agentlessPolicy);

I suspect that we don't need this state at all and it should be enough to just set updateAgentPolicy(undefined); when setupTechnology is AGENT_BASED

Copy link
Contributor Author

@Omolola-Akinleye Omolola-Akinleye Apr 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless remove the useEffect and refactor, there will be a race condition to agent policy reset to agentless policy. This essentially affect the agent count and causes the bug.

  1. we set it in the effect that fetches the agentless policy. But this is incorrect logically, as there is no connection between this effect and setting up the actual agent policy. It doesn't do any updateAgentPolicy

Since the Agentless Feature Flag is enabled and Agentless option is selected by default. I set the currentAgentPolicy state to agentless policy.

I added the isDirty check; however, the race condition continues so my next thought is to keep track of the current agent policy. The currentAgentPolIcy state helps track the latest agent policy when user selects setupTechnogy which can be the following agentless, undefined, and agent-based, When the handleSetupTechnologyChange gets passed a new value, currentAgentPolicy has separate state isn't affected by the useEffect race condition which keep updating agent policy to agentless.

I suspect that we don't need this state at all and it should be enough to just set updateAgentPolicy(undefined); when setupTechnology is AGENT_BASED

I tried before calling updateAgentPolicy(undefined) and again useEffect kept resetting agent policy id to agentless. Adding the return fixes the issue.

@@ -226,10 +226,14 @@ export const CreatePackagePolicySinglePage: CreatePackagePolicyParams = ({
}
};

if (selectedPolicyTab === SelectedPolicyTab.NEW) {
setAgentCount(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we need to return after setting the agent count to 0. Otherwise, as agentPolicyId is available when switching from EXISTING to NEW it will be set to the agent count of the selected agent policy from the EXISTING tab

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made changes.

Copy link
Contributor

@seanrathier seanrathier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good, I have just a comment about using useEffect. When you have time read the React doc I linked.

setSetupTechnology(value);
setIsDirty(true);
};

useEffect(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these useEffects necessary? I don't see a need for useEffect here or below.

https://react.dev/learn/you-might-not-need-an-effect

Copy link
Contributor Author

@Omolola-Akinleye Omolola-Akinleye Apr 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Sean, thank you. I agree that using the useEffect should be avoided generally. I apply a fix because I believe this useEffect was placed before to check edge case. cc: @maxcold @kfirpeled @seanrathier For this bug useEffect is causing race condition for bugs. Do want to open a tech debt ticket or refactor in this PR? Refactoring can cause changes in architecture on how Agentless is setup and may take additional effort keep in mind.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Omolola-Akinleye If we can open. tech debt ticket that would be great and I can approve it after. Thanks for responding.

@Omolola-Akinleye Omolola-Akinleye enabled auto-merge (squash) April 29, 2024 22:26
@kibana-ci
Copy link
Collaborator

💛 Build succeeded, but was flaky

Failed CI Steps

Metrics [docs]

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
cloudSecurityPosture 452.1KB 452.2KB +75.0B
fleet 1.3MB 1.3MB +30.0B
total +105.0B

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

cc @Omolola-Akinleye

Copy link
Contributor

@juliaElastic juliaElastic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fleet change LGTM

@Omolola-Akinleye Omolola-Akinleye merged commit 310bba0 into elastic:main May 2, 2024
20 checks passed
@kibanamachine kibanamachine added the backport:skip This commit does not require backporting label May 2, 2024
yuliacech pushed a commit to yuliacech/kibana that referenced this pull request May 3, 2024
…c#181503)

## 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting bug Fixes for quality problems that affect the customer experience release_note:skip Skip the PR/issue when compiling release notes Team:Cloud Security Cloud Security team related Team:Fleet Team label for Observability Data Collection Fleet team v8.15.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants