Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
patrykkopycinski committed Feb 24, 2025
1 parent 4a88e6a commit 3b689f3
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,7 @@ export const BaseUpdateProps = BaseCreateProps.partial().merge(
);

export type BaseResponseProps = z.infer<typeof BaseResponseProps>;
export const BaseResponseProps = BaseRequiredFields.merge(BaseDefaultableFields.required()).merge(
z.object({
/**
* Users who have access to the Knowledge Base Entry, defaults to current user. Empty array provides access to all users.
*/
users: z.array(User),
})
);
export const BaseResponseProps = BaseRequiredFields.merge(BaseDefaultableFields.required());

export type ResponseFields = z.infer<typeof ResponseFields>;
export const ResponseFields = z.object({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,6 @@ components:
- $ref: "#/components/schemas/BaseRequiredFields"
- $ref: "#/components/schemas/BaseDefaultableFields"
x-modify: required
- type: object
properties:
users:
type: array
description: Users who have access to the Knowledge Base Entry, defaults to current user. Empty array provides access to all users.
items:
$ref: "../../common_attributes.schema.yaml#/components/schemas/User"
required:
- users

ResponseFields:
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import pRetry from 'p-retry';
import { StructuredTool } from '@langchain/core/tools';
import { AnalyticsServiceSetup, AuditLogger, ElasticsearchClient } from '@kbn/core/server';
import { IndexPatternsFetcher } from '@kbn/data-views-plugin/server';
import { isArray, map } from 'lodash';
import { map } from 'lodash';
import { AIAssistantDataClient, AIAssistantDataClientParams } from '..';
import { GetElser } from '../../types';
import {
Expand All @@ -53,6 +53,7 @@ import {
} from './helpers';
import {
getKBUserFilter,
isGlobalEntry,
validateDocumentsModification,
} from '../../routes/knowledge_base/entries/utils';
import {
Expand Down Expand Up @@ -671,12 +672,7 @@ export class AIAssistantKnowledgeBaseDataClient extends AIAssistantDataClient {
);
}

const globalEntry =
(isArray(knowledgeBaseEntry.users) && knowledgeBaseEntry.users.length === 0) ||
knowledgeBaseEntry.global ||
false;

if (globalEntry && !this.options.manageGlobalKnowledgeBaseAIAssistant) {
if (isGlobalEntry(knowledgeBaseEntry) && !this.options.manageGlobalKnowledgeBaseAIAssistant) {
throw new Error('User lacks privileges to create global knowledge base entries');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import {
transformToCreateSchema,
transformToUpdateSchema,
} from '../../../ai_assistant_data_clients/knowledge_base/create_knowledge_base_entry';
import { validateDocumentsModification } from './utils';
import { isGlobalEntry, validateDocumentsModification } from './utils';

export interface BulkOperationError {
message: string;
Expand Down Expand Up @@ -241,8 +241,7 @@ export const bulkActionKnowledgeBaseEntriesRoute = (router: ElasticAssistantPlug
if (body.create && body.create.length > 0) {
// RBAC validation
body.create.forEach((entry) => {
const isGlobal = entry.global ?? (entry.users != null && entry.users.length === 0);
if (isGlobal && !manageGlobalKnowledgeBaseAIAssistant) {
if (isGlobalEntry(entry) && !manageGlobalKnowledgeBaseAIAssistant) {
throw new Error(`User lacks privileges to create global knowledge base entries`);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ export const createKnowledgeBaseEntryRoute = (router: ElasticAssistantPluginRout

logger.debug(() => `Creating KB Entry:\n${JSON.stringify(request.body)}`);
const createResponse = await kbDataClient?.createKnowledgeBaseEntry({
knowledgeBaseEntry: request.body,
knowledgeBaseEntry: {
...request.body,
...(request.body.global ? { users: [] } : {}),
},
auditLogger: ctx.elasticAssistant.auditLogger,
telemetry: ctx.elasticAssistant.telemetry,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ export const updateKnowledgeBaseEntryRoute = (router: ElasticAssistantPluginRout

const kbDataClient = await ctx.elasticAssistant.getAIAssistantKnowledgeBaseDataClient();
const updateResponse = await kbDataClient?.updateKnowledgeBaseEntry({
knowledgeBaseEntry: { ...request.body, id: request.params.id },
knowledgeBaseEntry: {
...request.body,
id: request.params.id,
...(request.body.global ? { users: [] } : {}),
},
auditLogger: ctx.elasticAssistant.auditLogger,
telemetry: ctx.elasticAssistant.telemetry,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
*/

import { AuthenticatedUser } from '@kbn/core-security-common';
import {
KnowledgeBaseEntryCreateProps,
KnowledgeBaseEntryResponse,
} from '@kbn/elastic-assistant-common';
import { isArray } from 'lodash';
import { AIAssistantKnowledgeBaseDataClient } from '../../../ai_assistant_data_clients/knowledge_base';
import { transformESSearchToKnowledgeBaseEntry } from '../../../ai_assistant_data_clients/knowledge_base/transforms';
import { EsKnowledgeBaseEntrySchema } from '../../../ai_assistant_data_clients/knowledge_base/types';
Expand All @@ -28,6 +33,9 @@ export const getKBUserFilter = (user: AuthenticatedUser | null) => {
return `(${globalFilter}${userFilter})`;
};

export const isGlobalEntry = (entry: KnowledgeBaseEntryResponse | KnowledgeBaseEntryCreateProps) =>
entry.global ?? (isArray(entry.users) && entry.users.length);

export const validateDocumentsModification = async (
kbDataClient: AIAssistantKnowledgeBaseDataClient | null,
authenticatedUser: AuthenticatedUser | null,
Expand All @@ -50,8 +58,7 @@ export const validateDocumentsModification = async (
const availableEntries = entries ? transformESSearchToKnowledgeBaseEntry(entries.data) : [];
availableEntries.forEach((entry) => {
// RBAC validation
const isGlobal = entry.global ?? (entry.users != null && entry.users.length === 0);
if (isGlobal && !manageGlobalKnowledgeBaseAIAssistant) {
if (isGlobalEntry(entry) && !manageGlobalKnowledgeBaseAIAssistant) {
throw new Error(`User lacks privileges to ${operation} global knowledge base entries`);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ export default ({ getService }: FtrProviderContext) => {
const updatedDocumentEntry = {
id: entry.id,
...globalDocumentEntry,
users: null,
global: false,
text: 'This is a sample of updated global document entry',
};
const response = await updateEntry({
Expand Down

0 comments on commit 3b689f3

Please sign in to comment.