-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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] Investigation guide - insights in markdown #145240
Merged
kqualters-elastic
merged 36 commits into
elastic:main
from
kqualters-elastic:investigation-guide-insights
Nov 16, 2022
Merged
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
843821a
WIP starting to work
kqualters-elastic a6fb6fa
WIP starting to work
kqualters-elastic 6963564
Merge remote-tracking branch 'upstream/main' into investigation-guide…
kqualters-elastic dafe3ee
Merge remote-tracking branch 'upstream/main' into investigation-guide…
kqualters-elastic 5c75894
WIP query working minus timerange
kqualters-elastic e3a0fa9
Merge remote-tracking branch 'upstream/main' into investigation-guide…
kqualters-elastic 0096785
Mostly working
kqualters-elastic 2957a12
Merge branch 'main' into investigation-guide-insights
kqualters-elastic 95b177f
WIP
kqualters-elastic e85e7d1
Merge branch 'main' into investigation-guide-insights
kqualters-elastic 8f0c80a
Merge branch 'investigation-guide-insights' of github.com:kqualters-e…
kqualters-elastic 3d065c3
Fix types, code working
kqualters-elastic 119e094
Merge branch 'main' into investigation-guide-insights
kqualters-elastic 7f55bb7
Remove unused variable
kqualters-elastic 5fd36e7
Remove unused prop
kqualters-elastic 3a6b4f6
Add useDataProvider tests
kqualters-elastic 9b32d75
Add locator
kqualters-elastic 89eb612
Add sanity test for query hook
kqualters-elastic cf46b22
Add sanity cypress test for insight markdown
kqualters-elastic f716b8f
Update cypress selector, add error message for invalid field
kqualters-elastic 7ca6049
Disable button if combine queries fails
kqualters-elastic 7cac44c
Remove delay when using markdown editor, use better prop names
kqualters-elastic 28f00d6
Merge remote-tracking branch 'upstream/main' into investigation-guide…
kqualters-elastic 386ac1b
Fix types
kqualters-elastic 3ad85e3
Fix types without breaking pagination
kqualters-elastic 710f476
PR feedback
kqualters-elastic 4a2b16e
Merge remote-tracking branch 'upstream/main' into investigation-guide…
kqualters-elastic be6cf77
Remove unused variable
kqualters-elastic b5fa33d
Merge remote-tracking branch 'upstream/main' into investigation-guide…
kqualters-elastic 89bdd49
Merge remote-tracking branch 'upstream/main' into investigation-guide…
kqualters-elastic 5558d77
Remove noop
kqualters-elastic b845243
Merge remote-tracking branch 'upstream/main' into investigation-guide…
kqualters-elastic 8273e2f
Prevent infinite loop with invalid markup in provider
kqualters-elastic 06c3c18
Merge remote-tracking branch 'upstream/main' into investigation-guide…
kqualters-elastic 997bcf5
Merge branch 'main' into investigation-guide-insights
jamster10 012863c
Merge branch 'main' into investigation-guide-insights
michaelolo24 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 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
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
146 changes: 146 additions & 0 deletions
146
...gins/security_solution/public/common/components/markdown_editor/plugins/insight/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,146 @@ | ||
/* | ||
* 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 { Plugin } from 'unified'; | ||
import React, { useContext, useMemo } from 'react'; | ||
import type { RemarkTokenizer } from '@elastic/eui'; | ||
import { EuiLoadingSpinner, EuiIcon } from '@elastic/eui'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { useAppToasts } from '../../../../hooks/use_app_toasts'; | ||
import { useInsightQuery } from './use_insight_query'; | ||
import { useInsightDataProviders } from './use_insight_data_providers'; | ||
import { BasicAlertDataContext } from '../../../event_details/investigation_guide_view'; | ||
import { InvestigateInTimelineButton } from '../../../event_details/table/investigate_in_timeline_button'; | ||
import type { AbsoluteTimeRange } from '../../../../store/inputs/model'; | ||
|
||
interface InsightComponentProps { | ||
label?: string; | ||
description?: string; | ||
providers?: string; | ||
} | ||
|
||
export const parser: Plugin = function () { | ||
const Parser = this.Parser; | ||
const tokenizers = Parser.prototype.inlineTokenizers; | ||
const methods = Parser.prototype.inlineMethods; | ||
const insightPrefix = '!{insight'; | ||
|
||
const tokenizeInsight: RemarkTokenizer = function (eat, value, silent) { | ||
if (value.startsWith(insightPrefix) === false) { | ||
return false; | ||
} | ||
|
||
const nextChar = value[insightPrefix.length]; | ||
if (nextChar !== '{' && nextChar !== '}') return false; | ||
if (silent) { | ||
return true; | ||
} | ||
|
||
// is there a configuration? | ||
const hasConfiguration = nextChar === '{'; | ||
|
||
let configuration: InsightComponentProps = {}; | ||
if (hasConfiguration) { | ||
let configurationString = ''; | ||
let openObjects = 0; | ||
|
||
for (let i = insightPrefix.length; i < value.length; i++) { | ||
const char = value[i]; | ||
if (char === '{') { | ||
openObjects++; | ||
configurationString += char; | ||
} else if (char === '}') { | ||
openObjects--; | ||
if (openObjects === -1) { | ||
break; | ||
} | ||
configurationString += char; | ||
} else { | ||
configurationString += char; | ||
} | ||
} | ||
|
||
try { | ||
configuration = JSON.parse(configurationString); | ||
return eat(value)({ | ||
type: 'insight', | ||
...configuration, | ||
providers: JSON.stringify(configuration.providers), | ||
}); | ||
} catch (err) { | ||
const now = eat.now(); | ||
this.file.fail( | ||
i18n.translate('xpack.securitySolution.markdownEditor.plugins.insightConfigError', { | ||
values: { err }, | ||
defaultMessage: 'Unable to parse insight JSON configuration: {err}', | ||
}), | ||
{ | ||
line: now.line, | ||
column: now.column + insightPrefix.length, | ||
} | ||
); | ||
} | ||
} | ||
return false; | ||
}; | ||
tokenizeInsight.locator = (value: string, fromIndex: number) => { | ||
return value.indexOf(insightPrefix, fromIndex); | ||
}; | ||
tokenizers.insight = tokenizeInsight; | ||
methods.splice(methods.indexOf('text'), 0, 'insight'); | ||
}; | ||
|
||
// receives the configuration from the parser and renders | ||
const InsightComponent = ({ label, description, providers }: InsightComponentProps) => { | ||
const { addError } = useAppToasts(); | ||
let parsedProviders = []; | ||
try { | ||
if (providers !== undefined) { | ||
parsedProviders = JSON.parse(providers); | ||
} | ||
} catch (err) { | ||
addError(err, { | ||
title: i18n.translate('xpack.securitySolution.markdownEditor.plugins.insightProviderError', { | ||
defaultMessage: 'Unable to parse insight provider configuration', | ||
}), | ||
}); | ||
} | ||
const { data: alertData } = useContext(BasicAlertDataContext); | ||
const dataProviders = useInsightDataProviders({ | ||
providers: parsedProviders, | ||
alertData, | ||
}); | ||
const { totalCount, isQueryLoading, oldestTimestamp, hasError } = useInsightQuery({ | ||
dataProviders, | ||
}); | ||
const timerange: AbsoluteTimeRange = useMemo(() => { | ||
return { | ||
kind: 'absolute', | ||
from: oldestTimestamp ?? '', | ||
to: new Date().toISOString(), | ||
}; | ||
}, [oldestTimestamp]); | ||
if (isQueryLoading) { | ||
return <EuiLoadingSpinner size="l" />; | ||
} else { | ||
return ( | ||
<InvestigateInTimelineButton | ||
asEmptyButton={false} | ||
isDisabled={hasError} | ||
dataProviders={dataProviders} | ||
timeRange={timerange} | ||
keepDataView={true} | ||
data-test-subj="insight-investigate-in-timeline-button" | ||
> | ||
<EuiIcon type="timeline" /> | ||
{` ${label} (${totalCount}) - ${description}`} | ||
</InvestigateInTimelineButton> | ||
); | ||
} | ||
}; | ||
|
||
export { InsightComponent as renderer }; |
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.
providers can never be null?
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.
shouldn't be, but if they are, we just pass an empty array for data providers, and the button will render but show 0