Skip to content

Commit

Permalink
[Fleet] Set package policy compile streams on bulkCreate (#140694)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 authored Sep 14, 2022
1 parent 5ad7802 commit 02e1d7f
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions x-pack/plugins/fleet/server/services/package_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,31 +242,47 @@ class PackagePolicyClientImpl implements PackagePolicyClient {
force?: true;
}
): Promise<PackagePolicy[]> {
const agentPolicyIds = new Set(packagePolicies.map((pkgPol) => pkgPol.policy_id));
const agentPolicyIds = new Set(packagePolicies.map((pkgPolicy) => pkgPolicy.policy_id));

for (const agentPolicyId of agentPolicyIds) {
await validateIsNotHostedPolicy(soClient, agentPolicyId, options?.force);
}

const packageInfos = await getPackageInfoForPackagePolicies(packagePolicies, soClient);

const isoDate = new Date().toISOString();
// eslint-disable-next-line @typescript-eslint/naming-convention
const { saved_objects } = await soClient.bulkCreate<PackagePolicySOAttributes>(
packagePolicies.map((packagePolicy) => {
await pMap(packagePolicies, async (packagePolicy) => {
const packagePolicyId = packagePolicy.id ?? uuid.v4();
const agentPolicyId = packagePolicy.policy_id;

const inputs = packagePolicy.inputs.map((input) =>
let inputs = packagePolicy.inputs.map((input) =>
assignStreamIdToInput(packagePolicyId, input)
);

const { id, ...pkgPolicyWithoutId } = packagePolicy;

let elasticsearch: PackagePolicy['elasticsearch'];
if (packagePolicy.package) {
const pkgInfo = packageInfos.get(
`${packagePolicy.package.name}-${packagePolicy.package.version}`
);

inputs = pkgInfo
? await _compilePackagePolicyInputs(pkgInfo, packagePolicy.vars || {}, inputs)
: inputs;

elasticsearch = pkgInfo?.elasticsearch;
}

return {
type: SAVED_OBJECT_TYPE,
id: packagePolicyId,
attributes: {
...pkgPolicyWithoutId,
inputs,
elasticsearch,
policy_id: agentPolicyId,
revision: 1,
created_at: isoDate,
Expand Down Expand Up @@ -1457,6 +1473,36 @@ export interface NewPackagePolicyWithId extends NewPackagePolicy {

export const packagePolicyService: PackagePolicyClient = new PackagePolicyClientImpl();

async function getPackageInfoForPackagePolicies(
packagePolicies: NewPackagePolicyWithId[],
soClient: SavedObjectsClientContract
) {
const pkgInfoMap = new Map<string, PackagePolicyPackage>();

packagePolicies.forEach(({ package: pkg }) => {
if (pkg) {
pkgInfoMap.set(`${pkg.name}-${pkg.version}`, pkg);
}
});

const resultMap = new Map<string, PackageInfo>();

await pMap(pkgInfoMap.keys(), async (pkgKey) => {
const pkgInfo = pkgInfoMap.get(pkgKey);
if (pkgInfo) {
const pkgInfoData = await getPackageInfo({
savedObjectsClient: soClient,
pkgName: pkgInfo.name,
pkgVersion: pkgInfo.version,
});

resultMap.set(pkgKey, pkgInfoData);
}
});

return resultMap;
}

export function updatePackageInputs(
basePackagePolicy: NewPackagePolicy,
packageInfo: PackageInfo,
Expand Down

0 comments on commit 02e1d7f

Please sign in to comment.