[Elastic Assistant] Use server-side authc.getCurrentUser from core.security (#186928)

Part of https://github.com/elastic/kibana/issues/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
This commit is contained in:
Tim Sullivan 2024-06-28 09:27:32 -07:00 committed by GitHub
parent 7cf7365987
commit 18adcec803
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
35 changed files with 36 additions and 51 deletions

View file

@ -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,

View file

@ -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();

View file

@ -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';

View file

@ -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';

View file

@ -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';

View file

@ -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();

View file

@ -5,7 +5,7 @@
* 2.0.
*/
import { ElasticsearchClient, Logger } from '@kbn/core/server';
import { AuthenticatedUser, ElasticsearchClient, Logger } from '@kbn/core/server';
import {
AttackDiscoveryResponse,
AttackDiscoveryStatus,
@ -13,7 +13,6 @@ import {
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';

View file

@ -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(),

View file

@ -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';

View file

@ -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',

View file

@ -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';

View file

@ -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 '..';

View file

@ -5,7 +5,7 @@
* 2.0.
*/
import { AuthenticatedUser } from '@kbn/security-plugin/server';
import { AuthenticatedUser } from '@kbn/core-security-common';
import {
ConversationCreateProps,
ConversationResponse,

View file

@ -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',

View file

@ -5,7 +5,7 @@
* 2.0.
*/
import { ElasticsearchClient, Logger } from '@kbn/core/server';
import { AuthenticatedUser, ElasticsearchClient, Logger } from '@kbn/core/server';
import {
ConversationResponse,
Reader,
@ -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';

View file

@ -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']>;

View file

@ -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';

View file

@ -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';

View file

@ -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';

View file

@ -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[] => {

View file

@ -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';

View file

@ -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';

View file

@ -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', () => {

View file

@ -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;

View file

@ -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,

View file

@ -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,

View file

@ -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,

View file

@ -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>;

View file

@ -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>;

View file

@ -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>;

View file

@ -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>;

View file

@ -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>;

View file

@ -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>;

View file

@ -10,6 +10,7 @@ import type {
PluginStartContract as ActionsPluginStart,
} from '@kbn/actions-plugin/server';
import type {
AuthenticatedUser,
CoreRequestHandlerContext,
CoreSetup,
AnalyticsServiceSetup,
@ -17,12 +18,12 @@ import type {
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 {
@ -99,7 +100,7 @@ export interface ElasticAssistantPluginSetupDependencies {
export interface ElasticAssistantPluginStartDependencies {
actions: ActionsPluginStart;
spaces?: SpacesPluginStart;
security: SecurityPluginStart;
security: SecurityServiceStart;
}
export interface ElasticAssistantApiRequestHandlerContext {

View file

@ -34,12 +34,10 @@
"@kbn/core-saved-objects-utils-server",
"@kbn/core-elasticsearch-client-server-mocks",
"@kbn/task-manager-plugin",
"@kbn/security-plugin",
"@kbn/es-query",
"@kbn/es-types",
"@kbn/config-schema",
"@kbn/spaces-plugin",
"@kbn/security-plugin-types-common",
"@kbn/ml-response-stream",
"@kbn/data-plugin",
"@kbn/i18n",