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] Allow force install package policy to agentless agent policy #173553

Merged
merged 4 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { SelectedPolicyTab } from '../../components';
import { useOnSaveNavigate } from '../../hooks';
import { prepareInputPackagePolicyDataset } from '../../services/prepare_input_pkg_policy_dataset';
import { getCloudFormationPropsFromPackagePolicy } from '../../../../../services';
import { AGENTLESS_POLICY_ID } from './setup_technology';

async function createAgentPolicy({
packagePolicy,
Expand Down Expand Up @@ -298,12 +299,15 @@ export function useOnSubmit({
}
}

const agentPolicyIdToSave = createdPolicy?.id ?? packagePolicy.policy_id;
const forceInstall = force || agentPolicyIdToSave === AGENTLESS_POLICY_ID;
Copy link
Member

Choose a reason for hiding this comment

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

maybe worth checking we are in serverless too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

potentially there will be agentless on ESS, the work kicked off in parallel, but I agree that it makes sense to be on the safer side for now and check for serverless, or at least that it's not on-prem. will look into it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nchaulet updated the pr including the check for serverless env and agentless FF being enabled


setFormState('LOADING');
// passing pkgPolicy with policy_id here as setPackagePolicy doesn't propagate immediately
const { error, data } = await savePackagePolicy({
...packagePolicy,
policy_id: createdPolicy?.id ?? packagePolicy.policy_id,
force,
policy_id: agentPolicyIdToSave,
force: forceInstall,
});

const hasAzureArmTemplate = data?.item
Expand Down Expand Up @@ -373,9 +377,11 @@ export function useOnSubmit({
} else {
if (isVerificationError(error)) {
setFormState('VALID'); // don't show the add agent modal
const forceInstall = await confirmForceInstall(packagePolicy.package!);
const forceInstallUnverifiedIntegration = await confirmForceInstall(
packagePolicy.package!
Copy link
Contributor

Choose a reason for hiding this comment

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

wouldn't it be safer to add the packagePolicy.package to the if statement instead of using non null assertion?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

in this line only the variable name changed, the rest is the same as it was. As we are not the code owner, I wouldn't touch what is not broken :)

);

if (forceInstall) {
if (forceInstallUnverifiedIntegration) {
// skip creating the agent policy because it will have already been successfully created
onSubmit({ overrideCreatedAgentPolicy: createdPolicy, force: true });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { SetupTechnology } from '../../../../../types';
import { sendGetOneAgentPolicy, useStartServices } from '../../../../../hooks';
import { SelectedPolicyTab } from '../../components';

const AGENTLESS_POLICY_ID = 'agentless';
export const AGENTLESS_POLICY_ID = 'agentless';

export function useSetupTechnology({
updateNewAgentPolicy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import {
sendCreatePackagePolicy,
sendCreateAgentPolicy,
sendGetAgentStatus,
sendGetOneAgentPolicy,
useIntraAppState,
useStartServices,
useGetAgentPolicies,
useGetPackageInfoByKeyQuery,
} from '../../../../hooks';

Expand Down Expand Up @@ -95,6 +97,7 @@ jest.mock('react-router-dom', () => ({
}));

import { CreatePackagePolicySinglePage } from '.';
import { AGENTLESS_POLICY_ID } from './hooks/setup_technology';

// mock console.debug to prevent noisy logs from console.debugs in ./index.tsx
let consoleDebugMock: any;
Expand Down Expand Up @@ -290,6 +293,7 @@ describe('when on the package policy create page', () => {
expect(sendCreatePackagePolicy as jest.MockedFunction<any>).toHaveBeenCalledWith({
...newPackagePolicy,
policy_id: 'agent-policy-1',
force: false,
});
expect(sendCreateAgentPolicy as jest.MockedFunction<any>).not.toHaveBeenCalled();

Expand Down Expand Up @@ -441,6 +445,7 @@ describe('when on the package policy create page', () => {
expect(sendCreatePackagePolicy as jest.MockedFunction<any>).toHaveBeenCalledWith({
...newPackagePolicy,
policy_id: 'agent-policy-2',
force: false,
});

await waitFor(() => {
Expand Down Expand Up @@ -503,6 +508,7 @@ describe('when on the package policy create page', () => {
expect(sendCreatePackagePolicy as jest.MockedFunction<any>).toHaveBeenCalledWith({
...newPackagePolicy,
policy_id: 'agent-policy-1',
force: false,
});

await waitFor(() => {
Expand Down Expand Up @@ -573,10 +579,52 @@ describe('when on the package policy create page', () => {
],
},
],
force: false,
});
});
});
});

describe('with agentless policy available', () => {
beforeEach(async () => {
(sendGetOneAgentPolicy as jest.MockedFunction<any>).mockResolvedValue({
data: { item: { id: AGENTLESS_POLICY_ID, name: 'Agentless CSPM', namespace: 'default' } },
});
(useGetAgentPolicies as jest.MockedFunction<any>).mockReturnValue({
data: {
items: [{ id: AGENTLESS_POLICY_ID, name: 'Agentless CSPM', namespace: 'default' }],
},
error: undefined,
isLoading: false,
resendRequest: jest.fn(),
});

await act(async () => {
render();
});
});

test('should force create package policy', async () => {
await act(async () => {
fireEvent.click(renderResult.getByText('Existing hosts')!);
});

await act(async () => {
fireEvent.click(renderResult.getByText(/Save and continue/).closest('button')!);
});

expect(sendCreateAgentPolicy as jest.MockedFunction<any>).not.toHaveBeenCalled();
expect(sendCreatePackagePolicy as jest.MockedFunction<any>).toHaveBeenCalledWith({
...newPackagePolicy,
force: true,
policy_id: AGENTLESS_POLICY_ID,
});

await waitFor(() => {
expect(renderResult.getByText('Nginx integration added')).toBeInTheDocument();
});
});
});
});
});

Expand Down