Skip to content

Commit

Permalink
[8.x] [Fleet] Fix fleet server host removal from managed policies (#1…
Browse files Browse the repository at this point in the history
…95377) (#195398)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Fleet] Fix fleet server host removal from managed policies
(#195377)](#195377)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Nicolas
Chaulet","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-10-08T12:28:41Z","message":"[Fleet]
Fix fleet server host removal from managed policies
(#195377)","sha":"0a6be076ead7061bb638365ff1fed007bfa1faf0","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:Fleet","v9.0.0","backport:prev-minor","v8.16.0"],"title":"[Fleet]
Fix fleet server host removal from managed
policies","number":195377,"url":"https://github.com/elastic/kibana/pull/195377","mergeCommit":{"message":"[Fleet]
Fix fleet server host removal from managed policies
(#195377)","sha":"0a6be076ead7061bb638365ff1fed007bfa1faf0"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/195377","number":195377,"mergeCommit":{"message":"[Fleet]
Fix fleet server host removal from managed policies
(#195377)","sha":"0a6be076ead7061bb638365ff1fed007bfa1faf0"}},{"branch":"8.x","label":"v8.16.0","branchLabelMappingKey":"^v8.16.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Nicolas Chaulet <[email protected]>
  • Loading branch information
kibanamachine and nchaulet authored Oct 8, 2024
1 parent 1f383a5 commit 291adf0
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 291adf0

Please sign in to comment.