Skip to content

Commit

Permalink
keep uiSettings config validation in NP only
Browse files Browse the repository at this point in the history
  • Loading branch information
mshustov committed Oct 8, 2019
1 parent 8a003c3 commit b7114c7
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 22 deletions.
3 changes: 3 additions & 0 deletions src/core/server/http/http_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const config = {
validate: match(validBasePathRegex, "must start with a slash, don't end with one"),
})
),
defaultRoute: schema.maybe(schema.string()),
cors: schema.conditional(
schema.contextRef('dev'),
true,
Expand Down Expand Up @@ -106,6 +107,7 @@ export class HttpConfig {
public basePath?: string;
public rewriteBasePath: boolean;
public publicDir: string;
public defaultRoute?: string;
public ssl: SslConfig;

/**
Expand All @@ -123,5 +125,6 @@ export class HttpConfig {
this.rewriteBasePath = rawConfig.rewriteBasePath;
this.publicDir = env.staticFilesDir;
this.ssl = new SslConfig(rawConfig.ssl || {});
this.defaultRoute = rawConfig.defaultRoute;
}
}
15 changes: 15 additions & 0 deletions src/core/server/http/http_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ export type HttpServiceSetup = Omit<HttpServerSetup, 'registerRouter'> & {
contextName: T,
provider: RequestHandlerContextProvider<T>
) => RequestHandlerContextContainer;

config: {
/**
* @internalRemarks
* Deprecated part of the server config, provided until
* https://github.com/elastic/kibana/issues/40255
*
* @deprecated
* */
defaultRoute?: string;
};
};

/** @public */
Expand Down Expand Up @@ -152,6 +163,10 @@ export class HttpService implements CoreService<HttpServiceSetup, HttpServiceSta
contextName: T,
provider: RequestHandlerContextProvider<T>
) => this.requestHandlerContext!.registerContext(pluginOpaqueId, contextName, provider),

config: {
defaultRoute: config.defaultRoute,
},
};

return contract;
Expand Down
4 changes: 3 additions & 1 deletion src/core/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ export class Server {
http: httpSetup,
});

const uiSettingsSetup = await this.uiSettings.setup({});
const uiSettingsSetup = await this.uiSettings.setup({
http: httpSetup,
});

const coreSetup = {
context: contextServiceSetup,
Expand Down
3 changes: 3 additions & 0 deletions src/core/server/ui_settings/ui_settings_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ export const config = {
path: 'uiSettings',
schema: schema.object({
overrides: schema.object({}, { allowUnknowns: true }),
// Deprecation is implemented in LP.
// We define schema here not to fail on the validation step.
enabled: schema.maybe(schema.boolean()),
}),
};
51 changes: 34 additions & 17 deletions src/core/server/ui_settings/ui_settings_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ import { CoreContext } from '../core_context';
import { Logger } from '../logging';

import { SavedObjectsClientContract, SavedObjectAttribute } from '../saved_objects/types';
import { HttpServiceSetup } from '../http';
import { UiSettingsConfigType } from './ui_settings_config';
import { IUiSettingsClient, UiSettingsClient } from './ui_settings_client';
import { mapToObject } from '../../utils/';

interface SetupDeps {} // eslint-disable-line @typescript-eslint/no-empty-interface
interface SetupDeps {
http: HttpServiceSetup;
}

export type UiSettingsType = 'json' | 'markdown' | 'number' | 'select' | 'boolean' | 'string';

Expand Down Expand Up @@ -65,30 +68,20 @@ export class UiSettingsService implements CoreService<UiSettingsServiceSetup> {

public async setup(deps: SetupDeps): Promise<UiSettingsServiceSetup> {
this.log.debug('Setting up ui settings service');
const config = await this.config$.pipe(first()).toPromise();
const { overrides } = config;
const overrides = await this.getOverrides(deps);
const { version, buildNum } = this.coreContext.env.packageInfo;
const getDefaults = () => mapToObject(this.uiSettingsDefaults);
const log = this.log;

return {
setDefaults: (values: Record<string, UiSettingsParams>) => {
Object.entries(values).forEach(([key, value]) => {
// can they change over time?
// if (this.uiSettingsDefaults.has(key)) {
// throw new Error(`uiSettingsDefaults for key ${key} has been already set`);
// }
this.uiSettingsDefaults.set(key, value);
});
},
asScopedToClient(savedObjectsClient: SavedObjectsClientContract) {
setDefaults: this.setDefaults.bind(this),
asScopedToClient: (savedObjectsClient: SavedObjectsClientContract) => {
return new UiSettingsClient({
type: 'config',
id: version,
buildNum,
savedObjectsClient,
getDefaults,
defaults: mapToObject(this.uiSettingsDefaults),
overrides,
log,
log: this.log,
});
},
};
Expand All @@ -97,4 +90,28 @@ export class UiSettingsService implements CoreService<UiSettingsServiceSetup> {
public async start() {}

public async stop() {}

private setDefaults(values: Record<string, UiSettingsParams> = {}) {
Object.entries(values).forEach(([key, value]) => {
if (this.uiSettingsDefaults.has(key)) {
throw new Error(`uiSettings defaults for key ${key} has been already set`);
}
this.uiSettingsDefaults.set(key, value);
});
}

private async getOverrides(deps: SetupDeps) {
const config = await this.config$.pipe(first()).toPromise();
const overrides: Record<string, SavedObjectAttribute> = config;
// manually implemented deprecation until New platform Config service
// supports them https://github.com/elastic/kibana/issues/40255
if (typeof deps.http.config.defaultRoute !== 'undefined') {
overrides.defaultRoute = deps.http.config.defaultRoute;
this.log.warn(
'Config key "server.defaultRoute" is deprecated. It has been replaced with "uiSettings.overrides.defaultRoute"'
);
}

return overrides;
}
}
4 changes: 1 addition & 3 deletions src/legacy/server/config/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ export default () => Joi.object({
ssl: HANDLED_IN_NEW_PLATFORM,
}).default(),

uiSettings: Joi.object().keys({
overrides: Joi.object().unknown(true).default()
}).default(),
uiSettings: HANDLED_IN_NEW_PLATFORM,

logging: Joi.object().keys({
silent: Joi.boolean().default(false),
Expand Down
1 change: 0 additions & 1 deletion src/legacy/server/config/transform_deprecations.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ const cspRules = (settings, log) => {

const deprecations = [
//server
rename('server.defaultRoute', 'uiSettings.overrides.defaultRoute'),
unused('server.xsrf.token'),
unused('uiSettings.enabled'),
rename('optimize.lazy', 'optimize.watch'),
Expand Down

0 comments on commit b7114c7

Please sign in to comment.