From ad61b9d1010c13dd9c6c3faa5f848ce6d35a9e76 Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Fri, 22 Nov 2024 12:00:44 -0600 Subject: [PATCH] [Fleet] Fix settings response when space awareness enabled (#201416) --- .../fleet/server/services/settings.test.ts | 36 ++++++++++++++++ .../plugins/fleet/server/services/settings.ts | 42 +++++++++++-------- 2 files changed, 60 insertions(+), 18 deletions(-) diff --git a/x-pack/plugins/fleet/server/services/settings.test.ts b/x-pack/plugins/fleet/server/services/settings.test.ts index f88e735dfcb69..9a75dc72abf3e 100644 --- a/x-pack/plugins/fleet/server/services/settings.test.ts +++ b/x-pack/plugins/fleet/server/services/settings.test.ts @@ -143,6 +143,42 @@ describe('getSettings', () => { await getSettings(soClient); }); + + it('should handle null values for space awareness migration fields', async () => { + const soClient = savedObjectsClientMock.create(); + + soClient.find.mockResolvedValueOnce({ + saved_objects: [ + { + id: GLOBAL_SETTINGS_ID, + attributes: { + use_space_awareness_migration_status: null, + use_space_awareness_migration_started_at: null, + }, + references: [], + type: GLOBAL_SETTINGS_SAVED_OBJECT_TYPE, + score: 0, + }, + ], + page: 1, + per_page: 10, + total: 1, + }); + + const settings = await getSettings(soClient); + expect(settings).toEqual({ + delete_unenrolled_agents: undefined, + has_seen_add_data_notice: undefined, + id: 'fleet-default-settings', + output_secret_storage_requirements_met: undefined, + preconfigured_fields: [], + prerelease_integrations_enabled: undefined, + secret_storage_requirements_met: undefined, + use_space_awareness_migration_started_at: undefined, + use_space_awareness_migration_status: undefined, + version: undefined, + }); + }); }); describe('saveSettings', () => { diff --git a/x-pack/plugins/fleet/server/services/settings.ts b/x-pack/plugins/fleet/server/services/settings.ts index 3288ec1090e41..5e1948d5d3797 100644 --- a/x-pack/plugins/fleet/server/services/settings.ts +++ b/x-pack/plugins/fleet/server/services/settings.ts @@ -6,7 +6,11 @@ */ import Boom from '@hapi/boom'; -import type { SavedObjectsClientContract, SavedObjectsUpdateOptions } from '@kbn/core/server'; +import type { + SavedObject, + SavedObjectsClientContract, + SavedObjectsUpdateOptions, +} from '@kbn/core/server'; import { omit } from 'lodash'; import { GLOBAL_SETTINGS_SAVED_OBJECT_TYPE, GLOBAL_SETTINGS_ID } from '../../common/constants'; @@ -18,21 +22,7 @@ import { DeleteUnenrolledAgentsPreconfiguredError } from '../errors'; import { appContextService } from './app_context'; import { auditLoggingService } from './audit_logging'; -export async function getSettings(soClient: SavedObjectsClientContract): Promise { - const res = await soClient.find({ - type: GLOBAL_SETTINGS_SAVED_OBJECT_TYPE, - }); - auditLoggingService.writeCustomSoAuditLog({ - action: 'get', - id: GLOBAL_SETTINGS_ID, - savedObjectType: GLOBAL_SETTINGS_SAVED_OBJECT_TYPE, - }); - - if (res.total === 0) { - throw Boom.notFound('Global settings not found'); - } - const settingsSo = res.saved_objects[0]; - +function mapSettingsSO(settingsSo: SavedObject): Settings { return { id: settingsSo.id, version: settingsSo.version, @@ -42,14 +32,30 @@ export async function getSettings(soClient: SavedObjectsClientContract): Promise has_seen_add_data_notice: settingsSo.attributes.has_seen_add_data_notice, prerelease_integrations_enabled: settingsSo.attributes.prerelease_integrations_enabled, use_space_awareness_migration_status: - settingsSo.attributes.use_space_awareness_migration_status, + settingsSo.attributes.use_space_awareness_migration_status ?? undefined, use_space_awareness_migration_started_at: - settingsSo.attributes.use_space_awareness_migration_started_at, + settingsSo.attributes.use_space_awareness_migration_started_at ?? undefined, preconfigured_fields: getConfigFleetServerHosts() ? ['fleet_server_hosts'] : [], delete_unenrolled_agents: settingsSo.attributes.delete_unenrolled_agents, }; } +export async function getSettings(soClient: SavedObjectsClientContract): Promise { + const res = await soClient.find({ + type: GLOBAL_SETTINGS_SAVED_OBJECT_TYPE, + }); + auditLoggingService.writeCustomSoAuditLog({ + action: 'get', + id: GLOBAL_SETTINGS_ID, + savedObjectType: GLOBAL_SETTINGS_SAVED_OBJECT_TYPE, + }); + + if (res.total === 0) { + throw Boom.notFound('Global settings not found'); + } + return mapSettingsSO(res.saved_objects[0]); +} + export async function getSettingsOrUndefined( soClient: SavedObjectsClientContract ): Promise {