forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Security Solution] Security Assistant: useSecurityAssistantQuery hoo…
…k and New chat button #5 - adds the useSecurityAssistantQuery hook - adds a `New chat` button that renders a query in a popover
- Loading branch information
1 parent
562e5d9
commit 199af3c
Showing
9 changed files
with
352 additions
and
93 deletions.
There are no files selected for viewing
This file contains 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 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 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
41 changes: 41 additions & 0 deletions
41
x-pack/plugins/security_solution/public/security_assistant/helpers.ts
This file contains 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,41 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export interface SecurityAssistantUiSettings { | ||
virusTotal: { | ||
apiKey: string; | ||
baseUrl: string; | ||
}; | ||
|
||
openAI: { | ||
apiKey: string; | ||
baseUrl: string; | ||
}; | ||
} | ||
|
||
export async function fetchVirusTotalReport({ | ||
hash, | ||
settings: { virusTotal, openAI }, | ||
}: { | ||
hash: string; | ||
settings: SecurityAssistantUiSettings; | ||
}): Promise<unknown> { | ||
const url = `${virusTotal.baseUrl}/files/${hash}`; | ||
|
||
const response = await fetch(url, { | ||
headers: { | ||
'x-apikey': virusTotal.apiKey, | ||
}, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`VirusTotal API request failed with status ${response.status}`); | ||
} | ||
|
||
const data = await response.json(); | ||
return data; | ||
} |
68 changes: 68 additions & 0 deletions
68
x-pack/plugins/security_solution/public/security_assistant/new_chat/index.tsx
This file contains 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,68 @@ | ||
/* | ||
* 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 { EuiButtonEmpty, EuiPopover } from '@elastic/eui'; | ||
import React, { useCallback, useMemo, useState } from 'react'; | ||
import styled from 'styled-components'; | ||
|
||
import { useToasts } from '../../common/lib/kibana'; | ||
import type { TimelineEventsDetailsItem } from '../../../common/search_strategy'; | ||
import { SecurityAssistant } from '../security_assistant'; | ||
import * as i18n from './translations'; | ||
import { useSecurityAssistantQuery } from '../use_security_assistant_query'; | ||
|
||
const SecurityAssistantContainer = styled.div` | ||
max-height: 1020px; | ||
max-width: 600px; | ||
`; | ||
|
||
const NewChatComponent: React.FC<{ | ||
data: TimelineEventsDetailsItem[]; | ||
}> = ({ data }) => { | ||
const toasts = useToasts(); | ||
const [query, setQuery] = useState<string>(''); | ||
|
||
const [isPopoverOpen, setIsPopoverOpen] = useState(false); | ||
const closePopover = () => setIsPopoverOpen(false); | ||
|
||
const { getQuery } = useSecurityAssistantQuery({ data }); | ||
|
||
const onStartConversation = useCallback(async () => { | ||
try { | ||
setQuery(await getQuery()); | ||
setIsPopoverOpen((isOpen) => !isOpen); | ||
} catch (error) { | ||
toasts.addError(error, { title: i18n.ERROR_FETCHING_SECURITY_ASSISTANT_QUERY }); | ||
} | ||
}, [getQuery, toasts]); | ||
|
||
const NewChatButton = useMemo( | ||
() => ( | ||
<EuiButtonEmpty onClick={onStartConversation} iconType="discuss"> | ||
{i18n.NEW_CHAT} | ||
</EuiButtonEmpty> | ||
), | ||
[onStartConversation] | ||
); | ||
|
||
return ( | ||
<EuiPopover | ||
button={NewChatButton} | ||
closePopover={closePopover} | ||
isOpen={isPopoverOpen} | ||
panelPaddingSize="none" | ||
> | ||
<SecurityAssistantContainer> | ||
<SecurityAssistant input={query} /> | ||
</SecurityAssistantContainer> | ||
</EuiPopover> | ||
); | ||
}; | ||
|
||
NewChatComponent.displayName = 'NewChatComponent'; | ||
|
||
export const NewChat = React.memo(NewChatComponent); |
19 changes: 19 additions & 0 deletions
19
x-pack/plugins/security_solution/public/security_assistant/new_chat/translations.ts
This file contains 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 { i18n } from '@kbn/i18n'; | ||
|
||
export const NEW_CHAT = i18n.translate('xpack.securitySolution.securityAssistant.newChatButton', { | ||
defaultMessage: 'New chat', | ||
}); | ||
|
||
export const ERROR_FETCHING_SECURITY_ASSISTANT_QUERY = i18n.translate( | ||
'xpack.securitySolution.securityAssistant.errorFetchingSecurityAssistantQuery', | ||
{ | ||
defaultMessage: 'Error fetching security assistant query', | ||
} | ||
); |
Oops, something went wrong.