From 6408492f58152447770108277e9acfcbbbd2723a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20=C3=81brah=C3=A1m?= Date: Fri, 11 Oct 2024 12:10:55 +0200 Subject: [PATCH] backfill function for set_extended_host_information --- ...v15_advanced_package_policy_fields.test.ts | 137 +++++++++--------- .../v15_advanced_package_policy_fields.ts | 6 + 2 files changed, 76 insertions(+), 67 deletions(-) diff --git a/x-pack/plugins/fleet/server/saved_objects/model_versions/security_solution/v15_advanced_package_policy_fields.test.ts b/x-pack/plugins/fleet/server/saved_objects/model_versions/security_solution/v15_advanced_package_policy_fields.test.ts index 4e82e445da2b3..93dd331f5452c 100644 --- a/x-pack/plugins/fleet/server/saved_objects/model_versions/security_solution/v15_advanced_package_policy_fields.test.ts +++ b/x-pack/plugins/fleet/server/saved_objects/model_versions/security_solution/v15_advanced_package_policy_fields.test.ts @@ -8,6 +8,7 @@ import type { SavedObject } from '@kbn/core-saved-objects-api-server'; import type { ModelVersionTestMigrator } from '@kbn/core-test-helpers-model-versions'; import { createModelVersionTestMigrator } from '@kbn/core-test-helpers-model-versions'; +import { set } from '@kbn/safer-lodash-set'; import { getSavedObjectTypes } from '../..'; @@ -64,77 +65,79 @@ describe('Defend integration advanced policy fields v8.16.0', () => { }; }); - describe('when updating to model version 15', () => { - describe('backfilling `aggregate_process` with `false`', () => { - it('should backfill when there are no advanced options yet', () => { - const migratedPolicyConfigSO = migrator.migrate({ - document: policyConfigSO, - fromVersion: 14, - toVersion: 15, - }); - - const migratedPolicyConfig = getConfig(migratedPolicyConfigSO); - - expectConfigToHave(migratedPolicyConfig, 'advanced.events.aggregate_process', false); - }); - - it('should backfill without modifying other advanced options', () => { - const policyConfig = getConfig(policyConfigSO); - policyConfig.windows.advanced = { cheese: 'brie' }; - policyConfig.mac.advanced = { cheese: 'maasdam' }; - policyConfig.linux.advanced = { cheese: 'camambert' }; - - const migratedPolicyConfigSO = migrator.migrate({ - document: policyConfigSO, - fromVersion: 14, - toVersion: 15, - }); - - const migratedPolicyConfig = getConfig(migratedPolicyConfigSO); - - expectConfigToHave(migratedPolicyConfig, 'advanced.events.aggregate_process', false); - expect(migratedPolicyConfig.windows.advanced.cheese).toBe('brie'); - expect(migratedPolicyConfig.mac.advanced.cheese).toBe('maasdam'); - expect(migratedPolicyConfig.linux.advanced.cheese).toBe('camambert'); - }); + /** Builds object key paths for all parent objects + * + * @param path e.g. `advanced.events.optionName` + * @returns e.g. ['advanced', 'advanced.events'] + */ + const getParentObjectKeyPaths = (path: string): string[] => + path + .split('.') // ['advanced', 'events', 'optionName'] + .slice(0, -1) // ['advanced', 'events'] + .map((parentObject) => path.match(`^.*${parentObject}`)![0]); // ['advanced', 'advanced.events'] - it('should backfill without modifying other event options', () => { - const policyConfig = getConfig(policyConfigSO); - policyConfig.windows.advanced = { events: { cheese: 'brie' } }; - policyConfig.mac.advanced = { events: { cheese: 'maasdam' } }; - policyConfig.linux.advanced = { events: { cheese: 'camambert' } }; - - const migratedPolicyConfigSO = migrator.migrate({ - document: policyConfigSO, - fromVersion: 14, - toVersion: 15, + describe('when updating to model version 15', () => { + describe.each` + name | path | backfill + ${'aggregate_process'} | ${'advanced.events.aggregate_process'} | ${false} + ${'set_extended_host_information'} | ${'advanced.set_extended_host_information'} | ${true} + `( + 'backfilling `$name` with `$backfill`', + ({ path, backfill }: { path: string; backfill: boolean }) => { + it('should backfill when there are no advanced options yet', () => { + const migratedPolicyConfigSO = migrator.migrate({ + document: policyConfigSO, + fromVersion: 14, + toVersion: 15, + }); + + const migratedPolicyConfig = getConfig(migratedPolicyConfigSO); + + expectConfigToHave(migratedPolicyConfig, path, backfill); }); - const migratedPolicyConfig = getConfig(migratedPolicyConfigSO); - - expectConfigToHave(migratedPolicyConfig, 'advanced.events.aggregate_process', false); - expect(migratedPolicyConfig.windows.advanced.events.cheese).toBe('brie'); - expect(migratedPolicyConfig.mac.advanced.events.cheese).toBe('maasdam'); - expect(migratedPolicyConfig.linux.advanced.events.cheese).toBe('camambert'); - }); - - it('should not backfill if field is already present', () => { - const policyConfig = getConfig(policyConfigSO); - policyConfig.windows.advanced = { events: { aggregate_process: true } }; - policyConfig.mac.advanced = { events: { aggregate_process: true } }; - policyConfig.linux.advanced = { events: { aggregate_process: true } }; - - const migratedPolicyConfigSO = migrator.migrate({ - document: policyConfigSO, - fromVersion: 14, - toVersion: 15, + it.each(getParentObjectKeyPaths(path))( + 'should backfill without modifying other options in parent object `%s`', + (parentObjectKeyPath) => { + const policyConfig = getConfig(policyConfigSO); + const dummyField = `${parentObjectKeyPath}.cheese`; + set(policyConfig.windows, dummyField, 'brie'); + set(policyConfig.mac, dummyField, 'maasdam'); + set(policyConfig.linux, dummyField, 'camambert'); + + const migratedPolicyConfigSO = migrator.migrate({ + document: policyConfigSO, + fromVersion: 14, + toVersion: 15, + }); + + const migratedPolicyConfig = getConfig(migratedPolicyConfigSO); + + expectConfigToHave(migratedPolicyConfig, path, backfill); + expect(migratedPolicyConfig.windows).toHaveProperty(dummyField, 'brie'); + expect(migratedPolicyConfig.mac).toHaveProperty(dummyField, 'maasdam'); + expect(migratedPolicyConfig.linux).toHaveProperty(dummyField, 'camambert'); + } + ); + + it('should not backfill if field is already present', () => { + const policyConfig = getConfig(policyConfigSO); + set(policyConfig.windows, path, !backfill); + set(policyConfig.mac, path, !backfill); + set(policyConfig.linux, path, !backfill); + + const migratedPolicyConfigSO = migrator.migrate({ + document: policyConfigSO, + fromVersion: 14, + toVersion: 15, + }); + + const migratedPolicyConfig = getConfig(migratedPolicyConfigSO); + + expectConfigToHave(migratedPolicyConfig, path, !backfill); }); - - const migratedPolicyConfig = getConfig(migratedPolicyConfigSO); - - expectConfigToHave(migratedPolicyConfig, 'advanced.events.aggregate_process', true); - }); - }); + } + ); }); const getConfig = (so: SavedObject) => diff --git a/x-pack/plugins/fleet/server/saved_objects/model_versions/security_solution/v15_advanced_package_policy_fields.ts b/x-pack/plugins/fleet/server/saved_objects/model_versions/security_solution/v15_advanced_package_policy_fields.ts index bc3671f8e6d8b..ff86390f9dcea 100644 --- a/x-pack/plugins/fleet/server/saved_objects/model_versions/security_solution/v15_advanced_package_policy_fields.ts +++ b/x-pack/plugins/fleet/server/saved_objects/model_versions/security_solution/v15_advanced_package_policy_fields.ts @@ -28,7 +28,9 @@ export const packagePolicyV15AdvancedFieldsForEndpointV816: SavedObjectModelData const policy = input.config.policy.value; policy.windows.advanced = { + set_extended_host_information: true, ...policy.windows.advanced, + events: { aggregate_process: false, ...policy.windows.advanced?.events, @@ -36,7 +38,9 @@ export const packagePolicyV15AdvancedFieldsForEndpointV816: SavedObjectModelData }; policy.mac.advanced = { + set_extended_host_information: true, ...policy.mac.advanced, + events: { aggregate_process: false, ...policy.mac.advanced?.events, @@ -44,7 +48,9 @@ export const packagePolicyV15AdvancedFieldsForEndpointV816: SavedObjectModelData }; policy.linux.advanced = { + set_extended_host_information: true, ...policy.linux.advanced, + events: { aggregate_process: false, ...policy.linux.advanced?.events,