[8.12] [Fleet] Fix output backfill for preconfigured outputs (#173088) (#173118)

# Backport

This will backport the following commits from `main` to `8.12`:
- [[Fleet] Fix output backfill for preconfigured outputs
(#173088)](https://github.com/elastic/kibana/pull/173088)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Kyle
Pollich","email":"kyle.pollich@elastic.co"},"sourceCommit":{"committedDate":"2023-12-11T21:16:34Z","message":"[Fleet]
Fix output backfill for preconfigured outputs (#173088)\n\n##
Summary\r\n\r\nCloses #173081 \r\n\r\nFixes output backfill for existing
preconfigured outputs. \r\n\r\n### Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\n---------\r\n\r\nCo-authored-by: Nicolas Chaulet
<nicolas.chaulet@elastic.co>","sha":"61b413ea05734b94c82cd07cef048f281deda3bd","branchLabelMapping":{"^v8.13.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:Fleet","backport:prev-minor","v8.13.0"],"number":173088,"url":"https://github.com/elastic/kibana/pull/173088","mergeCommit":{"message":"[Fleet]
Fix output backfill for preconfigured outputs (#173088)\n\n##
Summary\r\n\r\nCloses #173081 \r\n\r\nFixes output backfill for existing
preconfigured outputs. \r\n\r\n### Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\n---------\r\n\r\nCo-authored-by: Nicolas Chaulet
<nicolas.chaulet@elastic.co>","sha":"61b413ea05734b94c82cd07cef048f281deda3bd"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.13.0","labelRegex":"^v8.13.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/173088","number":173088,"mergeCommit":{"message":"[Fleet]
Fix output backfill for preconfigured outputs (#173088)\n\n##
Summary\r\n\r\nCloses #173081 \r\n\r\nFixes output backfill for existing
preconfigured outputs. \r\n\r\n### Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\n---------\r\n\r\nCo-authored-by: Nicolas Chaulet
<nicolas.chaulet@elastic.co>","sha":"61b413ea05734b94c82cd07cef048f281deda3bd"}}]}]
BACKPORT-->

Co-authored-by: Kyle Pollich <kyle.pollich@elastic.co>
This commit is contained in:
Kibana Machine 2023-12-11 17:36:57 -05:00 committed by GitHub
parent 5935014365
commit 3768603ca0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 89 additions and 2 deletions

View file

@ -101,4 +101,20 @@ describe('outputYmlIncludesReservedPerformanceKey', () => {
expect(outputYmlIncludesReservedPerformanceKey(configYml, safeLoad)).toBe(false);
});
});
describe('comment', () => {
it('returns false when reserved key is present only in a comment', () => {
const configYml = `true`;
expect(outputYmlIncludesReservedPerformanceKey(configYml, safeLoad)).toBe(false);
});
});
describe('empty string', () => {
it('returns false when YML is empty', () => {
const configYml = ``;
expect(outputYmlIncludesReservedPerformanceKey(configYml, safeLoad)).toBe(false);
});
});
});

View file

@ -52,7 +52,10 @@ export function outputYmlIncludesReservedPerformanceKey(
const parsedYml = safeLoad(configYml);
if (!isObject(parsedYml)) {
return RESERVED_CONFIG_YML_KEYS.some((key) => parsedYml.includes(key));
if (typeof parsedYml === 'string') {
return RESERVED_CONFIG_YML_KEYS.some((key) => parsedYml.includes(key));
}
return false;
}
const flattenedYml = isObject(parsedYml) ? getFlattenedObject(parsedYml) : {};

View file

@ -1841,4 +1841,66 @@ describe('Output Service', () => {
});
});
});
describe('backfillAllOutputPresets', () => {
it('should update non-preconfigured output', async () => {
const soClient = getMockedSoClient({});
soClient.find.mockResolvedValue({
saved_objects: [
{
...mockOutputSO('non-preconfigured-output', {
is_preconfigured: false,
type: 'elasticsearch',
}),
score: 0,
},
],
total: 1,
per_page: 1,
page: 1,
});
soClient.get.mockResolvedValue({
...mockOutputSO('non-preconfigured-output', {
is_preconfigured: false,
type: 'elasticsearch',
}),
});
const promise = outputService.backfillAllOutputPresets(soClient, esClientMock);
await expect(promise).resolves.not.toThrow();
});
it('should update preconfigured output', async () => {
const soClient = getMockedSoClient({});
soClient.find.mockResolvedValue({
saved_objects: [
{
...mockOutputSO('preconfigured-output', {
is_preconfigured: true,
type: 'elasticsearch',
}),
score: 0,
},
],
total: 1,
per_page: 1,
page: 1,
});
soClient.get.mockResolvedValue({
...mockOutputSO('preconfigured-output', {
is_preconfigured: true,
type: 'elasticsearch',
}),
});
const promise = outputService.backfillAllOutputPresets(soClient, esClientMock);
await expect(promise).resolves.not.toThrow();
});
});
});

View file

@ -1023,7 +1023,13 @@ class OutputService {
async (output) => {
const preset = getDefaultPresetForEsOutput(output.config_yaml ?? '', safeLoad);
await outputService.update(soClient, esClient, output.id, { preset });
await outputService.update(
soClient,
esClient,
output.id,
{ preset },
{ fromPreconfiguration: true }
);
await agentPolicyService.bumpAllAgentPoliciesForOutput(soClient, esClient, output.id);
},
{