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

[UII] Restrict agentless integrations to deployments with agentless enabled #194885

Merged
merged 27 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
3412a60
Move useAgentless to top-level hook
jen-huang Oct 1, 2024
91bec0a
Fix setup technology tests
jen-huang Oct 2, 2024
41702be
Remove agentless only packages and integrations from browse page if a…
jen-huang Oct 3, 2024
9e0dd25
Prevent agentless integrations from being installed when agentless is…
jen-huang Oct 3, 2024
2d41a3b
Merge remote-tracking branch 'upstream/main' into feat/hide-agentless…
jen-huang Oct 3, 2024
2cc3a0e
Merge remote-tracking branch 'upstream/main' into feat/hide-agentless…
jen-huang Oct 7, 2024
0b7b867
Fix tests by adding condition to getCloud
jen-huang Oct 7, 2024
894674e
Move filtering of deployment mode to separate fn
jen-huang Oct 7, 2024
f896293
Try to fix tests
jen-huang Oct 8, 2024
b46ba6c
Merge remote-tracking branch 'upstream/main' into feat/hide-agentless…
jen-huang Oct 11, 2024
5f058fb
Fix integrations list not being returned 🤠
jen-huang Oct 11, 2024
c036bfe
Fix return
jen-huang Oct 11, 2024
7e922ad
Revert "Move useAgentless to top-level hook"
jen-huang Oct 11, 2024
8aa91e0
Revert "Fix setup technology tests"
jen-huang Oct 11, 2024
0a89826
Try reverting file moves
jen-huang Oct 11, 2024
32691a6
Fix test and imports
jen-huang Oct 11, 2024
822a53e
Add unit tests for install path
jen-huang Oct 11, 2024
42a9af2
Merge remote-tracking branch 'upstream/main' into feat/hide-agentless…
jen-huang Oct 14, 2024
ee69611
Fix test
jen-huang Oct 14, 2024
c849bfc
Fix test attempt 2
jen-huang Oct 14, 2024
3f1dc66
Allow fleet config to be null
jen-huang Oct 14, 2024
44e7db5
Allow fleet config to be read from start dependencies
jen-huang Oct 14, 2024
7a565ae
Revert "Allow fleet config to be null"
jen-huang Oct 14, 2024
c0372a5
Fix plugin start mock
jen-huang Oct 14, 2024
12a439b
Merge branch 'main' into feat/hide-agentless-integrations
jen-huang Oct 15, 2024
8921098
Merge branch 'main' into feat/hide-agentless-integrations
jen-huang Oct 15, 2024
c7aa1f2
Merge branch 'main' into feat/hide-agentless-integrations
jen-huang Oct 15, 2024
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
287 changes: 287 additions & 0 deletions x-pack/plugins/fleet/common/services/agentless_policy_helper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,287 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type { RegistryPolicyTemplate } from '../types';

import {
isAgentlessIntegration,
getAgentlessAgentPolicyNameFromPackagePolicyName,
isOnlyAgentlessIntegration,
isOnlyAgentlessPolicyTemplate,
} from './agentless_policy_helper';

