-
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
[Cloud Security] Show graph visualization in expanded flyout #198240
Merged
kfirpeled
merged 47 commits into
elastic:main
from
kfirpeled:cspm/cdr-graph-viz-expanded
Dec 12, 2024
+1,849
−155
Merged
Changes from all commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
a982ad7
Added node button to support node click to open a popover.
kfirpeled c94c04a
Fixed SvgDefsMarker so it won't capture clicks
kfirpeled 5c00b6e
Fixed missing padding when lazy loading the graph
kfirpeled 1589ef8
Added graph visualization tab with expand capability
kfirpeled 6f71b19
checks feature flag before showing graph tab
kfirpeled 3dc4523
Added filterbar support
kfirpeled b88b074
fixed prevent scrolling when not interactive
kfirpeled dbb2688
Added filter support
kfirpeled 008521f
lowered the minimum zoom
kfirpeled 70ee31d
refactoring
kfirpeled dbab169
Merge branch 'main' into cspm/cdr-graph-viz-expanded
kfirpeled 78246a7
Added open in timeline + time picking is around @timestamp
kfirpeled 5318512
Color fixes and auto fitview
kfirpeled 74ecb0e
Merge branch 'main' into cspm/cdr-graph-viz-expanded
kfirpeled b57dfac
Merge branch 'main' into cspm/cdr-graph-viz-expanded
kfirpeled 2d0d360
Removed trace message that might contain sensitive info
kfirpeled f94db1e
Removed duplicated code due to merge
kfirpeled 0899098
Merge branch 'main' into cspm/cdr-graph-viz-expanded
kfirpeled 702ef00
merge cleanup
kfirpeled 1ad2ce4
Merge branch 'main' into cspm/cdr-graph-viz-expanded
kfirpeled 30f73c8
refactoring, removed investigate in timeline
kfirpeled 3821ad9
Updated shared hooks
kfirpeled 33134f6
Switched feature flag to an existing advanced setting
kfirpeled c98ab19
Merge branch 'main' into cspm/cdr-graph-viz-expanded
kfirpeled 708300d
Added FTR to test e2e expanded flyout filtering
kfirpeled 34656ae
Added technical preview tag
kfirpeled b0f73b9
Merge branch 'main' into cspm/cdr-graph-viz-expanded
kfirpeled c7fb622
post merge fixes
kfirpeled 5c50026
Merge branch 'main' into cspm/cdr-graph-viz-expanded
kfirpeled 6969f69
disables flyout expansion in rule preview and in panel preview
kfirpeled 21d04e2
refactoring
kfirpeled fef5ea3
refactoring
kfirpeled 6b81c5d
fix tests
kfirpeled e8c6367
Merge branch 'main' into cspm/cdr-graph-viz-expanded
kfirpeled 4b1512e
Refactored graph visualization component
kfirpeled b13bc2a
[CI] Auto-commit changed files from 'node scripts/notice'
kibanamachine 2f0ea4a
tests fixes
kfirpeled 20be49b
fix i18n
kfirpeled 0b52af2
quick check fix
kfirpeled bcbe7f6
linting fixes
kfirpeled dc5bb1c
Merge branch 'main' into cspm/cdr-graph-viz-expanded
kfirpeled 1f0bcec
added back graph vis in flyout experimental feature
kfirpeled 6ac1a58
minor fix - just for the code to make more sense
kfirpeled 5e90b2c
typecheck fix
kfirpeled 6085cbd
Merge branch 'main' into cspm/cdr-graph-viz-expanded
kfirpeled 6a55f50
refactoring
kfirpeled 40e66c4
refactoring
kfirpeled 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ | |
*/ | ||
|
||
export * from './src/components'; | ||
export { useFetchGraphData } from './src/hooks'; |
12 changes: 12 additions & 0 deletions
12
x-pack/packages/kbn-cloud-security-posture/graph/src/common/constants.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,12 @@ | ||
/* | ||
* 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 const EVENT_GRAPH_VISUALIZATION_API = '/internal/cloud_security_posture/graph' as const; | ||
|
||
export const RELATED_ENTITY = 'related.entity' as const; | ||
export const ACTOR_ENTITY_ID = 'actor.entity.id' as const; | ||
export const TARGET_ENTITY_ID = 'target.entity.id' as const; |
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
258 changes: 258 additions & 0 deletions
258
...n-cloud-security-posture/graph/src/components/graph_investigation/graph_investigation.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,258 @@ | ||
/* | ||
* 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, { memo, useCallback, useMemo, useState } from 'react'; | ||
import { SearchBar } from '@kbn/unified-search-plugin/public'; | ||
import { useKibana } from '@kbn/kibana-react-plugin/public'; | ||
import type { DataView } from '@kbn/data-views-plugin/public'; | ||
import { | ||
BooleanRelation, | ||
buildEsQuery, | ||
isCombinedFilter, | ||
buildCombinedFilter, | ||
isFilter, | ||
FilterStateStore, | ||
} from '@kbn/es-query'; | ||
import type { Filter, Query, TimeRange, PhraseFilter } from '@kbn/es-query'; | ||
import { css } from '@emotion/react'; | ||
import { getEsQueryConfig } from '@kbn/data-service'; | ||
import { EuiFlexGroup, EuiFlexItem } from '@elastic/eui'; | ||
import { Graph } from '../../..'; | ||
import { useGraphNodeExpandPopover } from './use_graph_node_expand_popover'; | ||
import { useFetchGraphData } from '../../hooks/use_fetch_graph_data'; | ||
import { GRAPH_INVESTIGATION_TEST_ID } from '../test_ids'; | ||
import { ACTOR_ENTITY_ID, RELATED_ENTITY, TARGET_ENTITY_ID } from '../../common/constants'; | ||
|
||
const CONTROLLED_BY_GRAPH_INVESTIGATION_FILTER = 'graph-investigation'; | ||
|
||
const buildPhraseFilter = (field: string, value: string, dataViewId?: string): PhraseFilter => ({ | ||
meta: { | ||
key: field, | ||
index: dataViewId, | ||
negate: false, | ||
disabled: false, | ||
type: 'phrase', | ||
field, | ||
controlledBy: CONTROLLED_BY_GRAPH_INVESTIGATION_FILTER, | ||
params: { | ||
query: value, | ||
}, | ||
}, | ||
query: { | ||
match_phrase: { | ||
[field]: value, | ||
}, | ||
}, | ||
}); | ||
|
||
/** | ||
* Adds a filter to the existing list of filters based on the provided key and value. | ||
* It will always use the first filter in the list to build a combined filter with the new filter. | ||
* | ||
* @param dataViewId - The ID of the data view to which the filter belongs. | ||
* @param prev - The previous list of filters. | ||
* @param key - The key for the filter. | ||
* @param value - The value for the filter. | ||
* @returns A new list of filters with the added filter. | ||
*/ | ||
const addFilter = (dataViewId: string, prev: Filter[], key: string, value: string) => { | ||
const [firstFilter, ...otherFilters] = prev; | ||
|
||
if (isCombinedFilter(firstFilter) && firstFilter?.meta?.relation === BooleanRelation.OR) { | ||
return [ | ||
{ | ||
...firstFilter, | ||
meta: { | ||
...firstFilter.meta, | ||
params: [ | ||
...(Array.isArray(firstFilter.meta.params) ? firstFilter.meta.params : []), | ||
buildPhraseFilter(key, value), | ||
], | ||
}, | ||
}, | ||
...otherFilters, | ||
]; | ||
} else if (isFilter(firstFilter) && firstFilter.meta?.type !== 'custom') { | ||
return [ | ||
buildCombinedFilter(BooleanRelation.OR, [firstFilter, buildPhraseFilter(key, value)], { | ||
id: dataViewId, | ||
}), | ||
...otherFilters, | ||
]; | ||
} else { | ||
return [ | ||
{ | ||
$state: { | ||
store: FilterStateStore.APP_STATE, | ||
}, | ||
...buildPhraseFilter(key, value, dataViewId), | ||
}, | ||
...prev, | ||
]; | ||
} | ||
}; | ||
|
||
const useGraphPopovers = ( | ||
dataViewId: string, | ||
setSearchFilters: React.Dispatch<React.SetStateAction<Filter[]>> | ||
) => { | ||
const nodeExpandPopover = useGraphNodeExpandPopover({ | ||
onExploreRelatedEntitiesClick: (node) => { | ||
setSearchFilters((prev) => addFilter(dataViewId, prev, RELATED_ENTITY, node.id)); | ||
}, | ||
onShowActionsByEntityClick: (node) => { | ||
setSearchFilters((prev) => addFilter(dataViewId, prev, ACTOR_ENTITY_ID, node.id)); | ||
}, | ||
onShowActionsOnEntityClick: (node) => { | ||
setSearchFilters((prev) => addFilter(dataViewId, prev, TARGET_ENTITY_ID, node.id)); | ||
}, | ||
}); | ||
|
||
const openPopoverCallback = useCallback( | ||
(cb: Function, ...args: unknown[]) => { | ||
[nodeExpandPopover].forEach(({ actions: { closePopover } }) => { | ||
closePopover(); | ||
}); | ||
cb(...args); | ||
}, | ||
[nodeExpandPopover] | ||
); | ||
|
||
return { nodeExpandPopover, openPopoverCallback }; | ||
}; | ||
|
||
interface GraphInvestigationProps { | ||
dataView: DataView; | ||
eventIds: string[]; | ||
timestamp: string | null; | ||
} | ||
|
||
/** | ||
* Graph investigation view allows the user to expand nodes and view related entities. | ||
*/ | ||
export const GraphInvestigation: React.FC<GraphInvestigationProps> = memo( | ||
({ dataView, eventIds, timestamp = new Date().toISOString() }: GraphInvestigationProps) => { | ||
const [searchFilters, setSearchFilters] = useState<Filter[]>(() => []); | ||
const [timeRange, setTimeRange] = useState<TimeRange>({ | ||
from: `${timestamp}||-30m`, | ||
to: `${timestamp}||+30m`, | ||
}); | ||
|
||
const { | ||
services: { uiSettings }, | ||
} = useKibana(); | ||
const query = useMemo( | ||
() => | ||
buildEsQuery( | ||
dataView, | ||
[], | ||
[...searchFilters], | ||
getEsQueryConfig(uiSettings as Parameters<typeof getEsQueryConfig>[0]) | ||
), | ||
[searchFilters, dataView, uiSettings] | ||
); | ||
|
||
const { nodeExpandPopover, openPopoverCallback } = useGraphPopovers( | ||
dataView?.id ?? '', | ||
setSearchFilters | ||
); | ||
const expandButtonClickHandler = (...args: unknown[]) => | ||
openPopoverCallback(nodeExpandPopover.onNodeExpandButtonClick, ...args); | ||
const isPopoverOpen = [nodeExpandPopover].some(({ state: { isOpen } }) => isOpen); | ||
const { data, refresh, isFetching } = useFetchGraphData({ | ||
req: { | ||
query: { | ||
eventIds, | ||
esQuery: query, | ||
start: timeRange.from, | ||
end: timeRange.to, | ||
}, | ||
}, | ||
options: { | ||
refetchOnWindowFocus: false, | ||
keepPreviousData: true, | ||
}, | ||
}); | ||
|
||
const nodes = useMemo(() => { | ||
return ( | ||
data?.nodes.map((node) => { | ||
const nodeHandlers = | ||
node.shape !== 'label' && node.shape !== 'group' | ||
? { | ||
expandButtonClick: expandButtonClickHandler, | ||
} | ||
: undefined; | ||
return { ...node, ...nodeHandlers }; | ||
}) ?? [] | ||
); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [data?.nodes]); | ||
|
||
return ( | ||
<> | ||
<EuiFlexGroup | ||
data-test-subj={GRAPH_INVESTIGATION_TEST_ID} | ||
direction="column" | ||
gutterSize="none" | ||
css={css` | ||
height: 100%; | ||
`} | ||
> | ||
{dataView && ( | ||
<EuiFlexItem grow={false}> | ||
<SearchBar<Query> | ||
{...{ | ||
appName: 'graph-investigation', | ||
intl: null, | ||
showFilterBar: true, | ||
showDatePicker: true, | ||
showAutoRefreshOnly: false, | ||
showSaveQuery: false, | ||
showQueryInput: false, | ||
isLoading: isFetching, | ||
isAutoRefreshDisabled: true, | ||
dateRangeFrom: timeRange.from, | ||
dateRangeTo: timeRange.to, | ||
query: { query: '', language: 'kuery' }, | ||
indexPatterns: [dataView], | ||
filters: searchFilters, | ||
submitButtonStyle: 'iconOnly', | ||
onFiltersUpdated: (newFilters) => { | ||
setSearchFilters(newFilters); | ||
}, | ||
onQuerySubmit: (payload, isUpdate) => { | ||
if (isUpdate) { | ||
setTimeRange({ ...payload.dateRange }); | ||
} else { | ||
refresh(); | ||
} | ||
}, | ||
}} | ||
/> | ||
</EuiFlexItem> | ||
)} | ||
<EuiFlexItem> | ||
<Graph | ||
css={css` | ||
height: 100%; | ||
width: 100%; | ||
`} | ||
nodes={nodes} | ||
edges={data?.edges ?? []} | ||
interactive={true} | ||
isLocked={isPopoverOpen} | ||
/> | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
<nodeExpandPopover.PopoverComponent /> | ||
</> | ||
); | ||
} | ||
); | ||
|
||
GraphInvestigation.displayName = 'GraphInvestigation'; |
78 changes: 78 additions & 0 deletions
78
...d-security-posture/graph/src/components/graph_investigation/graph_node_expand_popover.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,78 @@ | ||
/* | ||
* 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, { memo } from 'react'; | ||
import { EuiListGroup } from '@elastic/eui'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { ExpandPopoverListItem } from '../styles'; | ||
import { GraphPopover } from '../../..'; | ||
import { | ||
GRAPH_NODE_EXPAND_POPOVER_TEST_ID, | ||
GRAPH_NODE_POPOVER_SHOW_RELATED_ITEM_ID, | ||
GRAPH_NODE_POPOVER_SHOW_ACTIONS_BY_ITEM_ID, | ||
GRAPH_NODE_POPOVER_SHOW_ACTIONS_ON_ITEM_ID, | ||
} from '../test_ids'; | ||
|
||
interface GraphNodeExpandPopoverProps { | ||
isOpen: boolean; | ||
anchorElement: HTMLElement | null; | ||
closePopover: () => void; | ||
onShowRelatedEntitiesClick: () => void; | ||
onShowActionsByEntityClick: () => void; | ||
onShowActionsOnEntityClick: () => void; | ||
} | ||
|
||
export const GraphNodeExpandPopover: React.FC<GraphNodeExpandPopoverProps> = memo( | ||
({ | ||
isOpen, | ||
anchorElement, | ||
closePopover, | ||
onShowRelatedEntitiesClick, | ||
onShowActionsByEntityClick, | ||
onShowActionsOnEntityClick, | ||
}) => { | ||
return ( | ||
<GraphPopover | ||
panelPaddingSize="s" | ||
anchorPosition="rightCenter" | ||
isOpen={isOpen} | ||
anchorElement={anchorElement} | ||
closePopover={closePopover} | ||
data-test-subj={GRAPH_NODE_EXPAND_POPOVER_TEST_ID} | ||
> | ||
<EuiListGroup gutterSize="none" bordered={false} flush={true}> | ||
<ExpandPopoverListItem | ||
iconType="users" | ||
label={i18n.translate('xpack.csp.graph.graphNodeExpandPopover.showActionsByEntity', { | ||
defaultMessage: 'Show actions by this entity', | ||
})} | ||
onClick={onShowActionsByEntityClick} | ||
data-test-subj={GRAPH_NODE_POPOVER_SHOW_ACTIONS_BY_ITEM_ID} | ||
/> | ||
<ExpandPopoverListItem | ||
iconType="storage" | ||
label={i18n.translate('xpack.csp.graph.graphNodeExpandPopover.showActionsOnEntity', { | ||
defaultMessage: 'Show actions on this entity', | ||
})} | ||
onClick={onShowActionsOnEntityClick} | ||
data-test-subj={GRAPH_NODE_POPOVER_SHOW_ACTIONS_ON_ITEM_ID} | ||
/> | ||
<ExpandPopoverListItem | ||
iconType="visTagCloud" | ||
label={i18n.translate('xpack.csp.graph.graphNodeExpandPopover.showRelatedEvents', { | ||
defaultMessage: 'Show related events', | ||
})} | ||
onClick={onShowRelatedEntitiesClick} | ||
data-test-subj={GRAPH_NODE_POPOVER_SHOW_RELATED_ITEM_ID} | ||
/> | ||
</EuiListGroup> | ||
</GraphPopover> | ||
); | ||
} | ||
); | ||
|
||
GraphNodeExpandPopover.displayName = 'GraphNodeExpandPopover'; |
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.
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.
what this logic of combined vs phrase filter is about? not really sure, maybe also worth a comment explaining the edge cases and/or add unit tests wich would explain different scenarious
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.
so we keep always a single filter with
OR
statementsSo whenever addFilter is called, it checks first if it exists or not, and then adds OR statement when needed
I'll add comments now and UT later, once I'll have graph_investigation in storybook (it requires some extra mocking and this PR is already quite big).