Skip to content

Commit

Permalink
[Elastic Assistant] Use server-side authc.getCurrentUser from core.se…
Browse files Browse the repository at this point in the history
…curity (#186928)

Part of #186574

Background: This PR serves as an example of a plugin migrating away from
depending on the Security plugin, which is a high priority effort for
the last release before 9.0. The Elastic Assistant plugin uses the
`authc.getCurrentUser` indirectly in several data clients in
`x-pack/plugins/elastic_assistant/server/routes/request_context_factory.ts`.

### Checklist

Delete any items that are not applicable to this PR.

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
  • Loading branch information
tsullivan authored Jun 28, 2024
1 parent 7cf7365 commit 18adcec
Show file tree
Hide file tree
Showing 35 changed files with 36 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
AnonymizationFieldResponse,
AnonymizationFieldUpdateProps,
} from '@kbn/elastic-assistant-common/impl/schemas/anonymization_fields/bulk_crud_anonymization_fields_route.gen';
import { AuthenticatedUser } from '@kbn/security-plugin-types-common';
import { AuthenticatedUser } from '@kbn/core-security-common';
import {
CreateAnonymizationFieldSchema,
EsAnonymizationFieldsSchema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { elasticsearchServiceMock } from '@kbn/core-elasticsearch-server-mocks';

import { createAttackDiscovery } from './create_attack_discovery';
import { AttackDiscoveryCreateProps, AttackDiscoveryResponse } from '@kbn/elastic-assistant-common';
import { AuthenticatedUser } from '@kbn/security-plugin-types-common';
import { AuthenticatedUser } from '@kbn/core-security-common';
import { getAttackDiscovery } from './get_attack_discovery';
import { loggerMock } from '@kbn/logging-mocks';
const mockEsClient = elasticsearchServiceMock.createElasticsearchClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
*/

import { v4 as uuidv4 } from 'uuid';
import { ElasticsearchClient, Logger } from '@kbn/core/server';
import { AuthenticatedUser, ElasticsearchClient, Logger } from '@kbn/core/server';

import { AttackDiscoveryCreateProps, AttackDiscoveryResponse } from '@kbn/elastic-assistant-common';
import { AuthenticatedUser } from '@kbn/security-plugin-types-common';
import { getAttackDiscovery } from './get_attack_discovery';
import { CreateAttackDiscoverySchema } from './types';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
* 2.0.
*/

import { ElasticsearchClient, Logger } from '@kbn/core/server';
import { AuthenticatedUser, ElasticsearchClient, Logger } from '@kbn/core/server';
import { AttackDiscoveryResponse } from '@kbn/elastic-assistant-common';
import { AuthenticatedUser } from '@kbn/security-plugin/common';
import { EsAttackDiscoverySchema } from './types';
import { transformESSearchToAttackDiscovery } from './transforms';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
* 2.0.
*/

import { ElasticsearchClient, Logger } from '@kbn/core/server';
import { AuthenticatedUser, ElasticsearchClient, Logger } from '@kbn/core/server';
import { AttackDiscoveryResponse } from '@kbn/elastic-assistant-common';
import { AuthenticatedUser } from '@kbn/security-plugin/common';
import { EsAttackDiscoverySchema } from './types';
import { transformESSearchToAttackDiscovery } from './transforms';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
AttackDiscoveryStatus,
AttackDiscoveryUpdateProps,
} from '@kbn/elastic-assistant-common';
import { AuthenticatedUser } from '@kbn/security-plugin/common';
import { AuthenticatedUser } from '@kbn/core-security-common';
jest.mock('./get_attack_discovery');
const mockEsClient = elasticsearchServiceMock.createElasticsearchClient();
const mockLogger = loggerMock.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
* 2.0.
*/

import { ElasticsearchClient, Logger } from '@kbn/core/server';
import { AuthenticatedUser, ElasticsearchClient, Logger } from '@kbn/core/server';
import {
AttackDiscoveryResponse,
AttackDiscoveryStatus,
AttackDiscoveryUpdateProps,
Provider,
UUID,
} from '@kbn/elastic-assistant-common';
import { AuthenticatedUser } from '@kbn/security-plugin/common';
import * as uuid from 'uuid';
import { EsReplacementSchema } from '../conversations/types';
import { getAttackDiscovery } from './get_attack_discovery';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { estypes } from '@elastic/elasticsearch';
import { EsConversationSchema } from './types';
import { getConversation } from './get_conversation';
import { ConversationCreateProps, ConversationResponse } from '@kbn/elastic-assistant-common';
import { AuthenticatedUser } from '@kbn/security-plugin-types-common';
import { AuthenticatedUser } from '@kbn/core-security-common';