describe('agentless_policy_helper', () => {
describe('isAgentlessIntegration', () => {
it('should return true if packageInfo is defined and has at least one agentless integration', () => {
const packageInfo = {
policy_templates: [
{
name: 'template1',
title: 'Template 1',
description: '',
deployment_modes: {
default: {
enabled: true,
},
agentless: {
enabled: true,
},
},
},
{
name: 'template2',
title: 'Template 2',
description: '',
deployment_modes: {
default: {
enabled: true,
},
},
},
] as RegistryPolicyTemplate[],
};

const result = isAgentlessIntegration(packageInfo);

expect(result).toBe(true);
});

it('should return false if packageInfo is defined but does not have agentless integrations', () => {
const packageInfo = {
policy_templates: [
{
name: 'template1',
title: 'Template 1',
description: '',
deployment_modes: {
default: {
enabled: true,
},
agentless: {
enabled: false,
},
},
},
{
name: 'template2',
title: 'Template 2',
description: '',
deployment_modes: {
default: {
enabled: false,
},
agentless: {
enabled: false,
},
},
},
] as RegistryPolicyTemplate[],
};

const result = isAgentlessIntegration(packageInfo);

expect(result).toBe(false);
});

it('should return false if packageInfo has no policy templates', () => {
const packageInfo = {
policy_templates: [],
};

const result = isAgentlessIntegration(packageInfo);

expect(result).toBe(false);
});

it('should return false if packageInfo is undefined', () => {
const packageInfo = undefined;

const result = isAgentlessIntegration(packageInfo);

expect(result).toBe(false);
});
});

describe('getAgentlessAgentPolicyNameFromPackagePolicyName', () => {
it('should return the agentless agent policy name based on the package policy name', () => {
const packagePolicyName = 'example-package-policy';

const result = getAgentlessAgentPolicyNameFromPackagePolicyName(packagePolicyName);

expect(result).toBe('Agentless policy for example-package-policy');
});
});

describe('isOnlyAgentlessIntegration', () => {
it('should return true if packageInfo is defined and has only agentless integration', () => {
const packageInfo = {
policy_templates: [
{
name: 'template1',
title: 'Template 1',
description: '',
deployment_modes: {
default: {
enabled: false,
},
agentless: {
enabled: true,
},
},
},
{
name: 'template2',
title: 'Template 2',
description: '',
deployment_modes: {
agentless: {
enabled: true,
},
},
},
] as RegistryPolicyTemplate[],
};

const result = isOnlyAgentlessIntegration(packageInfo);

expect(result).toBe(true);
});

it('should return false if packageInfo is defined but has other deployment types', () => {
const packageInfo = {
policy_templates: [
{
name: 'template1',
title: 'Template 1',
description: '',
deployment_modes: {
default: {
enabled: true,
},
agentless: {
enabled: true,
},
},
},
{
name: 'template2',
title: 'Template 2',
description: '',
deployment_modes: {
default: {
enabled: true,
},
},
},
] as RegistryPolicyTemplate[],
};

const result = isOnlyAgentlessIntegration(packageInfo);

expect(result).toBe(false);
});

it('should return false if packageInfo has no policy templates', () => {
const packageInfo = {
policy_templates: [],
};

const result = isOnlyAgentlessIntegration(packageInfo);

expect(result).toBe(false);
});

it('should return false if packageInfo is undefined', () => {
const packageInfo = undefined;

const result = isOnlyAgentlessIntegration(packageInfo);

expect(result).toBe(false);
});
});

describe('isOnlyAgentlessPolicyTemplate', () => {
it('should return true if the policy template is only agentless', () => {
const policyTemplate = {
name: 'template1',
title: 'Template 1',
description: '',
deployment_modes: {
default: {
enabled: false,
},
agentless: {
enabled: true,
},
},
};
const policyTemplate2 = {
name: 'template2',
title: 'Template 2',
description: '',
deployment_modes: {
agentless: {
enabled: true,
},
},
};

const result = isOnlyAgentlessPolicyTemplate(policyTemplate);
const result2 = isOnlyAgentlessPolicyTemplate(policyTemplate2);

expect(result).toBe(true);
expect(result2).toBe(true);
});

it('should return false if the policy template has other deployment types', () => {
const policyTemplate = {
name: 'template1',
title: 'Template 1',
description: '',
deployment_modes: {
default: {
enabled: true,
},
agentless: {
enabled: true,
},
},
};
const policyTemplate2 = {
name: 'template2',
title: 'Template 2',
description: '',
deployment_modes: {
default: {
enabled: true,
},
agentless: {
enabled: false,
},
},
};

const result = isOnlyAgentlessPolicyTemplate(policyTemplate);
const result2 = isOnlyAgentlessPolicyTemplate(policyTemplate2);

expect(result).toBe(false);
expect(result2).toBe(false);
});

it('should return false if the policy template has no deployment modes', () => {
const policyTemplate = {
name: 'template1',
title: 'Template 1',
description: '',
};

const result = isOnlyAgentlessPolicyTemplate(policyTemplate);

expect(result).toBe(false);
});
});
});
41 changes: 41 additions & 0 deletions x-pack/plugins/fleet/common/services/agentless_policy_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,47 @@
* 2.0.
*/

