Skip to content

Commit

Permalink
[Fleet] Fix output backfill for preconfigured outputs (#173088)
Browse files Browse the repository at this point in the history
## Summary

Closes #173081 

Fixes output backfill for existing preconfigured outputs. 

### Checklist

- [x] [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

---------

Co-authored-by: Nicolas Chaulet <nicolas.chaulet@elastic.co>
  • Loading branch information
kpollich and nchaulet committed Dec 11, 2023
1 parent cd0648c commit 61b413e
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
16 changes: 16 additions & 0 deletions x-pack/plugins/fleet/common/services/output_helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
5 changes: 4 additions & 1 deletion x-pack/plugins/fleet/common/services/output_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) : {};
Expand Down
62 changes: 62 additions & 0 deletions x-pack/plugins/fleet/server/services/output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
8 changes: 7 additions & 1 deletion x-pack/plugins/fleet/server/services/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
{
Expand Down

0 comments on commit 61b413e

Please sign in to comment.