Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import type {
PostPackagePolicyPostCreateCallback,
PostPackagePolicyDeleteCallback,
UpdatePackagePolicy,
AssetsMap,
} from '../types';
import { createPackagePolicyMock } from '../../common/mocks';

Expand All @@ -58,6 +59,7 @@ import type {
DeletePackagePoliciesResponse,
PackagePolicyAssetsMap,
PreconfiguredInputs,
ArchiveEntry,
} from '../../common/types';
import { packageToPackagePolicy, packageToPackagePolicyInputs } from '../../common/services';

Expand All @@ -73,6 +75,7 @@ import {
_compilePackagePolicyInputs,
_validateRestrictedFieldsNotModifiedOrThrow,
_normalizePackagePolicyKuery,
_getAssetForTemplatePath,
} from './package_policy';
import { appContextService } from '.';

Expand Down Expand Up @@ -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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ import type {
CloudConnectorSecretVar,
AwsCloudConnectorVars,
PackagePolicyConfigRecord,
ArchiveEntry,
} from '../../common/types';
import {
FleetError,
Expand Down Expand Up @@ -113,6 +114,7 @@ import type {
PostPackagePolicyCreateCallback,
PostPackagePolicyPostCreateCallback,
PutPackagePolicyPostUpdateCallback,
AssetsMap,
} from '../types';
import type { ExternalCallback } from '..';

Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -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) {
Expand Down
Loading