import type { PackageInfo, RegistryPolicyTemplate } from '../types';

export const isAgentlessIntegration = (
Copy link
Contributor

Choose a reason for hiding this comment

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

It recently came up that we don't have a way to specify which policy template to check. For example, the CSPM integration has multiple policy templates (KSPM, CSPM, CNVM) however only CSPM supports agentless.

Would it be possible to have an optional parameter that we can pass cspm and check individual policy templates?

packageInfo: Pick<PackageInfo, 'policy_templates'> | undefined
) => {
if (
packageInfo?.policy_templates &&
packageInfo?.policy_templates.length > 0 &&
!!packageInfo?.policy_templates.find(
(policyTemplate) => policyTemplate?.deployment_modes?.agentless.enabled === true
)
) {
return true;
}
return false;
};

export const getAgentlessAgentPolicyNameFromPackagePolicyName = (packagePolicyName: string) => {
return `Agentless policy for ${packagePolicyName}`;
};

export const isOnlyAgentlessIntegration = (
packageInfo: Pick<PackageInfo, 'policy_templates'> | undefined
) => {
if (
packageInfo?.policy_templates &&
packageInfo?.policy_templates.length > 0 &&
packageInfo?.policy_templates.every((policyTemplate) =>
isOnlyAgentlessPolicyTemplate(policyTemplate)
)
) {
return true;
}
return false;
};

export const isOnlyAgentlessPolicyTemplate = (policyTemplate: RegistryPolicyTemplate) => {
return Boolean(
policyTemplate.deployment_modes &&
policyTemplate.deployment_modes.agentless.enabled === true &&
(!policyTemplate.deployment_modes.default ||
policyTemplate.deployment_modes.default.enabled === false)
);
};
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment is not for you to fix now but rather something we should discuss. This hook is starting to get complicated. I see the hook being used in many places and we are prop drilling the results from this to components and child components.

We should consider using a React.Context for Agentless and the components and custom extensions can pull this information from the Context.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import { SetupTechnology } from '../../../../../types';
import { sendGetOneAgentPolicy, useStartServices } from '../../../../../hooks';
import { SelectedPolicyTab } from '../../components';
import { AGENTLESS_POLICY_ID } from '../../../../../../../../common/constants';
import { getAgentlessAgentPolicyNameFromPackagePolicyName } from '../../../../../../../../common/services/agentless_policy_helper';
import {
isAgentlessIntegration as isAgentlessIntegrationFn,
getAgentlessAgentPolicyNameFromPackagePolicyName,
} from '../../../../../../../../common/services/agentless_policy_helper';

export const useAgentless = () => {
const config = useConfig();
Expand All @@ -45,14 +48,7 @@ export const useAgentless = () => {

// When an integration has at least a policy template enabled for agentless
const isAgentlessIntegration = (packageInfo: PackageInfo | undefined) => {
if (
isAgentlessEnabled &&
packageInfo?.policy_templates &&
packageInfo?.policy_templates.length > 0 &&
!!packageInfo?.policy_templates.find(
(policyTemplate) => policyTemplate?.deployment_modes?.agentless.enabled === true
)
) {
if (isAgentlessEnabled && isAgentlessIntegrationFn(packageInfo)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: the fact that the rename was needed shows that probably isAgentlessIntegration in this file does too much. I wonder if it should check isAgentlessEnabled inside. I'd rather move this check to the consumers but I think it's out of the scope of this PR. cc @seanrathier @opauloh

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with @max that isAgentlessIntegration in this hook does not do much, however, I am trying to find another place where the helper function is imported (other than tests) and cannot find one. I think it would be better to keep it on the hook.

return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ export function usePackagePolicySteps({
setNewAgentPolicy,
updateAgentPolicies,
setSelectedPolicyTab,
packageInfo,
packagePolicy,
isEditPage: true,
agentPolicies,
Expand Down
Loading