diff --git a/x-pack/plugins/fleet/server/collectors/fleet_server_collector.ts b/x-pack/plugins/fleet/server/collectors/fleet_server_collector.ts index f89b471a8d083..4438b23c8a285 100644 --- a/x-pack/plugins/fleet/server/collectors/fleet_server_collector.ts +++ b/x-pack/plugins/fleet/server/collectors/fleet_server_collector.ts @@ -5,11 +5,11 @@ * 2.0. */ -import { isBoom } from '@hapi/boom'; import type { SavedObjectsClient, ElasticsearchClient } from '@kbn/core/server'; -import { packagePolicyService, settingsService } from '../services'; +import { packagePolicyService } from '../services'; import { getAgentStatusForAgentPolicy } from '../services/agents'; +import { listFleetServerHosts } from '../services/fleet_server_host'; const DEFAULT_USAGE = { total_all_statuses: 0, @@ -39,16 +39,8 @@ export const getFleetServerUsage = async ( return DEFAULT_USAGE; } - const numHostsUrls = await settingsService - .getSettings(soClient) - .then((settings) => settings.fleet_server_hosts?.length ?? 0) - .catch((err) => { - if (isBoom(err) && err.output.statusCode === 404) { - return 0; - } - - throw err; - }); + const fleetServerHosts = await listFleetServerHosts(soClient); + const numHostsUrls = fleetServerHosts.items.flatMap((host) => host.host_urls).length; // Find all policies with Fleet server than query agent status let hasMore = true; diff --git a/x-pack/plugins/fleet/server/services/settings.ts b/x-pack/plugins/fleet/server/services/settings.ts index e710251c39f91..7fe6c25d24b86 100644 --- a/x-pack/plugins/fleet/server/services/settings.ts +++ b/x-pack/plugins/fleet/server/services/settings.ts @@ -13,6 +13,7 @@ import { GLOBAL_SETTINGS_SAVED_OBJECT_TYPE, GLOBAL_SETTINGS_ID } from '../../com import type { SettingsSOAttributes, Settings, BaseSettings } from '../../common/types'; import { appContextService } from './app_context'; +import { listFleetServerHosts } from './fleet_server_host'; export async function getSettings(soClient: SavedObjectsClientContract): Promise { const res = await soClient.find({ @@ -23,10 +24,12 @@ export async function getSettings(soClient: SavedObjectsClientContract): Promise throw Boom.notFound('Global settings not found'); } const settingsSo = res.saved_objects[0]; + const fleetServerHosts = await listFleetServerHosts(soClient); + return { id: settingsSo.id, ...settingsSo.attributes, - fleet_server_hosts: settingsSo.attributes.fleet_server_hosts || [], + fleet_server_hosts: fleetServerHosts.items.flatMap((item) => item.host_urls), preconfigured_fields: getConfigFleetServerHosts() ? ['fleet_server_hosts'] : [], }; } diff --git a/x-pack/test/fleet_api_integration/apis/fleet_telemetry.ts b/x-pack/test/fleet_api_integration/apis/fleet_telemetry.ts index d1e7cd8d999c6..3d72d01cc3224 100644 --- a/x-pack/test/fleet_api_integration/apis/fleet_telemetry.ts +++ b/x-pack/test/fleet_api_integration/apis/fleet_telemetry.ts @@ -81,9 +81,14 @@ export default function (providerContext: FtrProviderContext) { } await supertest - .put(`/api/fleet/settings`) + .post(`/api/fleet/fleet_server_hosts`) .set('kbn-xsrf', 'xxxx') - .send({ fleet_server_hosts: ['https://test1.fr', 'https://test2.fr'] }) + .send({ + id: 'test-default-123', + name: 'Default', + is_default: true, + host_urls: ['https://test.com:8080', 'https://test.com:8081'], + }) .expect(200); // Default Fleet Server diff --git a/x-pack/test/fleet_api_integration/apis/settings/get.ts b/x-pack/test/fleet_api_integration/apis/settings/get.ts new file mode 100644 index 0000000000000..3d6a3adb394c2 --- /dev/null +++ b/x-pack/test/fleet_api_integration/apis/settings/get.ts @@ -0,0 +1,56 @@ +/* + * 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 expect from '@kbn/expect'; +import { FtrProviderContext } from '../../../api_integration/ftr_provider_context'; +import { skipIfNoDockerRegistry } from '../../helpers'; +import { setupFleetAndAgents } from '../agents/services'; + +export default function (providerContext: FtrProviderContext) { + const { getService } = providerContext; + const supertest = getService('supertest'); + const esArchiver = getService('esArchiver'); + const kibanaServer = getService('kibanaServer'); + + describe('Settings - get', async function () { + skipIfNoDockerRegistry(providerContext); + before(async () => { + await esArchiver.load('x-pack/test/functional/es_archives/fleet/empty_fleet_server'); + }); + setupFleetAndAgents(providerContext); + + after(async () => { + await kibanaServer.savedObjects.cleanStandardList(); + await esArchiver.unload('x-pack/test/functional/es_archives/fleet/empty_fleet_server'); + }); + + it('should respond with fleet_server_hosts', async function () { + // Create a fleet server host + await supertest + .post(`/api/fleet/fleet_server_hosts`) + .set('kbn-xsrf', 'xxxx') + .send({ + id: 'test-default-123', + name: 'Default', + is_default: true, + host_urls: ['https://test.com:8080', 'https://test.com:8081'], + }) + .expect(200); + + // Assert that the hosts appear in the setting response + const response = await supertest + .get(`/api/fleet/settings`) + .set('kbn-xsrf', 'xxxx') + .expect(200); + + expect(response.body.item.fleet_server_hosts).to.eql([ + 'https://test.com:8080', + 'https://test.com:8081', + ]); + }); + }); +} diff --git a/x-pack/test/fleet_api_integration/apis/settings/index.js b/x-pack/test/fleet_api_integration/apis/settings/index.js index 30e5538622e20..3b5ecb6359b19 100644 --- a/x-pack/test/fleet_api_integration/apis/settings/index.js +++ b/x-pack/test/fleet_api_integration/apis/settings/index.js @@ -7,6 +7,7 @@ export default function loadTests({ loadTestFile }) { describe('Settings Endpoints', () => { + loadTestFile(require.resolve('./get')); loadTestFile(require.resolve('./update')); }); } diff --git a/x-pack/test/fleet_api_integration/apis/settings/update.ts b/x-pack/test/fleet_api_integration/apis/settings/update.ts index 0398d9155a07a..cc9f499967055 100644 --- a/x-pack/test/fleet_api_integration/apis/settings/update.ts +++ b/x-pack/test/fleet_api_integration/apis/settings/update.ts @@ -18,7 +18,8 @@ export default function (providerContext: FtrProviderContext) { const esClient = getService('es'); const esArchiver = getService('esArchiver'); - describe('Settings - update', async function () { + // Skipped as the Fleet Server hosts settings values are no longer used as of https://github.com/elastic/kibana/issues/137785 + describe.skip('Settings - update', async function () { skipIfNoDockerRegistry(providerContext); before(async () => { await esArchiver.load('x-pack/test/functional/es_archives/fleet/empty_fleet_server');