[Fleet] Fix fleet_server_hosts value in fleet/settings API (#144898)

## Summary

Resolve `fleet_server_hosts` from the new Fleet Server Host SO type
instead of the settings SO.

Would like to add an integration test for this, but requires setting up
a new "get settings" suite. Will push a follow-up commit.

Fixes https://github.com/elastic/fleet-server/issues/2068

### Checklist

- [ ] [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


![image](https://user-images.githubusercontent.com/6766512/200846767-81111fd7-5609-47b1-932d-9274c257f0be.png)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Kyle Pollich 2022-11-10 11:29:19 -05:00 committed by GitHub
parent 6804cffc5f
commit db3572ab72
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 74 additions and 16 deletions

View file

@ -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;

View file

@ -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<Settings> {
const res = await soClient.find<SettingsSOAttributes>({
@ -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'] : [],
};
}

View file

@ -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

View file

@ -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',
]);
});
});
}

View file

@ -7,6 +7,7 @@
export default function loadTests({ loadTestFile }) {
describe('Settings Endpoints', () => {
loadTestFile(require.resolve('./get'));
loadTestFile(require.resolve('./update'));
});
}

View file

@ -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');