Skip to content

Commit

Permalink
[Fleet] Filter integrations/packages list shown depending on the `pol…
Browse files Browse the repository at this point in the history
…icy_templates_behavior` field (#200605)

## Summary
Closes #198145

Add support to filter the tiles shown in the integrations UI as well as
the packages shown in the global search provider depending on the
`policy_templates_behaviour` field introduced in
elastic/package-spec#802.

If this new field is not present in the package manifest, the same
behavior is kept. Therefore, it is shown a tile for the package itself
plus a tile for each policy template defined in the manifest.


Tested using a custom Elastic Package Registry with some packages
defining this new `policy_templates_behavior` via the key:
```yaml
xpack.fleet.registryUrl: http://localhost:8080
```

### Screenshots

Checked option "Elastic Agent only" in the integrations UI to avoid
tutorials based on Beats.

Example with `azure_metrics` package in the Integrations UI:
- `policy_templates_behavior: all`
![All policy
templates](https://github.com/user-attachments/assets/907618e3-f2db-44df-b1ac-3965b1978b2c)

- `policy_templates_behavior: combined_policy`
![Just combined
policy](https://github.com/user-attachments/assets/77293616-8125-4d01-81f3-b3f17135ca49)

- `policy_templates_behavior: individual_policies`
![Just individual policy
templates](https://github.com/user-attachments/assets/b68ad474-8aac-464b-9946-9ae6104dd2ae)


Example in the Global Search with `azure_metrics` package and `combined
policy` behavior:
![global search with azure metrics
package](https://github.com/user-attachments/assets/e70315b7-d303-4b32-aa9e-8e1e9b056239)


### Checklist

Check the PR satisfies following conditions. 

Reviewers should verify this PR satisfies this list as well.

- [ ] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)
- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [ ] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
- [ ] This was checked for breaking HTTP API changes, and any breaking
changes have been approved by the breaking-change committee. The
`release_note:breaking` label should be applied in these situations.
- [ ] [Flaky Test
Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was
used on any tests changed
- [ ] The PR description includes the appropriate Release Notes section,
and the correct `release_node:*` label is applied per the
[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

### Identify risks

Does this PR introduce any risks? For example, consider risks like hard
to test bugs, performance regression, potential of data loss.

Describe the risk, its severity, and mitigation for each identified
risk. Invite stakeholders and evaluate how to proceed before merging.

- [ ]
  • Loading branch information
mrodm authored Nov 19, 2024
1 parent dde4271 commit 8352b86
Show file tree
Hide file tree
Showing 8 changed files with 385 additions and 26 deletions.
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/common/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export {
isIntegrationPolicyTemplate,
getNormalizedInputs,
getNormalizedDataStreams,
filterPolicyTemplatesTiles,
} from './policy_template';
export { doesPackageHaveIntegrations } from './packages_with_integrations';
export type {
Expand Down
124 changes: 124 additions & 0 deletions x-pack/plugins/fleet/common/services/policy_template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import type {
RegistryPolicyIntegrationTemplate,
PackageInfo,
RegistryVarType,
PackageListItem,
} from '../types';

import {
isInputOnlyPolicyTemplate,
isIntegrationPolicyTemplate,
getNormalizedInputs,
getNormalizedDataStreams,
filterPolicyTemplatesTiles,
} from './policy_template';

describe('isInputOnlyPolicyTemplate', () => {
Expand Down Expand Up @@ -280,3 +282,125 @@ describe('getNormalizedDataStreams', () => {
expect(result?.[0].streams?.[0]?.vars).toEqual([datasetVar]);
});
});

describe('filterPolicyTemplatesTiles', () => {
const topPackagePolicy: PackageListItem = {
id: 'nginx',
integration: 'nginx',
title: 'Nginx',
name: 'nginx',
version: '0.0.1',
status: 'not_installed',
};

const childPolicyTemplates: PackageListItem[] = [
{
id: 'nginx-template1',
integration: 'nginx-template-1',
title: 'Nginx Template 1',
name: 'nginx',
version: '0.0.1',
status: 'not_installed',
},
{
id: 'nginx-template2',
integration: 'nginx-template-2',
title: 'Nginx Template 2',
name: 'nginx',
version: '0.0.1',
status: 'not_installed',
},
];
it('should return all tiles as undefined value', () => {
expect(filterPolicyTemplatesTiles(undefined, topPackagePolicy, childPolicyTemplates)).toEqual([
{
id: 'nginx',
integration: 'nginx',
title: 'Nginx',
name: 'nginx',
version: '0.0.1',
status: 'not_installed',
},
{
id: 'nginx-template1',
integration: 'nginx-template-1',
title: 'Nginx Template 1',
name: 'nginx',
version: '0.0.1',
status: 'not_installed',
},
{
id: 'nginx-template2',
integration: 'nginx-template-2',
title: 'Nginx Template 2',
name: 'nginx',
version: '0.0.1',
status: 'not_installed',
},
]);
});
it('should return all tiles', () => {
expect(filterPolicyTemplatesTiles('all', topPackagePolicy, childPolicyTemplates)).toEqual([
{
id: 'nginx',
integration: 'nginx',
title: 'Nginx',
name: 'nginx',
version: '0.0.1',
status: 'not_installed',
},
{
id: 'nginx-template1',
integration: 'nginx-template-1',
title: 'Nginx Template 1',
name: 'nginx',
version: '0.0.1',
status: 'not_installed',
},
{
id: 'nginx-template2',
integration: 'nginx-template-2',
title: 'Nginx Template 2',
name: 'nginx',
version: '0.0.1',
status: 'not_installed',
},
]);
});
it('should return just the combined policy tile', () => {
expect(
filterPolicyTemplatesTiles('combined_policy', topPackagePolicy, childPolicyTemplates)
).toEqual([
{
id: 'nginx',
integration: 'nginx',
title: 'Nginx',
name: 'nginx',
version: '0.0.1',
status: 'not_installed',
},
]);
});
it('should return just the individual policies (tiles)', () => {
expect(
filterPolicyTemplatesTiles('individual_policies', topPackagePolicy, childPolicyTemplates)
).toEqual([
{
id: 'nginx-template1',
integration: 'nginx-template-1',
title: 'Nginx Template 1',
name: 'nginx',
version: '0.0.1',
status: 'not_installed',
},
{
id: 'nginx-template2',
integration: 'nginx-template-2',
title: 'Nginx Template 2',
name: 'nginx',
version: '0.0.1',
status: 'not_installed',
},
]);
});
});
25 changes: 25 additions & 0 deletions x-pack/plugins/fleet/common/services/policy_template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export function packageHasNoPolicyTemplates(packageInfo: PackageInfo): boolean {
)
);
}

export function isInputOnlyPolicyTemplate(
policyTemplate: RegistryPolicyTemplate
): policyTemplate is RegistryPolicyInputOnlyTemplate {
Expand Down Expand Up @@ -142,3 +143,27 @@ const createDefaultDatasetName = (
packageInfo: { name: string },
policyTemplate: { name: string }
): string => packageInfo.name + '.' + policyTemplate.name;

export function filterPolicyTemplatesTiles<T>(
templatesBehavior: string | undefined,
packagePolicy: T,
packagePolicyTemplates: T[]
): T[] {
switch (templatesBehavior || 'all') {
case 'combined_policy':
return [packagePolicy];
case 'individual_policies':
return [
...(packagePolicyTemplates && packagePolicyTemplates.length > 1
? packagePolicyTemplates
: []),
];
default:
return [
packagePolicy,
...(packagePolicyTemplates && packagePolicyTemplates.length > 1
? packagePolicyTemplates
: []),
];
}
}
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/common/types/models/epm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ export type RegistrySearchResult = Pick<
| 'icons'
| 'internal'
| 'data_streams'
| 'policy_templates_behavior'
| 'policy_templates'
| 'categories'
>;
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/common/types/models/package_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface PackageSpecManifest {
conditions?: PackageSpecConditions;
icons?: PackageSpecIcon[];
screenshots?: PackageSpecScreenshot[];
policy_templates_behavior?: 'all' | 'combined_policy' | 'individual_policies';
policy_templates?: RegistryPolicyTemplate[];
vars?: RegistryVarsEntry[];
owner: { github?: string; type?: 'elastic' | 'partner' | 'community' };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { doesPackageHaveIntegrations, ExperimentalFeaturesService } from '../../
import {
isInputOnlyPolicyTemplate,
isIntegrationPolicyTemplate,
filterPolicyTemplatesTiles,
} from '../../../../../../../../common/services';

import {
Expand Down Expand Up @@ -83,30 +84,33 @@ const packageListToIntegrationsList = (packages: PackageList): PackageList => {
categories: getAllCategoriesFromIntegrations(pkg),
};

return [
...acc,
const integrationsPolicyTemplates = doesPackageHaveIntegrations(pkg)
? policyTemplates.map((policyTemplate) => {
const { name, title, description, icons } = policyTemplate;

const categories =
isIntegrationPolicyTemplate(policyTemplate) && policyTemplate.categories
? policyTemplate.categories
: [];
const allCategories = [...topCategories, ...categories];
return {
...restOfPackage,
id: `${restOfPackage.id}-${name}`,
integration: name,
title,
description,
icons: icons || restOfPackage.icons,
categories: uniq(allCategories),
};
})
: [];

const tiles = filterPolicyTemplatesTiles<PackageListItem>(
pkg.policy_templates_behavior,
topPackage,
...(doesPackageHaveIntegrations(pkg)
? policyTemplates.map((policyTemplate) => {
const { name, title, description, icons } = policyTemplate;

const categories =
isIntegrationPolicyTemplate(policyTemplate) && policyTemplate.categories
? policyTemplate.categories
: [];
const allCategories = [...topCategories, ...categories];
return {
...restOfPackage,
id: `${restOfPackage.id}-${name}`,
integration: name,
title,
description,
icons: icons || restOfPackage.icons,
categories: uniq(allCategories),
};
})
: []),
];
integrationsPolicyTemplates
);
return [...acc, ...tiles];
}, []);
};

Expand Down
Loading

0 comments on commit 8352b86

Please sign in to comment.