Skip to content

Commit

Permalink
[7.x] Spaces - server-side to NP plugin (#46181) (#48222)
Browse files Browse the repository at this point in the history
  • Loading branch information
legrego authored Oct 15, 2019
1 parent e5f46b8 commit 30f69df
Show file tree
Hide file tree
Showing 122 changed files with 2,736 additions and 2,324 deletions.
2 changes: 1 addition & 1 deletion x-pack/.i18nrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"xpack.security": "legacy/plugins/security",
"xpack.server": "legacy/server",
"xpack.snapshotRestore": "legacy/plugins/snapshot_restore",
"xpack.spaces": "legacy/plugins/spaces",
"xpack.spaces": ["legacy/plugins/spaces", "plugins/spaces"],
"xpack.transform": "legacy/plugins/transform",
"xpack.upgradeAssistant": "legacy/plugins/upgrade_assistant",
"xpack.uptime": "legacy/plugins/uptime",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import Hapi from 'hapi';
import { EncryptedSavedObjectsStartContract } from '../shim';
import { SpacesPlugin as SpacesPluginStartContract } from '../../../spaces';
import { LegacySpacesPlugin as SpacesPluginStartContract } from '../../../spaces';
import { Logger } from '../../../../../../src/core/server';
import { validateParams, validateConfig, validateSecrets } from './validate_with_schema';
import {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/actions/server/shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ActionsConfigType } from './types';
import { TaskManager } from '../../task_manager';
import { XPackMainPlugin } from '../../xpack_main/xpack_main';
import KbnServer from '../../../../../src/legacy/server/kbn_server';
import { SpacesPlugin as SpacesPluginStartContract } from '../../spaces';
import { LegacySpacesPlugin as SpacesPluginStartContract } from '../../spaces';
import { EncryptedSavedObjectsPlugin } from '../../encrypted_saved_objects';
import { PluginSetupContract as SecurityPlugin } from '../../../../plugins/security/server';
import {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/alerting/server/shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import Hapi from 'hapi';
import { Legacy } from 'kibana';
import { SpacesPlugin as SpacesPluginStartContract } from '../../spaces';
import { LegacySpacesPlugin as SpacesPluginStartContract } from '../../spaces';
import { TaskManager } from '../../task_manager';
import { XPackMainPlugin } from '../../xpack_main/xpack_main';
import KbnServer from '../../../../../src/legacy/server/kbn_server';
Expand Down
4 changes: 2 additions & 2 deletions x-pack/legacy/plugins/ml/server/lib/spaces_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
*/

import { Request } from 'hapi';
import { SpacesPlugin } from '../../../spaces';
import { LegacySpacesPlugin } from '../../../spaces';
import { Space } from '../../../spaces/common/model/space';

interface GetActiveSpaceResponse {
valid: boolean;
space?: Space;
}

export function spacesUtilsProvider(spacesPlugin: SpacesPlugin, request: Request) {
export function spacesUtilsProvider(spacesPlugin: LegacySpacesPlugin, request: Request) {
async function activeSpace(): Promise<GetActiveSpaceResponse> {
try {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { SpacesPlugin } from '../../../../spaces';
import { LegacySpacesPlugin } from '../../../../spaces';
import { OptionalPlugin } from '../../../../../server/lib/optional_plugin';
import { checkPrivilegesDynamicallyWithRequestFactory } from './check_privileges_dynamically';

Expand All @@ -23,7 +23,7 @@ test(`checkPrivileges.atSpace when spaces is enabled`, async () => {
getBasePath: jest.fn(),
getScopedSpacesClient: jest.fn(),
getActiveSpace: jest.fn(),
} as OptionalPlugin<SpacesPlugin>;
} as OptionalPlugin<LegacySpacesPlugin>;
const request = Symbol();
const privilegeOrPrivileges = ['foo', 'bar'];
const checkPrivilegesDynamically = checkPrivilegesDynamicallyWithRequestFactory(
Expand All @@ -45,7 +45,7 @@ test(`checkPrivileges.globally when spaces is disabled`, async () => {
const mockCheckPrivilegesWithRequest = jest.fn().mockReturnValue(mockCheckPrivileges);
const mockSpaces = {
isEnabled: false,
} as OptionalPlugin<SpacesPlugin>;
} as OptionalPlugin<LegacySpacesPlugin>;
const request = Symbol();
const privilegeOrPrivileges = ['foo', 'bar'];
const checkPrivilegesDynamically = checkPrivilegesDynamicallyWithRequestFactory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { CheckPrivilegesAtResourceResponse, CheckPrivilegesWithRequest } from '.
* you may not use this file except in compliance with the Elastic License.
*/

import { SpacesPlugin } from '../../../../spaces';
import { LegacySpacesPlugin } from '../../../../spaces';
import { OptionalPlugin } from '../../../../../server/lib/optional_plugin';

export type CheckPrivilegesDynamically = (
Expand All @@ -26,7 +26,7 @@ export type CheckPrivilegesDynamicallyWithRequest = (

export function checkPrivilegesDynamicallyWithRequestFactory(
checkPrivilegesWithRequest: CheckPrivilegesWithRequest,
spaces: OptionalPlugin<SpacesPlugin>
spaces: OptionalPlugin<LegacySpacesPlugin>
): CheckPrivilegesDynamicallyWithRequest {
return function checkPrivilegesDynamicallyWithRequest(request: Legacy.Request) {
const checkPrivileges = checkPrivilegesWithRequest(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { SpacesPlugin } from '../../../../spaces';
import { LegacySpacesPlugin } from '../../../../spaces';
import { OptionalPlugin } from '../../../../../server/lib/optional_plugin';
import { checkSavedObjectsPrivilegesWithRequestFactory } from './check_saved_objects_privileges';

Expand All @@ -19,7 +19,7 @@ test(`checkPrivileges.atSpace when spaces is enabled`, async () => {
const mockSpaces = ({
isEnabled: true,
namespaceToSpaceId: jest.fn().mockReturnValue(spaceId),
} as unknown) as OptionalPlugin<SpacesPlugin>;
} as unknown) as OptionalPlugin<LegacySpacesPlugin>;
const request = Symbol();

const privilegeOrPrivileges = ['foo', 'bar'];
Expand Down Expand Up @@ -50,7 +50,7 @@ test(`checkPrivileges.globally when spaces is disabled`, async () => {
namespaceToSpaceId: jest.fn().mockImplementation(() => {
throw new Error('should not be called');
}),
} as unknown) as OptionalPlugin<SpacesPlugin>;
} as unknown) as OptionalPlugin<LegacySpacesPlugin>;

const request = Symbol();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { Legacy } from 'kibana';
import { SpacesPlugin } from '../../../../spaces';
import { LegacySpacesPlugin } from '../../../../spaces';
import { OptionalPlugin } from '../../../../../server/lib/optional_plugin';
import { CheckPrivilegesAtResourceResponse, CheckPrivilegesWithRequest } from './check_privileges';

Expand All @@ -19,7 +19,7 @@ export type CheckSavedObjectsPrivileges = (

export const checkSavedObjectsPrivilegesWithRequestFactory = (
checkPrivilegesWithRequest: CheckPrivilegesWithRequest,
spaces: OptionalPlugin<SpacesPlugin>
spaces: OptionalPlugin<LegacySpacesPlugin>
): CheckSavedObjectsPrivilegesWithRequest => {
return function checkSavedObjectsPrivilegesWithRequest(request: Legacy.Request) {
return async function checkSavedObjectsPrivileges(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { Server } from 'hapi';

import { getClient } from '../../../../../server/lib/get_client_shield';
import { SpacesPlugin } from '../../../../spaces';
import { LegacySpacesPlugin } from '../../../../spaces';
import { XPackFeature, XPackMainPlugin } from '../../../../xpack_main/xpack_main';
import { APPLICATION_PREFIX } from '../../../common/constants';
import { OptionalPlugin } from '../../../../../server/lib/optional_plugin';
Expand Down Expand Up @@ -38,7 +38,7 @@ export function createAuthorizationService(
server: Server,
securityXPackFeature: XPackFeature,
xpackMainPlugin: XPackMainPlugin,
spaces: OptionalPlugin<SpacesPlugin>
spaces: OptionalPlugin<LegacySpacesPlugin>
): AuthorizationService {
const shieldClient = getClient(server);
const config = server.config();
Expand Down
2 changes: 0 additions & 2 deletions x-pack/legacy/plugins/spaces/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@

export { isReservedSpace } from './is_reserved_space';
export { MAX_SPACE_INITIALS } from './constants';

export { getSpaceIdFromPath, addSpaceIdToPath } from './lib/spaces_url_parser';
113 changes: 44 additions & 69 deletions x-pack/legacy/plugins/spaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@
* you may not use this file except in compliance with the Elastic License.
*/

import * as Rx from 'rxjs';
import { resolve } from 'path';
import KbnServer, { Server } from 'src/legacy/server/kbn_server';
import { CoreSetup, PluginInitializerContext } from 'src/core/server';
import { Legacy } from 'kibana';
import { KibanaRequest } from '../../../../src/core/server';
import { SpacesServiceSetup } from '../../../plugins/spaces/server/spaces_service/spaces_service';
import { SpacesPluginSetup } from '../../../plugins/spaces/server';
import { createOptionalPlugin } from '../../server/lib/optional_plugin';
// @ts-ignore
import { AuditLogger } from '../../server/lib/audit_logger';
import mappings from './mappings.json';
import { wrapError } from './server/lib/errors';
import { getActiveSpace } from './server/lib/get_active_space';
import { migrateToKibana660 } from './server/lib/migrations';
import { plugin } from './server/new_platform';
import { SecurityPlugin } from '../security';
import { SpacesServiceSetup } from './server/new_platform/spaces_service/spaces_service';
// @ts-ignore
import { watchStatusAndLicenseToInitialize } from '../../server/lib/watch_status_and_license_to_initialize';
import { initSpaceSelectorView, initEnterSpaceView } from './server/routes/views';

export interface SpacesPlugin {
getSpaceId: SpacesServiceSetup['getSpaceId'];
getActiveSpace: SpacesServiceSetup['getActiveSpace'];
export interface LegacySpacesPlugin {
getSpaceId: (request: Legacy.Request) => ReturnType<SpacesServiceSetup['getSpaceId']>;
getActiveSpace: (request: Legacy.Request) => ReturnType<SpacesServiceSetup['getActiveSpace']>;
spaceIdToNamespace: SpacesServiceSetup['spaceIdToNamespace'];
namespaceToSpaceId: SpacesServiceSetup['namespaceToSpaceId'];
getBasePath: SpacesServiceSetup['getBasePath'];
getScopedSpacesClient: SpacesServiceSetup['scopedClient'];
}

export const spaces = (kibana: Record<string, any>) =>
Expand All @@ -36,13 +36,6 @@ export const spaces = (kibana: Record<string, any>) =>
publicDir: resolve(__dirname, 'public'),
require: ['kibana', 'elasticsearch', 'xpack_main'],

config(Joi: any) {
return Joi.object({
enabled: Joi.boolean().default(true),
maxSpaces: Joi.number().default(1000),
}).default();
},

uiCapabilities() {
return {
spaces: {
Expand Down Expand Up @@ -92,18 +85,20 @@ export const spaces = (kibana: Record<string, any>) =>
},
async replaceInjectedVars(
vars: Record<string, any>,
request: Record<string, any>,
request: Legacy.Request,
server: Record<string, any>
) {
const spacesClient = await server.plugins.spaces.getScopedSpacesClient(request);
const spacesPlugin = server.newPlatform.setup.plugins.spaces as SpacesPluginSetup;
if (!spacesPlugin) {
throw new Error('New Platform XPack Spaces plugin is not available.');
}
const kibanaRequest = KibanaRequest.from(request);
const spaceId = spacesPlugin.spacesService.getSpaceId(kibanaRequest);
const spacesClient = await spacesPlugin.spacesService.scopedClient(kibanaRequest);
try {
vars.activeSpace = {
valid: true,
space: await getActiveSpace(
spacesClient,
request.getBasePath(),
server.config().get('server.basePath')
),
space: await spacesClient.get(spaceId),
};
} catch (e) {
vars.activeSpace = {
Expand All @@ -118,52 +113,18 @@ export const spaces = (kibana: Record<string, any>) =>

async init(server: Server) {
const kbnServer = (server as unknown) as KbnServer;
const initializerContext = {
config: {
create: () => {
return Rx.of({
maxSpaces: server.config().get('xpack.spaces.maxSpaces'),
});
},
},
logger: {
get(...contextParts: string[]) {
return kbnServer.newPlatform.coreContext.logger.get(
'plugins',
'spaces',
...contextParts
);
},
},
} as PluginInitializerContext;

const core = (kbnServer.newPlatform.setup.core as unknown) as CoreSetup;

const plugins = {
xpackMain: server.plugins.xpack_main,
// TODO: Spaces has a circular dependency with Security right now.
// Security is not yet available when init runs, so this is wrapped in an optional function for the time being.
security: createOptionalPlugin<SecurityPlugin>(
server.config(),
'xpack.security',
server.plugins,
'security'
),
spaces: this,
};

const { spacesService, registerLegacyAPI } = await plugin(initializerContext).setup(
core,
plugins
);
const spacesPlugin = kbnServer.newPlatform.setup.plugins.spaces as SpacesPluginSetup;
if (!spacesPlugin) {
throw new Error('New Platform XPack Spaces plugin is not available.');
}

const config = server.config();

const { registerLegacyAPI, createDefaultSpace } = spacesPlugin.__legacyCompat;

registerLegacyAPI({
router: server.route.bind(server),
legacyConfig: {
serverBasePath: config.get('server.basePath'),
serverDefaultRoute: config.get('server.defaultRoute'),
kibanaIndex: config.get('kibana.index'),
},
savedObjects: server.savedObjects,
Expand All @@ -178,16 +139,30 @@ export const spaces = (kibana: Record<string, any>) =>
create: (pluginId: string) =>
new AuditLogger(server, pluginId, server.config(), server.plugins.xpack_main.info),
},
security: createOptionalPlugin<SecurityPlugin>(
server.config(),
'xpack.security',
server.plugins,
'security'
),
xpackMain: server.plugins.xpack_main,
});

initEnterSpaceView(server);
initSpaceSelectorView(server);

server.expose('getSpaceId', (request: any) => spacesService.getSpaceId(request));
server.expose('getActiveSpace', spacesService.getActiveSpace);
server.expose('spaceIdToNamespace', spacesService.spaceIdToNamespace);
server.expose('namespaceToSpaceId', spacesService.namespaceToSpaceId);
server.expose('getBasePath', spacesService.getBasePath);
server.expose('getScopedSpacesClient', spacesService.scopedClient);
watchStatusAndLicenseToInitialize(server.plugins.xpack_main, this, async () => {
await createDefaultSpace();
});

server.expose('getSpaceId', (request: Legacy.Request) =>
spacesPlugin.spacesService.getSpaceId(request)
);
server.expose('getActiveSpace', (request: Legacy.Request) =>
spacesPlugin.spacesService.getActiveSpace(request)
);
server.expose('spaceIdToNamespace', spacesPlugin.spacesService.spaceIdToNamespace);
server.expose('namespaceToSpaceId', spacesPlugin.spacesService.namespaceToSpaceId);
server.expose('getBasePath', spacesPlugin.spacesService.getBasePath);
},
});
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/spaces/public/lib/spaces_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Space } from '../../common/model/space';
import { GetSpacePurpose } from '../../common/model/types';
import { CopySavedObjectsToSpaceResponse } from './copy_saved_objects_to_space/types';
import { ENTER_SPACE_PATH } from '../../common/constants';
import { addSpaceIdToPath } from '../../common';
import { addSpaceIdToPath } from '../../../../../plugins/spaces/common';

export class SpacesManager extends EventEmitter {
constructor(private readonly serverBasePath: string) {
Expand Down
41 changes: 0 additions & 41 deletions x-pack/legacy/plugins/spaces/server/lib/check_license.ts

This file was deleted.

Loading

0 comments on commit 30f69df

Please sign in to comment.