jest.mock('./get_conversation', () => ({
getConversation: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
*/

import { v4 as uuidv4 } from 'uuid';
import { ElasticsearchClient, Logger } from '@kbn/core/server';
import { AuthenticatedUser, ElasticsearchClient, Logger } from '@kbn/core/server';

import {
ConversationCategoryEnum,
ConversationCreateProps,
ConversationResponse,
} from '@kbn/elastic-assistant-common';
import { AuthenticatedUser } from '@kbn/security-plugin-types-common';
import { getConversation } from './get_conversation';
import { CreateMessageSchema } from './types';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
* 2.0.
*/

import type { Logger } from '@kbn/core/server';
import type { AuthenticatedUser, Logger } from '@kbn/core/server';
import { elasticsearchClientMock } from '@kbn/core-elasticsearch-client-server-mocks';
import { getConversation } from './get_conversation';
import { estypes } from '@elastic/elasticsearch';
import { EsConversationSchema } from './types';
import { loggingSystemMock } from '@kbn/core-logging-server-mocks';
import { ConversationResponse } from '@kbn/elastic-assistant-common';
import { AuthenticatedUser } from '@kbn/security-plugin-types-common';

export const getConversationResponseMock = (): ConversationResponse => ({
createdAt: '2020-04-20T15:25:31.830Z',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
* 2.0.
*/

import { ElasticsearchClient, Logger } from '@kbn/core/server';
import { AuthenticatedUser, ElasticsearchClient, Logger } from '@kbn/core/server';
import { ConversationResponse } from '@kbn/elastic-assistant-common';
import { AuthenticatedUser } from '@kbn/security-plugin/common';
import { EsConversationSchema } from './types';
import { transformESSearchToConversations } from './transforms';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { elasticsearchServiceMock, loggingSystemMock } from '@kbn/core/server/mocks';
import type { UpdateByQueryRequest } from '@elastic/elasticsearch/lib/api/types';
import { AIAssistantConversationsDataClient } from '.';
import { AuthenticatedUser } from '@kbn/security-plugin/server';
import { AuthenticatedUser } from '@kbn/core-security-common';
import { getUpdateConversationSchemaMock } from '../../__mocks__/conversations_schema.mock';
import { AIAssistantDataClientParams } from '..';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { AuthenticatedUser } from '@kbn/security-plugin/server';
import { AuthenticatedUser } from '@kbn/core-security-common';
import {
ConversationCreateProps,
ConversationResponse,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from './update_conversation';
import { getConversation } from './get_conversation';
import { ConversationResponse, ConversationUpdateProps } from '@kbn/elastic-assistant-common';
import { AuthenticatedUser } from '@kbn/security-plugin-types-common';
import { AuthenticatedUser } from '@kbn/core-security-common';

export const getUpdateConversationOptionsMock = (): ConversationUpdateProps => ({
id: 'test',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { ElasticsearchClient, Logger } from '@kbn/core/server';
import { AuthenticatedUser, ElasticsearchClient, Logger } from '@kbn/core/server';
import {
ConversationResponse,
Reader,
Expand All @@ -15,7 +15,6 @@ import {
ConversationSummary,
UUID,
} from '@kbn/elastic-assistant-common';
import { AuthenticatedUser } from '@kbn/security-plugin/common';
import { getConversation } from './get_conversation';
import { getUpdateScript } from './helpers';
import { EsReplacementSchema } from './types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
import { elasticsearchServiceMock, loggingSystemMock } from '@kbn/core/server/mocks';
import { AIAssistantDataClient, AIAssistantDataClientParams } from '.';
import { AuthenticatedUser } from '@kbn/security-plugin/server';
import { AuthenticatedUser } from '@kbn/core-security-common';

const date = '2023-03-28T22:27:28.159Z';
let logger: ReturnType<typeof loggingSystemMock['createLogger']>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
* 2.0.
*/

import { ElasticsearchClient, Logger } from '@kbn/core/server';
import { AuthenticatedUser, ElasticsearchClient, Logger } from '@kbn/core/server';

import { DEFAULT_NAMESPACE_STRING } from '@kbn/core-saved-objects-utils-server';
import { ESSearchRequest, ESSearchResponse } from '@kbn/es-types';
import { AuthenticatedUser } from '@kbn/security-plugin/server';
import { estypes } from '@elastic/elasticsearch';
import { IIndexPatternString } from '../types';
import { getIndexTemplateAndPattern } from '../lib/data_stream/helpers';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
*/

import { v4 as uuidv4 } from 'uuid';
import { ElasticsearchClient, Logger } from '@kbn/core/server';
import { AuthenticatedUser, ElasticsearchClient, Logger } from '@kbn/core/server';

import {
KnowledgeBaseEntryCreateProps,
KnowledgeBaseEntryResponse,
} from '@kbn/elastic-assistant-common';
import { AuthenticatedUser } from '@kbn/security-plugin-types-common';
import { getKnowledgeBaseEntry } from './get_knowledge_base_entry';
import { CreateKnowledgeBaseEntrySchema } from './types';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
* 2.0.
*/

import { ElasticsearchClient, Logger } from '@kbn/core/server';
import { AuthenticatedUser, ElasticsearchClient, Logger } from '@kbn/core/server';
import { KnowledgeBaseEntryResponse } from '@kbn/elastic-assistant-common';
import { AuthenticatedUser } from '@kbn/security-plugin/common';
import { EsKnowledgeBaseEntrySchema } from './types';
import { transformESSearchToKnowledgeBaseEntry } from './transforms';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
PromptResponse,
PromptUpdateProps,
} from '@kbn/elastic-assistant-common/impl/schemas/prompts/bulk_crud_prompts_route.gen';
import { AuthenticatedUser } from '@kbn/security-plugin-types-common';
import { AuthenticatedUser } from '@kbn/core-security-common';
import { CreatePromptSchema, EsPromptsSchema, UpdatePromptSchema } from './types';

export const transformESToPrompts = (response: EsPromptsSchema[]): PromptResponse[] => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { IndicesGetDataStreamResponse } from '@elastic/elasticsearch/lib/api/typ
import { errors as EsErrors } from '@elastic/elasticsearch';
import { ReplaySubject, Subject } from 'rxjs';
import { taskManagerMock } from '@kbn/task-manager-plugin/server/mocks';
import { AuthenticatedUser } from '@kbn/security-plugin/server';
import { AuthenticatedUser } from '@kbn/core-security-common';
import { DEFAULT_NAMESPACE_STRING } from '@kbn/core-saved-objects-utils-server';
import { conversationsDataClientMock } from '../__mocks__/data_clients.mock';
import { AIAssistantConversationsDataClient } from '../ai_assistant_data_clients/conversations';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

import { DataStreamSpacesAdapter, FieldMap } from '@kbn/data-stream-adapter';
import { DEFAULT_NAMESPACE_STRING } from '@kbn/core-saved-objects-utils-server';
import type { Logger, ElasticsearchClient } from '@kbn/core/server';
import type { AuthenticatedUser, Logger, ElasticsearchClient } from '@kbn/core/server';
import type { TaskManagerSetupContract } from '@kbn/task-manager-plugin/server';
import type { MlPluginSetup } from '@kbn/ml-plugin/server';
import { AuthenticatedUser } from '@kbn/security-plugin/server';
import { Subject } from 'rxjs';
import { attackDiscoveryFieldMap } from '../ai_assistant_data_clients/attack_discovery/field_maps_configuration';
import { getDefaultAnonymizationFields } from '../../common/anonymization';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
* 2.0.
*/

import type { ElasticsearchClient, Logger } from '@kbn/core/server';
import type { AuthenticatedUser, ElasticsearchClient, Logger } from '@kbn/core/server';
import { loggingSystemMock, elasticsearchServiceMock } from '@kbn/core/server/mocks';
import {
getCreateConversationSchemaMock,
getUpdateConversationSchemaMock,
} from '../../__mocks__/conversations_schema.mock';
import { AuthenticatedUser } from '@kbn/security-plugin-types-common';
import { DocumentsDataWriter } from './documents_data_writer';

describe('DocumentsDataWriter', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import type {
BulkResponseItem,
Script,
} from '@elastic/elasticsearch/lib/api/types';
import type { Logger, ElasticsearchClient } from '@kbn/core/server';
import type { AuthenticatedUser, Logger, ElasticsearchClient } from '@kbn/core/server';
import { UUID } from '@kbn/elastic-assistant-common';
import { AuthenticatedUser } from '@kbn/security-plugin-types-common';

export interface BulkOperationError {
message: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
getEmptyFindResult,
getFindAnonymizationFieldsResultWithSingleHit,
} from '../../__mocks__/response';
import { AuthenticatedUser } from '@kbn/security-plugin-types-common';
import { AuthenticatedUser } from '@kbn/core-security-common';
import { bulkActionAnonymizationFieldsRoute } from './bulk_actions_route';
import {
getAnonymizationFieldMock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { requestContextMock } from '../../__mocks__/request_context';
import { getPromptsBulkActionRequest, requestMock } from '../../__mocks__/request';
import { ELASTIC_AI_ASSISTANT_PROMPTS_URL_BULK_ACTION } from '@kbn/elastic-assistant-common';
import { getEmptyFindResult, getFindPromptsResultWithSingleHit } from '../../__mocks__/response';
import { AuthenticatedUser } from '@kbn/security-plugin-types-common';
import { AuthenticatedUser } from '@kbn/core-security-common';
import { bulkPromptsRoute } from './bulk_actions_route';
import {
getCreatePromptSchemaMock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class RequestContextFactory implements IRequestContextFactory {
const getSpaceId = (): string =>
startPlugins.spaces?.spacesService?.getSpaceId(request) || DEFAULT_NAMESPACE_STRING;

const getCurrentUser = () => startPlugins.security?.authc.getCurrentUser(request);
const getCurrentUser = () => coreContext.security.authc.getCurrentUser();

return {
core: coreContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
getUpdateConversationSchemaMock,
} from '../../__mocks__/conversations_schema.mock';
import { appendConversationMessageRoute } from './append_conversation_messages_route';
import { AuthenticatedUser } from '@kbn/security-plugin-types-common';
import { AuthenticatedUser } from '@kbn/core-security-common';

describe('Append conversation messages route', () => {
let server: ReturnType<typeof serverMock.create>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
getPerformBulkActionSchemaMock,
getUpdateConversationSchemaMock,
} from '../../__mocks__/conversations_schema.mock';
import { AuthenticatedUser } from '@kbn/security-plugin-types-common';
import { AuthenticatedUser } from '@kbn/core-security-common';

describe('Perform bulk action route', () => {
let server: ReturnType<typeof serverMock.create>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
getQueryConversationParams,
} from '../../__mocks__/conversations_schema.mock';
import { ELASTIC_AI_ASSISTANT_CONVERSATIONS_URL } from '@kbn/elastic-assistant-common';
import { AuthenticatedUser } from '@kbn/security-plugin-types-common';
import { AuthenticatedUser } from '@kbn/core-security-common';

describe('Create conversation route', () => {
let server: ReturnType<typeof serverMock.create>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
getConversationMock,
getQueryConversationParams,
} from '../../__mocks__/conversations_schema.mock';
import { AuthenticatedUser } from '@kbn/security-plugin-types-common';
import { AuthenticatedUser } from '@kbn/core-security-common';

describe('Delete conversation route', () => {
let server: ReturnType<typeof serverMock.create>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
getQueryConversationParams,
} from '../../__mocks__/conversations_schema.mock';
import { ELASTIC_AI_ASSISTANT_CONVERSATIONS_URL_BY_ID } from '@kbn/elastic-assistant-common';
import { AuthenticatedUser } from '@kbn/security-plugin-types-common';
import { AuthenticatedUser } from '@kbn/core-security-common';

describe('Read conversation route', () => {
let server: ReturnType<typeof serverMock.create>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
getUpdateConversationSchemaMock,
} from '../../__mocks__/conversations_schema.mock';
import { updateConversationRoute } from './update_route';
import { AuthenticatedUser } from '@kbn/security-plugin-types-common';
import { AuthenticatedUser } from '@kbn/core-security-common';

describe('Update conversation route', () => {
let server: ReturnType<typeof serverMock.create>;
Expand Down
5 changes: 3 additions & 2 deletions x-pack/plugins/elastic_assistant/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ import type {
PluginStartContract as ActionsPluginStart,
} from '@kbn/actions-plugin/server';
import type {
AuthenticatedUser,
CoreRequestHandlerContext,
CoreSetup,
AnalyticsServiceSetup,
CustomRequestHandlerContext,
IRouter,
KibanaRequest,
Logger,
SecurityServiceStart,
} from '@kbn/core/server';
import { type MlPluginSetup } from '@kbn/ml-plugin/server';
import { DynamicStructuredTool, Tool } from '@langchain/core/tools';
import { SpacesPluginSetup, SpacesPluginStart } from '@kbn/spaces-plugin/server';
import { TaskManagerSetupContract } from '@kbn/task-manager-plugin/server';
import { AuthenticatedUser, SecurityPluginStart } from '@kbn/security-plugin/server';
import { RetrievalQAChain } from 'langchain/chains';
import { ElasticsearchClient } from '@kbn/core/server';
import {
Expand Down Expand Up @@ -99,7 +100,7 @@ export interface ElasticAssistantPluginSetupDependencies {
export interface ElasticAssistantPluginStartDependencies {
actions: ActionsPluginStart;
spaces?: SpacesPluginStart;
security: SecurityPluginStart;
security: SecurityServiceStart;
}

export interface ElasticAssistantApiRequestHandlerContext {
Expand Down
Loading

0 comments on commit 18adcec

Please sign in to comment.