From cdfeefd9ac013a298bb6cf8b67a649fb8ab77441 Mon Sep 17 00:00:00 2001 From: Paul Sebastian Date: Thu, 4 Apr 2024 17:25:38 -0700 Subject: [PATCH] prevent logged out datasources call (#1653) (#1672) * prevent logged out datasources call Signed-off-by: Paul Sebastian * check if security plugin is installed Signed-off-by: Paul Sebastian * clean up logic Signed-off-by: Paul Sebastian * include a catch clause to register datasources when account api has non 401 error Signed-off-by: Paul Sebastian * included comment Signed-off-by: Paul Sebastian --------- Signed-off-by: Paul Sebastian (cherry picked from commit c14c3f813c43bc296e1ded355a5b7e9bd425869b) --- opensearch_dashboards.json | 5 +++-- public/plugin.tsx | 42 ++++++++++++++++++++++++++++---------- public/types.ts | 1 + 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/opensearch_dashboards.json b/opensearch_dashboards.json index 744b9726f..a2f825d18 100644 --- a/opensearch_dashboards.json +++ b/opensearch_dashboards.json @@ -20,9 +20,10 @@ ], "optionalPlugins": [ "managementOverview", - "assistantDashboards" + "assistantDashboards", + "securityDashboards" ], "configPath": [ "observability" ] -} \ No newline at end of file +} diff --git a/public/plugin.tsx b/public/plugin.tsx index cbcc35c73..62876acda 100644 --- a/public/plugin.tsx +++ b/public/plugin.tsx @@ -20,6 +20,7 @@ import { createGetterSetter } from '../../../src/plugins/opensearch_dashboards_u import { CREATE_TAB_PARAM, CREATE_TAB_PARAM_KEY, TAB_CHART_ID } from '../common/constants/explorer'; import { DATACONNECTIONS_BASE, + SECURITY_PLUGIN_ACCOUNT_API, observabilityApplicationsID, observabilityApplicationsPluginOrder, observabilityApplicationsTitle, @@ -388,18 +389,37 @@ export class ObservabilityPlugin const { dataSourceService, dataSourceFactory } = startDeps.data.dataSources; // register all s3 datasources - dataSourceFactory.registerDataSourceType(S3_DATASOURCE_TYPE, S3DataSource); - core.http.get(`${DATACONNECTIONS_BASE}`).then((s3DataSources) => { - s3DataSources.map((s3ds) => { - dataSourceService.registerDataSource( - dataSourceFactory.getDataSourceInstance(S3_DATASOURCE_TYPE, { - name: s3ds.name, - type: s3ds.connector.toLowerCase(), - metadata: s3ds, - }) - ); + const registerS3Datasource = () => { + dataSourceFactory.registerDataSourceType(S3_DATASOURCE_TYPE, S3DataSource); + core.http.get(`${DATACONNECTIONS_BASE}`).then((s3DataSources) => { + s3DataSources.map((s3ds) => { + dataSourceService.registerDataSource( + dataSourceFactory.getDataSourceInstance(S3_DATASOURCE_TYPE, { + name: s3ds.name, + type: s3ds.connector.toLowerCase(), + metadata: s3ds, + }) + ); + }); }); - }); + }; + + if (startDeps.securityDashboards) { + core.http + .get(SECURITY_PLUGIN_ACCOUNT_API) + .then(() => { + registerS3Datasource(); + }) + .catch((e) => { + if (e?.response?.status !== 401) { + // accounts api should not return any error status other than 401 if security installed, + // this datasource register is included just in case + registerS3Datasource(); + } + }); + } else { + registerS3Datasource(); + } core.http.intercept({ request: catalogRequestIntercept(), diff --git a/public/types.ts b/public/types.ts index 8f791bdd3..d276f0538 100644 --- a/public/types.ts +++ b/public/types.ts @@ -25,6 +25,7 @@ export interface AppPluginStartDependencies { dashboard: DashboardStart; savedObjectsClient: SavedObjectsClient; data: DataPublicPluginStart; + securityDashboards?: {}; } export interface SetupDependencies {