2018-07-11 10:19:35 +02:00
|
|
|
/*
|
|
|
|
* Licensed to Elasticsearch B.V. under one or more contributor
|
|
|
|
* license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright
|
|
|
|
* ownership. Elasticsearch B.V. licenses this file to you under
|
|
|
|
* the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
2020-09-16 09:17:05 +02:00
|
|
|
|
2020-09-15 12:41:27 +02:00
|
|
|
import type { PublicMethodsOf } from '@kbn/utility-types';
|
2018-10-04 09:18:40 +02:00
|
|
|
import { Type } from '@kbn/config-schema';
|
2018-07-11 10:19:35 +02:00
|
|
|
import { isEqual } from 'lodash';
|
2019-12-13 09:49:56 +01:00
|
|
|
import { BehaviorSubject, combineLatest, Observable } from 'rxjs';
|
|
|
|
import { distinctUntilChanged, first, map, shareReplay, take } from 'rxjs/operators';
|
2020-09-16 09:17:05 +02:00
|
|
|
import { Logger, LoggerFactory } from '@kbn/logging';
|
2018-07-11 10:19:35 +02:00
|
|
|
|
2019-05-21 10:57:33 +02:00
|
|
|
import { Config, ConfigPath, Env } from '.';
|
2019-05-21 18:45:23 +02:00
|
|
|
import { hasConfigPathIntersection } from './config';
|
2020-09-16 09:17:05 +02:00
|
|
|
import { RawConfigurationProvider } from './raw/raw_config_service';
|
2019-12-13 09:49:56 +01:00
|
|
|
import {
|
|
|
|
applyDeprecations,
|
|
|
|
ConfigDeprecationWithContext,
|
|
|
|
ConfigDeprecationProvider,
|
|
|
|
configDeprecationFactory,
|
|
|
|
} from './deprecation';
|
2020-09-16 09:17:05 +02:00
|
|
|
import { LegacyObjectToConfigAdapter } from './legacy';
|
2018-07-11 10:19:35 +02:00
|
|
|
|
2019-10-01 09:11:33 +02:00
|
|
|
/** @internal */
|
|
|
|
export type IConfigService = PublicMethodsOf<ConfigService>;
|
|
|
|
|
2019-05-09 08:10:49 +02:00
|
|
|
/** @internal */
|
2018-07-11 10:19:35 +02:00
|
|
|
export class ConfigService {
|
|
|
|
private readonly log: Logger;
|
2019-12-13 09:49:56 +01:00
|
|
|
private readonly deprecationLog: Logger;
|
|
|
|
|
|
|
|
private readonly config$: Observable<Config>;
|
2018-07-11 10:19:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whenever a config if read at a path, we mark that path as 'handled'. We can
|
|
|
|
* then list all unhandled config paths when the startup process is completed.
|
|
|
|
*/
|
|
|
|
private readonly handledPaths: ConfigPath[] = [];
|
2019-05-10 14:47:45 +02:00
|
|
|
private readonly schemas = new Map<string, Type<unknown>>();
|
2019-12-13 09:49:56 +01:00
|
|
|
private readonly deprecations = new BehaviorSubject<ConfigDeprecationWithContext[]>([]);
|
2018-07-11 10:19:35 +02:00
|
|
|
|
|
|
|
constructor(
|
2019-12-13 09:49:56 +01:00
|
|
|
private readonly rawConfigProvider: RawConfigurationProvider,
|
2019-04-10 14:56:09 +02:00
|
|
|
private readonly env: Env,
|
2018-07-11 10:19:35 +02:00
|
|
|
logger: LoggerFactory
|
|
|
|
) {
|
|
|
|
this.log = logger.get('config');
|
2019-12-13 09:49:56 +01:00
|
|
|
this.deprecationLog = logger.get('config', 'deprecation');
|
|
|
|
|
|
|
|
this.config$ = combineLatest([this.rawConfigProvider.getConfig$(), this.deprecations]).pipe(
|
|
|
|
map(([rawConfig, deprecations]) => {
|
|
|
|
const migrated = applyDeprecations(rawConfig, deprecations);
|
|
|
|
return new LegacyObjectToConfigAdapter(migrated);
|
|
|
|
}),
|
|
|
|
shareReplay(1)
|
|
|
|
);
|
2018-07-11 10:19:35 +02:00
|
|
|
}
|
|
|
|
|
2019-05-10 14:47:45 +02:00
|
|
|
/**
|
|
|
|
* Set config schema for a path and performs its validation
|
|
|
|
*/
|
|
|
|
public async setSchema(path: ConfigPath, schema: Type<unknown>) {
|
|
|
|
const namespace = pathToString(path);
|
|
|
|
if (this.schemas.has(namespace)) {
|
2019-11-18 10:25:21 +01:00
|
|
|
throw new Error(`Validation schema for [${path}] was already registered.`);
|
2019-05-10 14:47:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.schemas.set(namespace, schema);
|
2019-12-13 09:49:56 +01:00
|
|
|
this.markAsHandled(path);
|
|
|
|
}
|
2019-05-10 14:47:45 +02:00
|
|
|
|
2019-12-13 09:49:56 +01:00
|
|
|
/**
|
|
|
|
* Register a {@link ConfigDeprecationProvider} to be used when validating and migrating the configuration
|
|
|
|
*/
|
|
|
|
public addDeprecationProvider(path: ConfigPath, provider: ConfigDeprecationProvider) {
|
|
|
|
const flatPath = pathToString(path);
|
|
|
|
this.deprecations.next([
|
|
|
|
...this.deprecations.value,
|
2020-05-22 09:08:58 +02:00
|
|
|
...provider(configDeprecationFactory).map((deprecation) => ({
|
2019-12-13 09:49:56 +01:00
|
|
|
deprecation,
|
|
|
|
path: flatPath,
|
|
|
|
})),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate the whole configuration and log the deprecation warnings.
|
|
|
|
*
|
|
|
|
* This must be done after every schemas and deprecation providers have been registered.
|
|
|
|
*/
|
|
|
|
public async validate() {
|
|
|
|
const namespaces = [...this.schemas.keys()];
|
|
|
|
for (let i = 0; i < namespaces.length; i++) {
|
2020-05-22 09:08:58 +02:00
|
|
|
await this.validateConfigAtPath(namespaces[i]).pipe(first()).toPromise();
|
2019-12-13 09:49:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
await this.logDeprecation();
|
2019-05-10 14:47:45 +02:00
|
|
|
}
|
|
|
|
|
2018-07-11 10:19:35 +02:00
|
|
|
/**
|
|
|
|
* Returns the full config object observable. This is not intended for
|
|
|
|
* "normal use", but for features that _need_ access to the full object.
|
|
|
|
*/
|
|
|
|
public getConfig$() {
|
|
|
|
return this.config$;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads the subset of the config at the specified `path` and validates it
|
|
|
|
* against the static `schema` on the given `ConfigClass`.
|
|
|
|
*
|
2019-04-10 14:56:09 +02:00
|
|
|
* @param path - The path to the desired subset of the config.
|
2018-07-11 10:19:35 +02:00
|
|
|
*/
|
2019-05-21 10:57:33 +02:00
|
|
|
public atPath<TSchema>(path: ConfigPath) {
|
2019-12-13 09:49:56 +01:00
|
|
|
return this.validateConfigAtPath(path) as Observable<TSchema>;
|
2018-07-11 10:19:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Same as `atPath`, but returns `undefined` if there is no config at the
|
|
|
|
* specified path.
|
|
|
|
*
|
2019-04-10 14:56:09 +02:00
|
|
|
* {@link ConfigService.atPath}
|
2018-07-11 10:19:35 +02:00
|
|
|
*/
|
2019-05-21 10:57:33 +02:00
|
|
|
public optionalAtPath<TSchema>(path: ConfigPath) {
|
2018-08-28 14:17:55 +02:00
|
|
|
return this.getDistinctConfig(path).pipe(
|
2020-05-22 09:08:58 +02:00
|
|
|
map((config) => {
|
2019-05-10 14:47:45 +02:00
|
|
|
if (config === undefined) return undefined;
|
2019-12-13 09:49:56 +01:00
|
|
|
return this.validateAtPath(path, config) as TSchema;
|
2019-05-10 14:47:45 +02:00
|
|
|
})
|
2018-07-11 10:19:35 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async isEnabledAtPath(path: ConfigPath) {
|
2019-11-18 10:25:21 +01:00
|
|
|
const namespace = pathToString(path);
|
|
|
|
|
|
|
|
const validatedConfig = this.schemas.has(namespace)
|
2020-05-22 09:08:58 +02:00
|
|
|
? await this.atPath<{ enabled?: boolean }>(path).pipe(first()).toPromise()
|
2019-11-18 10:25:21 +01:00
|
|
|
: undefined;
|
2018-07-11 10:19:35 +02:00
|
|
|
|
2019-11-18 10:25:21 +01:00
|
|
|
const enabledPath = createPluginEnabledPath(path);
|
2018-08-17 01:04:31 +02:00
|
|
|
const config = await this.config$.pipe(first()).toPromise();
|
2019-11-18 10:25:21 +01:00
|
|
|
|
|
|
|
// if plugin hasn't got a config schema, we try to read "enabled" directly
|
|
|
|
const isEnabled =
|
|
|
|
validatedConfig && validatedConfig.enabled !== undefined
|
|
|
|
? validatedConfig.enabled
|
|
|
|
: config.get(enabledPath);
|
|
|
|
|
|
|
|
// not declared. consider that plugin is enabled by default
|
|
|
|
if (isEnabled === undefined) {
|
2018-07-11 10:19:35 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isEnabled === false) {
|
|
|
|
// If the plugin is _not_ enabled, we mark the entire plugin path as
|
|
|
|
// handled, as it's expected that it won't be used.
|
|
|
|
this.markAsHandled(path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If plugin enabled we mark the enabled path as handled, as we for example
|
|
|
|
// can have plugins that don't have _any_ config except for this field, and
|
|
|
|
// therefore have no reason to try to get the config.
|
|
|
|
this.markAsHandled(enabledPath);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-04 20:57:14 +01:00
|
|
|
public async getUnusedPaths() {
|
2018-08-17 01:04:31 +02:00
|
|
|
const config = await this.config$.pipe(first()).toPromise();
|
2018-07-11 10:19:35 +02:00
|
|
|
const handledPaths = this.handledPaths.map(pathToString);
|
|
|
|
|
2020-05-22 09:08:58 +02:00
|
|
|
return config.getFlattenedPaths().filter((path) => !isPathHandled(path, handledPaths));
|
2018-07-11 10:19:35 +02:00
|
|
|
}
|
|
|
|
|
2018-12-04 20:57:14 +01:00
|
|
|
public async getUsedPaths() {
|
|
|
|
const config = await this.config$.pipe(first()).toPromise();
|
|
|
|
const handledPaths = this.handledPaths.map(pathToString);
|
|
|
|
|
2020-05-22 09:08:58 +02:00
|
|
|
return config.getFlattenedPaths().filter((path) => isPathHandled(path, handledPaths));
|
2018-12-04 20:57:14 +01:00
|
|
|
}
|
|
|
|
|
2019-12-13 09:49:56 +01:00
|
|
|
private async logDeprecation() {
|
2020-05-22 09:08:58 +02:00
|
|
|
const rawConfig = await this.rawConfigProvider.getConfig$().pipe(take(1)).toPromise();
|
2019-12-13 09:49:56 +01:00
|
|
|
const deprecations = await this.deprecations.pipe(take(1)).toPromise();
|
|
|
|
const deprecationMessages: string[] = [];
|
|
|
|
const logger = (msg: string) => deprecationMessages.push(msg);
|
|
|
|
applyDeprecations(rawConfig, deprecations, logger);
|
2020-05-22 09:08:58 +02:00
|
|
|
deprecationMessages.forEach((msg) => {
|
2019-12-13 09:49:56 +01:00
|
|
|
this.deprecationLog.warn(msg);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private validateAtPath(path: ConfigPath, config: Record<string, unknown>) {
|
2019-05-10 14:47:45 +02:00
|
|
|
const namespace = pathToString(path);
|
|
|
|
const schema = this.schemas.get(namespace);
|
|
|
|
if (!schema) {
|
2019-11-18 10:25:21 +01:00
|
|
|
throw new Error(`No validation schema has been defined for [${namespace}]`);
|
2018-07-11 10:19:35 +02:00
|
|
|
}
|
2019-05-10 14:47:45 +02:00
|
|
|
return schema.validate(
|
2018-08-28 14:17:55 +02:00
|
|
|
config,
|
2018-07-11 10:19:35 +02:00
|
|
|
{
|
2018-08-23 22:31:32 +02:00
|
|
|
dev: this.env.mode.dev,
|
|
|
|
prod: this.env.mode.prod,
|
|
|
|
...this.env.packageInfo,
|
2018-07-11 10:19:35 +02:00
|
|
|
},
|
2019-11-18 10:25:21 +01:00
|
|
|
`config validation of [${namespace}]`
|
2018-07-11 10:19:35 +02:00
|
|
|
);
|
2019-05-10 14:47:45 +02:00
|
|
|
}
|
|
|
|
|
2019-12-13 09:49:56 +01:00
|
|
|
private validateConfigAtPath(path: ConfigPath) {
|
2020-05-22 09:08:58 +02:00
|
|
|
return this.getDistinctConfig(path).pipe(map((config) => this.validateAtPath(path, config)));
|
2019-05-10 14:47:45 +02:00
|
|
|
}
|
|
|
|
|
2018-08-28 14:17:55 +02:00
|
|
|
private getDistinctConfig(path: ConfigPath) {
|
2018-07-11 10:19:35 +02:00
|
|
|
this.markAsHandled(path);
|
|
|
|
|
2018-09-27 11:54:09 +02:00
|
|
|
return this.config$.pipe(
|
2020-05-22 09:08:58 +02:00
|
|
|
map((config) => config.get(path)),
|
2018-09-27 11:54:09 +02:00
|
|
|
distinctUntilChanged(isEqual)
|
|
|
|
);
|
2018-07-11 10:19:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private markAsHandled(path: ConfigPath) {
|
|
|
|
this.log.debug(`Marking config path as handled: ${path}`);
|
|
|
|
this.handledPaths.push(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const createPluginEnabledPath = (configPath: string | string[]) => {
|
|
|
|
if (Array.isArray(configPath)) {
|
|
|
|
return configPath.concat('enabled');
|
|
|
|
}
|
|
|
|
return `${configPath}.enabled`;
|
|
|
|
};
|
|
|
|
|
|
|
|
const pathToString = (path: ConfigPath) => (Array.isArray(path) ? path.join('.') : path);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A path is considered 'handled' if it is a subset of any of the already
|
|
|
|
* handled paths.
|
|
|
|
*/
|
|
|
|
const isPathHandled = (path: string, handledPaths: string[]) =>
|
2020-05-22 09:08:58 +02:00
|
|
|
handledPaths.some((handledPath) => hasConfigPathIntersection(path, handledPath));
|