Skip to content

Commit

Permalink
[Fleet] Fix fleet server host removal from managed policies (#195377)
Browse files Browse the repository at this point in the history
(cherry picked from commit 0a6be07)
  • Loading branch information
nchaulet committed Oct 8, 2024
1 parent 916a578 commit cb1c1e4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
6 changes: 5 additions & 1 deletion x-pack/plugins/fleet/server/services/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,8 @@ class AgentPolicyService {
*/
public async removeFleetServerHostFromAll(
esClient: ElasticsearchClient,
fleetServerHostId: string
fleetServerHostId: string,
options?: { force?: boolean }
) {
const savedObjectType = await getAgentPolicySavedObjectType();
const agentPolicies = (
Expand All @@ -965,6 +966,9 @@ class AgentPolicyService {
agentPolicy.id,
{
fleet_server_host_id: null,
},
{
force: options?.force,
}
),
{
Expand Down
49 changes: 47 additions & 2 deletions x-pack/plugins/fleet/server/services/fleet_server_host.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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,
}
);
});
});
4 changes: 3 additions & 1 deletion x-pack/plugins/fleet/server/services/fleet_server_host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit cb1c1e4

Please sign in to comment.