Skip to content

Commit

Permalink
[Fleet] Fix settings response when space awareness enabled (elastic#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet authored Nov 22, 2024
1 parent 8e6698f commit ad61b9d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 18 deletions.
36 changes: 36 additions & 0 deletions x-pack/plugins/fleet/server/services/settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
42 changes: 24 additions & 18 deletions x-pack/plugins/fleet/server/services/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<Settings> {
const res = await soClient.find<SettingsSOAttributes>({
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<SettingsSOAttributes>): Settings {
return {
id: settingsSo.id,
version: settingsSo.version,
Expand All @@ -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<Settings> {
const res = await soClient.find<SettingsSOAttributes>({
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<Settings | undefined> {
Expand Down

0 comments on commit ad61b9d

Please sign in to comment.