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

[8.x] [UII] Restrict agentless integrations to deployments with agentless enabled (#194885) #196459

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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 = (
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)
);
};
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)) {
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
Loading