From d3f231a2183232d4344b1c7b23bfb96828f986ef Mon Sep 17 00:00:00 2001 From: CJ Cenizal Date: Thu, 15 Apr 2021 15:08:32 -0700 Subject: [PATCH 1/3] Rename license_api_guard READM -> README. --- docs/developer/plugin-list.asciidoc | 4 ++-- x-pack/plugins/license_api_guard/READM.md | 3 --- x-pack/plugins/license_api_guard/README.md | 3 +++ 3 files changed, 5 insertions(+), 5 deletions(-) delete mode 100644 x-pack/plugins/license_api_guard/READM.md create mode 100644 x-pack/plugins/license_api_guard/README.md diff --git a/docs/developer/plugin-list.asciidoc b/docs/developer/plugin-list.asciidoc index de7253e34d103d..c7fffb09248e92 100644 --- a/docs/developer/plugin-list.asciidoc +++ b/docs/developer/plugin-list.asciidoc @@ -444,8 +444,8 @@ the infrastructure monitoring use-case within Kibana. |Visualization editor allowing to quickly and easily configure compelling visualizations to use on dashboards and canvas workpads. -|{kib-repo}blob/{branch}/x-pack/plugins/license_api_guard[licenseApiGuard] -|WARNING: Missing README. +|{kib-repo}blob/{branch}/x-pack/plugins/license_api_guard/README.md[licenseApiGuard] +|This plugin is used by ES UI plugins to reject API requests when the plugin is unsupported by the user's license. |{kib-repo}blob/{branch}/x-pack/plugins/license_management/README.md[licenseManagement] diff --git a/x-pack/plugins/license_api_guard/READM.md b/x-pack/plugins/license_api_guard/READM.md deleted file mode 100644 index 767223125b12c3..00000000000000 --- a/x-pack/plugins/license_api_guard/READM.md +++ /dev/null @@ -1,3 +0,0 @@ -# License API guard plugin - -This plugin is used by ES UI plugins to reject API requests to plugins that are unsupported by the user's license. \ No newline at end of file diff --git a/x-pack/plugins/license_api_guard/README.md b/x-pack/plugins/license_api_guard/README.md new file mode 100644 index 00000000000000..bf2a9fdff71221 --- /dev/null +++ b/x-pack/plugins/license_api_guard/README.md @@ -0,0 +1,3 @@ +# License API guard plugin + +This plugin is used by ES UI plugins to reject API requests when the plugin is unsupported by the user's license. \ No newline at end of file From c836c9adac7f95456e4d4bc7e03d925b76e4cf70 Mon Sep 17 00:00:00 2001 From: CJ Cenizal Date: Thu, 15 Apr 2021 15:09:44 -0700 Subject: [PATCH 2/3] Import http server mocks from public interface. --- x-pack/plugins/license_api_guard/server/license.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/x-pack/plugins/license_api_guard/server/license.test.ts b/x-pack/plugins/license_api_guard/server/license.test.ts index e9da393f534786..17f870d11df208 100644 --- a/x-pack/plugins/license_api_guard/server/license.test.ts +++ b/x-pack/plugins/license_api_guard/server/license.test.ts @@ -7,8 +7,7 @@ import { of } from 'rxjs'; import type { KibanaRequest, RequestHandlerContext } from 'src/core/server'; -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { httpServerMock } from 'src/core/server/http/http_server.mocks'; +import { httpServerMock } from 'src/core/server/mocks'; import { License } from './license'; import { LicenseCheckState, licensingMock } from './shared_imports'; From a4cebf579c92023da8b35e7f2323a36fdb7cc972 Mon Sep 17 00:00:00 2001 From: CJ Cenizal Date: Thu, 15 Apr 2021 15:55:22 -0700 Subject: [PATCH 3/3] Reject basic minimum licenses. --- .../license_api_guard/server/license.test.ts | 132 +++++++++++------- .../license_api_guard/server/license.ts | 6 + 2 files changed, 90 insertions(+), 48 deletions(-) diff --git a/x-pack/plugins/license_api_guard/server/license.test.ts b/x-pack/plugins/license_api_guard/server/license.test.ts index 17f870d11df208..400af7261ff871 100644 --- a/x-pack/plugins/license_api_guard/server/license.test.ts +++ b/x-pack/plugins/license_api_guard/server/license.test.ts @@ -6,17 +6,38 @@ */ import { of } from 'rxjs'; -import type { KibanaRequest, RequestHandlerContext } from 'src/core/server'; +import type { Logger, KibanaRequest, RequestHandlerContext } from 'src/core/server'; import { httpServerMock } from 'src/core/server/mocks'; - import { License } from './license'; -import { LicenseCheckState, licensingMock } from './shared_imports'; +import { LicenseCheckState, licensingMock, LicenseType } from './shared_imports'; describe('License API guard', () => { const pluginName = 'testPlugin'; - const currentLicenseType = 'basic'; - const testRoute = ({ licenseState }: { licenseState: string }) => { + const mockLicensingService = ({ + licenseType, + licenseState, + }: { + licenseType: LicenseType; + licenseState: LicenseCheckState; + }) => { + const licenseMock = licensingMock.createLicenseMock(); + licenseMock.type = licenseType; + licenseMock.check('test', 'gold'); // Flush default mocked state + licenseMock.check.mockReturnValue({ state: licenseState }); // Replace with new mocked state + + return { + license$: of(licenseMock), + }; + }; + + const testRoute = ({ + licenseType, + licenseState, + }: { + licenseType: LicenseType; + licenseState: LicenseCheckState; + }) => { const license = new License(); const logger = { @@ -24,19 +45,11 @@ describe('License API guard', () => { }; license.setup({ pluginName, logger }); - - const licenseMock = licensingMock.createLicenseMock(); - licenseMock.type = currentLicenseType; - licenseMock.check('test', 'basic'); // Flush default mocked state - licenseMock.check.mockReturnValue({ state: licenseState as LicenseCheckState }); // Replace with new mocked state - - const licensing = { - license$: of(licenseMock), - }; + const licensing = mockLicensingService({ licenseType, licenseState }); license.start({ pluginId: 'id', - minimumLicenseType: 'basic', + minimumLicenseType: 'gold', licensing, }); @@ -60,44 +73,67 @@ describe('License API guard', () => { }; }; - describe('valid license', () => { - it('the original route is called and nothing is logged', () => { - const { errorResponse, logMesssage, route } = testRoute({ licenseState: 'valid' }); - - expect(errorResponse).toBeUndefined(); - expect(logMesssage).toBeUndefined(); - expect(route).toHaveBeenCalled(); + describe('basic minimum license', () => { + it('is rejected', () => { + const license = new License(); + license.setup({ pluginName, logger: {} as Logger }); + expect(() => { + license.start({ + pluginId: pluginName, + minimumLicenseType: 'basic', + licensing: mockLicensingService({ licenseType: 'gold', licenseState: 'valid' }), + }); + }).toThrowError( + `Basic licenses don't restrict the use of plugins. Please don't use license_api_guard in the ${pluginName} plugin, or provide a more restrictive minimumLicenseType.` + ); }); }); - [ - { - licenseState: 'invalid', - expectedMessage: `Your ${currentLicenseType} license does not support ${pluginName}. Please upgrade your license.`, - }, - { - licenseState: 'expired', - expectedMessage: `You cannot use ${pluginName} because your ${currentLicenseType} license has expired.`, - }, - { - licenseState: 'unavailable', - expectedMessage: `You cannot use ${pluginName} because license information is not available at this time.`, - }, - ].forEach(({ licenseState, expectedMessage }) => { - describe(`${licenseState} license`, () => { - it('replies with and logs the error message', () => { - const { errorResponse, logMesssage, route } = testRoute({ licenseState }); - - // We depend on the call to `response.forbidden()` to generate the 403 status code, - // so we can't assert for it here. - expect(errorResponse).toEqual({ - body: { - message: expectedMessage, - }, + describe('non-basic minimum license', () => { + const licenseType = 'gold'; + + describe('when valid', () => { + it('the original route is called and nothing is logged', () => { + const { errorResponse, logMesssage, route } = testRoute({ + licenseType, + licenseState: 'valid', }); - expect(logMesssage).toBe(expectedMessage); - expect(route).not.toHaveBeenCalled(); + expect(errorResponse).toBeUndefined(); + expect(logMesssage).toBeUndefined(); + expect(route).toHaveBeenCalled(); + }); + }); + + [ + { + licenseState: 'invalid' as LicenseCheckState, + expectedMessage: `Your ${licenseType} license does not support ${pluginName}. Please upgrade your license.`, + }, + { + licenseState: 'expired' as LicenseCheckState, + expectedMessage: `You cannot use ${pluginName} because your ${licenseType} license has expired.`, + }, + { + licenseState: 'unavailable' as LicenseCheckState, + expectedMessage: `You cannot use ${pluginName} because license information is not available at this time.`, + }, + ].forEach(({ licenseState, expectedMessage }) => { + describe(`when ${licenseState}`, () => { + it('replies with and logs the error message', () => { + const { errorResponse, logMesssage, route } = testRoute({ licenseType, licenseState }); + + // We depend on the call to `response.forbidden()` to generate the 403 status code, + // so we can't assert for it here. + expect(errorResponse).toEqual({ + body: { + message: expectedMessage, + }, + }); + + expect(logMesssage).toBe(expectedMessage); + expect(route).not.toHaveBeenCalled(); + }); }); }); }); diff --git a/x-pack/plugins/license_api_guard/server/license.ts b/x-pack/plugins/license_api_guard/server/license.ts index 3b0fbc8422d637..66e47f02b6e289 100644 --- a/x-pack/plugins/license_api_guard/server/license.ts +++ b/x-pack/plugins/license_api_guard/server/license.ts @@ -44,6 +44,12 @@ export class License { } start({ pluginId, minimumLicenseType, licensing }: StartSettings) { + if (minimumLicenseType === 'basic') { + throw Error( + `Basic licenses don't restrict the use of plugins. Please don't use license_api_guard in the ${pluginId} plugin, or provide a more restrictive minimumLicenseType.` + ); + } + licensing.license$.subscribe((license: ILicense) => { this.licenseType = license.type; this.licenseCheckState = license.check(pluginId, minimumLicenseType!).state;