Skip to content

Commit

Permalink
Generate legacy vars when rendering all applications (elastic#54768)
Browse files Browse the repository at this point in the history
* Generate legacy vars when rendering all applications

* Move rendering functional tests and add user settings tests

* Make rendering integration tests more robust, get data from page

* Address review nits, fix CI failures

* Remove extraneous file

* Fix type error
  • Loading branch information
eliperelman committed Jan 16, 2020
1 parent f604572 commit 8ea9900
Show file tree
Hide file tree
Showing 22 changed files with 368 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ Generate a `KibanaResponse` which renders an HTML page bootstrapped with the `co
<b>Signature:</b>

```typescript
render(options?: IRenderOptions): Promise<string>;
render(options?: Pick<IRenderOptions, 'includeUserSettings'>): Promise<string>;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| options | <code>IRenderOptions</code> | |
| options | <code>Pick&lt;IRenderOptions, 'includeUserSettings'&gt;</code> | |

<b>Returns:</b>

Expand Down
2 changes: 1 addition & 1 deletion src/core/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export {
SessionCookieValidationResult,
SessionStorageFactory,
} from './http';
export { RenderingServiceSetup, IRenderOptions, LegacyRenderOptions } from './rendering';
export { RenderingServiceSetup, IRenderOptions } from './rendering';
export { Logger, LoggerFactory, LogMeta, LogRecord, LogLevel } from './logging';

export {
Expand Down
14 changes: 10 additions & 4 deletions src/core/server/legacy/legacy_internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

import { Server } from 'hapi';

import { LegacyRequest } from '../http';
import { KibanaRequest, LegacyRequest } from '../http';
import { ensureRawRequest } from '../http/router';
import { mergeVars } from './merge_vars';
import { ILegacyInternals, LegacyVars, VarsInjector, LegacyConfig, LegacyUiExports } from './types';

Expand Down Expand Up @@ -51,11 +52,12 @@ export class LegacyInternals implements ILegacyInternals {
));
}

private replaceVars(vars: LegacyVars, request: LegacyRequest) {
private replaceVars(vars: LegacyVars, request: KibanaRequest | LegacyRequest) {
const { injectedVarsReplacers = [] } = this.uiExports;

return injectedVarsReplacers.reduce(
async (injected, replacer) => replacer(await injected, request, this.server),
async (injected, replacer) =>
replacer(await injected, ensureRawRequest(request), this.server),
Promise.resolve(vars)
);
}
Expand All @@ -78,7 +80,11 @@ export class LegacyInternals implements ILegacyInternals {
);
}

