Skip to content

Commit

Permalink
Fix error returned when creating an alert with ES security disabled (#…
Browse files Browse the repository at this point in the history
…51639)

* Fix error returned when creating an alert with ES security disabled

* Add test to ensure error gets thrown when inner function throws
  • Loading branch information
mikecote authored Nov 28, 2019
1 parent cd109fa commit af23f30
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ test('createAPIKey() returns { created: false } when security is disabled', asyn
expect(createAPIKeyResult).toEqual({ created: false });
});

test('createAPIKey() returns { created: false } when security is enabled but ES security is disabled', async () => {
const factory = new AlertsClientFactory(alertsClientFactoryParams);
factory.create(KibanaRequest.from(fakeRequest), fakeRequest);
const constructorCall = jest.requireMock('../alerts_client').AlertsClient.mock.calls[0][0];

securityPluginSetup.authc.createAPIKey.mockResolvedValueOnce(null);
const createAPIKeyResult = await constructorCall.createAPIKey();
expect(createAPIKeyResult).toEqual({ created: false });
});

test('createAPIKey() returns an API key when security is enabled', async () => {
const factory = new AlertsClientFactory({
...alertsClientFactoryParams,
Expand All @@ -105,3 +115,17 @@ test('createAPIKey() returns an API key when security is enabled', async () => {
const createAPIKeyResult = await constructorCall.createAPIKey();
expect(createAPIKeyResult).toEqual({ created: true, result: { api_key: '123', id: 'abc' } });
});

test('createAPIKey() throws when security plugin createAPIKey throws an error', async () => {
const factory = new AlertsClientFactory({
...alertsClientFactoryParams,
securityPluginSetup: securityPluginSetup as any,
});
factory.create(KibanaRequest.from(fakeRequest), fakeRequest);
const constructorCall = jest.requireMock('../alerts_client').AlertsClient.mock.calls[0][0];

securityPluginSetup.authc.createAPIKey.mockRejectedValueOnce(new Error('TLS disabled'));
await expect(constructorCall.createAPIKey()).rejects.toThrowErrorMatchingInlineSnapshot(
`"TLS disabled"`
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,16 @@ export class AlertsClientFactory {
if (!securityPluginSetup) {
return { created: false };
}
const createAPIKeyResult = await securityPluginSetup.authc.createAPIKey(request, {
name: `source: alerting, generated uuid: "${uuid.v4()}"`,
role_descriptors: {},
});
if (!createAPIKeyResult) {
return { created: false };
}
return {
created: true,
result: (await securityPluginSetup.authc.createAPIKey(request, {
name: `source: alerting, generated uuid: "${uuid.v4()}"`,
role_descriptors: {},
}))!,
result: createAPIKeyResult,
};
},
});
Expand Down

0 comments on commit af23f30

Please sign in to comment.