From a74ca64af01da3b85d1d2cd73a5dbd49d356163a Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Wed, 14 Aug 2024 17:34:34 +1000 Subject: [PATCH] [8.15] [Obs AI Assistant] Use internal user when fetching connectors (#190462) (#190474) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Backport This will backport the following commits from `main` to `8.15`: - [[Obs AI Assistant] Use internal user when fetching connectors (#190462)](https://github.com/elastic/kibana/pull/190462) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Søren Louv-Jansen --- .../service/knowledge_base_service/index.ts | 3 +- .../recall_from_connectors.ts | 37 ++++++++++++------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/x-pack/plugins/observability_solution/observability_ai_assistant/server/service/knowledge_base_service/index.ts b/x-pack/plugins/observability_solution/observability_ai_assistant/server/service/knowledge_base_service/index.ts index 45330f9da2f2..e0446d3194f9 100644 --- a/x-pack/plugins/observability_solution/observability_ai_assistant/server/service/knowledge_base_service/index.ts +++ b/x-pack/plugins/observability_solution/observability_ai_assistant/server/service/knowledge_base_service/index.ts @@ -360,7 +360,7 @@ export class KnowledgeBaseService { categories?: string[]; user?: { name: string }; namespace: string; - esClient: { asCurrentUser: ElasticsearchClient }; + esClient: { asCurrentUser: ElasticsearchClient; asInternalUser: ElasticsearchClient }; uiSettingsClient: IUiSettingsClient; }): Promise<{ entries: RecalledEntry[]; @@ -388,6 +388,7 @@ export class KnowledgeBaseService { uiSettingsClient, queries, modelId, + logger: this.dependencies.logger, }).catch((error) => { this.dependencies.logger.debug('Error getting data from search indices'); this.dependencies.logger.debug(error); diff --git a/x-pack/plugins/observability_solution/observability_ai_assistant/server/service/knowledge_base_service/recall_from_connectors.ts b/x-pack/plugins/observability_solution/observability_ai_assistant/server/service/knowledge_base_service/recall_from_connectors.ts index 34c8a6208d65..27c133e7b88d 100644 --- a/x-pack/plugins/observability_solution/observability_ai_assistant/server/service/knowledge_base_service/recall_from_connectors.ts +++ b/x-pack/plugins/observability_solution/observability_ai_assistant/server/service/knowledge_base_service/recall_from_connectors.ts @@ -8,6 +8,7 @@ import { ElasticsearchClient } from '@kbn/core-elasticsearch-server'; import { IUiSettingsClient } from '@kbn/core-ui-settings-server'; import { isEmpty } from 'lodash'; +import type { Logger } from '@kbn/logging'; import { RecalledEntry } from '.'; import { aiAssistantSearchConnectorIndexPattern } from '../../../common'; @@ -16,15 +17,17 @@ export async function recallFromConnectors({ esClient, uiSettingsClient, modelId, + logger, }: { queries: Array<{ text: string; boost?: number }>; - esClient: { asCurrentUser: ElasticsearchClient }; + esClient: { asCurrentUser: ElasticsearchClient; asInternalUser: ElasticsearchClient }; uiSettingsClient: IUiSettingsClient; modelId: string; + logger: Logger; }): Promise { const ML_INFERENCE_PREFIX = 'ml.inference.'; - - const connectorIndices = await getConnectorIndices(esClient, uiSettingsClient); + const connectorIndices = await getConnectorIndices(esClient, uiSettingsClient, logger); + logger.debug(`Found connector indices: ${connectorIndices}`); const fieldCaps = await esClient.asCurrentUser.fieldCaps({ index: connectorIndices, @@ -96,17 +99,25 @@ export async function recallFromConnectors({ } async function getConnectorIndices( - esClient: { asCurrentUser: ElasticsearchClient }, - uiSettingsClient: IUiSettingsClient + esClient: { asCurrentUser: ElasticsearchClient; asInternalUser: ElasticsearchClient }, + uiSettingsClient: IUiSettingsClient, + logger: Logger ) { // improve performance by running this in parallel with the `uiSettingsClient` request - const responsePromise = esClient.asCurrentUser.transport.request({ - method: 'GET', - path: '_connector', - querystring: { - filter_path: 'results.index_name', - }, - }); + const responsePromise = esClient.asInternalUser.transport + .request<{ + results?: Array<{ index_name: string }>; + }>({ + method: 'GET', + path: '_connector', + querystring: { + filter_path: 'results.index_name', + }, + }) + .catch((e) => { + logger.warn(`Failed to fetch connector indices due to ${e.message}`); + return { results: [] }; + }); const customSearchConnectorIndex = await uiSettingsClient.get( aiAssistantSearchConnectorIndexPattern @@ -116,7 +127,7 @@ async function getConnectorIndices( return customSearchConnectorIndex.split(','); } - const response = (await responsePromise) as { results?: Array<{ index_name: string }> }; + const response = await responsePromise; const connectorIndices = response.results?.map((result) => result.index_name); // preserve backwards compatibility with 8.14 (may not be needed in the future)