From c2edf410d455af1638bcb83812a3e578529d9670 Mon Sep 17 00:00:00 2001 From: Jialiang Liang Date: Wed, 17 Jul 2024 18:52:24 -0700 Subject: [PATCH] [Bug Fix] Add mds support for routers and fix the missing `callAsCurrentUser` (#1942) * [MDS] Add mds support for routers Signed-off-by: Ryan Liang * Fix the missing call as current user when mds is disabled Signed-off-by: Ryan Liang --------- Signed-off-by: Ryan Liang --- .../data_connections_router.ts | 45 ++++++- server/routes/dsl.ts | 116 +++++++++++++++++- server/routes/index.ts | 2 +- 3 files changed, 158 insertions(+), 5 deletions(-) diff --git a/server/routes/data_connections/data_connections_router.ts b/server/routes/data_connections/data_connections_router.ts index 3ecd3d4348..7e35f96762 100644 --- a/server/routes/data_connections/data_connections_router.ts +++ b/server/routes/data_connections/data_connections_router.ts @@ -221,9 +221,9 @@ export function registerDataConnectionsRoute(router: IRouter, dataSourceEnabled: const client = await context.dataSource.opensearch.legacy.getClient(dataSourceMDSId); dataConnectionsresponse = await client.callAPI('ppl.getDataConnections'); } else { - dataConnectionsresponse = await context.observability_plugin.observabilityClient.asScoped( - request - ); + dataConnectionsresponse = await context.observability_plugin.observabilityClient + .asScoped(request) + .callAsCurrentUser('ppl.getDataConnections'); } return response.ok({ body: dataConnectionsresponse, @@ -237,4 +237,43 @@ export function registerDataConnectionsRoute(router: IRouter, dataSourceEnabled: } } ); + + router.get( + { + path: `${DATACONNECTIONS_BASE}/{name}/dataSourceMDSId={dataSourceMDSId?}`, + validate: { + params: schema.object({ + name: schema.string(), + dataSourceMDSId: schema.maybe(schema.string({ defaultValue: '' })), + }), + }, + }, + async (context, request, response): Promise => { + const dataSourceMDSId = request.params.dataSourceMDSId; + try { + let dataConnectionsresponse; + if (dataSourceEnabled && dataSourceMDSId) { + const client = await context.dataSource.opensearch.legacy.getClient(dataSourceMDSId); + dataConnectionsresponse = await client.callAPI('ppl.getDataConnectionById', { + dataconnection: request.params.name, + }); + } else { + dataConnectionsresponse = await context.observability_plugin.observabilityClient + .asScoped(request) + .callAsCurrentUser('ppl.getDataConnectionById', { + dataconnection: request.params.name, + }); + } + return response.ok({ + body: dataConnectionsresponse, + }); + } catch (error: any) { + console.error('Issue in fetching data connection:', error); + return response.custom({ + statusCode: error.statusCode || 500, + body: error.message, + }); + } + } + ); } diff --git a/server/routes/dsl.ts b/server/routes/dsl.ts index 9bdc453427..0560ae049f 100644 --- a/server/routes/dsl.ts +++ b/server/routes/dsl.ts @@ -15,7 +15,10 @@ import { DSL_SETTINGS, } from '../../common/constants/shared'; -export function registerDslRoute({ router }: { router: IRouter; facet: DSLFacet }) { +export function registerDslRoute( + { router }: { router: IRouter; facet: DSLFacet }, + dataSourceEnabled: boolean +) { router.post( { path: `${DSL_BASE}${DSL_SEARCH}`, @@ -122,4 +125,115 @@ export function registerDslRoute({ router }: { router: IRouter; facet: DSLFacet } } ); + + router.get( + { + path: `${DSL_BASE}${DSL_CAT}/dataSourceMDSId={dataSourceMDSId?}`, + validate: { + query: schema.object({ + format: schema.string(), + index: schema.maybe(schema.string()), + }), + params: schema.object({ + dataSourceMDSId: schema.maybe(schema.string({ defaultValue: '' })), + }), + }, + }, + async (context, request, response) => { + const dataSourceMDSId = request.params.dataSourceMDSId; + try { + let resp; + if (dataSourceEnabled && dataSourceMDSId) { + const client = await context.dataSource.opensearch.legacy.getClient(dataSourceMDSId); + resp = await client.callAPI('cat.indices', request.query); + } else { + resp = await context.core.opensearch.legacy.client.callAsCurrentUser( + 'cat.indices', + request.query + ); + } + return response.ok({ + body: resp, + }); + } catch (error) { + if (error.statusCode !== 404) console.error(error); + return response.custom({ + statusCode: error.statusCode || 500, + body: error.message, + }); + } + } + ); + + router.get( + { + path: `${DSL_BASE}${DSL_MAPPING}/dataSourceMDSId={dataSourceMDSId?}`, + validate: { + query: schema.any(), + params: schema.object({ + dataSourceMDSId: schema.maybe(schema.string({ defaultValue: '' })), + }), + }, + }, + async (context, request, response) => { + const dataSourceMDSId = request.params.dataSourceMDSId; + try { + let resp; + if (dataSourceEnabled && dataSourceMDSId) { + const client = await context.dataSource.opensearch.legacy.getClient(dataSourceMDSId); + resp = await client.callAPI('indices.getMapping', { index: request.query.index }); + } else { + resp = await context.core.opensearch.legacy.client.callAsCurrentUser( + 'indices.getMapping', + { index: request.query.index } + ); + } + return response.ok({ + body: resp, + }); + } catch (error) { + if (error.statusCode !== 404) console.error(error); + return response.custom({ + statusCode: error.statusCode || 500, + body: error.message, + }); + } + } + ); + + router.get( + { + path: `${DSL_BASE}${DSL_SETTINGS}/dataSourceMDSId={dataSourceMDSId?}`, + validate: { + query: schema.any(), + params: schema.object({ + dataSourceMDSId: schema.maybe(schema.string({ defaultValue: '' })), + }), + }, + }, + async (context, request, response) => { + const dataSourceMDSId = request.params.dataSourceMDSId; + try { + let resp; + if (dataSourceEnabled && dataSourceMDSId) { + const client = await context.dataSource.opensearch.legacy.getClient(dataSourceMDSId); + resp = await client.callAPI('indices.getSettings', { index: request.query.index }); + } else { + resp = await context.core.opensearch.legacy.client.callAsCurrentUser( + 'indices.getSettings', + { index: request.query.index } + ); + } + return response.ok({ + body: resp, + }); + } catch (error) { + if (error.statusCode !== 404) console.error(error); + return response.custom({ + statusCode: error.statusCode || 500, + body: error.message, + }); + } + } + ); } diff --git a/server/routes/index.ts b/server/routes/index.ts index 499fb29520..d8a6952332 100644 --- a/server/routes/index.ts +++ b/server/routes/index.ts @@ -37,7 +37,7 @@ export function setupRoutes({ PanelsRouter(router); VisualizationsRouter(router); registerPplRoute({ router, facet: new PPLFacet(client) }); - registerDslRoute({ router, facet: new DSLFacet(client) }); + registerDslRoute({ router, facet: new DSLFacet(client) }, dataSourceEnabled); registerEventAnalyticsRouter({ router, savedObjectFacet: new SavedObjectFacet(client) }); registerAppAnalyticsRouter(router);