Skip to content

Commit

Permalink
[Bug Fix] Add mds support for routers and fix the missing `callAsCurr…
Browse files Browse the repository at this point in the history
…entUser` (opensearch-project#1942)

* [MDS] Add mds support for routers

Signed-off-by: Ryan Liang <[email protected]>

* Fix the missing call as current user when mds is disabled

Signed-off-by: Ryan Liang <[email protected]>

---------

Signed-off-by: Ryan Liang <[email protected]>
  • Loading branch information
RyanL1997 authored Jul 18, 2024
1 parent 7566cff commit c2edf41
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 5 deletions.
45 changes: 42 additions & 3 deletions server/routes/data_connections/data_connections_router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<any> => {
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,
});
}
}
);
}
116 changes: 115 additions & 1 deletion server/routes/dsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down Expand Up @@ -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,
});
}
}
);
}
2 changes: 1 addition & 1 deletion server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit c2edf41

Please sign in to comment.