Skip to content

Commit d5f659e

Browse files
patrykkopycinskiCAWilson94
authored andcommitted
[Security Assistant] Fix inference rollover (elastic#214718)
## Summary Fixes elastic#214709 (comment) The issue was caused by the rollover of the Knowledge Base Data stream to use default inference endpoint. During the rollover it first got to this branch https://github.com/elastic/kibana/blob/main/x-pack/solutions/security/plugins/elastic_assistant/server/ai_assistant_service/index.ts#L347-L369 where it went through all the steps and continued, but it didn't override `this.knowledgeBaseStream`, so the next time someone hit API it was going through this path calling `getInitializedResources` to make sure all data streams were configured properly, but because we didn't update `this.knowledgeBaseStream` it was failing, because the original configuration that was created in service constructor was not called, that's why it was returning an error
1 parent 05e4c86 commit d5f659e

File tree

4 files changed

+25
-20
lines changed

4 files changed

+25
-20
lines changed

x-pack/platform/packages/shared/kbn-elastic-assistant/impl/knowledge_base/knowledge_base_settings_management/use_knowledge_base_table.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ const useUserProfile = ({ username, enabled = true }: { username: string; enable
4646
},
4747
select: (profile) => {
4848
return {
49-
username: profile?.[0].user.username ?? 'Unknown',
50-
avatar: profile?.[0].data.avatar,
49+
username: profile?.[0]?.user.username ?? username ?? 'Unknown',
50+
avatar: profile?.[0]?.data.avatar,
5151
};
5252
},
5353
enabled: !!(enabled && username?.length),

x-pack/solutions/security/plugins/elastic_assistant/server/ai_assistant_data_clients/knowledge_base/create_knowledge_base_entry.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export const transformToUpdateSchema = ({
120120
const base = {
121121
id: entry.id,
122122
updated_at: updatedAt,
123-
updated_by: user.profile_uid ?? 'unknown',
123+
updated_by: user.profile_uid ?? user.username ?? 'unknown',
124124
name: entry.name,
125125
type: entry.type,
126126
global: entry.global,
@@ -186,9 +186,9 @@ export const transformToCreateSchema = ({
186186
const base = {
187187
'@timestamp': createdAt,
188188
created_at: createdAt,
189-
created_by: user.profile_uid ?? 'unknown',
189+
created_by: user.profile_uid ?? user.username ?? 'unknown',
190190
updated_at: createdAt,
191-
updated_by: user.profile_uid ?? 'unknown',
191+
updated_by: user.profile_uid ?? user.username ?? 'unknown',
192192
name: entry.name,
193193
namespace: spaceId,
194194
type: entry.type,

x-pack/solutions/security/plugins/elastic_assistant/server/ai_assistant_service/index.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ export class AIAssistantService {
224224
private async rolloverDataStream(
225225
initialInferenceEndpointId: string,
226226
targetInferenceEndpointId: string
227-
): Promise<void> {
227+
): Promise<DataStreamSpacesAdapter> {
228228
const esClient = await this.options.elasticsearchClientPromise;
229229

230230
const currentDataStream = this.createDataStream({
@@ -289,6 +289,8 @@ export class AIAssistantService {
289289
} catch (e) {
290290
/* empty */
291291
}
292+
293+
return newDS;
292294
}
293295

294296
private async initializeResources(): Promise<InitializationPromise> {
@@ -340,12 +342,12 @@ export class AIAssistantService {
340342

341343
// Used only for testing purposes
342344
if (this.modelIdOverride && !isUsingDedicatedInferenceEndpoint) {
343-
await this.rolloverDataStream(
345+
this.knowledgeBaseDataStream = await this.rolloverDataStream(
344346
ELASTICSEARCH_ELSER_INFERENCE_ID,
345347
ASSISTANT_ELSER_INFERENCE_ID
346348
);
347349
} else if (isUsingDedicatedInferenceEndpoint) {
348-
await this.rolloverDataStream(
350+
this.knowledgeBaseDataStream = await this.rolloverDataStream(
349351
ASSISTANT_ELSER_INFERENCE_ID,
350352
ELASTICSEARCH_ELSER_INFERENCE_ID
351353
);
@@ -362,7 +364,7 @@ export class AIAssistantService {
362364
`Deleted existing inference endpoint ${ASSISTANT_ELSER_INFERENCE_ID} for ELSER model '${elserId}'`
363365
);
364366
} catch (error) {
365-
this.options.logger.error(
367+
this.options.logger.debug(
366368
`Error deleting inference endpoint ${ASSISTANT_ELSER_INFERENCE_ID} for ELSER model '${elserId}':\n${error}`
367369
);
368370
}
@@ -385,13 +387,14 @@ export class AIAssistantService {
385387
},
386388
writeIndexOnly: true,
387389
});
388-
await this.knowledgeBaseDataStream.install({
389-
esClient,
390-
logger: this.options.logger,
391-
pluginStop$: this.options.pluginStop$,
392-
});
393390
}
394391

392+
await this.knowledgeBaseDataStream.install({
393+
esClient,
394+
logger: this.options.logger,
395+
pluginStop$: this.options.pluginStop$,
396+
});
397+
395398
await this.promptsDataStream.install({
396399
esClient,
397400
logger: this.options.logger,

x-pack/solutions/security/plugins/elastic_assistant/server/lib/data_stream/documents_data_writer.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,15 @@ export class DocumentsDataWriter implements DocumentsDataWriter {
136136
path: 'users',
137137
query: {
138138
bool: {
139-
must: [
140-
{
141-
match: authenticatedUser.profile_uid
142-
? { 'users.id': authenticatedUser.profile_uid }
143-
: { 'users.name': authenticatedUser.username },
144-
},
139+
should: [
140+
// Match on users.id if profile_uid exists
141+
...(authenticatedUser.profile_uid
142+
? [{ term: { 'users.id': authenticatedUser.profile_uid } }]
143+
: []),
144+
// Always try to match on users.name
145+
{ term: { 'users.name': authenticatedUser.username } },
145146
],
147+
minimum_should_match: 1,
146148
},
147149
},
148150
},

0 commit comments

Comments
 (0)