Skip to content
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

[Security Solution] Elastic Security Assistant #156933

Merged
merged 53 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
562e5d9
Hiiiiii world
spong May 5, 2023
199af3c
[Security Solution] Security Assistant: useSecurityAssistantQuery hoo…
andrew-goldstein May 9, 2023
6b9298e
Adds overlay assistant, new quick prompt pills, and cleans up assista…
spong May 9, 2023
77af478
Merge branch 'main' of github.com:elastic/kibana into security-assist…
spong May 9, 2023
3473f39
Adds conversation selector, custom localstorage keys, and UI cleanup
spong May 10, 2023
767523c
[Security Solution] Security Assistant: context provider hooks #6
andrew-goldstein May 10, 2023
8a66b9c
Removes self from state jail
spong May 11, 2023
f9f58f5
Extracts and fixes conversation selector and adds createConversation …
spong May 11, 2023
7964a64
Brings back localstorage support and updates localstorage hook to sup…
spong May 11, 2023
07ac4f3
Adds support for autofocusing prompt when opening timeline or selecti…
spong May 11, 2023
a3ddec0
Adds new useSecurityAssistantOverlay hook for programmatically openin…
spong May 12, 2023
521f26a
[Security Solution] Security Assistant: Prompt Editor / Sends System …
andrew-goldstein May 12, 2023
41d7233
- previews the prompt context before sending it (#8)
andrew-goldstein May 12, 2023
734012a
Adds new markdown parser for send to timeline actions for eql, kql an…
spong May 12, 2023
8a9aef8
Factored out codeblock analysis, fixed styling in timeline, extracted…
spong May 12, 2023
8b05944
[Security Solution] Security Assistant: Add note to Timeline #9
andrew-goldstein May 15, 2023
a7f4db3
Adds selected rules as prompt context, fixes some styling and ux
spong May 15, 2023
0aa6690
Fixes data issue with translations
spong May 15, 2023
e10e191
Fixes sent to timeline action render issue
spong May 15, 2023
56b8cab
[Security Solution] Security Assistant: fixes i18n errors (#10)
andrew-goldstein May 15, 2023
f8577a0
[Security Solution] Security Assistant: bootstraps the @kbn/elastic-a…
andrew-goldstein May 18, 2023
a5be762
[Security Solution] Security Assistant: Moves the assistant to the @k…
andrew-goldstein May 23, 2023
c1ca34f
Merge branch 'main' of github.com:elastic/kibana into security-assist…
spong May 23, 2023
e055574
[Security Solution] Security Assistant: fixes linter issues (#13)
andrew-goldstein May 23, 2023
456c5d1
[Security Solution] Security Assistant: take out the trash (icon) (#14)
andrew-goldstein May 23, 2023
7de6b51
Merge branch 'main' of github.com:elastic/kibana into security-assist…
spong May 25, 2023
6d567fe
Integrates with GenAI Connector
spong May 25, 2023
42e86e3
Merge branch 'main' of github.com:elastic/kibana into security-assist…
spong May 25, 2023
578fb89
[Security Solution] Security Assistant: Reduces the public API surfac…
andrew-goldstein May 26, 2023
cb99251
Merge branch 'main' of github.com:elastic/kibana into security-assist…
spong May 26, 2023
893c7c9
Removes export of OpenAiProviderType that was added in main
spong May 26, 2023
7f81b1b
Swathe of usability and bugfixes around connector integration
spong May 26, 2023
2caa3cc
Merge branch 'main' of github.com:elastic/kibana into security-assist…
spong May 26, 2023
6f2f1bc
[Security Solution] Security Assistant: Data Quality dashboard _New C…
andrew-goldstein May 31, 2023
e821e36
Merge branch 'main' of github.com:elastic/kibana into security-assist…
spong May 31, 2023
4aa6401
Adds CRUD for QuickPrompts and extracts them and base PromptContexts …
spong Jun 2, 2023
b235e15
Merge branch 'main' of github.com:elastic/kibana into security-assist…
spong Jun 2, 2023
705f9ee
i18n updates from James
spong Jun 2, 2023
4385ffd
[Security Solution] Security Assistant: adds the assistantEnabled fea…
andrew-goldstein Jun 2, 2023
313b7ef
Fixes from PR review and desk testing
spong Jun 2, 2023
d527032
Merge branch 'main' of github.com:elastic/kibana into security-assist…
spong Jun 2, 2023
7df9859
Updating api error language
spong Jun 2, 2023
93bbcdb
Merge branch 'main' of github.com:elastic/kibana into security-assist…
spong Jun 2, 2023
6154e21
[Security Solution] Security Assistant: fixes project lint errors (#18)
andrew-goldstein Jun 2, 2023
0bed49d
[Security Solution] Security Assistant: updates CODEOWNERS (#19)
andrew-goldstein Jun 2, 2023
71f0da8
Removes space key listener to resolve cypress issues
spong Jun 2, 2023
ff5d0dd
[Security Solution] Security Assistant: updates mocks (#20)
andrew-goldstein Jun 2, 2023
dca195e
Test fixes, i18n update, and additional feature flag conditional
spong Jun 2, 2023
52657fa
Merge branch 'main' of github.com:elastic/kibana into security-assist…
spong Jun 2, 2023
c19347b
Fixes prompt submit issue
spong Jun 2, 2023
435a988
Merge branch 'main' of github.com:elastic/kibana into security-assist…
spong Jun 2, 2023
be6c41f
Skips flakey cypress test
spong Jun 2, 2023
542f3e5
Merge branch 'main' of github.com:elastic/kibana into security-assist…
spong Jun 2, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,7 @@ module.exports = {
// front end and common typescript and javascript files only
files: [
'x-pack/plugins/ecs_data_quality_dashboard/common/**/*.{js,mjs,ts,tsx}',
'x-pack/packages/kbn-elastic-assistant/**/*.{js,mjs,ts,tsx}',
'x-pack/packages/security-solution/**/*.{js,mjs,ts,tsx}',
'x-pack/plugins/security_solution/public/**/*.{js,mjs,ts,tsx}',
'x-pack/plugins/security_solution/common/**/*.{js,mjs,ts,tsx}',
Expand Down Expand Up @@ -1009,13 +1010,15 @@ module.exports = {
// This should be a very small set as most linter rules are useful for tests as well.
files: [
'x-pack/plugins/ecs_data_quality_dashboard/**/*.{ts,tsx}',
'x-pack/packages/kbn-elastic-assistant/**/*.{ts,tsx}',
'x-pack/packages/security-solution/**/*.{ts,tsx}',
'x-pack/plugins/security_solution/**/*.{ts,tsx}',
'x-pack/plugins/timelines/**/*.{ts,tsx}',
'x-pack/plugins/cases/**/*.{ts,tsx}',
],
excludedFiles: [
'x-pack/plugins/ecs_data_quality_dashboard/**/*.{test,mock,test_helper}.{ts,tsx}',
'x-pack/packages/kbn-elastic-assistant/**/*.{test,mock,test_helper}.{ts,tsx}',
'x-pack/packages/security-solution/**/*.{test,mock,test_helper}.{ts,tsx}',
'x-pack/plugins/security_solution/**/*.{test,mock,test_helper}.{ts,tsx}',
'x-pack/plugins/timelines/**/*.{test,mock,test_helper}.{ts,tsx}',
Expand All @@ -1029,6 +1032,7 @@ module.exports = {
// typescript only for front and back end
files: [
'x-pack/plugins/ecs_data_quality_dashboard/**/*.{ts,tsx}',
'x-pack/packages/kbn-elastic-assistant/**/*.{ts,tsx}',
'x-pack/packages/security-solution/**/*.{ts,tsx}',
'x-pack/plugins/security_solution/**/*.{ts,tsx}',
'x-pack/plugins/timelines/**/*.{ts,tsx}',
Expand Down Expand Up @@ -1061,6 +1065,7 @@ module.exports = {
// typescript and javascript for front and back end
files: [
'x-pack/plugins/ecs_data_quality_dashboard/**/*.{js,mjs,ts,tsx}',
'x-pack/packages/kbn-elastic-assistant/**/*.{js,mjs,ts,tsx}',
'x-pack/packages/security-solution/**/*.{js,mjs,ts,tsx}',
'x-pack/plugins/security_solution/**/*.{js,mjs,ts,tsx}',
'x-pack/plugins/timelines/**/*.{js,mjs,ts,tsx}',
Expand Down
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ packages/kbn-ebt-tools @elastic/kibana-core
packages/kbn-ecs @elastic/kibana-core @elastic/security-threat-hunting-investigations
x-pack/packages/security-solution/ecs_data_quality_dashboard @elastic/security-threat-hunting-investigations
x-pack/plugins/ecs_data_quality_dashboard @elastic/security-threat-hunting-investigations
x-pack/packages/kbn-elastic-assistant @elastic/security-solution
test/plugin_functional/plugins/elasticsearch_client_plugin @elastic/kibana-core
x-pack/test/plugin_api_integration/plugins/elasticsearch_client @elastic/kibana-core
x-pack/plugins/embeddable_enhanced @elastic/kibana-presentation
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@
"@kbn/ecs": "link:packages/kbn-ecs",
"@kbn/ecs-data-quality-dashboard": "link:x-pack/packages/security-solution/ecs_data_quality_dashboard",
"@kbn/ecs-data-quality-dashboard-plugin": "link:x-pack/plugins/ecs_data_quality_dashboard",
"@kbn/elastic-assistant": "link:x-pack/packages/kbn-elastic-assistant",
"@kbn/elasticsearch-client-plugin": "link:test/plugin_functional/plugins/elasticsearch_client_plugin",
"@kbn/elasticsearch-client-xpack-plugin": "link:x-pack/test/plugin_api_integration/plugins/elasticsearch_client",
"@kbn/embeddable-enhanced-plugin": "link:x-pack/plugins/embeddable_enhanced",
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-babel-preset/styled_components_files.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = {
/src[\/\\]plugins[\/\\](kibana_react)[\/\\]/,
/x-pack[\/\\]plugins[\/\\](apm|beats_management|cases|fleet|infra|lists|observability|observability_shared|exploratory_view|osquery|security_solution|timelines|synthetics|ux)[\/\\]/,
/x-pack[\/\\]test[\/\\]plugin_functional[\/\\]plugins[\/\\]resolver_test[\/\\]/,
/x-pack[\/\\]packages[\/\\]elastic_assistant[\/\\]/,
/x-pack[\/\\]packages[\/\\]security-solution[\/\\]ecs_data_quality_dashboard[\/\\]/,
],
};
2 changes: 2 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,8 @@
"@kbn/ecs-data-quality-dashboard/*": ["x-pack/packages/security-solution/ecs_data_quality_dashboard/*"],
"@kbn/ecs-data-quality-dashboard-plugin": ["x-pack/plugins/ecs_data_quality_dashboard"],
"@kbn/ecs-data-quality-dashboard-plugin/*": ["x-pack/plugins/ecs_data_quality_dashboard/*"],
"@kbn/elastic-assistant": ["x-pack/packages/kbn-elastic-assistant"],
"@kbn/elastic-assistant/*": ["x-pack/packages/kbn-elastic-assistant/*"],
"@kbn/elasticsearch-client-plugin": ["test/plugin_functional/plugins/elasticsearch_client_plugin"],
"@kbn/elasticsearch-client-plugin/*": ["test/plugin_functional/plugins/elasticsearch_client_plugin/*"],
"@kbn/elasticsearch-client-xpack-plugin": ["x-pack/test/plugin_api_integration/plugins/elasticsearch_client"],
Expand Down
1 change: 1 addition & 0 deletions x-pack/.i18nrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"xpack.dashboard": "plugins/dashboard_enhanced",
"xpack.discover": "plugins/discover_enhanced",
"xpack.crossClusterReplication": "plugins/cross_cluster_replication",
"xpack.elasticAssistant": "packages/kbn-elastic-assistant",
"xpack.embeddableEnhanced": "plugins/embeddable_enhanced",
"xpack.endpoint": "plugins/endpoint",
"xpack.enterpriseSearch": "plugins/enterprise_search",
Expand Down
20 changes: 20 additions & 0 deletions x-pack/packages/kbn-elastic-assistant/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# @kbn/elastic-assistant

The `Elastic Assistant` is a user interface for interacting with generative AIs, like `ChatGPT`.

This package provides:

- Components for rendering the `Elastic Assistant`
- Hooks for passing context (for example, fields in an alert) to the `Elastic Assistant`, enabling users to include this content in their queries

## Maintainers

Maintained by the Security Solution team

## Running unit tests with code coverage

To (interactively) run unit tests with code coverage, run the following command:

```sh
cd $KIBANA_HOME && node scripts/jest --watch x-pack/packages/kbn-elastic-assistant --coverage
```
84 changes: 84 additions & 0 deletions x-pack/packages/kbn-elastic-assistant/impl/assistant/api.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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 { OpenAiProviderType } from '@kbn/stack-connectors-plugin/public/common';

import { HttpSetup } from '@kbn/core-http-browser';
import type { Message } from '../assistant_context/types';
import { Conversation } from '../assistant_context/types';
import { API_ERROR } from './translations';

export interface FetchConnectorExecuteAction {
apiConfig: Conversation['apiConfig'];
http: HttpSetup;
messages: Message[];
signal?: AbortSignal | undefined;
}

export const fetchConnectorExecuteAction = async ({
http,
messages,
apiConfig,
signal,
}: FetchConnectorExecuteAction): Promise<string> => {
const outboundMessages = messages.map((msg) => ({
role: msg.role,
content: msg.content,
}));

const body =
apiConfig?.provider === OpenAiProviderType.OpenAi
? {
model: 'gpt-3.5-turbo',
messages: outboundMessages,
n: 1,
stop: null,
temperature: 0.2,
}
: {
messages: outboundMessages,
};

const requestBody = {
params: {
subActionParams: {
body: JSON.stringify(body),
},
subAction: 'test',
},
};

try {
// TODO: Find return type for this API
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const response = await http.fetch<any>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is fine for the first iteration, but as an alternative to the connector API we can use ActionsClient and I prefer it better, because it will be easier to tune its methods to the custom needs like bringing streams functionality.

I was thinking maybe that will be more extendable architecture if that logic will be outside of that package. And handleSendMessage could be a consumer callback with the Connector way integration or some other method for sending messages.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh gosh, I totally missed there was a client! Thank you -- this will be much preferred 👍. Made a note to swap to the client (but probably won't do in this initial PR if that's okay).

`/api/actions/connector/${apiConfig?.connectorId}/_execute`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody),
signal,
}
);

const data = response.data;
if (response.status !== 'ok') {
return API_ERROR;
}

if (data.choices && data.choices.length > 0 && data.choices[0].message.content) {
const result = data.choices[0].message.content.trim();
return result;
} else {
return API_ERROR;
}
} catch (error) {
return API_ERROR;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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 React, { useCallback, useEffect, useState } from 'react';
import { EuiModal } from '@elastic/eui';

import useEvent from 'react-use/lib/useEvent';
// eslint-disable-next-line @kbn/eslint/module_migration
import styled from 'styled-components';
import { ShowAssistantOverlayProps, useAssistantContext } from '../../assistant_context';
import { Assistant } from '..';
import { WELCOME_CONVERSATION_TITLE } from '../use_conversation/translations';

const isMac = navigator.platform.toLowerCase().indexOf('mac') >= 0;

const StyledEuiModal = styled(EuiModal)`
min-width: 1200px;
max-height: 100%;
height: 100%;
`;

/**
* Modal container for Security Assistant conversations, receiving the page contents as context, plus whatever
* component currently has focus and any specific context it may provide through the SAssInterface.
*/
export const AssistantOverlay: React.FC = React.memo(() => {
const [isModalVisible, setIsModalVisible] = useState(false);
const [conversationId, setConversationId] = useState<string | undefined>(
WELCOME_CONVERSATION_TITLE
);
const [promptContextId, setPromptContextId] = useState<string | undefined>();
const { setShowAssistantOverlay } = useAssistantContext();

// Bind `showAssistantOverlay` in SecurityAssistantContext to this modal instance
const showOverlay = useCallback(
() =>
({
showOverlay: so,
promptContextId: pid,
conversationId: cid,
}: ShowAssistantOverlayProps) => {
setIsModalVisible(so);
setPromptContextId(pid);
setConversationId(cid);
},
[setIsModalVisible]
);
useEffect(() => {
setShowAssistantOverlay(showOverlay);
}, [setShowAssistantOverlay, showOverlay]);

// Register keyboard listener to show the modal when cmd + ; is pressed
const onKeyDown = useCallback(
(event: KeyboardEvent) => {
if (event.key === ';' && (isMac ? event.metaKey : event.ctrlKey)) {
event.preventDefault();
setIsModalVisible(!isModalVisible);
}
},
[isModalVisible]
);
useEvent('keydown', onKeyDown);

// Modal control functions
const cleanupAndCloseModal = useCallback(() => {
setIsModalVisible(false);
setPromptContextId(undefined);
setConversationId(conversationId);
}, [conversationId]);

const handleCloseModal = useCallback(() => {
cleanupAndCloseModal();
}, [cleanupAndCloseModal]);

return (
<>
{isModalVisible && (
<StyledEuiModal onClose={handleCloseModal}>
<Assistant conversationId={conversationId} promptContextId={promptContextId} />
</StyledEuiModal>
)}
</>
);
});

AssistantOverlay.displayName = 'AssistantOverlay';
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 { i18n } from '@kbn/i18n';

export const CANCEL_BUTTON = i18n.translate(
'xpack.elasticAssistant.assistant.overlay.CancelButton',
{
defaultMessage: 'Cancel',
}
);
Loading