Skip to content

Commit 770bc4c

Browse files
authored
[Fleet] Fix creation of POLICY_CHANGE action during 7.9 => 7.10 migration (#81041)
1 parent b0a56b9 commit 770bc4c

File tree

5 files changed

+58
-9
lines changed

5 files changed

+58
-9
lines changed

x-pack/plugins/ingest_manager/server/services/agent_policy_update.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
import { KibanaRequest, SavedObjectsClientContract } from 'src/core/server';
88
import { generateEnrollmentAPIKey, deleteEnrollmentApiKeyForAgentPolicyId } from './api_keys';
9-
import { unenrollForAgentPolicyId } from './agents';
10-
import { outputService } from './output';
9+
import { isAgentsSetup, unenrollForAgentPolicyId } from './agents';
1110
import { agentPolicyService } from './agent_policy';
1211
import { appContextService } from './app_context';
1312

@@ -31,11 +30,8 @@ export async function agentPolicyUpdateEventHandler(
3130
action: string,
3231
agentPolicyId: string
3332
) {
34-
const adminUser = await outputService.getAdminUser(soClient);
35-
const outputId = await outputService.getDefaultOutputId(soClient);
36-
37-
// If no admin user and no default output fleet is not enabled just skip this hook
38-
if (!adminUser || !outputId) {
33+
// If Agents are not setup skip this hook
34+
if (!(await isAgentsSetup(soClient))) {
3935
return;
4036
}
4137

x-pack/plugins/ingest_manager/server/services/agents/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ export * from './update';
1616
export * from './actions';
1717
export * from './reassign';
1818
export * from './authenticate';
19+
export * from './setup';
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { SavedObjectsClientContract } from 'src/core/server';
8+
import { SO_SEARCH_LIMIT } from '../../constants';
9+
import { agentPolicyService } from '../agent_policy';
10+
import { outputService } from '../output';
11+
import { getLatestConfigChangeAction } from './actions';
12+
13+
export async function isAgentsSetup(soClient: SavedObjectsClientContract): Promise<boolean> {
14+
const adminUser = await outputService.getAdminUser(soClient, false);
15+
const outputId = await outputService.getDefaultOutputId(soClient);
16+
// If admin user (fleet_enroll) and output id exist Agents are correctly setup
17+
return adminUser && outputId ? true : false;
18+
}
19+
20+
/**
21+
* During the migration from 7.9 to 7.10 we introduce a new agent action POLICY_CHANGE per policy
22+
* this function ensure that action exist for each policy
23+
*
24+
* @param soClient
25+
*/
26+
export async function ensureAgentActionPolicyChangeExists(soClient: SavedObjectsClientContract) {
27+
// If Agents are not setup skip
28+
if (!(await isAgentsSetup(soClient))) {
29+
return;
30+
}
31+
32+
const { items: agentPolicies } = await agentPolicyService.list(soClient, {
33+
perPage: SO_SEARCH_LIMIT,
34+
});
35+
36+
await Promise.all(
37+
agentPolicies.map(async (agentPolicy) => {
38+
const policyChangeActionExist = !!(await getLatestConfigChangeAction(
39+
soClient,
40+
agentPolicy.id
41+
));
42+
43+
if (!policyChangeActionExist) {
44+
return agentPolicyService.createFleetPolicyChangeAction(soClient, agentPolicy.id);
45+
}
46+
})
47+
);
48+
}

x-pack/plugins/ingest_manager/server/services/output.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ class OutputService {
6565
return outputs.saved_objects[0].id;
6666
}
6767

68-
public async getAdminUser(soClient: SavedObjectsClientContract) {
69-
if (cachedAdminUser) {
68+
public async getAdminUser(soClient: SavedObjectsClientContract, useCache = true) {
69+
if (useCache && cachedAdminUser) {
7070
return cachedAdminUser;
7171
}
7272

x-pack/plugins/ingest_manager/server/services/setup.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { generateEnrollmentAPIKey } from './api_keys';
2929
import { settingsService } from '.';
3030
import { awaitIfPending } from './setup_utils';
3131
import { createDefaultSettings } from './settings';
32+
import { ensureAgentActionPolicyChangeExists } from './agents';
3233

3334
const FLEET_ENROLL_USERNAME = 'fleet_enroll';
3435
const FLEET_ENROLL_ROLE = 'fleet_enroll';
@@ -80,6 +81,7 @@ async function createSetupSideEffects(
8081
) {
8182
throw new Error('Policy not found');
8283
}
84+
8385
for (const installedPackage of installedPackages) {
8486
const packageShouldBeInstalled = DEFAULT_AGENT_POLICIES_PACKAGES.some(
8587
(packageName) => installedPackage.name === packageName
@@ -105,6 +107,8 @@ async function createSetupSideEffects(
105107
}
106108
}
107109

110+
await ensureAgentActionPolicyChangeExists(soClient);
111+
108112
return { isIntialized: true };
109113
}
110114

0 commit comments

Comments
 (0)