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

[Lists] Add an instance of ExceptionListClient with server extension points turned off to context object provided to callbacks #123885

Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -12,6 +12,7 @@ import {
createExtensionPointStorageMock,
} from '../extension_points/extension_point_storage.mock';
import type { ExtensionPointCallbackDataArgument } from '../extension_points';
import { httpServerMock } from '../../../../../../src/core/server/mocks';

import {
getCreateExceptionListItemOptionsMock,
Expand Down Expand Up @@ -49,13 +50,16 @@ describe('exception_list_client', () => {
describe('server extension points execution', () => {
let extensionPointStorageContext: ExtensionPointStorageContextMock;
let exceptionListClient: ExceptionListClient;
let kibanaRequest: ReturnType<typeof httpServerMock.createKibanaRequest>;

beforeEach(() => {
extensionPointStorageContext = createExtensionPointStorageMock();
kibanaRequest = httpServerMock.createKibanaRequest();
});

it('should initialize class instance with `enableServerExtensionPoints` enabled by default', async () => {
exceptionListClient = new ExceptionListClient({
request: kibanaRequest,
savedObjectsClient: getExceptionListSavedObjectClientMock(),
serverExtensionsClient: extensionPointStorageContext.extensionPointStorage.getClient(),
user: 'elastic',
Expand Down Expand Up @@ -98,6 +102,7 @@ describe('exception_list_client', () => {
describe('and server extension points are enabled', () => {
beforeEach(() => {
exceptionListClient = new ExceptionListClient({
request: kibanaRequest,
savedObjectsClient: getExceptionListSavedObjectClientMock(),
serverExtensionsClient:
extensionPointStorageContext.extensionPointStorage.getClient(),
Expand All @@ -111,6 +116,15 @@ describe('exception_list_client', () => {
expect(getExtensionPointCallback()).toHaveBeenCalled();
});

it('should provide `context` object to extension point callbacks', async () => {
await callExceptionListClientMethod();

expect(getExtensionPointCallback().mock.calls[0][0].context).toEqual({
exceptionListClient: expect.any(ExceptionListClient),
request: kibanaRequest,
});
});

it('should error if extension point callback throws an error', async () => {
const error = new Error('foo');
const extensionCallback = getExtensionPointCallback();
Expand Down Expand Up @@ -157,6 +171,7 @@ describe('exception_list_client', () => {
beforeEach(() => {
exceptionListClient = new ExceptionListClient({
enableServerExtensionPoints: false,
request: kibanaRequest,
savedObjectsClient: getExceptionListSavedObjectClientMock(),
serverExtensionsClient:
extensionPointStorageContext.extensionPointStorage.getClient(),
Expand Down Expand Up @@ -304,6 +319,7 @@ describe('exception_list_client', () => {
(methodName, callExceptionListClientMethod, getExtensionPointCallback) => {
beforeEach(() => {
exceptionListClient = new ExceptionListClient({
request: kibanaRequest,
savedObjectsClient: getExceptionListSavedObjectClientMock(),
serverExtensionsClient: extensionPointStorageContext.extensionPointStorage.getClient(),
user: 'elastic',
Expand All @@ -316,6 +332,15 @@ describe('exception_list_client', () => {
expect(getExtensionPointCallback()).toHaveBeenCalled();
});

it('should provide `context` object to extension point callbacks', async () => {
await callExceptionListClientMethod();

expect(getExtensionPointCallback().mock.calls[0][0].context).toEqual({
exceptionListClient: expect.any(ExceptionListClient),
request: kibanaRequest,
});
});

it('should error if extension point callback throws an error', async () => {
const error = new Error('foo');
const extensionCallback = getExtensionPointCallback();
Expand All @@ -331,6 +356,7 @@ describe('exception_list_client', () => {
beforeEach(() => {
exceptionListClient = new ExceptionListClient({
enableServerExtensionPoints: false,
request: kibanaRequest,
savedObjectsClient: getExceptionListSavedObjectClientMock(),
serverExtensionsClient:
extensionPointStorageContext.extensionPointStorage.getClient(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,24 @@ export class ExceptionListClient {
}

private getServerExtensionCallbackContext(): ServerExtensionCallbackContext {
const { user, serverExtensionsClient, savedObjectsClient, request } = this;
let exceptionListClient: undefined | ExceptionListClient;

return {
// Lazy getter so that we only initialize a new instance of the class if needed
get exceptionListClient(): ExceptionListClient {
if (!exceptionListClient) {
exceptionListClient = new ExceptionListClient({
enableServerExtensionPoints: false,
request,
savedObjectsClient,
serverExtensionsClient,
user,
});
}

return exceptionListClient;
},
request: this.request,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { MockedLogger, loggerMock } from '@kbn/logging/mocks';

import { httpServerMock } from '../../../../../../src/core/server/mocks';
import { ExceptionListClient } from '../exception_lists/exception_list_client';

import { ExtensionPointStorage } from './extension_point_storage';
import {
Expand Down Expand Up @@ -122,6 +123,7 @@ export const createExtensionPointStorageMock = (

return {
callbackContext: {
exceptionListClient: {} as unknown as ExceptionListClient,
request: httpServerMock.createKibanaRequest(),
},
exceptionPreCreate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
UpdateExceptionListItemOptions,
} from '../exception_lists/exception_list_client_types';
import { PromiseFromStreams } from '../exception_lists/import_exception_list_and_items';
import type { ExceptionListClient } from '../exception_lists/exception_list_client';

/**
* The `this` context provided to extension point's callback function
Expand All @@ -30,6 +31,13 @@ export interface ServerExtensionCallbackContext {
* is not triggered via one of the HTTP handlers
*/
request?: KibanaRequest;

/**
* An `ExceptionListClient` instance that **DOES NOT** execute server extension point callbacks.
* This client should be used when needing to access Exception List content from within an Extension
* Point to avoid circular infinite loops
*/
exceptionListClient: ExceptionListClient;
}

export type ServerExtensionCallback<A extends object | void = void, R = A> = (args: {
Expand Down