-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[Security Solution] GenAI API Integration Tests #176357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
stephmilovic
merged 10 commits into
elastic:main
from
stephmilovic:genai_api_integration
Feb 7, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a3a8087
new suite
stephmilovic b029169
fixed
stephmilovic 83a20ec
revert + codeowners
stephmilovic d611f90
fix port
stephmilovic 90725a6
fix serverless
stephmilovic 49b9853
add tests for qa
stephmilovic b060b1c
[CI] Auto-commit changed files from 'node scripts/generate codeowners'
kibanamachine 83e5d14
update CODEOWNERS
stephmilovic 9907f00
add skipInQA tag
stephmilovic 082c4d0
Merge branch 'main' into genai_api_integration
kibanamachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
...solution_api_integration/test_suites/genai/invoke_ai/trial_license_complete_tier/basic.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import expect from '@kbn/expect'; | ||
|
|
||
| import { BedrockSimulator } from '@kbn/actions-simulators-plugin/server/bedrock_simulation'; | ||
| import { OpenAISimulator } from '@kbn/actions-simulators-plugin/server/openai_simulation'; | ||
| import { FtrProviderContext } from '../../../../ftr_provider_context'; | ||
| import { postActionsClientExecute } from '../utils/post_actions_client_execute'; | ||
| import { ObjectRemover } from '../utils/object_remover'; | ||
| import { createConnector } from '../utils/create_connector'; | ||
|
|
||
| const mockRequest = { | ||
| params: { | ||
| subActionParams: { | ||
| messages: [ | ||
| { role: 'user', content: '\\n\\n\\n\\nWhat is my name?' }, | ||
| { | ||
| role: 'assistant', | ||
| content: | ||
| "I'm sorry, but I don't have the information about your name. You can tell me your name if you'd like, and we can continue our conversation from there.", | ||
| }, | ||
| { role: 'user', content: '\\n\\nMy name is Andrew' }, | ||
| { | ||
| role: 'assistant', | ||
| content: "Hello, Andrew! It's nice to meet you. What would you like to talk about today?", | ||
| }, | ||
| { role: 'user', content: '\\n\\nDo you know my name?' }, | ||
| ], | ||
| }, | ||
| subAction: 'invokeAI', | ||
| }, | ||
| isEnabledKnowledgeBase: false, | ||
| isEnabledRAGAlerts: false, | ||
| llmType: 'bedrock', | ||
| }; | ||
|
|
||
| export default ({ getService }: FtrProviderContext) => { | ||
| const supertest = getService('supertest'); | ||
| const objectRemover = new ObjectRemover(supertest); | ||
| const configService = getService('config'); | ||
|
|
||
| // @skipInQA tag because the simulators do not work in the QA env | ||
| describe('@ess @serverless @skipInQA Basic Security AI Assistant Invoke AI [non-streaming, non-LangChain]', async () => { | ||
| after(() => { | ||
| objectRemover.removeAll(); | ||
| }); | ||
|
|
||
| describe('With Bedrock connector', () => { | ||
| const simulator = new BedrockSimulator({ | ||
| proxy: { | ||
| config: configService.get('kbnTestServer.serverArgs'), | ||
| }, | ||
| }); | ||
| let apiUrl: string; | ||
| let bedrockActionId: string; | ||
|
|
||
| before(async () => { | ||
| apiUrl = await simulator.start(); | ||
| bedrockActionId = await createConnector(supertest, objectRemover, apiUrl, 'bedrock'); | ||
| }); | ||
|
|
||
| after(() => { | ||
| simulator.close(); | ||
| }); | ||
| it('should execute a chat completion', async () => { | ||
| const response = await postActionsClientExecute(bedrockActionId, mockRequest, supertest); | ||
|
|
||
| const expected = { | ||
| connector_id: bedrockActionId, | ||
| data: 'Hello there! How may I assist you today?', | ||
| status: 'ok', | ||
| }; | ||
|
|
||
| expect(response.body).to.eql(expected); | ||
| }); | ||
| }); | ||
|
|
||
| describe('With OpenAI connector', () => { | ||
| const simulator = new OpenAISimulator({ | ||
| returnError: false, | ||
| proxy: { | ||
| config: configService.get('kbnTestServer.serverArgs'), | ||
| }, | ||
| }); | ||
| let apiUrl: string; | ||
| let openaiActionId: string; | ||
|
|
||
| before(async () => { | ||
| apiUrl = await simulator.start(); | ||
| openaiActionId = await createConnector(supertest, objectRemover, apiUrl, 'openai'); | ||
| }); | ||
|
|
||
| after(() => { | ||
| simulator.close(); | ||
| }); | ||
| it('should execute a chat completion', async () => { | ||
| const response = await postActionsClientExecute( | ||
| openaiActionId, | ||
| { ...mockRequest, llmType: 'openai' }, | ||
| supertest | ||
| ); | ||
|
|
||
| const expected = { | ||
| connector_id: openaiActionId, | ||
| data: 'Hello there! How may I assist you today?', | ||
| status: 'ok', | ||
| }; | ||
|
|
||
| expect(response.body).to.eql(expected); | ||
| }); | ||
| }); | ||
| }); | ||
| }; |
34 changes: 34 additions & 0 deletions
34
...integration/test_suites/genai/invoke_ai/trial_license_complete_tier/configs/ess.config.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import { FtrConfigProviderContext } from '@kbn/test'; | ||
| import getPort from 'get-port'; | ||
|
|
||
| export default async function ({ readConfigFile }: FtrConfigProviderContext) { | ||
| const functionalConfig = await readConfigFile( | ||
| require.resolve('../../../../../config/ess/config.base.trial') | ||
| ); | ||
|
|
||
| const proxyPort = await getPort({ port: getPort.makeRange(6200, 6299) }); | ||
|
|
||
| return { | ||
| ...functionalConfig.getAll(), | ||
| kbnTestServer: { | ||
| ...functionalConfig.get('kbnTestServer'), | ||
| serverArgs: [ | ||
| ...functionalConfig.get('kbnTestServer.serverArgs'), | ||
| // used for connector simulators | ||
| `--xpack.actions.proxyUrl=http://localhost:${proxyPort}`, | ||
| `--xpack.actions.enabledActionTypes=${JSON.stringify(['.bedrock', '.gen-ai'])}`, | ||
| ], | ||
| }, | ||
| testFiles: [require.resolve('..')], | ||
| junit: { | ||
| reportName: 'GenAI - Invoke AI Tests - ESS Env - Trial License', | ||
| }, | ||
| }; | ||
| } |
19 changes: 19 additions & 0 deletions
19
...tion/test_suites/genai/invoke_ai/trial_license_complete_tier/configs/serverless.config.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import { createTestConfig } from '../../../../../config/serverless/config.base'; | ||
|
|
||
| export default createTestConfig({ | ||
| kbnTestServerArgs: [ | ||
| // used for connector simulators | ||
| `--xpack.actions.proxyUrl=http://localhost:6200`, | ||
| ], | ||
| testFiles: [require.resolve('..')], | ||
| junit: { | ||
| reportName: 'GenAI - Invoke AI Tests - Serverless Env - Complete Tier', | ||
| }, | ||
| }); | ||
15 changes: 15 additions & 0 deletions
15
...solution_api_integration/test_suites/genai/invoke_ai/trial_license_complete_tier/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import { FtrProviderContext } from '../../../../ftr_provider_context'; | ||
|
|
||
| export default function ({ loadTestFile }: FtrProviderContext) { | ||
| // this is the test suite for the inaptly named post_actions_connector_execute route | ||
| describe('GenAI - Invoke AI', function () { | ||
| loadTestFile(require.resolve('./basic')); | ||
| }); | ||
| } |
73 changes: 73 additions & 0 deletions
73
...t/security_solution_api_integration/test_suites/genai/invoke_ai/utils/create_connector.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import type SuperTest from 'supertest'; | ||
| import { | ||
| ELASTIC_HTTP_VERSION_HEADER, | ||
| X_ELASTIC_INTERNAL_ORIGIN_REQUEST, | ||
| } from '@kbn/core-http-common'; | ||
| import { getUrlPrefix } from './space_test_utils'; | ||
| import { ObjectRemover } from './object_remover'; | ||
|
|
||
| const connectorSetup = { | ||
| bedrock: { | ||
| connectorTypeId: '.bedrock', | ||
| name: 'A bedrock action', | ||
| secrets: { | ||
| accessKey: 'bedrockAccessKey', | ||
| secret: 'bedrockSecret', | ||
| }, | ||
| config: { | ||
| defaultModel: 'anthropic.claude-v2', | ||
| }, | ||
| }, | ||
| openai: { | ||
| connectorTypeId: '.gen-ai', | ||
| name: 'An openai action', | ||
| secrets: { | ||
| apiKey: 'genAiApiKey', | ||
| }, | ||
| config: { | ||
| apiProvider: 'OpenAI', | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| /** | ||
| * Creates a connector | ||
| * @param supertest The supertest agent. | ||
| * @param apiUrl The url of the api | ||
| * @param connectorType The type of connector to create | ||
| * @param spaceId The space id | ||
| */ | ||
| export const createConnector = async ( | ||
| supertest: SuperTest.SuperTest<SuperTest.Test>, | ||
| objectRemover: ObjectRemover, | ||
| apiUrl: string, | ||
| connectorType: 'bedrock' | 'openai', | ||
| spaceId?: string | ||
| ) => { | ||
| const { connectorTypeId, config, name, secrets } = connectorSetup[connectorType]; | ||
| const result = await supertest | ||
| .post(`${getUrlPrefix(spaceId ?? 'default')}/api/actions/connector`) | ||
| .set('kbn-xsrf', 'foo') | ||
| .set(ELASTIC_HTTP_VERSION_HEADER, '1') | ||
| .set(X_ELASTIC_INTERNAL_ORIGIN_REQUEST, 'kibana') | ||
| .send({ | ||
| name, | ||
| connector_type_id: connectorTypeId, | ||
| config: { ...config, apiUrl }, | ||
| secrets, | ||
| }) | ||
| .expect(200); | ||
|
|
||
| const { body } = result; | ||
|
|
||
| objectRemover.add(spaceId ?? 'default', body.id, 'connector', 'actions'); | ||
|
|
||
| return body.id; | ||
| }; |
55 changes: 55 additions & 0 deletions
55
...est/security_solution_api_integration/test_suites/genai/invoke_ai/utils/object_remover.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import { | ||
| ELASTIC_HTTP_VERSION_HEADER, | ||
| X_ELASTIC_INTERNAL_ORIGIN_REQUEST, | ||
| } from '@kbn/core-http-common'; | ||
| import { getUrlPrefix } from './space_test_utils'; | ||
|
|
||
| interface ObjectToRemove { | ||
| spaceId: string; | ||
| id: string; | ||
| type: string; | ||
| plugin: string; | ||
| isInternal?: boolean; | ||
| } | ||
|
|
||
| export class ObjectRemover { | ||
| private readonly supertest: any; | ||
| private objectsToRemove: ObjectToRemove[] = []; | ||
|
|
||
| constructor(supertest: any) { | ||
| this.supertest = supertest; | ||
| } | ||
|
|
||
| add( | ||
| spaceId: ObjectToRemove['spaceId'], | ||
| id: ObjectToRemove['id'], | ||
| type: ObjectToRemove['type'], | ||
| plugin: ObjectToRemove['plugin'], | ||
| isInternal?: ObjectToRemove['isInternal'] | ||
| ) { | ||
| this.objectsToRemove.push({ spaceId, id, type, plugin, isInternal }); | ||
| } | ||
|
|
||
| async removeAll() { | ||
| await Promise.all( | ||
| this.objectsToRemove.map(({ spaceId, id, type, plugin, isInternal }) => { | ||
| return this.supertest | ||
| .delete( | ||
| `${getUrlPrefix(spaceId)}/${isInternal ? 'internal' : 'api'}/${plugin}/${type}/${id}` | ||
| ) | ||
| .set('kbn-xsrf', 'foo') | ||
| .set(ELASTIC_HTTP_VERSION_HEADER, '1') | ||
| .set(X_ELASTIC_INTERNAL_ORIGIN_REQUEST, 'kibana') | ||
| .expect(plugin === 'saved_objects' ? 200 : 204); | ||
| }) | ||
| ); | ||
| this.objectsToRemove = []; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Idk that I can use
get-porthere. I'm wondering if @YulNaumenko can speak to the importance of the dynamic port number here as I see your name in the git blame from the spot I stole this code from (x-pack/test/alerting_api_integration/common/config.ts) https://github.com/elastic/kibana/pull/75232/files#diff-1477d89e2965e56261180479a84bfb0e762793a73b88baa29300a7ed023d0f28R61-R62