public async getVars(id: string, request: LegacyRequest, injected: LegacyVars = {}) {
public async getVars(
id: string,
request: KibanaRequest | LegacyRequest,
injected: LegacyVars = {}
) {
return this.replaceVars(
mergeVars(this.defaultVars, await this.getInjectedUiAppVars(id), injected),
request
Expand Down
9 changes: 8 additions & 1 deletion src/core/server/legacy/legacy_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { PathConfigType } from '../path';
import { findLegacyPluginSpecs } from './plugins';
import { convertLegacyDeprecationProvider } from './config';
import {
ILegacyInternals,
LegacyServiceSetupDeps,
LegacyServiceStartDeps,
LegacyPlugins,
Expand Down Expand Up @@ -82,6 +83,7 @@ export class LegacyService implements CoreService {
private legacyRawConfig?: LegacyConfig;
private legacyPlugins?: LegacyPlugins;
private settings?: LegacyVars;
public legacyInternals?: ILegacyInternals;

constructor(private readonly coreContext: CoreContext) {
const { logger, configService } = coreContext;
Expand Down Expand Up @@ -183,6 +185,11 @@ export class LegacyService implements CoreService {
// propagate the instance uuid to the legacy config, as it was the legacy way to access it.
this.legacyRawConfig!.set('server.uuid', setupDeps.core.uuid.getInstanceUuid());
this.setupDeps = setupDeps;
this.legacyInternals = new LegacyInternals(
this.legacyPlugins.uiExports,
this.legacyRawConfig!,
setupDeps.core.http.server
);
}

public async start(startDeps: LegacyServiceStartDeps) {
Expand Down Expand Up @@ -317,7 +324,7 @@ export class LegacyService implements CoreService {
rendering: setupDeps.core.rendering,
uiSettings: setupDeps.core.uiSettings,
savedObjectsClientProvider: startDeps.core.savedObjects.clientProvider,
legacy: new LegacyInternals(legacyPlugins.uiExports, config, setupDeps.core.http.server),
legacy: this.legacyInternals,
},
logger: this.coreContext.logger,
},
Expand Down
8 changes: 6 additions & 2 deletions src/core/server/legacy/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { Server } from 'hapi';

import { ChromeNavLink } from '../../public';
import { LegacyRequest } from '../http';
import { KibanaRequest, LegacyRequest } from '../http';
import { InternalCoreSetup, InternalCoreStart } from '../internal_types';
import { PluginsServiceSetup, PluginsServiceStart } from '../plugins';
import { RenderingServiceSetup } from '../rendering';
Expand Down Expand Up @@ -198,7 +198,11 @@ export interface ILegacyInternals {
/**
* Get the metadata vars for a particular plugin
*/
getVars(id: string, request: LegacyRequest, injected?: LegacyVars): Promise<LegacyVars>;
getVars(
id: string,
request: KibanaRequest | LegacyRequest,
injected?: LegacyVars
): Promise<LegacyVars>;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/core/server/rendering/rendering_service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import { CoreService } from '../../types';
import { CoreContext } from '../core_context';
import { Template } from './views';
import {
IRenderOptions,
RenderingSetupDeps,
RenderingServiceSetup,
RenderingMetadata,
LegacyRenderOptions,
} from './types';

/** @internal */
Expand All @@ -56,7 +56,7 @@ export class RenderingService implements CoreService<RenderingServiceSetup> {
app = { getId: () => 'core' },
includeUserSettings = true,
vars = {},
}: LegacyRenderOptions = {}
}: IRenderOptions = {}
) => {
const { env } = this.coreContext;
const basePath = http.basePath.get(request);
Expand Down
14 changes: 6 additions & 8 deletions src/core/server/rendering/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,19 @@ export interface IRenderOptions {
* `true` by default.
*/
includeUserSettings?: boolean;
}

/**
* @internal
* @deprecated for legacy use only, remove with ui_render_mixin
*/
export interface LegacyRenderOptions extends IRenderOptions {
/**
* Render the bootstrapped HTML content for an optional legacy application.
* Defaults to `core`.
* @deprecated for legacy use only, remove with ui_render_mixin
* @internal
*/
app?: { getId(): string };

/**
* Inject custom vars into the page metadata.
* @deprecated for legacy use only, remove with ui_render_mixin
* @internal
*/
vars?: Record<string, any>;
}
Expand All @@ -123,7 +121,7 @@ export interface IScopedRenderingClient {
* );
* ```
*/
render(options?: IRenderOptions): Promise<string>;
render(options?: Pick<IRenderOptions, 'includeUserSettings'>): Promise<string>;
}

/** @internal */
Expand All @@ -140,6 +138,6 @@ export interface RenderingServiceSetup {
render<R extends KibanaRequest | LegacyRequest>(
request: R,
uiSettings: IUiSettingsClient,
options?: R extends LegacyRequest ? LegacyRenderOptions : IRenderOptions
options?: IRenderOptions
): Promise<string>;
}
20 changes: 9 additions & 11 deletions src/core/server/server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,13 @@ export interface IndexSettingsDeprecationInfo {

// @public (undocumented)
export interface IRenderOptions {
// @internal @deprecated
app?: {
getId(): string;
};
includeUserSettings?: boolean;
// @internal @deprecated
vars?: Record<string, any>;
}

// @public
Expand Down Expand Up @@ -832,7 +838,7 @@ export type IScopedClusterClient = Pick<ScopedClusterClient, 'callAsCurrentUser'

// @public (undocumented)
export interface IScopedRenderingClient {
render(options?: IRenderOptions): Promise<string>;
render(options?: Pick<IRenderOptions, 'includeUserSettings'>): Promise<string>;
}

// @public
Expand Down Expand Up @@ -932,21 +938,13 @@ export class LegacyInternals implements ILegacyInternals {
// (undocumented)
getInjectedUiAppVars(id: string): Promise<Record<string, any>>;
// (undocumented)
getVars(id: string, request: LegacyRequest, injected?: LegacyVars): Promise<Record<string, any>>;
getVars(id: string, request: KibanaRequest | LegacyRequest, injected?: LegacyVars): Promise<Record<string, any>>;
// Warning: (ae-forgotten-export) The symbol "VarsInjector" needs to be exported by the entry point index.d.ts
//
// (undocumented)
injectUiAppVars(id: string, injector: VarsInjector): void;
}

// @internal @deprecated (undocumented)
export interface LegacyRenderOptions extends IRenderOptions {
app?: {
getId(): string;
};
vars?: Record<string, any>;
}

// @public @deprecated (undocumented)
export interface LegacyRequest extends Request {
}
Expand Down Expand Up @@ -1233,7 +1231,7 @@ export type RedirectResponseOptions = HttpResponseOptions & {

// @internal (undocumented)
export interface RenderingServiceSetup {
render<R extends KibanaRequest | LegacyRequest>(request: R, uiSettings: IUiSettingsClient, options?: R extends LegacyRequest ? LegacyRenderOptions : IRenderOptions): Promise<string>;
render<R extends KibanaRequest | LegacyRequest>(request: R, uiSettings: IUiSettingsClient, options?: IRenderOptions): Promise<string>;
}

// @public
Expand Down
6 changes: 5 additions & 1 deletion src/core/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,11 @@ export class Server {

return {
rendering: {
render: rendering.render.bind(rendering, req, uiSettingsClient),
render: async (options = {}) =>
rendering.render(req, uiSettingsClient, {
...options,
vars: await this.legacy.legacyInternals!.getVars('core', req),
}),
},
savedObjects: {
client: savedObjectsClient,
Expand Down
24 changes: 0 additions & 24 deletions src/plugins/testbed/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { schema, TypeOf } from '@kbn/config-schema';
import {
CoreSetup,
CoreStart,
LegacyRenderOptions,
Logger,
PluginInitializerContext,
PluginConfigDescriptor,
Expand Down Expand Up @@ -78,29 +77,6 @@ class Plugin {
}
);

router.get(
{
path: '/requestcontext/render/{id}',
validate: {
params: schema.object({
id: schema.maybe(schema.string()),
}),
},
},
async (context, req, res) => {
const { id } = req.params;
const options: Partial<LegacyRenderOptions> = { app: { getId: () => id! } };
const body = await context.core.rendering.render(options);

return res.ok({
body,
headers: {
'content-securty-policy': core.http.csp.header,
},
});
}
);

return {
data$: this.initializerContext.config.create<ConfigType>().pipe(
map(configValue => {
Expand Down
5 changes: 0 additions & 5 deletions test/api_integration/apis/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ export default function({ getService }) {
200,
'SavedObjects client: {"page":1,"per_page":20,"total":0,"saved_objects":[]}'
));

it('provides access to application rendering client', async () => {
await supertest.get('/requestcontext/render/core').expect(200, /app:core/);
await supertest.get('/requestcontext/render/testbed').expect(200, /app:testbed/);
});
});

describe('compression', () => {
Expand Down
8 changes: 8 additions & 0 deletions test/plugin_functional/plugins/rendering_plugin/kibana.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": "rendering_plugin",
"version": "0.0.1",
"kibanaVersion": "kibana",
"configPath": ["rendering_plugin"],
"server": true,
"ui": true
}
17 changes: 17 additions & 0 deletions test/plugin_functional/plugins/rendering_plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "rendering_plugin",
"version": "1.0.0",
"main": "target/test/plugin_functional/plugins/rendering_plugin",
"kibana": {
"version": "kibana",
"templateVersion": "1.0.0"
},
"license": "Apache-2.0",
"scripts": {
"kbn": "node ../../../../scripts/kbn.js",
"build": "rm -rf './target' && tsc"
},
"devDependencies": {
"typescript": "3.5.3"
}
}
23 changes: 23 additions & 0 deletions test/plugin_functional/plugins/rendering_plugin/public/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.
*/

import { PluginInitializer } from 'kibana/public';
import { RenderingPlugin } from './plugin';

export const plugin: PluginInitializer<void, void> = () => new RenderingPlugin();
41 changes: 41 additions & 0 deletions test/plugin_functional/plugins/rendering_plugin/public/plugin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.
*/

import React from 'react';
import { render, unmountComponentAtNode } from 'react-dom';
import { Plugin, CoreSetup } from 'kibana/public';

export class RenderingPlugin implements Plugin {
public setup(core: CoreSetup) {
core.application.register({
id: 'rendering',
title: 'Rendering',
appRoute: '/render',
async mount(context, { element }) {
render(<h1 data-test-subj="renderingHeader">rendering service</h1>, element);

return () => unmountComponentAtNode(element);
},
});
}

public start() {}

public stop() {}
}
Loading

0 comments on commit 8ea9900

Please sign in to comment.