Skip to content

Commit

Permalink
Spaces - NP updates for usage collection and capabilities (#57693)
Browse files Browse the repository at this point in the history
* remove kibanaIndex from LegacyAPI

* moving capabilities, adding tests

* moving usage collection

* cleanup

* don't toggle capabilities on unauthenticated routes

* reintroduce exception handling

* pipe dat config

* start addressing PR feedback

* fix CoreSetup's generic type

* fix usage collector tests

* PR review updates

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
legrego and elasticmachine authored Feb 28, 2020
1 parent 3f7abe3 commit 91330d2
Show file tree
Hide file tree
Showing 23 changed files with 409 additions and 119 deletions.
6 changes: 5 additions & 1 deletion src/core/server/http/http_server.mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ interface RequestFixtureOptions<P = any, Q = any, B = any> {
method?: RouteMethod;
socket?: Socket;
routeTags?: string[];
routeAuthRequired?: false;
validation?: {
params?: RouteValidationSpec<P>;
query?: RouteValidationSpec<Q>;
Expand All @@ -59,6 +60,7 @@ function createKibanaRequestMock<P = any, Q = any, B = any>({
method = 'get',
socket = new Socket(),
routeTags,
routeAuthRequired,
validation = {},
}: RequestFixtureOptions<P, Q, B> = {}) {
const queryString = stringify(query, { sort: false });
Expand All @@ -77,7 +79,9 @@ function createKibanaRequestMock<P = any, Q = any, B = any>({
query: queryString,
search: queryString ? `?${queryString}` : queryString,
},
route: { settings: { tags: routeTags } },
route: {
settings: { tags: routeTags, auth: routeAuthRequired },
},
raw: {
req: { socket },
},
Expand Down
38 changes: 38 additions & 0 deletions src/plugins/usage_collection/server/mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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 { loggingServiceMock } from '../../../core/server/mocks';
import { UsageCollectionSetup } from './plugin';
import { CollectorSet } from './collector';

const createSetupContract = () => {
return {
...new CollectorSet({
logger: loggingServiceMock.createLogger(),
maximumWaitTimeForAllCollectorsInS: 1,
}),
registerLegacySavedObjects: jest.fn() as jest.Mocked<
UsageCollectionSetup['registerLegacySavedObjects']
>,
} as UsageCollectionSetup;
};

export const usageCollectionPluginMock = {
createSetupContract,
};
18 changes: 0 additions & 18 deletions x-pack/legacy/plugins/spaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,6 @@ export const spaces = (kibana: Record<string, any>) =>
publicDir: resolve(__dirname, 'public'),
require: ['kibana', 'elasticsearch', 'xpack_main'],

uiCapabilities() {
return {
spaces: {
manage: true,
},
management: {
kibana: {
spaces: true,
},
},
};
},

uiExports: {
styleSheetPaths: resolve(__dirname, 'public/index.scss'),
managementSections: [],
Expand Down Expand Up @@ -110,14 +97,9 @@ export const spaces = (kibana: Record<string, any>) =>
throw new Error('New Platform XPack Spaces plugin is not available.');
}

const config = server.config();

const { registerLegacyAPI, createDefaultSpace } = spacesPlugin.__legacyCompat;

registerLegacyAPI({
legacyConfig: {
kibanaIndex: config.get('kibana.index'),
},
savedObjects: server.savedObjects,
auditLogger: {
create: (pluginId: string) =>
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/features/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Plugin } from './plugin';
export { uiCapabilitiesRegex } from './feature_schema';

export { Feature, FeatureWithAllOrReadPrivileges, FeatureKibanaPrivileges } from '../common';
export { PluginSetupContract } from './plugin';
export { PluginSetupContract, PluginStartContract } from './plugin';

export const plugin = (initializerContext: PluginInitializerContext) =>
new Plugin(initializerContext);
27 changes: 27 additions & 0 deletions x-pack/plugins/features/server/mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { PluginSetupContract, PluginStartContract } from './plugin';

const createSetup = (): jest.Mocked<PluginSetupContract> => {
return {
getFeatures: jest.fn(),
getFeaturesUICapabilities: jest.fn(),
registerFeature: jest.fn(),
registerLegacyAPI: jest.fn(),
};
};

const createStart = (): jest.Mocked<PluginStartContract> => {
return {
getFeatures: jest.fn(),
};
};

export const featuresPluginMock = {
createSetup,
createStart,
};
23 changes: 15 additions & 8 deletions x-pack/plugins/features/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export interface PluginSetupContract {
registerLegacyAPI: (legacyAPI: LegacyAPI) => void;
}

export interface PluginStartContract {
getFeatures(): Feature[];
}

/**
* Describes a set of APIs that are available in the legacy platform only and required by this plugin
* to function properly.
Expand All @@ -45,6 +49,8 @@ export interface LegacyAPI {
export class Plugin {
private readonly logger: Logger;

private readonly featureRegistry: FeatureRegistry = new FeatureRegistry();

private legacyAPI?: LegacyAPI;
private readonly getLegacyAPI = () => {
if (!this.legacyAPI) {
Expand All @@ -61,18 +67,16 @@ export class Plugin {
core: CoreSetup,
{ timelion }: { timelion?: TimelionSetupContract }
): Promise<RecursiveReadonly<PluginSetupContract>> {
const featureRegistry = new FeatureRegistry();

defineRoutes({
router: core.http.createRouter(),
featureRegistry,
featureRegistry: this.featureRegistry,
getLegacyAPI: this.getLegacyAPI,
});

return deepFreeze({
registerFeature: featureRegistry.register.bind(featureRegistry),
getFeatures: featureRegistry.getAll.bind(featureRegistry),
getFeaturesUICapabilities: () => uiCapabilitiesForFeatures(featureRegistry.getAll()),
registerFeature: this.featureRegistry.register.bind(this.featureRegistry),
getFeatures: this.featureRegistry.getAll.bind(this.featureRegistry),
getFeaturesUICapabilities: () => uiCapabilitiesForFeatures(this.featureRegistry.getAll()),

registerLegacyAPI: (legacyAPI: LegacyAPI) => {
this.legacyAPI = legacyAPI;
Expand All @@ -82,14 +86,17 @@ export class Plugin {
savedObjectTypes: this.legacyAPI.savedObjectTypes,
includeTimelion: timelion !== undefined && timelion.uiEnabled,
})) {
featureRegistry.register(feature);
this.featureRegistry.register(feature);
}
},
});
}

public start() {
public start(): RecursiveReadonly<PluginStartContract> {
this.logger.debug('Starting plugin');
return deepFreeze({
getFeatures: this.featureRegistry.getAll.bind(this.featureRegistry),
});
}

public stop() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { capabilitiesProvider } from './capabilities_provider';

describe('Capabilities provider', () => {
it('provides the expected capabilities', () => {
expect(capabilitiesProvider()).toMatchInlineSnapshot(`
Object {
"management": Object {
"kibana": Object {
"spaces": true,
},
},
"spaces": Object {
"manage": true,
},
}
`);
});
});
16 changes: 16 additions & 0 deletions x-pack/plugins/spaces/server/capabilities/capabilities_provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

export const capabilitiesProvider = () => ({
spaces: {
manage: true,
},
management: {
kibana: {
spaces: true,
},
},
});
Loading

0 comments on commit 91330d2

Please sign in to comment.