Skip to content

Commit

Permalink
add licence checker wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
mshustov committed Apr 9, 2020
1 parent 877fa0f commit f3385ee
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions x-pack/plugins/licensing/common/licensing.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const createLicenseMock = () => {
};
mock.check.mockReturnValue({ state: 'valid' });
mock.hasAtLeast.mockReturnValue(true);
mock.getFeature.mockReturnValue({ isAvailable: true, isEnabled: true });
return mock;
};
export const licenseMock = {
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/licensing/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ export const plugin = (context: PluginInitializerContext) => new LicensingPlugin
export * from '../common/types';
export * from './types';
export { config } from './licensing_config';
export {
CheckLicense,
licenseCheckerRouteHandlerWrapper,
} from './license_checker_route_handler_wrapper';
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import {
RequestHandler,
RequestHandlerContext,
KibanaRequest,
RouteMethod,
KibanaResponseFactory,
} from 'src/core/server';

import { ILicense } from '../common/types';

export type CheckLicense = (
license: ILicense
) => { valid: false; message: string } | { valid: true; message: null };

export function licenseCheckerRouteHandlerWrapper<P, Q, B>(
checkLicense: CheckLicense,
handler: RequestHandler<P, Q, B>
): RequestHandler<P, Q, B> {
return async (
context: RequestHandlerContext,
request: KibanaRequest<P, Q, B, RouteMethod>,
response: KibanaResponseFactory
) => {
const licenseCheckResult = checkLicense(context.licensing.license);

if (licenseCheckResult.valid) {
return handler(context, request, response);
} else {
return response.forbidden({
body: licenseCheckResult.message,
});
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { httpServerMock } from 'src/core/server/mocks';

import {
licenseCheckerRouteHandlerWrapper,
CheckLicense,
} from './license_checker_route_handler_wrapper';

const context = {
licensing: {
license: {},
},
} as any;
const request = httpServerMock.createKibanaRequest();

describe('licenseCheckerRouteHandlerWrapper', () => {
it('calls route handler if checkLicense returns "valid": true', async () => {
const checkLicense: CheckLicense = () => ({ valid: true, message: null });
const routeHandler = jest.fn();
const wrapper = licenseCheckerRouteHandlerWrapper(checkLicense, routeHandler);
const response = httpServerMock.createResponseFactory();

await wrapper(context, request, response);

expect(routeHandler).toHaveBeenCalledTimes(1);
expect(routeHandler).toHaveBeenCalledWith(context, request, response);
});

it('does not call route handler if checkLicense returns "valid": false', async () => {
const checkLicense: CheckLicense = () => ({ valid: false, message: 'reason' });
const routeHandler = jest.fn();
const wrapper = licenseCheckerRouteHandlerWrapper(checkLicense, routeHandler);
const response = httpServerMock.createResponseFactory();

await wrapper(context, request, response);

expect(routeHandler).toHaveBeenCalledTimes(0);
expect(response.forbidden).toHaveBeenCalledTimes(1);
expect(response.forbidden).toHaveBeenCalledWith({ body: 'reason' });
});

it('allows an exception to bubble up if handler throws', async () => {
const checkLicense: CheckLicense = () => ({ valid: true, message: null });
const routeHandler = () => {
throw new Error('reason');
};
const wrapper = licenseCheckerRouteHandlerWrapper(checkLicense, routeHandler);
const response = httpServerMock.createResponseFactory();

await expect(wrapper(context, request, response)).rejects.toThrowErrorMatchingInlineSnapshot(
`"reason"`
);
});
});

0 comments on commit f3385ee

Please sign in to comment.