Skip to content

Commit a7d5e97

Browse files
committed
[NP] Expose global config to the plugins
1 parent e09cde9 commit a7d5e97

7 files changed

Lines changed: 89 additions & 4 deletions

File tree

docs/development/core/server/kibana-plugin-server.plugininitializercontext.config.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
```typescript
1010
config: {
11+
globalConfig__deprecated$: Observable<Config>;
1112
create: <T = ConfigSchema>() => Observable<T>;
1213
createIfExists: <T = ConfigSchema>() => Observable<T | undefined>;
1314
};

docs/development/core/server/kibana-plugin-server.plugininitializercontext.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export interface PluginInitializerContext<ConfigSchema = unknown>
1616

1717
| Property | Type | Description |
1818
| --- | --- | --- |
19-
| [config](./kibana-plugin-server.plugininitializercontext.config.md) | <code>{</code><br/><code> create: &lt;T = ConfigSchema&gt;() =&gt; Observable&lt;T&gt;;</code><br/><code> createIfExists: &lt;T = ConfigSchema&gt;() =&gt; Observable&lt;T &#124; undefined&gt;;</code><br/><code> }</code> | |
19+
| [config](./kibana-plugin-server.plugininitializercontext.config.md) | <code>{</code><br/><code> globalConfig__deprecated$: Observable&lt;Config&gt;;</code><br/><code> create: &lt;T = ConfigSchema&gt;() =&gt; Observable&lt;T&gt;;</code><br/><code> createIfExists: &lt;T = ConfigSchema&gt;() =&gt; Observable&lt;T &#124; undefined&gt;;</code><br/><code> }</code> | |
2020
| [env](./kibana-plugin-server.plugininitializercontext.env.md) | <code>{</code><br/><code> mode: EnvironmentMode;</code><br/><code> packageInfo: Readonly&lt;PackageInfo&gt;;</code><br/><code> }</code> | |
2121
| [logger](./kibana-plugin-server.plugininitializercontext.logger.md) | <code>LoggerFactory</code> | |
2222
| [opaqueId](./kibana-plugin-server.plugininitializercontext.opaqueid.md) | <code>PluginOpaqueId</code> | |

src/core/server/mocks.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
import { of } from 'rxjs';
19+
import { of, BehaviorSubject } from 'rxjs';
2020
import { PluginInitializerContext, CoreSetup, CoreStart } from '.';
2121
import { loggingServiceMock } from './logging/logging_service.mock';
2222
import { elasticsearchServiceMock } from './elasticsearch/elasticsearch_service.mock';
2323
import { httpServiceMock } from './http/http_service.mock';
2424
import { contextServiceMock } from './context/context_service.mock';
2525
import { uiSettingsServiceMock } from './ui_settings/ui_settings_service.mock';
26+
import { ObjectToConfigAdapter } from './config';
2627

2728
export { httpServerMock } from './http/http_server.mocks';
2829
export { sessionStorageMock } from './http/cookie_session_storage.mocks';
@@ -33,8 +34,9 @@ export { loggingServiceMock } from './logging/logging_service.mock';
3334
export { savedObjectsClientMock } from './saved_objects/service/saved_objects_client.mock';
3435
export { uiSettingsServiceMock } from './ui_settings/ui_settings_service.mock';
3536

36-
export function pluginInitializerContextConfigMock<T>(config: T) {
37+
export function pluginInitializerContextConfigMock<T>(config: T, globalConfig = {}) {
3738
const mock: jest.Mocked<PluginInitializerContext<T>['config']> = {
39+
globalConfig__deprecated$: new BehaviorSubject(new ObjectToConfigAdapter(globalConfig)),
3840
create: jest.fn().mockReturnValue(of(config)),
3941
createIfExists: jest.fn().mockReturnValue(of(config)),
4042
};
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import { createPluginInitializerContext } from './plugin_context';
21+
import { CoreContext } from '../core_context';
22+
import { Env, ObjectToConfigAdapter } from '../config';
23+
import { configServiceMock } from '../config/config_service.mock';
24+
import { loggingServiceMock } from '../logging/logging_service.mock';
25+
import { getEnvOptions } from '../config/__mocks__/env';
26+
import { PluginManifest } from './types';
27+
import { first } from 'rxjs/operators';
28+
29+
const logger = loggingServiceMock.create();
30+
const configService = configServiceMock.create({ getConfig$: { globalConfig: { subpath: 1 } } });
31+
32+
let coreId: symbol;
33+
let env: Env;
34+
let coreContext: CoreContext;
35+
36+
function createPluginManifest(manifestProps: Partial<PluginManifest> = {}): PluginManifest {
37+
return {
38+
id: 'some-plugin-id',
39+
version: 'some-version',
40+
configPath: 'path',
41+
kibanaVersion: '7.0.0',
42+
requiredPlugins: ['some-required-dep'],
43+
optionalPlugins: ['some-optional-dep'],
44+
server: true,
45+
ui: true,
46+
...manifestProps,
47+
};
48+
}
49+
50+
beforeEach(() => {
51+
coreId = Symbol('core');
52+
env = Env.createDefault(getEnvOptions());
53+
coreContext = { coreId, env, logger, configService };
54+
});
55+
56+
test('should return a globalConfig handler in the context (to be deprecated)', async () => {
57+
const manifest = createPluginManifest();
58+
const opaqueId = Symbol();
59+
const pluginInitializerContext = createPluginInitializerContext(coreContext, opaqueId, manifest);
60+
61+
expect(pluginInitializerContext.config.globalConfig__deprecated$).toBeDefined();
62+
63+
const configObject = await pluginInitializerContext.config.globalConfig__deprecated$
64+
.pipe(first())
65+
.toPromise();
66+
expect(configObject).toBeInstanceOf(ObjectToConfigAdapter);
67+
68+
const configPaths = configObject.getFlattenedPaths();
69+
expect(configPaths).toHaveLength(1);
70+
expect(configPaths).toStrictEqual(['globalConfig.subpath']);
71+
expect(configObject.has('globalConfig.subpath')).toBe(true);
72+
expect(configObject.get('globalConfig.subpath')).toBe(1);
73+
});

src/core/server/plugins/plugin_context.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ export function createPluginInitializerContext(
6565
* Core configuration functionality, enables fetching a subset of the config.
6666
*/
6767
config: {
68+
/**
69+
* Global configuration
70+
* Note: naming not final here, it will be renamed in a near future (https://github.com/elastic/kibana/issues/46240)
71+
* @deprecated
72+
*/
73+
globalConfig__deprecated$: coreContext.configService.getConfig$(),
74+
6875
/**
6976
* Reads the subset of the config at the `configPath` defined in the plugin
7077
* manifest and validates it against the schema in the static `schema` on

src/core/server/plugins/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import { Observable } from 'rxjs';
2121
import { Type } from '@kbn/config-schema';
2222

23-
import { ConfigPath, EnvironmentMode, PackageInfo } from '../config';
23+
import { ConfigPath, EnvironmentMode, PackageInfo, Config } from '../config';
2424
import { LoggerFactory } from '../logging';
2525
import { CoreSetup, CoreStart } from '..';
2626

@@ -209,6 +209,7 @@ export interface PluginInitializerContext<ConfigSchema = unknown> {
209209
};
210210
logger: LoggerFactory;
211211
config: {
212+
globalConfig__deprecated$: Observable<Config>;
212213
create: <T = ConfigSchema>() => Observable<T>;
213214
createIfExists: <T = ConfigSchema>() => Observable<T | undefined>;
214215
};

src/core/server/server.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,7 @@ export type PluginInitializer<TSetup, TStart, TPluginsSetup extends object = obj
977977
export interface PluginInitializerContext<ConfigSchema = unknown> {
978978
// (undocumented)
979979
config: {
980+
globalConfig__deprecated$: Observable<Config>;
980981
create: <T = ConfigSchema>() => Observable<T>;
981982
createIfExists: <T = ConfigSchema>() => Observable<T | undefined>;
982983
};

0 commit comments

Comments
 (0)