Skip to content

Commit aca8d6f

Browse files
authored
[Fleet] Fix asset for template path filter (#240750)
## Summary This PR fixes the filter that was selecting the asset for a given `template_path`. When compiling the template for a given input, fleet was selecting it through the asset map taking into account the rule "path ends with X". This method opened the door to some bug behavour in the following case: - A template path `log.yml.hbs` - An asset map with `log.yml.hbs` and `syslog.yml.hbs` Both assets where selected, the first one was chosen, but not necessary the first was `log.yml.hbs` although both end with this string. The change introduces a change when selecting the template path to get the exact file, although a fallback to the old logic is kept as there are cases where the endsWith logic makes sense. This is when a default template path is used (`stream.yml.hbs`) and the asset is called `filestream.yml.hbs` Related change at package-spec elastic/package-spec#1002 Original issue elastic/package-spec#703 ### Checklist Check the PR satisfies following conditions. Reviewers should verify this PR satisfies this list as well. - [ ] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md) - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [ ] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [ ] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [ ] This was checked for breaking HTTP API changes, and any breaking changes have been approved by the breaking-change committee. The `release_note:breaking` label should be applied in these situations. - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [ ] The PR description includes the appropriate Release Notes section, and the correct `release_note:*` label is applied per the [guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) - [ ] Review the [backport guidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing) and apply applicable `backport:*` labels. ## Release note Fixes `template_path` asset selection for some cases of integration packages
1 parent 1109b60 commit aca8d6f

File tree

2 files changed

+85
-3
lines changed

2 files changed

+85
-3
lines changed

x-pack/platform/plugins/shared/fleet/server/services/package_policy.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import type {
3838
PostPackagePolicyPostCreateCallback,
3939
PostPackagePolicyDeleteCallback,
4040
UpdatePackagePolicy,
41+
AssetsMap,
4142
} from '../types';
4243
import { createPackagePolicyMock } from '../../common/mocks';
4344

@@ -58,6 +59,7 @@ import type {
5859
DeletePackagePoliciesResponse,
5960
PackagePolicyAssetsMap,
6061
PreconfiguredInputs,
62+
ArchiveEntry,
6163
} from '../../common/types';
6264
import { packageToPackagePolicy, packageToPackagePolicyInputs } from '../../common/services';
6365

@@ -73,6 +75,7 @@ import {
7375
_compilePackagePolicyInputs,
7476
_validateRestrictedFieldsNotModifiedOrThrow,
7577
_normalizePackagePolicyKuery,
78+
_getAssetForTemplatePath,
7679
} from './package_policy';
7780
import { appContextService } from '.';
7881

@@ -8808,3 +8811,54 @@ describe('_normalizePackagePolicyKuery', () => {
88088811
expect(res).toEqual('ingest-package-policies.attributes.name:test');
88098812
});
88108813
});
8814+
8815+
describe('_getAssetForTemplatePath()', () => {
8816+
it('should return the asset for the given template path', () => {
8817+
const pkgInfo: PackageInfo = {
8818+
name: 'test_package',
8819+
version: '1.0.0',
8820+
type: 'integration',
8821+
} as any;
8822+
const datasetPath = 'test_data_stream';
8823+
const templatePath = 'log.yml.hbs';
8824+
const assetsMap: AssetsMap = new Map();
8825+
assetsMap.set(
8826+
`test_package-1.0.0/data_stream/${datasetPath}/agent/stream/syslog.yml.hbs`,
8827+
Buffer.from('wrong match asset')
8828+
);
8829+
assetsMap.set(
8830+
`test_package-1.0.0/data_stream/${datasetPath}/agent/stream/log.yml.hbs`,
8831+
Buffer.from('exact match asset')
8832+
);
8833+
8834+
const expectedAsset: ArchiveEntry = {
8835+
path: 'test_package-1.0.0/data_stream/test_data_stream/agent/stream/log.yml.hbs',
8836+
buffer: Buffer.from('exact match asset'),
8837+
};
8838+
const asset = _getAssetForTemplatePath(pkgInfo, assetsMap, datasetPath, templatePath);
8839+
expect(asset).toEqual(expectedAsset);
8840+
});
8841+
it('should return fallback asset it exact match is not found', () => {
8842+
// representing the scenario where the templatePath has the default value 'stream.yml.hbs'
8843+
// but the actual asset uses a prefixed name like 'filestream.yml.hbs'
8844+
const pkgInfo: PackageInfo = {
8845+
name: 'test_package',
8846+
version: '1.0.0',
8847+
type: 'integration',
8848+
} as any;
8849+
const datasetPath = 'test_data_stream';
8850+
const templatePath = 'stream.yml.hbs';
8851+
const assetsMap: AssetsMap = new Map();
8852+
assetsMap.set(
8853+
`test_package-1.0.0/data_stream/${datasetPath}/agent/stream/filestream.yml.hbs`,
8854+
Buffer.from('ends with match asset')
8855+
);
8856+
8857+
const expectedFallbackAsset: ArchiveEntry = {
8858+
path: 'test_package-1.0.0/data_stream/test_data_stream/agent/stream/filestream.yml.hbs',
8859+
buffer: Buffer.from('ends with match asset'),
8860+
};
8861+
const asset = _getAssetForTemplatePath(pkgInfo, assetsMap, datasetPath, templatePath);
8862+
expect(asset).toEqual(expectedFallbackAsset);
8863+
});
8864+
});

