Skip to content

Commit fb877fa

Browse files
authored
Fix error returned when creating an alert with ES security disabled (#51639) (#51879)
* Fix error returned when creating an alert with ES security disabled * Add test to ensure error gets thrown when inner function throws
1 parent bde8c42 commit fb877fa

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

x-pack/legacy/plugins/alerting/server/lib/alerts_client_factory.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ test('createAPIKey() returns { created: false } when security is disabled', asyn
9393
expect(createAPIKeyResult).toEqual({ created: false });
9494
});
9595

96+
test('createAPIKey() returns { created: false } when security is enabled but ES security is disabled', async () => {
97+
const factory = new AlertsClientFactory(alertsClientFactoryParams);
98+
factory.create(KibanaRequest.from(fakeRequest), fakeRequest);
99+
const constructorCall = jest.requireMock('../alerts_client').AlertsClient.mock.calls[0][0];
100+
101+
securityPluginSetup.authc.createAPIKey.mockResolvedValueOnce(null);
102+
const createAPIKeyResult = await constructorCall.createAPIKey();
103+
expect(createAPIKeyResult).toEqual({ created: false });
104+
});
105+
96106
test('createAPIKey() returns an API key when security is enabled', async () => {
97107
const factory = new AlertsClientFactory({
98108
...alertsClientFactoryParams,
@@ -105,3 +115,17 @@ test('createAPIKey() returns an API key when security is enabled', async () => {
105115
const createAPIKeyResult = await constructorCall.createAPIKey();
106116
expect(createAPIKeyResult).toEqual({ created: true, result: { api_key: '123', id: 'abc' } });
107117
});
118+
119+
test('createAPIKey() throws when security plugin createAPIKey throws an error', async () => {
120+
const factory = new AlertsClientFactory({
121+
...alertsClientFactoryParams,
122+
securityPluginSetup: securityPluginSetup as any,
123+
});
124+
factory.create(KibanaRequest.from(fakeRequest), fakeRequest);
125+
const constructorCall = jest.requireMock('../alerts_client').AlertsClient.mock.calls[0][0];
126+
127+
securityPluginSetup.authc.createAPIKey.mockRejectedValueOnce(new Error('TLS disabled'));
128+
await expect(constructorCall.createAPIKey()).rejects.toThrowErrorMatchingInlineSnapshot(
129+
`"TLS disabled"`
130+
);
131+
});

x-pack/legacy/plugins/alerting/server/lib/alerts_client_factory.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,16 @@ export class AlertsClientFactory {
5353
if (!securityPluginSetup) {
5454
return { created: false };
5555
}
56+
const createAPIKeyResult = await securityPluginSetup.authc.createAPIKey(request, {
57+
name: `source: alerting, generated uuid: "${uuid.v4()}"`,
58+
role_descriptors: {},
59+
});
60+
if (!createAPIKeyResult) {
61+
return { created: false };
62+
}
5663
return {
5764
created: true,
58-
result: (await securityPluginSetup.authc.createAPIKey(request, {
59-
name: `source: alerting, generated uuid: "${uuid.v4()}"`,
60-
role_descriptors: {},
61-
}))!,
65+
result: createAPIKeyResult,
6266
};
6367
},
6468
});

0 commit comments

Comments
 (0)