[Fleet] Create default fleet server hosts during fleet migration (#98913) (#98946)

Co-authored-by: Nicolas Chaulet <nicolas.chaulet@elastic.co>
This commit is contained in:
Kibana Machine 2021-04-30 18:53:32 -04:00 committed by GitHub
parent 09397fba63
commit 3cfef0993c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 180 additions and 10 deletions

View file

@ -5,14 +5,19 @@
* 2.0.
*/
import { savedObjectsClientMock } from 'src/core/server/mocks';
import { appContextService } from './app_context';
import { getCloudFleetServersHosts } from './settings';
import { getCloudFleetServersHosts, settingsSetup } from './settings';
jest.mock('./app_context');
const mockedAppContextService = appContextService as jest.Mocked<typeof appContextService>;
describe('getCloudFleetServersHosts', () => {
afterEach(() => {
mockedAppContextService.getCloud.mockReset();
});
it('should return undefined if cloud is not setup', () => {
expect(getCloudFleetServersHosts()).toBeUndefined();
});
@ -49,3 +54,154 @@ describe('getCloudFleetServersHosts', () => {
`);
});
});
describe('settingsSetup', () => {
afterEach(() => {
mockedAppContextService.getCloud.mockReset();
});
it('should create settings if there is no settings', async () => {
const soClientMock = savedObjectsClientMock.create();
soClientMock.find.mockResolvedValue({
total: 0,
page: 0,
per_page: 10,
saved_objects: [],
});
soClientMock.create.mockResolvedValue({
id: 'created',
attributes: {},
references: [],
type: 'so_type',
});
await settingsSetup(soClientMock);
expect(soClientMock.create).toBeCalled();
});
it('should do nothing if there is settings and no default fleet server hosts', async () => {
const soClientMock = savedObjectsClientMock.create();
soClientMock.find.mockResolvedValue({
total: 1,
page: 0,
per_page: 10,
saved_objects: [
{
id: 'defaultsettings',
attributes: {},
type: 'so_type',
references: [],
score: 0,
},
],
});
soClientMock.create.mockResolvedValue({
id: 'created',
attributes: {},
references: [],
type: 'so_type',
});
await settingsSetup(soClientMock);
expect(soClientMock.create).not.toBeCalled();
});
it('should update settings if there is settings without fleet server hosts and default fleet server hosts', async () => {
const soClientMock = savedObjectsClientMock.create();
mockedAppContextService.getCloud.mockReturnValue({
cloudId:
'test:dGVzdC5mcjo5MjQzJGRhM2I2YjNkYWY5ZDRjODE4ZjI4ZmEzNDdjMzgzODViJDgxMmY4NWMxZjNjZTQ2YTliYjgxZjFjMWIxMzRjNmRl',
isCloudEnabled: true,
deploymentId: 'deployment-id-1',
apm: {},
});
soClientMock.find.mockResolvedValue({
total: 1,
page: 0,
per_page: 10,
saved_objects: [
{
id: 'defaultsettings',
attributes: {},
type: 'so_type',
references: [],
score: 0,
},
],
});
soClientMock.update.mockResolvedValue({
id: 'updated',
attributes: {},
references: [],
type: 'so_type',
});
soClientMock.create.mockResolvedValue({
id: 'created',
attributes: {},
references: [],
type: 'so_type',
});
await settingsSetup(soClientMock);
expect(soClientMock.create).not.toBeCalled();
expect(soClientMock.update).toBeCalledWith('ingest_manager_settings', 'defaultsettings', {
fleet_server_hosts: ['https://deployment-id-1.fleet.test.fr:9243'],
});
});
it('should not update settings if there is settings with fleet server hosts and default fleet server hosts', async () => {
const soClientMock = savedObjectsClientMock.create();
mockedAppContextService.getCloud.mockReturnValue({
cloudId:
'test:dGVzdC5mcjo5MjQzJGRhM2I2YjNkYWY5ZDRjODE4ZjI4ZmEzNDdjMzgzODViJDgxMmY4NWMxZjNjZTQ2YTliYjgxZjFjMWIxMzRjNmRl',
isCloudEnabled: true,
deploymentId: 'deployment-id-1',
apm: {},
});
soClientMock.find.mockResolvedValue({
total: 1,
page: 0,
per_page: 10,
saved_objects: [
{
id: 'defaultsettings',
attributes: {
fleet_server_hosts: ['http://fleetserver:1234'],
},
type: 'so_type',
references: [],
score: 0,
},
],
});
soClientMock.update.mockResolvedValue({
id: 'updated',
attributes: {},
references: [],
type: 'so_type',
});
soClientMock.create.mockResolvedValue({
id: 'created',
attributes: {},
references: [],
type: 'so_type',
});
await settingsSetup(soClientMock);
expect(soClientMock.create).not.toBeCalled();
expect(soClientMock.update).not.toBeCalled();
});
});

View file

@ -29,6 +29,28 @@ export async function getSettings(soClient: SavedObjectsClientContract): Promise
};
}
export async function settingsSetup(soClient: SavedObjectsClientContract) {
try {
const settings = await getSettings(soClient);
// Migration for < 7.13 Kibana
if (!settings.fleet_server_hosts || settings.fleet_server_hosts.length === 0) {
const defaultSettings = createDefaultSettings();
if (defaultSettings.fleet_server_hosts.length > 0) {
return saveSettings(soClient, {
fleet_server_hosts: defaultSettings.fleet_server_hosts,
});
}
}
} catch (e) {
if (e.isBoom && e.output.statusCode === 404) {
const defaultSettings = createDefaultSettings();
return saveSettings(soClient, defaultSettings);
}
throw e;
}
}
export async function saveSettings(
soClient: SavedObjectsClientContract,
newData: Partial<Omit<Settings, 'id'>>

View file

@ -18,7 +18,6 @@ import { outputService } from './output';
import { generateEnrollmentAPIKey, hasEnrollementAPIKeysForPolicy } from './api_keys';
import { settingsService } from '.';
import { awaitIfPending } from './setup_utils';
import { createDefaultSettings } from './settings';
import { ensureAgentActionPolicyChangeExists } from './agents';
import { awaitIfFleetServerSetupPending } from './fleet_server';
@ -40,14 +39,7 @@ async function createSetupSideEffects(
): Promise<SetupStatus> {
const [defaultOutput] = await Promise.all([
outputService.ensureDefaultOutput(soClient),
settingsService.getSettings(soClient).catch((e: any) => {
if (e.isBoom && e.output.statusCode === 404) {
const defaultSettings = createDefaultSettings();
return settingsService.saveSettings(soClient, defaultSettings);
}
return Promise.reject(e);
}),
settingsService.settingsSetup(soClient),
]);
await awaitIfFleetServerSetupPending();