From 1452ba793fb4d5529cce78a8fbb1646e752f60f9 Mon Sep 17 00:00:00 2001 From: shahzad31 Date: Fri, 1 Nov 2024 12:55:06 +0100 Subject: [PATCH] async bumpRevision --- .../fleet/server/services/agent_policy.ts | 14 +++-- .../fleet/server/services/package_policy.ts | 51 ++++++++++++++----- .../server/services/package_policy_service.ts | 4 +- .../synthetics_private_location.ts | 8 ++- 4 files changed, 58 insertions(+), 19 deletions(-) diff --git a/x-pack/plugins/fleet/server/services/agent_policy.ts b/x-pack/plugins/fleet/server/services/agent_policy.ts index f93bf583945a0..e64533c08a38e 100644 --- a/x-pack/plugins/fleet/server/services/agent_policy.ts +++ b/x-pack/plugins/fleet/server/services/agent_policy.ts @@ -30,6 +30,8 @@ import { asyncForEach } from '@kbn/std'; import type { SavedObjectError } from '@kbn/core-saved-objects-common'; +import { withSpan } from '@kbn/apm-utils'; + import { getAllowedOutputTypeForPolicy, packageToPackagePolicy, @@ -880,11 +882,13 @@ class AgentPolicyService { id: string, options?: { user?: AuthenticatedUser; removeProtection?: boolean } ): Promise { - await this._update(soClient, esClient, id, {}, options?.user, { - bumpRevision: true, - removeProtection: options?.removeProtection ?? false, - skipValidation: false, - returnUpdatedPolicy: false, + return withSpan('bump_agent_policy_revision', async () => { + await this._update(soClient, esClient, id, {}, options?.user, { + bumpRevision: true, + removeProtection: options?.removeProtection ?? false, + skipValidation: false, + returnUpdatedPolicy: false, + }); }); } diff --git a/x-pack/plugins/fleet/server/services/package_policy.ts b/x-pack/plugins/fleet/server/services/package_policy.ts index bc5bce9eea2a3..c371640559f77 100644 --- a/x-pack/plugins/fleet/server/services/package_policy.ts +++ b/x-pack/plugins/fleet/server/services/package_policy.ts @@ -480,6 +480,7 @@ class PackagePolicyClientImpl implements PackagePolicyClient { user?: AuthenticatedUser; bumpRevision?: boolean; force?: true; + asyncBumpAgentPolicyRevision?: boolean; } ): Promise<{ created: PackagePolicy[]; @@ -663,9 +664,15 @@ class PackagePolicyClientImpl implements PackagePolicyClient { if (options?.bumpRevision ?? true) { for (const agentPolicyId of agentPolicyIds) { - await agentPolicyService.bumpRevision(soClient, esClient, agentPolicyId, { - user: options?.user, - }); + if (options?.asyncBumpAgentPolicyRevision ?? false) { + agentPolicyService.bumpRevision(soClient, esClient, agentPolicyId, { + user: options?.user, + }); + } else { + await agentPolicyService.bumpRevision(soClient, esClient, agentPolicyId, { + user: options?.user, + }); + } } } logger.debug(`Created new package policies`); @@ -1176,7 +1183,7 @@ class PackagePolicyClientImpl implements PackagePolicyClient { soClient: SavedObjectsClientContract, esClient: ElasticsearchClient, packagePolicyUpdates: Array, - options?: { user?: AuthenticatedUser; force?: boolean } + options?: { user?: AuthenticatedUser; force?: boolean; asyncBumpAgentPolicyRevision?: boolean } ): Promise<{ updatedPolicies: PackagePolicy[] | null; failedPolicies: Array<{ @@ -1344,10 +1351,19 @@ class PackagePolicyClientImpl implements PackagePolicyClient { (assignedInOldPolicies && !assignedInUpdatedPolicies) || (!assignedInOldPolicies && assignedInUpdatedPolicies); - await agentPolicyService.bumpRevision(soClient, esClient, agentPolicyId, { - user: options?.user, - removeProtection, - }); + if (options?.asyncBumpAgentPolicyRevision ?? false) { + agentPolicyService + .bumpRevision(soClient, esClient, agentPolicyId, { + user: options?.user, + removeProtection, + }) + .catch((e) => {}); + } else { + await agentPolicyService.bumpRevision(soClient, esClient, agentPolicyId, { + user: options?.user, + removeProtection, + }); + } }); const pkgVersions: Record = {}; @@ -1571,10 +1587,20 @@ class PackagePolicyClientImpl implements PackagePolicyClient { const agentPolicy = agentPolicies.find((p) => p.id === policyId); if (agentPolicy) { // is the agent policy attached to package policy with endpoint - await agentPolicyService.bumpRevision(soClient, esClient, policyId, { - user: options?.user, - removeProtection: agentPoliciesWithEndpointPackagePolicies.has(policyId), - }); + // if the agent policy is attached to an endpoint package policy, we need to remove the protection + if (agentPoliciesWithEndpointPackagePolicies.has(policyId)) { + agentPolicyService + .bumpRevision(soClient, esClient, policyId, { + user: options?.user, + removeProtection: agentPoliciesWithEndpointPackagePolicies.has(policyId), + }) + .catch((e) => {}); + } else { + await agentPolicyService.bumpRevision(soClient, esClient, policyId, { + user: options?.user, + removeProtection: agentPoliciesWithEndpointPackagePolicies.has(policyId), + }); + } } } } @@ -2368,6 +2394,7 @@ class PackagePolicyClientWithAuthz extends PackagePolicyClientImpl { user?: AuthenticatedUser | undefined; bumpRevision?: boolean | undefined; force?: true | undefined; + asyncBumpAgentPolicyRevision?: boolean; } | undefined ): Promise<{ diff --git a/x-pack/plugins/fleet/server/services/package_policy_service.ts b/x-pack/plugins/fleet/server/services/package_policy_service.ts index 967efb1055cfb..0e0b8b1edb337 100644 --- a/x-pack/plugins/fleet/server/services/package_policy_service.ts +++ b/x-pack/plugins/fleet/server/services/package_policy_service.ts @@ -105,6 +105,7 @@ export interface PackagePolicyClient { bumpRevision?: boolean; force?: true; authorizationHeader?: HTTPAuthorizationHeader | null; + asyncBumpAgentPolicyRevision?: boolean; } ): Promise<{ created: PackagePolicy[]; @@ -115,7 +116,7 @@ export interface PackagePolicyClient { soClient: SavedObjectsClientContract, esClient: ElasticsearchClient, packagePolicyUpdates: UpdatePackagePolicy[], - options?: { user?: AuthenticatedUser; force?: boolean }, + options?: { user?: AuthenticatedUser; force?: boolean; asyncBumpAgentPolicyRevision?: boolean }, currentVersion?: string ): Promise<{ updatedPolicies: PackagePolicy[] | null; @@ -165,6 +166,7 @@ export interface PackagePolicyClient { user?: AuthenticatedUser; skipUnassignFromAgentPolicies?: boolean; force?: boolean; + asyncBumpAgentPolicyRevision?: boolean; }, context?: RequestHandlerContext, request?: KibanaRequest diff --git a/x-pack/plugins/observability_solution/synthetics/server/synthetics_service/private_location/synthetics_private_location.ts b/x-pack/plugins/observability_solution/synthetics/server/synthetics_service/private_location/synthetics_private_location.ts index fe5f74529121e..0289762f240fc 100644 --- a/x-pack/plugins/observability_solution/synthetics/server/synthetics_service/private_location/synthetics_private_location.ts +++ b/x-pack/plugins/observability_solution/synthetics/server/synthetics_service/private_location/synthetics_private_location.ts @@ -369,7 +369,10 @@ export class SyntheticsPrivateLocation { return await this.server.fleet.packagePolicyService.bulkCreate( soClient, esClient, - newPolicies + newPolicies, + { + asyncBumpAgentPolicyRevision: true, + } ); } } @@ -384,6 +387,7 @@ export class SyntheticsPrivateLocation { policiesToUpdate, { force: true, + asyncBumpAgentPolicyRevision: true, } ); return failedPolicies; @@ -401,6 +405,7 @@ export class SyntheticsPrivateLocation { policyIdsToDelete, { force: true, + asyncBumpAgentPolicyRevision: true, } ); } catch (e) { @@ -430,6 +435,7 @@ export class SyntheticsPrivateLocation { policyIdsToDelete, { force: true, + asyncBumpAgentPolicyRevision: true, } ); const failedPolicies = result?.filter((policy) => {