Skip to content
7 changes: 7 additions & 0 deletions x-pack/legacy/plugins/code/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ declare module 'src/core/server' {
interface RequestHandlerContext {
code: {
codeServices: CodeServices | null;
// @deprecated
legacy: {
securityPlugin: any;
};
};
}
}
Expand Down Expand Up @@ -94,6 +98,9 @@ export class CodePlugin {
npHttp.registerRouteHandlerContext('code', () => {
return {
codeServices: this.codeServices,
legacy: {
securityPlugin: server.plugins.security,
},
};
});
}
Expand Down
31 changes: 16 additions & 15 deletions x-pack/legacy/plugins/code/server/routes/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ export function fileRoute(router: CodeServerRouter, codeServices: CodeServices)

async function getRepoUriFromMeta(
context: RequestHandlerContext,
req: KibanaRequest,
repoUri: string
): Promise<string | undefined> {
const repoObjectClient = new RepositoryObjectClient(new EsClientWithRequest(context));
const repoObjectClient = new RepositoryObjectClient(new EsClientWithRequest(context, req));

try {
const repo = await repoObjectClient.getRepository(repoUri);
Expand Down Expand Up @@ -49,7 +50,7 @@ export function fileRoute(router: CodeServerRouter, codeServices: CodeServices)
const skip = queries.skip ? parseInt(queries.skip as string, 10) : 0;
const withParents = 'parents' in queries;
const flatten = 'flatten' in queries;
const repoUri = await getRepoUriFromMeta(context, uri);
const repoUri = await getRepoUriFromMeta(context, req, uri);
if (!repoUri) {
return res.notFound({ body: `repo ${uri} not found` });
}
Expand All @@ -69,7 +70,7 @@ export function fileRoute(router: CodeServerRouter, codeServices: CodeServices)
if (e.isBoom) {
return res.customError({
body: e.error,
statusCode: e.statusCode,
statusCode: e.statusCode ? e.statusCode : 500,
});
} else {
return res.internalError({ body: e.message || e.name });
Expand All @@ -88,7 +89,7 @@ export function fileRoute(router: CodeServerRouter, codeServices: CodeServices)
) {
const { uri, path, ref } = req.params as any;
const revision = decodeRevisionString(ref);
const repoUri = await getRepoUriFromMeta(context, uri);
const repoUri = await getRepoUriFromMeta(context, req, uri);
if (!repoUri) {
return res.notFound({ body: `repo ${uri} not found` });
}
Expand Down Expand Up @@ -130,7 +131,7 @@ export function fileRoute(router: CodeServerRouter, codeServices: CodeServices)
if (e.isBoom) {
return res.customError({
body: e.error,
statusCode: e.statusCode,
statusCode: e.statusCode ? e.statusCode : 500,
});
} else {
return res.internalError({ body: e.message || e.name });
Expand All @@ -149,7 +150,7 @@ export function fileRoute(router: CodeServerRouter, codeServices: CodeServices)
) {
const { uri, path, ref } = req.params as any;
const revision = decodeRevisionString(ref);
const repoUri = await getRepoUriFromMeta(context, uri);
const repoUri = await getRepoUriFromMeta(context, req, uri);
if (!repoUri) {
return res.notFound({ body: `repo ${uri} not found` });
}
Expand All @@ -172,7 +173,7 @@ export function fileRoute(router: CodeServerRouter, codeServices: CodeServices)
if (e.isBoom) {
return res.customError({
body: e.error,
statusCode: e.statusCode,
statusCode: e.statusCode ? e.statusCode : 500,
});
} else {
return res.internalError({ body: e.message || e.name });
Expand Down Expand Up @@ -204,7 +205,7 @@ export function fileRoute(router: CodeServerRouter, codeServices: CodeServices)
const count = queries.count ? parseInt(queries.count as string, 10) : 10;
const after = queries.after !== undefined;
try {
const repoUri = await getRepoUriFromMeta(context, uri);
const repoUri = await getRepoUriFromMeta(context, req, uri);
if (!repoUri) {
return res.notFound({ body: `repo ${uri} not found` });
}
Expand All @@ -221,7 +222,7 @@ export function fileRoute(router: CodeServerRouter, codeServices: CodeServices)
if (e.isBoom) {
return res.customError({
body: e.error,
statusCode: e.statusCode,
statusCode: e.statusCode ? e.statusCode : 500,
});
} else {
return res.internalError({ body: e.message || e.name });
Expand All @@ -238,7 +239,7 @@ export function fileRoute(router: CodeServerRouter, codeServices: CodeServices)
res: KibanaResponseFactory
) {
const { uri } = req.params as any;
const repoUri = await getRepoUriFromMeta(context, uri);
const repoUri = await getRepoUriFromMeta(context, req, uri);
if (!repoUri) {
return res.badRequest({ body: `repo ${uri} not found` });
}
Expand All @@ -251,7 +252,7 @@ export function fileRoute(router: CodeServerRouter, codeServices: CodeServices)
if (e.isBoom) {
return res.customError({
body: e.error,
statusCode: e.statusCode,
statusCode: e.statusCode ? e.statusCode : 500,
});
} else {
return res.internalError({ body: e.message || e.name });
Expand All @@ -269,7 +270,7 @@ export function fileRoute(router: CodeServerRouter, codeServices: CodeServices)
res: KibanaResponseFactory
) {
const { uri, revision } = req.params as any;
const repoUri = await getRepoUriFromMeta(context, uri);
const repoUri = await getRepoUriFromMeta(context, req, uri);
if (!repoUri) {
return res.notFound({ body: `repo ${uri} not found` });
}
Expand All @@ -284,7 +285,7 @@ export function fileRoute(router: CodeServerRouter, codeServices: CodeServices)
if (e.isBoom) {
return res.customError({
body: e.error,
statusCode: e.statusCode,
statusCode: e.statusCode ? e.statusCode : 500,
});
} else {
return res.internalError({ body: e.message || e.name });
Expand All @@ -302,7 +303,7 @@ export function fileRoute(router: CodeServerRouter, codeServices: CodeServices)
res: KibanaResponseFactory
) {
const { uri, path, revision } = req.params as any;
const repoUri = await getRepoUriFromMeta(context, uri);
const repoUri = await getRepoUriFromMeta(context, req, uri);
if (!repoUri) {
return res.notFound({ body: `repo ${uri} not found` });
}
Expand All @@ -319,7 +320,7 @@ export function fileRoute(router: CodeServerRouter, codeServices: CodeServices)
if (e.isBoom) {
return res.customError({
body: e.error,
statusCode: e.statusCode,
statusCode: e.statusCode ? e.statusCode : 500,
});
} else {
return res.internalError({ body: e.message || e.name });
Expand Down
42 changes: 19 additions & 23 deletions x-pack/legacy/plugins/code/server/routes/lsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ import { SymbolSearchResult } from '../../model';
const LANG_SERVER_ERROR = 'language server error';

export function lspRoute(
server: CodeServerRouter,
router: CodeServerRouter,
codeServices: CodeServices,
serverOptions: ServerOptions,
log: Logger
) {
const lspService = codeServices.serviceFor(LspServiceDefinition);
const gitService = codeServices.serviceFor(GitServiceDefinition);

server.route({
router.route({
path: '/api/code/lsp/textDocument/{method}',
async npHandler(
context: RequestHandlerContext,
Expand Down Expand Up @@ -79,7 +79,7 @@ export function lspRoute(
} else if (error.isBoom) {
return res.customError({
body: error.error,
statusCode: error.statusCode,
statusCode: error.statusCode ? error.statusCode : 500,
});
} else {
log.error(error);
Expand All @@ -99,7 +99,7 @@ export function lspRoute(
method: 'POST',
});

server.route({
router.route({
path: '/api/code/lsp/findDefinitions',
method: 'POST',
async npHandler(
Expand All @@ -110,7 +110,7 @@ export function lspRoute(
// @ts-ignore
const { textDocument, position } = req.body as any;
// @ts-ignore
const { qname } = res.params as any;
const { qname } = req.params as any;
const { uri } = textDocument;
const { repoUri } = parseLspUrl(uri);
await getReferenceHelper(context.core.savedObjects.client).ensureReference(repoUri);
Expand All @@ -130,7 +130,7 @@ export function lspRoute(
},
});
const title: string = await findTitleFromHover(hover, uri, position);
const symbolSearchClient = new SymbolSearchClient(new EsClientWithRequest(context), log);
const symbolSearchClient = new SymbolSearchClient(new EsClientWithRequest(context, req), log);

const locators = response.result as SymbolLocator[];
const locations = [];
Expand All @@ -153,7 +153,7 @@ export function lspRoute(
},
});

server.route({
router.route({
path: '/api/code/lsp/findReferences',
method: 'POST',
async npHandler(
Expand Down Expand Up @@ -197,7 +197,7 @@ export function lspRoute(
} else if (error.isBoom) {
return res.customError({
body: error.error,
statusCode: error.statusCode,
statusCode: error.statusCode ? error.statusCode : 500,
});
} else {
return res.custom({
Expand All @@ -219,25 +219,21 @@ export function symbolByQnameRoute(router: CodeServerRouter, log: Logger) {
req: KibanaRequest,
res: KibanaResponseFactory
) {
try {
// @ts-ignore
const { qname } = res.params as any;
const symbolSearchClient = new SymbolSearchClient(new EsClientWithRequest(context), log);
const repoScope = await getReferenceHelper(
context.core.savedObjects.client
).findReferences();
if (repoScope.length === 0) {
return {
// @ts-ignore
const { qname } = req.params as any;
const symbolSearchClient = new SymbolSearchClient(new EsClientWithRequest(context, req), log);
const repoScope = await getReferenceHelper(context.core.savedObjects.client).findReferences();
if (repoScope.length === 0) {
return res.ok({
body: {
symbols: [],
total: 0,
took: 0,
} as SymbolSearchResult;
}
const symbol = await symbolSearchClient.findByQname(qname, repoScope);
return res.ok({ body: symbol });
} catch (error) {
return res.internalError({ body: `Search Exception` });
} as SymbolSearchResult,
});
}
const symbol = await symbolSearchClient.findByQname(qname, repoScope);
return res.ok({ body: symbol });
},
});
}
16 changes: 8 additions & 8 deletions x-pack/legacy/plugins/code/server/routes/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function repositoryRoute(
}

const repo = RepositoryUtils.buildRepository(repoUrl);
const repoObjectClient = new RepositoryObjectClient(new EsClientWithRequest(context));
const repoObjectClient = new RepositoryObjectClient(new EsClientWithRequest(context, req));

try {
// Check if the repository already exists
Expand Down Expand Up @@ -137,7 +137,7 @@ export function repositoryRoute(
res: KibanaResponseFactory
) {
const { uri: repoUri } = req.params as any;
const repoObjectClient = new RepositoryObjectClient(new EsClientWithRequest(context));
const repoObjectClient = new RepositoryObjectClient(new EsClientWithRequest(context, req));
try {
// make sure the repo belongs to the current space
getReferenceHelper(context.core.savedObjects.client).ensureReference(repoUri);
Expand Down Expand Up @@ -189,7 +189,7 @@ export function repositoryRoute(
const { uri: repoUri } = req.params as any;
try {
await getReferenceHelper(context.core.savedObjects.client).ensureReference(repoUri);
const repoObjectClient = new RepositoryObjectClient(new EsClientWithRequest(context));
const repoObjectClient = new RepositoryObjectClient(new EsClientWithRequest(context, req));
const repo = await repoObjectClient.getRepository(repoUri);
return res.ok({ body: repo });
} catch (error) {
Expand All @@ -211,7 +211,7 @@ export function repositoryRoute(
) {
const { uri: repoUri } = req.params as any;
try {
const repoObjectClient = new RepositoryObjectClient(new EsClientWithRequest(context));
const repoObjectClient = new RepositoryObjectClient(new EsClientWithRequest(context, req));
let gitStatus = null;
let indexStatus = null;
let deleteStatus = null;
Expand Down Expand Up @@ -264,7 +264,7 @@ export function repositoryRoute(
) {
try {
const uris = await getReferenceHelper(context.core.savedObjects.client).findReferences();
const repoObjectClient = new RepositoryObjectClient(new EsClientWithRequest(context));
const repoObjectClient = new RepositoryObjectClient(new EsClientWithRequest(context, req));
const repo = await repoObjectClient.getRepositories(uris);
return res.ok({ body: repo });
} catch (error) {
Expand Down Expand Up @@ -292,7 +292,7 @@ export function repositoryRoute(
const reindex: boolean = (req.body as any).reindex;
try {
await getReferenceHelper(context.core.savedObjects.client).ensureReference(repoUri);
const repoObjectClient = new RepositoryObjectClient(new EsClientWithRequest(context));
const repoObjectClient = new RepositoryObjectClient(new EsClientWithRequest(context, req));
const cloneStatus = await repoObjectClient.getRepositoryGitStatus(repoUri);

const payload = {
Expand Down Expand Up @@ -324,7 +324,7 @@ export function repositoryRoute(
) {
const config: RepositoryConfig = req.body as RepositoryConfig;
const repoUri: RepositoryUri = config.uri;
const repoObjectClient = new RepositoryObjectClient(new EsClientWithRequest(context));
const repoObjectClient = new RepositoryObjectClient(new EsClientWithRequest(context, req));

try {
// Check if the repository exists
Expand Down Expand Up @@ -360,7 +360,7 @@ export function repositoryRoute(
const { uri: repoUri } = req.params as any;
try {
await getReferenceHelper(context.core.savedObjects.client).ensureReference(repoUri);
const repoObjectClient = new RepositoryObjectClient(new EsClientWithRequest(context));
const repoObjectClient = new RepositoryObjectClient(new EsClientWithRequest(context, req));
const config = await repoObjectClient.getRepositoryConfig(repoUri);
return res.ok({ body: config });
} catch (error) {
Expand Down
Loading