Skip to content

Commit

Permalink
async bumpRevision
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 committed Nov 1, 2024
1 parent c319060 commit 1452ba7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 19 deletions.
14 changes: 9 additions & 5 deletions x-pack/plugins/fleet/server/services/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -880,11 +882,13 @@ class AgentPolicyService {
id: string,
options?: { user?: AuthenticatedUser; removeProtection?: boolean }
): Promise<void> {
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,
});
});
}

Expand Down
51 changes: 39 additions & 12 deletions x-pack/plugins/fleet/server/services/package_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ class PackagePolicyClientImpl implements PackagePolicyClient {
user?: AuthenticatedUser;
bumpRevision?: boolean;
force?: true;
asyncBumpAgentPolicyRevision?: boolean;
}
): Promise<{
created: PackagePolicy[];
Expand Down Expand Up @@ -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`);
Expand Down Expand Up @@ -1176,7 +1183,7 @@ class PackagePolicyClientImpl implements PackagePolicyClient {
soClient: SavedObjectsClientContract,
esClient: ElasticsearchClient,
packagePolicyUpdates: Array<NewPackagePolicy & { version?: string; id: string }>,
options?: { user?: AuthenticatedUser; force?: boolean }
options?: { user?: AuthenticatedUser; force?: boolean; asyncBumpAgentPolicyRevision?: boolean }
): Promise<{
updatedPolicies: PackagePolicy[] | null;
failedPolicies: Array<{
Expand Down Expand Up @@ -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<string, { name: string; version: string }> = {};
Expand Down Expand Up @@ -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),
});
}
}
}
}
Expand Down Expand Up @@ -2368,6 +2394,7 @@ class PackagePolicyClientWithAuthz extends PackagePolicyClientImpl {
user?: AuthenticatedUser | undefined;
bumpRevision?: boolean | undefined;
force?: true | undefined;
asyncBumpAgentPolicyRevision?: boolean;
}
| undefined
): Promise<{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export interface PackagePolicyClient {
bumpRevision?: boolean;
force?: true;
authorizationHeader?: HTTPAuthorizationHeader | null;
asyncBumpAgentPolicyRevision?: boolean;
}
): Promise<{
created: PackagePolicy[];
Expand All @@ -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;
Expand Down Expand Up @@ -165,6 +166,7 @@ export interface PackagePolicyClient {
user?: AuthenticatedUser;
skipUnassignFromAgentPolicies?: boolean;
force?: boolean;
asyncBumpAgentPolicyRevision?: boolean;
},
context?: RequestHandlerContext,
request?: KibanaRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,10 @@ export class SyntheticsPrivateLocation {
return await this.server.fleet.packagePolicyService.bulkCreate(
soClient,
esClient,
newPolicies
newPolicies,
{
asyncBumpAgentPolicyRevision: true,
}
);
}
}
Expand All @@ -384,6 +387,7 @@ export class SyntheticsPrivateLocation {
policiesToUpdate,
{
force: true,
asyncBumpAgentPolicyRevision: true,
}
);
return failedPolicies;
Expand All @@ -401,6 +405,7 @@ export class SyntheticsPrivateLocation {
policyIdsToDelete,
{
force: true,
asyncBumpAgentPolicyRevision: true,
}
);
} catch (e) {
Expand Down Expand Up @@ -430,6 +435,7 @@ export class SyntheticsPrivateLocation {
policyIdsToDelete,
{
force: true,
asyncBumpAgentPolicyRevision: true,
}
);
const failedPolicies = result?.filter((policy) => {
Expand Down

0 comments on commit 1452ba7

Please sign in to comment.