Skip to content

Commit

Permalink
migrate to NP router
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth committed Feb 4, 2020
1 parent 9a53aea commit 03d8aa1
Show file tree
Hide file tree
Showing 19 changed files with 600 additions and 275 deletions.
32 changes: 13 additions & 19 deletions x-pack/legacy/plugins/remote_clusters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import { Legacy } from 'kibana';
import { resolve } from 'path';
import { PLUGIN } from './common';
import { Plugin as RemoteClustersPlugin } from './plugin';
import { createShim } from './shim';
import { plugin } from './server';

export function remoteClusters(kibana: any) {
return new kibana.Plugin({
Expand Down Expand Up @@ -43,25 +42,20 @@ export function remoteClusters(kibana: any) {
config.get('xpack.remote_clusters.enabled') && config.get('xpack.index_management.enabled')
);
},
init(server: Legacy.Server) {
const {
coreSetup,
pluginsSetup: {
license: { registerLicenseChecker },
},
} = createShim(server, PLUGIN.ID);

const remoteClustersPlugin = new RemoteClustersPlugin();
init(server: any) {
const { core: coreSetup } = server.newPlatform.setup;

// Set up plugin.
remoteClustersPlugin.setup(coreSetup);
const remoteClustersPluginInstance = plugin();

registerLicenseChecker(
server,
PLUGIN.ID,
PLUGIN.getI18nName(),
PLUGIN.MINIMUM_LICENSE_REQUIRED
);
remoteClustersPluginInstance.setup(coreSetup, {
__LEGACY: {
route: server.route.bind(server),
plugins: {
xpack_main: server.plugins.xpack_main,
remote_clusters: server.plugins[PLUGIN.ID],
},
},
});
},
});
}
9 changes: 9 additions & 0 deletions x-pack/legacy/plugins/remote_clusters/kibana.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"id": "remote_clusters",
"version": "kibana",
"requiredPlugins": [
"home"
],
"server": true,
"ui": true
}
30 changes: 0 additions & 30 deletions x-pack/legacy/plugins/remote_clusters/plugin.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class RemoteClusterEdit extends Component {
stopEditingCluster: PropTypes.func,
editCluster: PropTypes.func,
isEditingCluster: PropTypes.bool,
getEditClusterError: PropTypes.string,
getEditClusterError: PropTypes.object,
clearEditClusterErrors: PropTypes.func,
openDetailPanel: PropTypes.func,
};
Expand Down
8 changes: 8 additions & 0 deletions x-pack/legacy/plugins/remote_clusters/server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* 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 { RemoteClustersServerPlugin } from './plugin';

export const plugin = () => new RemoteClustersServerPlugin();
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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 { ElasticsearchServiceSetup } from 'kibana/server';
import { once } from 'lodash';

const callWithRequest = once((elasticsearchService: ElasticsearchServiceSetup) => {
return elasticsearchService.createClient('remote_clusters', {});
});

export const callWithRequestFactory = (
elasticsearchService: ElasticsearchServiceSetup,
request: any
) => {
return (...args: any[]) => {
return (
callWithRequest(elasticsearchService)
.asScoped(request)
// @ts-ignore
.callAsCurrentUser(...args)
);
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* 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 { callWithRequestFactory } from './call_with_request_factory';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* 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 { isEsError } from './is_es_error';
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* 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 * as legacyElasticsearch from 'elasticsearch';

const esErrorsParent = legacyElasticsearch.errors._Abstract;

export function isEsError(err: Error) {
return err instanceof esErrorsParent;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* 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 { licensePreRoutingFactory } from './license_pre_routing_factory';
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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 expect from '@kbn/expect';
import { licensePreRoutingFactory } from '.';
import {
LICENSE_STATUS_VALID,
LICENSE_STATUS_INVALID,
} from '../../../../../common/constants/license_status';
import { kibanaResponseFactory } from '../../../../../../../src/core/server';

describe('licensePreRoutingFactory()', () => {
let mockServer;
let mockLicenseCheckResults;

beforeEach(() => {
mockServer = {
plugins: {
xpack_main: {
info: {
feature: () => ({
getLicenseCheckResults: () => mockLicenseCheckResults,
}),
},
},
},
};
});

describe('status is invalid', () => {
beforeEach(() => {
mockLicenseCheckResults = {
status: LICENSE_STATUS_INVALID,
};
});

it('replies with 403', () => {
const routeWithLicenseCheck = licensePreRoutingFactory(mockServer, () => {});
const stubRequest = {};
const response = routeWithLicenseCheck({}, stubRequest, kibanaResponseFactory);
expect(response.status).to.be(403);
});
});

describe('status is valid', () => {
beforeEach(() => {
mockLicenseCheckResults = {
status: LICENSE_STATUS_VALID,
};
});

it('replies with nothing', () => {
const routeWithLicenseCheck = licensePreRoutingFactory(mockServer, () => null);
const stubRequest = {};
const response = routeWithLicenseCheck({}, stubRequest, kibanaResponseFactory);
expect(response).to.be(null);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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 {
KibanaRequest,
KibanaResponseFactory,
RequestHandler,
RequestHandlerContext,
} from 'src/core/server';
import { PLUGIN } from '../../../common';
import { LICENSE_STATUS_VALID } from '../../../../../common/constants/license_status';
import { ServerShim } from '../../types';

export const licensePreRoutingFactory = (
server: ServerShim,
handler: RequestHandler<any, any, any>
): RequestHandler<any, any, any> => {
const xpackMainPlugin = server.plugins.xpack_main;

// License checking and enable/disable logic
return function licensePreRouting(
ctx: RequestHandlerContext,
request: KibanaRequest,
response: KibanaResponseFactory
) {
const licenseCheckResults = xpackMainPlugin.info.feature(PLUGIN.ID).getLicenseCheckResults();
const { status } = licenseCheckResults;

if (status !== LICENSE_STATUS_VALID) {
return response.customError({
body: {
message: licenseCheckResults.messsage,
},
statusCode: 403,
});
}

return handler(ctx, request, response);
};
};
53 changes: 53 additions & 0 deletions x-pack/legacy/plugins/remote_clusters/server/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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 { CoreSetup, Plugin } from 'src/core/server';

import { registerLicenseChecker } from '../../../server/lib/register_license_checker';
import { PLUGIN } from '../common';
import { ServerShim, RouteDependencies } from './types';

import {
registerGetRoute,
registerAddRoute,
registerUpdateRoute,
registerDeleteRoute,
} from './routes/api';

export class RemoteClustersServerPlugin implements Plugin<void, void, any, any> {
async setup(
{ http, elasticsearch: elasticsearchService }: CoreSetup,
{
__LEGACY: serverShim,
}: {
__LEGACY: ServerShim;
}
) {
const elasticsearch = await elasticsearchService.adminClient;
const router = http.createRouter();
const routeDependencies: RouteDependencies = {
elasticsearch,
elasticsearchService,
router,
};

registerLicenseChecker(
serverShim as any,
PLUGIN.ID,
PLUGIN.getI18nName(),
PLUGIN.MINIMUM_LICENSE_REQUIRED
);

// Register routes.
registerGetRoute(routeDependencies, serverShim);
registerAddRoute(routeDependencies, serverShim);
registerUpdateRoute(routeDependencies, serverShim);
registerDeleteRoute(routeDependencies, serverShim);
}

start() {}

stop() {}
}
Loading

0 comments on commit 03d8aa1

Please sign in to comment.