|
| 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 | +} |
0 commit comments