-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[Security Solution] Onboarding check inference endpoint #217150
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
semd
merged 9 commits into
elastic:main
from
semd:siem_migrations/check_inference_endpoint
Apr 7, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
116b6a5
centralize ai connectors loading and authz checks
semd 842de03
add new files
semd ae8bb51
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine eb57d76
Merge remote-tracking branch 'upstream/main' into siem_migrations/che…
semd 3ae52c9
fix bug when conversation update fails
semd e7f0516
fix conflict
semd ea0deae
change load connectors naming and logic
semd ef506a2
Merge remote-tracking branch 'upstream/main' into siem_migrations/che…
semd 974f6b4
fix AI assistant card text
semd 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
126 changes: 126 additions & 0 deletions
126
...ublic/onboarding/components/onboarding_body/cards/common/connectors/ai_connectors.test.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,126 @@ | ||
| /* | ||
| * 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 { loadAiConnectors } from './ai_connectors'; | ||
| import { loadAllActions } from '@kbn/triggers-actions-ui-plugin/public/common/constants'; | ||
| import { isInferenceEndpointExists } from '@kbn/inference-endpoint-ui-common'; | ||
| import type { HttpSetup } from '@kbn/core-http-browser'; | ||
| import type { ActionConnector } from '@kbn/triggers-actions-ui-plugin/public/common/constants'; | ||
|
|
||
| jest.mock('@kbn/triggers-actions-ui-plugin/public/common/constants', () => ({ | ||
| loadAllActions: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('@kbn/inference-endpoint-ui-common', () => ({ | ||
| isInferenceEndpointExists: jest.fn(), | ||
| })); | ||
|
|
||
| const mockHttp = {} as HttpSetup; | ||
| const mockLoadAllActions = loadAllActions as jest.Mock; | ||
| const mockIsInferenceEndpointExists = isInferenceEndpointExists as jest.Mock; | ||
|
|
||
| describe('loadAiConnectors', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should return only valid external AI connectors', async () => { | ||
| const mockConnectors: ActionConnector[] = [ | ||
| { id: '1', actionTypeId: '.gen-ai', isMissingSecrets: false } as ActionConnector, | ||
| { id: '2', actionTypeId: '.gen-ai', isMissingSecrets: true } as ActionConnector, | ||
| { id: '3', actionTypeId: '.webhook', isMissingSecrets: false } as ActionConnector, | ||
| ]; | ||
|
|
||
| mockLoadAllActions.mockResolvedValue(mockConnectors); | ||
|
|
||
| const result = await loadAiConnectors(mockHttp); | ||
|
|
||
| expect(result).toEqual([{ id: '1', actionTypeId: '.gen-ai', isMissingSecrets: false }]); | ||
| }); | ||
|
|
||
| it('should include valid preconfigured inference connectors with existing endpoint', async () => { | ||
| const mockConnectors: ActionConnector[] = [ | ||
| { | ||
| id: '1', | ||
| actionTypeId: '.inference', | ||
| isMissingSecrets: false, | ||
| isPreconfigured: true, | ||
| config: { inferenceId: 'my-inference' }, | ||
| } as unknown as ActionConnector, | ||
| ]; | ||
|
|
||
| mockLoadAllActions.mockResolvedValue(mockConnectors); | ||
| mockIsInferenceEndpointExists.mockResolvedValue(true); | ||
|
|
||
| const result = await loadAiConnectors(mockHttp); | ||
|
|
||
| expect(mockIsInferenceEndpointExists).toHaveBeenCalledWith(mockHttp, 'my-inference'); | ||
| expect(result).toEqual(mockConnectors); | ||
| }); | ||
|
|
||
| it('should exclude inference connectors if endpoint does not exist', async () => { | ||
| const mockConnectors: ActionConnector[] = [ | ||
| { | ||
| id: '1', | ||
| actionTypeId: '.inference', | ||
| isMissingSecrets: false, | ||
| isPreconfigured: true, | ||
| config: { inferenceId: 'missing' }, | ||
| } as unknown as ActionConnector, | ||
| ]; | ||
|
|
||
| mockLoadAllActions.mockResolvedValue(mockConnectors); | ||
| mockIsInferenceEndpointExists.mockResolvedValue(false); | ||
|
|
||
| const result = await loadAiConnectors(mockHttp); | ||
|
|
||
| expect(result).toEqual([]); | ||
| }); | ||
|
|
||
| it('should exclude inference connectors if it is not configured correctly', async () => { | ||
| const mockConnectors: ActionConnector[] = [ | ||
| { | ||
| id: '1', | ||
| actionTypeId: '.inference', | ||
| isMissingSecrets: false, | ||
| isPreconfigured: true, | ||
| config: { inferenceId: undefined }, | ||
| } as unknown as ActionConnector, | ||
| ]; | ||
|
|
||
| mockLoadAllActions.mockResolvedValue(mockConnectors); | ||
| mockIsInferenceEndpointExists.mockResolvedValue(true); | ||
|
|
||
| const result = await loadAiConnectors(mockHttp); | ||
|
|
||
| expect(result).toEqual([]); | ||
| }); | ||
|
|
||
| it('should exclude connectors with missing secrets', async () => { | ||
| const mockConnectors: ActionConnector[] = [ | ||
| { id: '1', actionTypeId: '.bedrock', isMissingSecrets: true } as ActionConnector, | ||
| ]; | ||
|
|
||
| mockLoadAllActions.mockResolvedValue(mockConnectors); | ||
|
|
||
| const result = await loadAiConnectors(mockHttp); | ||
|
|
||
| expect(result).toEqual([]); | ||
| }); | ||
|
|
||
| it('should return an empty array if no connectors are valid', async () => { | ||
| const mockConnectors: ActionConnector[] = [ | ||
| { id: '1', actionTypeId: '.webhook', isMissingSecrets: false } as ActionConnector, | ||
| ]; | ||
|
|
||
| mockLoadAllActions.mockResolvedValue(mockConnectors); | ||
|
|
||
| const result = await loadAiConnectors(mockHttp); | ||
|
|
||
| expect(result).toEqual([]); | ||
| }); | ||
| }); |
75 changes: 75 additions & 0 deletions
75
...ion/public/onboarding/components/onboarding_body/cards/common/connectors/ai_connectors.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,75 @@ | ||
| /* | ||
| * 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 { HttpSetup } from '@kbn/core-http-browser'; | ||
| import { isInferenceEndpointExists } from '@kbn/inference-endpoint-ui-common'; | ||
| import { | ||
| loadAllActions, | ||
| type ActionConnector, | ||
| } from '@kbn/triggers-actions-ui-plugin/public/common/constants'; | ||
|
|
||
| const AllowedActionTypeIds = ['.bedrock', '.gen-ai', '.gemini', '.inference']; | ||
|
|
||
| type PreConfiguredInferenceConnector = ActionConnector & { | ||
| actionTypeId: '.inference'; | ||
| isPreconfigured: true; | ||
| config?: { | ||
| inferenceId?: string; | ||
| }; | ||
| }; | ||
|
|
||
| const isAllowedConnector = (connector: ActionConnector): boolean => | ||
| AllowedActionTypeIds.includes(connector.actionTypeId); | ||
|
|
||
| const isPreConfiguredInferenceConnector = ( | ||
| connector: ActionConnector | ||
| ): connector is PreConfiguredInferenceConnector => | ||
| connector.actionTypeId === '.inference' && connector.isPreconfigured; | ||
|
|
||
| const isValidAiConnector = async ( | ||
| connector: ActionConnector, | ||
| deps: { http: HttpSetup } | ||
| ): Promise<boolean> => { | ||
| if (connector.isMissingSecrets) { | ||
| return false; | ||
| } | ||
|
|
||
| if (!isAllowedConnector(connector)) { | ||
| return false; | ||
| } | ||
|
|
||
| if (isPreConfiguredInferenceConnector(connector)) { | ||
| const inferenceId = connector.config?.inferenceId; | ||
| if (!inferenceId) { | ||
| return false; | ||
| } | ||
| const exists = await isInferenceEndpointExists(deps.http, inferenceId); | ||
| if (!exists) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| }; | ||
|
|
||
| /** | ||
| * Loads all AI connectors that are valid, meaning that they don't miss secrets. | ||
| * And in the case of the inference connector, that the inference endpoint exists. | ||
| * @param http - The HTTP client to use for making requests. | ||
| * @returns A promise that resolves to an array of valid AI connectors. | ||
| */ | ||
| export const loadAiConnectors = async (http: HttpSetup) => { | ||
| const allConnectors = await loadAllActions({ http }); | ||
|
|
||
| const aiConnectors: ActionConnector[] = []; | ||
| for (const connector of allConnectors) { | ||
| const isValid = await isValidAiConnector(connector, { http }); | ||
| if (isValid) { | ||
| aiConnectors.push(connector); | ||
| } | ||
| } | ||
| return aiConnectors; | ||
| }; | ||
24 changes: 24 additions & 0 deletions
24
...ty_solution/public/onboarding/components/onboarding_body/cards/common/connectors/authz.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,24 @@ | ||
| /* | ||
| * 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 { Capabilities } from '@kbn/core/public'; | ||
| import { CapabilitiesChecker } from '../../../../../../common/lib/capabilities'; | ||
|
|
||
| export interface ConnectorsAuthz { | ||
| canReadConnectors: boolean; | ||
| canExecuteConnectors: boolean; | ||
| canCreateConnectors: boolean; | ||
| } | ||
|
|
||
| export const getConnectorsAuthz = (capabilities: Capabilities): ConnectorsAuthz => { | ||
| const checker = new CapabilitiesChecker(capabilities); | ||
semd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return { | ||
| canReadConnectors: checker.has('actions.show'), | ||
| canExecuteConnectors: checker.has([['actions.show', 'actions.execute']]), | ||
| canCreateConnectors: checker.has('actions.save'), | ||
| }; | ||
| }; | ||
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
8 changes: 0 additions & 8 deletions
8
...olution/public/onboarding/components/onboarding_body/cards/common/connectors/constants.ts
This file was deleted.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.