Skip to content

Commit a74ca64

Browse files
[8.15] [Obs AI Assistant] Use internal user when fetching connectors (#190462) (#190474)
# Backport This will backport the following commits from `main` to `8.15`: - [[Obs AI Assistant] Use internal user when fetching connectors (#190462)](#190462) <!--- Backport version: 9.4.3 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Søren Louv-Jansen","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-08-14T06:10:48Z","message":"[Obs AI Assistant] Use internal user when fetching connectors (#190462)\n\nCloses https://github.com/elastic/kibana/issues/187921\r\n\r\nThe Obs AI Assistant calls the endpoint `GET _connector` to get a list\r\nof connector indices. This works for admin users but not for users with\r\nlimited privileges like users with the `editor` role.\r\n\r\nCurrently an error is thrown but never caught. This causes the kibana\r\nserver to crash during development. In prod this problem means that\r\nusers cannot retrieve connector indices, and thus fallback to querying\r\n`search-*`.\r\n\r\nThis PR fixes it by using the internal user to call `GET _connector`. \r\n\r\n\r\nAdditional context:\r\nhttps://elastic.slack.com/archives/C05J7LXR5DE/p1723560268104559","sha":"be26e461166eca446a9bf4af4be687b77843b4da","branchLabelMapping":{"^v8.16.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:Obs AI Assistant","ci:project-deploy-observability","v8.16.0","v8.15.1"],"title":"[Obs AI Assistant] Use internal user when fetching connectors","number":190462,"url":"https://github.com/elastic/kibana/pull/190462","mergeCommit":{"message":"[Obs AI Assistant] Use internal user when fetching connectors (#190462)\n\nCloses https://github.com/elastic/kibana/issues/187921\r\n\r\nThe Obs AI Assistant calls the endpoint `GET _connector` to get a list\r\nof connector indices. This works for admin users but not for users with\r\nlimited privileges like users with the `editor` role.\r\n\r\nCurrently an error is thrown but never caught. This causes the kibana\r\nserver to crash during development. In prod this problem means that\r\nusers cannot retrieve connector indices, and thus fallback to querying\r\n`search-*`.\r\n\r\nThis PR fixes it by using the internal user to call `GET _connector`. \r\n\r\n\r\nAdditional context:\r\nhttps://elastic.slack.com/archives/C05J7LXR5DE/p1723560268104559","sha":"be26e461166eca446a9bf4af4be687b77843b4da"}},"sourceBranch":"main","suggestedTargetBranches":["8.15"],"targetPullRequestStates":[{"branch":"main","label":"v8.16.0","branchLabelMappingKey":"^v8.16.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/190462","number":190462,"mergeCommit":{"message":"[Obs AI Assistant] Use internal user when fetching connectors (#190462)\n\nCloses https://github.com/elastic/kibana/issues/187921\r\n\r\nThe Obs AI Assistant calls the endpoint `GET _connector` to get a list\r\nof connector indices. This works for admin users but not for users with\r\nlimited privileges like users with the `editor` role.\r\n\r\nCurrently an error is thrown but never caught. This causes the kibana\r\nserver to crash during development. In prod this problem means that\r\nusers cannot retrieve connector indices, and thus fallback to querying\r\n`search-*`.\r\n\r\nThis PR fixes it by using the internal user to call `GET _connector`. \r\n\r\n\r\nAdditional context:\r\nhttps://elastic.slack.com/archives/C05J7LXR5DE/p1723560268104559","sha":"be26e461166eca446a9bf4af4be687b77843b4da"}},{"branch":"8.15","label":"v8.15.1","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> Co-authored-by: Søren Louv-Jansen <[email protected]>
1 parent 12570f9 commit a74ca64

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

x-pack/plugins/observability_solution/observability_ai_assistant/server/service/knowledge_base_service/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ export class KnowledgeBaseService {
360360
categories?: string[];
361361
user?: { name: string };
362362
namespace: string;
363-
esClient: { asCurrentUser: ElasticsearchClient };
363+
esClient: { asCurrentUser: ElasticsearchClient; asInternalUser: ElasticsearchClient };
364364
uiSettingsClient: IUiSettingsClient;
365365
}): Promise<{
366366
entries: RecalledEntry[];
@@ -388,6 +388,7 @@ export class KnowledgeBaseService {
388388
uiSettingsClient,
389389
queries,
390390
modelId,
391+
logger: this.dependencies.logger,
391392
}).catch((error) => {
392393
this.dependencies.logger.debug('Error getting data from search indices');
393394
this.dependencies.logger.debug(error);

x-pack/plugins/observability_solution/observability_ai_assistant/server/service/knowledge_base_service/recall_from_connectors.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import { ElasticsearchClient } from '@kbn/core-elasticsearch-server';
99
import { IUiSettingsClient } from '@kbn/core-ui-settings-server';
1010
import { isEmpty } from 'lodash';
11+
import type { Logger } from '@kbn/logging';
1112
import { RecalledEntry } from '.';
1213
import { aiAssistantSearchConnectorIndexPattern } from '../../../common';
1314

@@ -16,15 +17,17 @@ export async function recallFromConnectors({
1617
esClient,
1718
uiSettingsClient,
1819
modelId,
20+
logger,
1921
}: {
2022
queries: Array<{ text: string; boost?: number }>;
21-
esClient: { asCurrentUser: ElasticsearchClient };
23+
esClient: { asCurrentUser: ElasticsearchClient; asInternalUser: ElasticsearchClient };
2224
uiSettingsClient: IUiSettingsClient;
2325
modelId: string;
26+
logger: Logger;
2427
}): Promise<RecalledEntry[]> {
2528
const ML_INFERENCE_PREFIX = 'ml.inference.';
26-
27-
const connectorIndices = await getConnectorIndices(esClient, uiSettingsClient);
29+
const connectorIndices = await getConnectorIndices(esClient, uiSettingsClient, logger);
30+
logger.debug(`Found connector indices: ${connectorIndices}`);
2831

2932
const fieldCaps = await esClient.asCurrentUser.fieldCaps({
3033
index: connectorIndices,
@@ -96,17 +99,25 @@ export async function recallFromConnectors({
9699
}
97100

98101
async function getConnectorIndices(
99-
esClient: { asCurrentUser: ElasticsearchClient },
100-
uiSettingsClient: IUiSettingsClient
102+
esClient: { asCurrentUser: ElasticsearchClient; asInternalUser: ElasticsearchClient },
103+
uiSettingsClient: IUiSettingsClient,
104+
logger: Logger
101105
) {
102106
// improve performance by running this in parallel with the `uiSettingsClient` request
103-
const responsePromise = esClient.asCurrentUser.transport.request({
104-
method: 'GET',
105-
path: '_connector',
106-
querystring: {
107-
filter_path: 'results.index_name',
108-
},
109-
});
107+
const responsePromise = esClient.asInternalUser.transport
108+
.request<{
109+
results?: Array<{ index_name: string }>;
110+
}>({
111+
method: 'GET',
112+
path: '_connector',
113+
querystring: {
114+
filter_path: 'results.index_name',
115+
},
116+
})
117+
.catch((e) => {
118+
logger.warn(`Failed to fetch connector indices due to ${e.message}`);
119+
return { results: [] };
120+
});
110121

111122
const customSearchConnectorIndex = await uiSettingsClient.get<string>(
112123
aiAssistantSearchConnectorIndexPattern
@@ -116,7 +127,7 @@ async function getConnectorIndices(
116127
return customSearchConnectorIndex.split(',');
117128
}
118129

119-
const response = (await responsePromise) as { results?: Array<{ index_name: string }> };
130+
const response = await responsePromise;
120131
const connectorIndices = response.results?.map((result) => result.index_name);
121132

122133
// preserve backwards compatibility with 8.14 (may not be needed in the future)

0 commit comments

Comments
 (0)