-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
Changes from 17 commits
3412a60
91bec0a
41702be
9e0dd25
2d41a3b
2cc3a0e
0b7b867
894674e
f896293
b46ba6c
5f058fb
c036bfe
7e922ad
8aa91e0
0a89826
32691a6
822a53e
42a9af2
ee69611
c849bfc
3f1dc66
44e7db5
7a565ae
c0372a5
12a439b
8921098
c7aa1f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
|
@@ -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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: the fact that the rename was needed shows that probably There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with @max that |
||
return true; | ||
} | ||
return false; | ||
|
There was a problem hiding this comment.
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?