-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Fleet] Fix creation of POLICY_CHANGE action during 7.9 => 7.10 migration #81041
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { SavedObjectsClientContract } from 'src/core/server'; | ||
import { SO_SEARCH_LIMIT } from '../../constants'; | ||
import { agentPolicyService } from '../agent_policy'; | ||
import { outputService } from '../output'; | ||
import { getLatestConfigChangeAction } from './actions'; | ||
|
||
export async function isAgentsSetup(soClient: SavedObjectsClientContract): Promise<boolean> { | ||
const adminUser = await outputService.getAdminUser(soClient, false); | ||
const outputId = await outputService.getDefaultOutputId(soClient); | ||
// If admin user (fleet_enroll) and output id exist Agents are correctly setup | ||
return adminUser !== null && outputId !== null; | ||
} | ||
|
||
/** | ||
* During the migration from 7.9 to 7.10 we introduce a new agent action POLICY_CHANGE per policy | ||
* this function ensure that action exist for each policy | ||
* | ||
* @param soClient | ||
*/ | ||
export async function ensureAgentActionPolicyChangeExists(soClient: SavedObjectsClientContract) { | ||
// If Agents are not setup skip | ||
if (!(await isAgentsSetup(soClient))) { | ||
return; | ||
} | ||
|
||
const { items: agentPolicies } = await agentPolicyService.list(soClient, { | ||
perPage: SO_SEARCH_LIMIT, | ||
}); | ||
|
||
await Promise.all( | ||
agentPolicies.map(async (agentPolicy) => { | ||
const policyChangeActionExist = !!(await getLatestConfigChangeAction( | ||
soClient, | ||
agentPolicy.id | ||
)); | ||
|
||
if (!policyChangeActionExist) { | ||
return agentPolicyService.createFleetPolicyChangeAction(soClient, agentPolicy.id); | ||
} | ||
}) | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ import { generateEnrollmentAPIKey } from './api_keys'; | |
import { settingsService } from '.'; | ||
import { awaitIfPending } from './setup_utils'; | ||
import { createDefaultSettings } from './settings'; | ||
import { ensureAgentActionPolicyChangeExists } from './agents'; | ||
|
||
const FLEET_ENROLL_USERNAME = 'fleet_enroll'; | ||
const FLEET_ENROLL_ROLE = 'fleet_enroll'; | ||
|
@@ -80,6 +81,9 @@ async function createSetupSideEffects( | |
) { | ||
throw new Error('Policy not found'); | ||
} | ||
|
||
await ensureAgentActionPolicyChangeExists(soClient); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO it's a bit strange to have this function called in this order, in the middle of the logic for ensuring default packages for default policy. what do you think of moving it to L68, before the start of this logic, but after the rest of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we probably want to have it after the package installation so the config contains the package I should probably move it L111 |
||
|
||
for (const installedPackage of installedPackages) { | ||
const packageShouldBeInstalled = DEFAULT_AGENT_POLICIES_PACKAGES.some( | ||
(packageName) => installedPackage.name === packageName | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this condition seems to differ from the original, where we just check for truthiness. is there a specific need to change to check for null instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no there is no specific need I check for null to fix a type issue otherwise is not returning a boolean here, but I could replace it by