Skip to content

Commit 1c79c1b

Browse files
spacedragonzfy0701
authored andcommitted
[Code] use callWithRequest instead of callWithInternalUser in cluster routes (#33098)
1 parent feedd92 commit 1c79c1b

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

x-pack/plugins/code/server/code_node_client.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@ export const CODE_NODE_TYPE = 'code-node';
99
export const CODE_NODE_ID = 'code-node-info';
1010

1111
export class CodeNodeClient {
12-
private readonly objectsClient: any;
13-
constructor(server: Server) {
14-
const repo = server.savedObjects.getSavedObjectsRepository(
15-
server.plugins.elasticsearch.getCluster('admin').callWithInternalUser
16-
);
17-
this.objectsClient = new server.savedObjects.SavedObjectsClient(repo);
18-
}
12+
constructor(readonly objectsClient: any) {}
1913

2014
public async getCodeNodeInfo(): Promise<CodeNodeInfo | undefined> {
2115
try {
@@ -37,6 +31,18 @@ export class CodeNodeClient {
3731
}
3832
}
3933

34+
export function clientWithInternalUser(server: Server): CodeNodeClient {
35+
const repo = server.savedObjects.getSavedObjectsRepository(
36+
server.plugins.elasticsearch.getCluster('admin').callWithInternalUser
37+
);
38+
const objectsClient = new server.savedObjects.SavedObjectsClient(repo);
39+
return new CodeNodeClient(objectsClient);
40+
}
41+
42+
export function clientWithRequest(request: any): CodeNodeClient {
43+
return new CodeNodeClient(request.getSavedObjectsClient());
44+
}
45+
4046
export interface CodeNodeInfo {
4147
uuid: string;
4248
url: string;

x-pack/plugins/code/server/init.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import { Server } from 'hapi';
88
import { checkRepos } from './check_repos';
9-
import { CodeNodeClient, CodeNodeInfo } from './code_node_client';
9+
import { clientWithInternalUser, CodeNodeInfo } from './code_node_client';
1010
import { LspIndexerFactory, RepositoryIndexInitializerFactory, tryMigrateIndices } from './indexer';
1111
import { EsClient, Esqueue } from './lib/esqueue';
1212
import { Logger } from './log';
@@ -72,11 +72,11 @@ export function init(server: Server, options: any) {
7272
const kbnServer = this.kbnServer;
7373
kbnServer.ready().then(async () => {
7474
const serverUuid = await retryUntilAvailable(() => getServerUuid(server), 50);
75-
const codeNodeClient = new CodeNodeClient(server);
75+
const codeNodeClient = clientWithInternalUser(server);
7676
// enable security check in routes
7777
enableSecurity(server);
7878
// enable cluster status routes
79-
clusterRoute(server, codeNodeClient, log);
79+
clusterRoute(server);
8080
if (serverOptions.codeNode) {
8181
let info = await codeNodeClient.getCodeNodeInfo();
8282
if (!info) {

x-pack/plugins/code/server/routes/cluster.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66

77
import * as Boom from 'boom';
88
import { Server } from 'hapi';
9-
import { Logger } from 'vscode-jsonrpc';
10-
import { CodeNodeClient } from '../code_node_client';
9+
import { clientWithRequest } from '../code_node_client';
1110

12-
export function clusterRoute(server: Server, codeNodeClient: CodeNodeClient, log: Logger) {
13-
server.securedRoute({
11+
export function clusterRoute(server: Server) {
12+
server.route({
1413
path: '/api/code/cluster',
1514
method: 'GET',
16-
requireAdmin: false,
17-
async handler() {
15+
async handler(req: any) {
16+
const codeNodeClient = clientWithRequest(req);
1817
const info = codeNodeClient.getCodeNodeInfo();
1918
if (info) {
2019
return info;
@@ -24,11 +23,11 @@ export function clusterRoute(server: Server, codeNodeClient: CodeNodeClient, log
2423
},
2524
});
2625

27-
server.securedRoute({
26+
server.route({
2827
path: '/api/code/cluster',
2928
method: 'DELETE',
30-
requireAdmin: true,
31-
async handler() {
29+
async handler(req: any) {
30+
const codeNodeClient = clientWithRequest(req);
3231
const info = codeNodeClient.getCodeNodeInfo();
3332
if (info) {
3433
await codeNodeClient.deleteNodeInfo();

0 commit comments

Comments
 (0)