diff --git a/x-pack/plugins/canvas/server/config.test.ts b/x-pack/plugins/canvas/server/config.test.ts new file mode 100644 index 0000000000000..4c66c718d1b2d --- /dev/null +++ b/x-pack/plugins/canvas/server/config.test.ts @@ -0,0 +1,53 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +jest.mock('crypto', () => ({ + randomBytes: jest.fn(), + constants: jest.requireActual('crypto').constants, +})); + +jest.mock('@kbn/utils', () => ({ + getLogsPath: () => '/mock/kibana/logs/path', +})); + +import { ConfigSchema } from './config'; + +describe('config schema', () => { + it('generates proper defaults', () => { + expect(ConfigSchema.validate({})).toMatchInlineSnapshot(` + Object { + "enabled": true, + } + `); + + expect(ConfigSchema.validate({}, { dev: false })).toMatchInlineSnapshot(` + Object { + "enabled": true, + } + `); + + expect(ConfigSchema.validate({}, { dev: true })).toMatchInlineSnapshot(` + Object { + "enabled": true, + } + `); + }); + + it('should throw error if spaces is disabled', () => { + expect(() => ConfigSchema.validate({ enabled: false })).toThrow( + '[enabled]: Canvas can only be disabled in development mode' + ); + + expect(() => ConfigSchema.validate({ enabled: false }, { dev: false })).toThrow( + '[enabled]: Canvas can only be disabled in development mode' + ); + }); + + it('should not throw error if spaces is disabled in development mode', () => { + expect(() => ConfigSchema.validate({ enabled: false }, { dev: true })).not.toThrow(); + }); +}); diff --git a/x-pack/plugins/canvas/server/config.ts b/x-pack/plugins/canvas/server/config.ts new file mode 100644 index 0000000000000..6cbcff6930d88 --- /dev/null +++ b/x-pack/plugins/canvas/server/config.ts @@ -0,0 +1,24 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { schema } from '@kbn/config-schema'; + +export const ConfigSchema = schema.object({ + enabled: schema.conditional( + schema.contextRef('dev'), + true, + schema.boolean({ defaultValue: true }), + schema.boolean({ + validate: (rawValue) => { + if (rawValue === false) { + return 'Canvas can only be disabled in development mode'; + } + }, + defaultValue: true, + }) + ), +}); diff --git a/x-pack/plugins/canvas/server/index.ts b/x-pack/plugins/canvas/server/index.ts index d6d375b7259ac..74c5810481e00 100644 --- a/x-pack/plugins/canvas/server/index.ts +++ b/x-pack/plugins/canvas/server/index.ts @@ -5,8 +5,13 @@ * 2.0. */ -import { PluginInitializerContext } from '@kbn/core/server'; +import { PluginConfigDescriptor, PluginInitializerContext } from '@kbn/core/server'; import { CanvasPlugin } from './plugin'; +import { ConfigSchema } from './config'; + +export const config: PluginConfigDescriptor = { + schema: ConfigSchema, +}; export const plugin = (initializerContext: PluginInitializerContext) => new CanvasPlugin(initializerContext); diff --git a/x-pack/plugins/canvas/server/plugin.ts b/x-pack/plugins/canvas/server/plugin.ts index 3fcea2b5694c5..ec5cb4969be19 100644 --- a/x-pack/plugins/canvas/server/plugin.ts +++ b/x-pack/plugins/canvas/server/plugin.ts @@ -45,6 +45,7 @@ interface PluginsStart { export class CanvasPlugin implements Plugin { private readonly logger: Logger; + constructor(public readonly initializerContext: PluginInitializerContext) { this.logger = initializerContext.logger.get(); }