diff --git a/x-pack/platform/plugins/shared/fleet/server/services/package_policy.test.ts b/x-pack/platform/plugins/shared/fleet/server/services/package_policy.test.ts index 3d0d3b1d06ff9..09f9cf1e0fa11 100644 --- a/x-pack/platform/plugins/shared/fleet/server/services/package_policy.test.ts +++ b/x-pack/platform/plugins/shared/fleet/server/services/package_policy.test.ts @@ -38,6 +38,7 @@ import type { PostPackagePolicyPostCreateCallback, PostPackagePolicyDeleteCallback, UpdatePackagePolicy, + AssetsMap, } from '../types'; import { createPackagePolicyMock } from '../../common/mocks'; @@ -58,6 +59,7 @@ import type { DeletePackagePoliciesResponse, PackagePolicyAssetsMap, PreconfiguredInputs, + ArchiveEntry, } from '../../common/types'; import { packageToPackagePolicy, packageToPackagePolicyInputs } from '../../common/services'; @@ -73,6 +75,7 @@ import { _compilePackagePolicyInputs, _validateRestrictedFieldsNotModifiedOrThrow, _normalizePackagePolicyKuery, + _getAssetForTemplatePath, } from './package_policy'; import { appContextService } from '.'; @@ -8808,3 +8811,54 @@ describe('_normalizePackagePolicyKuery', () => { expect(res).toEqual('ingest-package-policies.attributes.name:test'); }); }); + +describe('_getAssetForTemplatePath()', () => { + it('should return the asset for the given template path', () => { + const pkgInfo: PackageInfo = { + name: 'test_package', + version: '1.0.0', + type: 'integration', + } as any; + const datasetPath = 'test_data_stream'; + const templatePath = 'log.yml.hbs'; + const assetsMap: AssetsMap = new Map(); + assetsMap.set( + `test_package-1.0.0/data_stream/${datasetPath}/agent/stream/syslog.yml.hbs`, + Buffer.from('wrong match asset') + ); + assetsMap.set( + `test_package-1.0.0/data_stream/${datasetPath}/agent/stream/log.yml.hbs`, + Buffer.from('exact match asset') + ); + + const expectedAsset: ArchiveEntry = { + path: 'test_package-1.0.0/data_stream/test_data_stream/agent/stream/log.yml.hbs', + buffer: Buffer.from('exact match asset'), + }; + const asset = _getAssetForTemplatePath(pkgInfo, assetsMap, datasetPath, templatePath); + expect(asset).toEqual(expectedAsset); + }); + it('should return fallback asset it exact match is not found', () => { + // representing the scenario where the templatePath has the default value 'stream.yml.hbs' + // but the actual asset uses a prefixed name like 'filestream.yml.hbs' + const pkgInfo: PackageInfo = { + name: 'test_package', + version: '1.0.0', + type: 'integration', + } as any; + const datasetPath = 'test_data_stream'; + const templatePath = 'stream.yml.hbs'; + const assetsMap: AssetsMap = new Map(); + assetsMap.set( + `test_package-1.0.0/data_stream/${datasetPath}/agent/stream/filestream.yml.hbs`, + Buffer.from('ends with match asset') + ); + + const expectedFallbackAsset: ArchiveEntry = { + path: 'test_package-1.0.0/data_stream/test_data_stream/agent/stream/filestream.yml.hbs', + buffer: Buffer.from('ends with match asset'), + }; + const asset = _getAssetForTemplatePath(pkgInfo, assetsMap, datasetPath, templatePath); + expect(asset).toEqual(expectedFallbackAsset); + }); +}); diff --git a/x-pack/platform/plugins/shared/fleet/server/services/package_policy.ts b/x-pack/platform/plugins/shared/fleet/server/services/package_policy.ts index b85cfcec13ac0..643032b599f2e 100644 --- a/x-pack/platform/plugins/shared/fleet/server/services/package_policy.ts +++ b/x-pack/platform/plugins/shared/fleet/server/services/package_policy.ts @@ -84,6 +84,7 @@ import type { CloudConnectorSecretVar, AwsCloudConnectorVars, PackagePolicyConfigRecord, + ArchiveEntry, } from '../../common/types'; import { FleetError, @@ -113,6 +114,7 @@ import type { PostPackagePolicyCreateCallback, PostPackagePolicyPostCreateCallback, PutPackagePolicyPostUpdateCallback, + AssetsMap, } from '../types'; import type { ExternalCallback } from '..'; @@ -3287,6 +3289,32 @@ export function _applyIndexPrivileges( return streamOut; } +export function _getAssetForTemplatePath( + pkgInfo: PackageInfo, + assetsMap: AssetsMap, + datasetPath: string, + templatePath: string +): ArchiveEntry { + const assets = getAssetsDataFromAssetsMap( + pkgInfo, + assetsMap, + (path: string) => path.endsWith(`/agent/stream/${templatePath}`), + datasetPath + ); + if (assets.length === 1) { + return assets[0]; + } + + // fallback to old path structure for backward compatibility + const [fallbackPkgStreamTemplate] = getAssetsDataFromAssetsMap( + pkgInfo, + assetsMap, + (path: string) => path.endsWith(templatePath), + datasetPath + ); + return fallbackPkgStreamTemplate; +} + function _compilePackageStream( pkgInfo: PackageInfo, vars: PackagePolicy['vars'], @@ -3334,11 +3362,11 @@ function _compilePackageStream( const datasetPath = packageDataStream.path; - const [pkgStreamTemplate] = getAssetsDataFromAssetsMap( + const pkgStreamTemplate = _getAssetForTemplatePath( pkgInfo, assetsMap, - (path: string) => path.endsWith(streamFromPkg.template_path), - datasetPath + datasetPath, + streamFromPkg.template_path ); if (!pkgStreamTemplate || !pkgStreamTemplate.buffer) {