Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error returned when creating an alert with ES security disabled #51639

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be a good idea to add a functional test around this to test what happens when createAPIKey throws an error, as the context seems to be one where we need to ensure we gracefully handle it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 4d30e0f. It is worth noting we won't gracefully handle errors thrown. The function will make sure errors are passed along. This is in the scenario TLS is disabled and we can't allow alerts being created without API keys (bad environment setup and will be handled here: #49403).

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