[Fleet] Fix fleet server host removal from managed policies (#195377)

This commit is contained in:
Nicolas Chaulet 2024-10-08 14:28:41 +02:00 committed by GitHub
parent 43a3d4a68a
commit 0a6be076ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 55 additions and 4 deletions

View file

@ -939,7 +939,8 @@ class AgentPolicyService {
*/
public async removeFleetServerHostFromAll(
esClient: ElasticsearchClient,
fleetServerHostId: string
fleetServerHostId: string,
options?: { force?: boolean }
) {
const savedObjectType = await getAgentPolicySavedObjectType();
const agentPolicies = (
@ -965,6 +966,9 @@ class AgentPolicyService {
agentPolicy.id,
{
fleet_server_host_id: null,
},
{
force: options?.force,
}
),
{

View file

@ -5,7 +5,7 @@
* 2.0.
*/
import { savedObjectsClientMock } from '@kbn/core/server/mocks';
import { savedObjectsClientMock, elasticsearchServiceMock } from '@kbn/core/server/mocks';
import { loggerMock } from '@kbn/logging-mocks';
import type { Logger } from '@kbn/core/server';
@ -19,15 +19,19 @@ import {
import { appContextService } from './app_context';
import { migrateSettingsToFleetServerHost } from './fleet_server_host';
import { deleteFleetServerHost, migrateSettingsToFleetServerHost } from './fleet_server_host';
import { agentPolicyService } from './agent_policy';
jest.mock('./app_context');
jest.mock('./agent_policy');
const mockedAppContextService = appContextService as jest.Mocked<typeof appContextService>;
mockedAppContextService.getSecuritySetup.mockImplementation(() => ({
...securityMock.createSetup(),
}));
mockedAppContextService.getExperimentalFeatures.mockReturnValue({} as any);
let mockedLogger: jest.Mocked<Logger>;
describe('migrateSettingsToFleetServerHost', () => {
@ -116,3 +120,44 @@ describe('migrateSettingsToFleetServerHost', () => {
);
});
});
describe('deleteFleetServerHost', () => {
it('should removeFleetServerHostFromAll agent policies without force if not deleted from preconfiguration', async () => {
const soMock = savedObjectsClientMock.create();
soMock.get.mockResolvedValue({
id: 'test1',
attributes: {},
} as any);
const esMock = elasticsearchServiceMock.createInternalClient();
await deleteFleetServerHost(soMock, esMock, 'test1', {});
expect(jest.mocked(agentPolicyService.removeFleetServerHostFromAll)).toBeCalledWith(
esMock,
'test1',
{
force: undefined,
}
);
});
it('should removeFleetServerHostFromAll agent policies with force if deleted from preconfiguration', async () => {
const soMock = savedObjectsClientMock.create();
soMock.get.mockResolvedValue({
id: 'test1',
attributes: {},
} as any);
const esMock = elasticsearchServiceMock.createInternalClient();
await deleteFleetServerHost(soMock, esMock, 'test1', {
fromPreconfiguration: true,
});
expect(jest.mocked(agentPolicyService.removeFleetServerHostFromAll)).toBeCalledWith(
esMock,
'test1',
{
force: true,
}
);
});
});

View file

@ -142,7 +142,9 @@ export async function deleteFleetServerHost(
);
}
await agentPolicyService.removeFleetServerHostFromAll(esClient, id);
await agentPolicyService.removeFleetServerHostFromAll(esClient, id, {
force: options?.fromPreconfiguration,
});
return await soClient.delete(FLEET_SERVER_HOST_SAVED_OBJECT_TYPE, id);
}