Skip to content

Commit

Permalink
[Code] use callWithRequest instead of callWithInternalUser in cluster…
Browse files Browse the repository at this point in the history
… routes (#33098)
  • Loading branch information
spacedragon authored and zfy0701 committed Mar 15, 2019
1 parent feedd92 commit 1c79c1b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
20 changes: 13 additions & 7 deletions x-pack/plugins/code/server/code_node_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ export const CODE_NODE_TYPE = 'code-node';
export const CODE_NODE_ID = 'code-node-info';

export class CodeNodeClient {
private readonly objectsClient: any;
constructor(server: Server) {
const repo = server.savedObjects.getSavedObjectsRepository(
server.plugins.elasticsearch.getCluster('admin').callWithInternalUser
);
this.objectsClient = new server.savedObjects.SavedObjectsClient(repo);
}
constructor(readonly objectsClient: any) {}

public async getCodeNodeInfo(): Promise<CodeNodeInfo | undefined> {
try {
Expand All @@ -37,6 +31,18 @@ export class CodeNodeClient {
}
}

export function clientWithInternalUser(server: Server): CodeNodeClient {
const repo = server.savedObjects.getSavedObjectsRepository(
server.plugins.elasticsearch.getCluster('admin').callWithInternalUser
);
const objectsClient = new server.savedObjects.SavedObjectsClient(repo);
return new CodeNodeClient(objectsClient);
}

export function clientWithRequest(request: any): CodeNodeClient {
return new CodeNodeClient(request.getSavedObjectsClient());
}

export interface CodeNodeInfo {
uuid: string;
url: string;
Expand Down
6 changes: 3 additions & 3 deletions x-pack/plugins/code/server/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { Server } from 'hapi';
import { checkRepos } from './check_repos';
import { CodeNodeClient, CodeNodeInfo } from './code_node_client';
import { clientWithInternalUser, CodeNodeInfo } from './code_node_client';
import { LspIndexerFactory, RepositoryIndexInitializerFactory, tryMigrateIndices } from './indexer';
import { EsClient, Esqueue } from './lib/esqueue';
import { Logger } from './log';
Expand Down Expand Up @@ -72,11 +72,11 @@ export function init(server: Server, options: any) {
const kbnServer = this.kbnServer;
kbnServer.ready().then(async () => {
const serverUuid = await retryUntilAvailable(() => getServerUuid(server), 50);
const codeNodeClient = new CodeNodeClient(server);
const codeNodeClient = clientWithInternalUser(server);
// enable security check in routes
enableSecurity(server);
// enable cluster status routes
clusterRoute(server, codeNodeClient, log);
clusterRoute(server);
if (serverOptions.codeNode) {
let info = await codeNodeClient.getCodeNodeInfo();
if (!info) {
Expand Down
17 changes: 8 additions & 9 deletions x-pack/plugins/code/server/routes/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@

import * as Boom from 'boom';
import { Server } from 'hapi';
import { Logger } from 'vscode-jsonrpc';
import { CodeNodeClient } from '../code_node_client';
import { clientWithRequest } from '../code_node_client';

export function clusterRoute(server: Server, codeNodeClient: CodeNodeClient, log: Logger) {
server.securedRoute({
export function clusterRoute(server: Server) {
server.route({
path: '/api/code/cluster',
method: 'GET',
requireAdmin: false,
async handler() {
async handler(req: any) {
const codeNodeClient = clientWithRequest(req);
const info = codeNodeClient.getCodeNodeInfo();
if (info) {
return info;
Expand All @@ -24,11 +23,11 @@ export function clusterRoute(server: Server, codeNodeClient: CodeNodeClient, log
},
});

server.securedRoute({
server.route({
path: '/api/code/cluster',
method: 'DELETE',
requireAdmin: true,
async handler() {
async handler(req: any) {
const codeNodeClient = clientWithRequest(req);
const info = codeNodeClient.getCodeNodeInfo();
if (info) {
await codeNodeClient.deleteNodeInfo();
Expand Down

0 comments on commit 1c79c1b

Please sign in to comment.