x-pack/platform/plugins/shared/fleet/server/services/package_policy.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ import type {
8484
CloudConnectorSecretVar,
8585
AwsCloudConnectorVars,
8686
PackagePolicyConfigRecord,
87+
ArchiveEntry,
8788
} from '../../common/types';
8889
import {
8990
FleetError,
@@ -113,6 +114,7 @@ import type {
113114
PostPackagePolicyCreateCallback,
114115
PostPackagePolicyPostCreateCallback,
115116
PutPackagePolicyPostUpdateCallback,
117+
AssetsMap,
116118
} from '../types';
117119
import type { ExternalCallback } from '..';
118120

@@ -3287,6 +3289,32 @@ export function _applyIndexPrivileges(
32873289
return streamOut;
32883290
}
32893291

3292+
export function _getAssetForTemplatePath(
3293+
pkgInfo: PackageInfo,
3294+
assetsMap: AssetsMap,
3295+
datasetPath: string,
3296+
templatePath: string
3297+
): ArchiveEntry {
3298+
const assets = getAssetsDataFromAssetsMap(
3299+
pkgInfo,
3300+
assetsMap,
3301+
(path: string) => path.endsWith(`/agent/stream/${templatePath}`),
3302+
datasetPath
3303+
);
3304+
if (assets.length === 1) {
3305+
return assets[0];
3306+
}
3307+
3308+
// fallback to old path structure for backward compatibility
3309+
const [fallbackPkgStreamTemplate] = getAssetsDataFromAssetsMap(
3310+
pkgInfo,
3311+
assetsMap,
3312+
(path: string) => path.endsWith(templatePath),
3313+
datasetPath
3314+
);
3315+
return fallbackPkgStreamTemplate;
3316+
}
3317+
32903318
function _compilePackageStream(
32913319
pkgInfo: PackageInfo,
32923320
vars: PackagePolicy['vars'],
@@ -3334,11 +3362,11 @@ function _compilePackageStream(
33343362

33353363
const datasetPath = packageDataStream.path;
33363364

3337-
const [pkgStreamTemplate] = getAssetsDataFromAssetsMap(
3365+
const pkgStreamTemplate = _getAssetForTemplatePath(
33383366
pkgInfo,
33393367
assetsMap,
3340-
(path: string) => path.endsWith(streamFromPkg.template_path),
3341-
datasetPath
3368+
datasetPath,
3369+
streamFromPkg.template_path
33423370
);
33433371

33443372
if (!pkgStreamTemplate || !pkgStreamTemplate.buffer) {

0 commit comments

Comments
 (0)