Skip to content

Commit 0c932fd

Browse files
[UII] Drop ssl field from output if it's in an invalid format (#211848)
## Summary Reported in elastic/sdh-beats#5676. When retrieving outputs information, drop the `ssl` field if it's in an invalid format (causing `JSON.parse` to fail). This can happen for ES output with `ssl` field set via API, and then subsequently editing it from the UI (ES output `ssl` fields are currently unsupported, soon to be supported by #207326. --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
1 parent aa59ccd commit 0c932fd

2 files changed

Lines changed: 56 additions & 8 deletions

File tree

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

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { agentPolicyService } from './agent_policy';
2222
import { packagePolicyService } from './package_policy';
2323
import { auditLoggingService } from './audit_logging';
2424
import { findAgentlessPolicies } from './outputs/helpers';
25+
import { outputSavedObjectToOutput } from './output';
2526

2627
jest.mock('./app_context');
2728
jest.mock('./agent_policy');
@@ -40,13 +41,14 @@ mockedAppContextService.getSecuritySetup.mockImplementation(() => ({
4041
...securityMock.createSetup(),
4142
}));
4243

44+
const mockedLogger = {
45+
debug: jest.fn(),
46+
info: jest.fn(),
47+
warn: jest.fn(),
48+
error: jest.fn(),
49+
} as unknown as Logger;
4350
mockedAppContextService.getLogger.mockImplementation(() => {
44-
return {
45-
debug: jest.fn(),
46-
info: jest.fn(),
47-
warn: jest.fn(),
48-
error: jest.fn(),
49-
} as unknown as Logger;
51+
return mockedLogger;
5052
});
5153

5254
mockedAppContextService.getExperimentalFeatures.mockReturnValue({} as any);
@@ -2590,4 +2592,42 @@ describe('Output Service', () => {
25902592
await expect(promise).resolves.not.toThrow();
25912593
});
25922594
});
2595+
2596+
describe('outputSavedObjectToOutput', () => {
2597+
it('should return output object with parsed SSL when SSL is a valid JSON string', () => {
2598+
const so = mockOutputSO('output-test', {
2599+
ssl: '{ "certificate": "cert", "key": "key" }',
2600+
});
2601+
2602+
const output = outputSavedObjectToOutput(so);
2603+
2604+
expect(output.ssl).toEqual({ certificate: 'cert', key: 'key' });
2605+
});
2606+
2607+
it('should return output object with no SSL field when SSL is an invalid JSON string', () => {
2608+
const so = mockOutputSO('output-test', {
2609+
ssl: 'invalid-json',
2610+
});
2611+
2612+
const output = outputSavedObjectToOutput(so);
2613+
2614+
expect(output.ssl).toEqual(undefined);
2615+
expect(mockedLogger.warn).toHaveBeenCalledWith(
2616+
expect.stringContaining(`Unable to parse ssl for output ${so.id}`)
2617+
);
2618+
expect(mockedLogger.warn).toHaveBeenCalledWith(
2619+
expect.stringContaining(`ssl value: invalid-json`)
2620+
);
2621+
});
2622+
2623+
it('should return output object with no SSL field when SSL is not a string', () => {
2624+
const so = mockOutputSO('output-test', {
2625+
ssl: { certificate: 'cert', key: 'key' },
2626+
});
2627+
2628+
const output = outputSavedObjectToOutput(so);
2629+
2630+
expect(output.ssl).toEqual(undefined);
2631+
});
2632+
});
25932633
});

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,21 @@ export function outputIdToUuid(id: string) {
122122
return uuidv5(id, uuidv5.DNS);
123123
}
124124

125-
function outputSavedObjectToOutput(so: SavedObject<OutputSOAttributes>): Output {
125+
export function outputSavedObjectToOutput(so: SavedObject<OutputSOAttributes>): Output {
126+
const logger = appContextService.getLogger();
126127
const { output_id: outputId, ssl, proxy_id: proxyId, ...atributes } = so.attributes;
127128

129+
let parsedSsl;
130+
try {
131+
parsedSsl = typeof ssl === 'string' ? JSON.parse(ssl) : undefined;
132+
} catch (e) {
133+
logger.warn(`Unable to parse ssl for output ${so.id}: ${e.message}`);
134+
logger.warn(`ssl value: ${ssl}`);
135+
}
128136
return {
129137
id: outputId ?? so.id,
130138
...atributes,
131-
...(ssl ? { ssl: JSON.parse(ssl as string) } : {}),
139+
...(parsedSsl ? { ssl: parsedSsl } : {}),
132140
...(proxyId ? { proxy_id: proxyId } : {}),
133141
};
134142
}

0 commit comments

Comments
 (0)