Skip to content

Commit

Permalink
[8.15] [Obs AI Assistant] Use internal user when fetching connectors (#…
Browse files Browse the repository at this point in the history
…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":"soren.louv@elastic.co"},"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 <soren.louv@elastic.co>
  • Loading branch information
kibanamachine and sorenlouv authored Aug 14, 2024
1 parent 12570f9 commit a74ca64
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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<RecalledEntry[]> {
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,
Expand Down Expand Up @@ -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<string>(
aiAssistantSearchConnectorIndexPattern
Expand All @@ -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)
Expand Down

0 comments on commit a74ca64

Please sign in to comment.