mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 09:48:58 -04:00
[Fleet] Fix - Increment system package name when adding agent policy with monitoring (#118403) (#118545)
Co-authored-by: Cristina Amico <criamico@users.noreply.github.com>
This commit is contained in:
parent
448788d1a8
commit
a2165580c4
2 changed files with 31 additions and 0 deletions
|
@ -37,6 +37,7 @@ import type {
|
|||
GetFullAgentConfigMapResponse,
|
||||
} from '../../../common';
|
||||
import { defaultIngestErrorHandler } from '../../errors';
|
||||
import { incrementPackageName } from '../../services/package_policy';
|
||||
|
||||
export const getAgentPoliciesHandler: RequestHandler<
|
||||
undefined,
|
||||
|
@ -108,6 +109,7 @@ export const createAgentPolicyHandler: RequestHandler<
|
|||
const esClient = context.core.elasticsearch.client.asCurrentUser;
|
||||
const user = (await appContextService.getSecurity()?.authc.getCurrentUser(request)) || undefined;
|
||||
const withSysMonitoring = request.query.sys_monitoring ?? false;
|
||||
|
||||
try {
|
||||
// eslint-disable-next-line prefer-const
|
||||
let [agentPolicy, newSysPackagePolicy] = await Promise.all<
|
||||
|
@ -131,6 +133,8 @@ export const createAgentPolicyHandler: RequestHandler<
|
|||
if (withSysMonitoring && newSysPackagePolicy !== undefined && agentPolicy !== undefined) {
|
||||
newSysPackagePolicy.policy_id = agentPolicy.id;
|
||||
newSysPackagePolicy.namespace = agentPolicy.namespace;
|
||||
newSysPackagePolicy.name = await incrementPackageName(soClient, FLEET_SYSTEM_PACKAGE);
|
||||
|
||||
await packagePolicyService.create(soClient, esClient, newSysPackagePolicy, {
|
||||
user,
|
||||
bumpRevision: false,
|
||||
|
|
|
@ -1195,3 +1195,30 @@ function deepMergeVars(original: any, override: any): any {
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function incrementPackageName(
|
||||
soClient: SavedObjectsClientContract,
|
||||
packageName: string
|
||||
) {
|
||||
// Fetch all packagePolicies having the package name
|
||||
const packagePolicyData = await packagePolicyService.list(soClient, {
|
||||
perPage: 1,
|
||||
kuery: `${PACKAGE_POLICY_SAVED_OBJECT_TYPE}.package.name: "${packageName}"`,
|
||||
});
|
||||
|
||||
// Retrieve highest number appended to package policy name and increment it by one
|
||||
const pkgPoliciesNamePattern = new RegExp(`${packageName}-(\\d+)`);
|
||||
|
||||
const pkgPoliciesWithMatchingNames = packagePolicyData?.items
|
||||
? packagePolicyData.items
|
||||
.filter((ds) => Boolean(ds.name.match(pkgPoliciesNamePattern)))
|
||||
.map((ds) => parseInt(ds.name.match(pkgPoliciesNamePattern)![1], 10))
|
||||
.sort()
|
||||
: [];
|
||||
|
||||
return `${packageName}-${
|
||||
pkgPoliciesWithMatchingNames.length
|
||||
? pkgPoliciesWithMatchingNames[pkgPoliciesWithMatchingNames.length - 1] + 1
|
||||
: 1
|
||||
}`;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue