Skip to content

Commit

Permalink
[Cloud Security] Update graph appearance (elastic#204610)
Browse files Browse the repository at this point in the history
## Summary

- [x] Remove the unknown or question symbols for entities who's type we
don't know
- [x] Replace edges to be sharp path instead of a smooth one 
- [x] Changed background of alerted node from `danger` to `primary`
- [x] Fix arrow popover color not in sync with background color 
- [x] Handles should be visible as a small dot
- [x] Expand button's color should be similar to the nodes color
- [x] Main alert path should be a solid line

Before:

![Screenshot 2024-12-18 at 18 45
31](https://github.com/user-attachments/assets/4fd24ec7-8a8b-4659-8605-a53685be8d78)


After:

![Screenshot 2024-12-18 at 19 08
31](https://github.com/user-attachments/assets/ebeae458-075d-48a3-8a42-66787440adc1)



https://github.com/user-attachments/assets/4253ec83-19da-4c34-bb80-cb4ca900672d


**How to test**

To test this PR you can run

```
yarn storybook cloud_security_posture_packages
```

To test e2e

- Enable the feature flag 

`kibana.dev.yml`:

```yaml
uiSettings.overrides.securitySolution:enableVisualizationsInFlyout: true
xpack.securitySolution.enableExperimental: ['graphVisualizationInFlyoutEnabled']
```

- Load mocked data:

```bash
node scripts/es_archiver load x-pack/test/cloud_security_posture_functional/es_archives/logs_gcp_audit \ 
  --es-url http://elastic:changeme@localhost:9200 \
  --kibana-url http://elastic:changeme@localhost:5601

node scripts/es_archiver load x-pack/test/cloud_security_posture_functional/es_archives/security_alerts \
  --es-url http://elastic:changeme@localhost:9200 \
  --kibana-url http://elastic:changeme@localhost:5601
```

- Make sure you include data from Oct 13 2024. (in the video I use Last
year)



### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: Sean Rathier <[email protected]>
Co-authored-by: Brad White <[email protected]>
Co-authored-by: seanrathier <[email protected]>
  • Loading branch information
4 people authored Dec 19, 2024
1 parent 715c59d commit 576f3c5
Show file tree
Hide file tree
Showing 19 changed files with 337 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,5 @@ export const edgeDataSchema = schema.object({
source: schema.string(),
target: schema.string(),
color: colorSchema,
type: schema.maybe(schema.oneOf([schema.literal('solid'), schema.literal('dashed')])),
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import React from 'react';
import React, { useEffect, useMemo, useRef } from 'react';
import { ThemeProvider } from '@emotion/react';
import {
ReactFlow,
Expand All @@ -17,14 +17,18 @@ import {
useEdgesState,
type BuiltInNode,
type NodeProps,
type Node as xyNode,
type Edge as xyEdge,
} from '@xyflow/react';
import { isEmpty, isEqual, pick, size, xorWith } from 'lodash';
import { Story } from '@storybook/react';
import { SvgDefsMarker } from './styles';
import { DefaultEdge } from '.';
import { LabelNode } from '../node';
import type { EdgeViewModel } from '../types';
import { SvgDefsMarker } from './markers';

import '@xyflow/react/dist/style.css';
import { LabelNode } from '../node';
import type { NodeViewModel } from '../types';
import { HandleStyleOverride } from '../node/styles';

export default {
title: 'Components/Graph Components/Default Edge',
Expand All @@ -34,22 +38,32 @@ export default {
options: ['primary', 'danger', 'warning'],
control: { type: 'radio' },
},
type: {
options: ['solid', 'dashed'],
control: { type: 'radio' },
},
},
};

const nodeTypes = {
default: ((props: NodeProps<BuiltInNode>) => {
const handleStyle = {
width: 0,
height: 0,
'min-width': 0,
'min-height': 0,
border: 'none',
};
// eslint-disable-next-line react/display-name
default: React.memo((props: NodeProps<BuiltInNode>) => {
return (
<div>
<Handle type="source" position={Position.Right} style={handleStyle} />
<Handle type="target" position={Position.Left} style={handleStyle} />
<Handle
type="target"
isConnectable={false}
position={Position.Left}
id="in"
style={HandleStyleOverride}
/>
<Handle
type="source"
isConnectable={false}
position={Position.Right}
id="out"
style={HandleStyleOverride}
/>
{props.data.label}
</div>
);
Expand All @@ -61,79 +75,101 @@ const edgeTypes = {
default: DefaultEdge,
};

const Template: Story<NodeViewModel> = (args: NodeViewModel) => {
const initialNodes = [
{
id: 'source',
type: 'default',
data: { label: 'source' },
position: { x: 0, y: 0 },
draggable: true,
},
{
id: 'target',
type: 'default',
data: { label: 'target' },
position: { x: 320, y: 100 },
draggable: true,
},
{
id: args.id,
type: 'label',
data: args,
position: { x: 160, y: 50 },
draggable: true,
},
];
const Template: Story<EdgeViewModel> = (args: EdgeViewModel) => {
const nodes = useMemo(
() => [
{
id: 'source',
type: 'default',
data: {
label: 'source',
},
position: { x: 0, y: 0 },
},
{
id: 'target',
type: 'default',
data: {
label: 'target',
},
position: { x: 420, y: 0 },
},
{
id: args.id,
type: 'label',
data: pick(args, ['id', 'label', 'interactive', 'source', 'target', 'color', 'type']),
position: { x: 230, y: 6 },
},
],
[args]
);

const initialEdges = [
{
id: `source-${args.id}`,
source: 'source',
target: args.id,
data: {
const edges = useMemo(
() => [
{
id: `source-${args.id}`,
source: 'source',
sourceShape: 'rectangle',
target: args.id,
targetShape: 'label',
color: args.color,
interactive: true,
data: {
id: `source-${args.id}`,
source: 'source',
sourceShape: 'custom',
target: args.id,
targetShape: 'label',
color: args.color,
type: args.type,
},
type: 'default',
},
type: 'default',
},
{
id: `${args.id}-target`,
source: args.id,
target: 'target',
data: {
{
id: `${args.id}-target`,
source: args.id,
sourceShape: 'label',
target: 'target',
targetShape: 'rectangle',
color: args.color,
interactive: true,
data: {
id: `${args.id}-target`,
source: args.id,
sourceShape: 'label',
target: 'target',
targetShape: 'custom',
color: args.color,
type: args.type,
},
type: 'default',
},
type: 'default',
},
];
],
[args]
);

const [nodes, _setNodes, onNodesChange] = useNodesState(initialNodes);
const [edges, _setEdges, onEdgesChange] = useEdgesState(initialEdges);
const [nodesState, setNodes, onNodesChange] = useNodesState<xyNode>(nodes);
const [edgesState, setEdges, onEdgesChange] = useEdgesState<xyEdge<EdgeViewModel>>(edges);
const currNodesRef = useRef(nodes);
const currEdgesRef = useRef(edges);

useEffect(() => {
if (
!isArrayOfObjectsEqual(nodes, currNodesRef.current) ||
!isArrayOfObjectsEqual(edges, currEdgesRef.current)
) {
setNodes(nodes);
setEdges(edges);
currNodesRef.current = nodes;
currEdgesRef.current = edges;
}
}, [setNodes, setEdges, nodes, edges]);

return (
<ThemeProvider theme={{ darkMode: false }}>
<SvgDefsMarker />
<ReactFlow
fitView
attributionPosition={undefined}
nodesDraggable={true}
nodeTypes={nodeTypes}
edgeTypes={edgeTypes}
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
nodes={nodes}
edges={edges}
nodes={nodesState}
edges={edgesState}
>
<Controls />
<Background />
Expand All @@ -148,6 +184,9 @@ Edge.args = {
id: 'siem-windows',
label: 'User login to OKTA',
color: 'primary',
icon: 'okta',
interactive: true,
type: 'solid',
};

const isArrayOfObjectsEqual = (x: object[], y: object[]) =>
size(x) === size(y) && isEmpty(xorWith(x, y, isEqual));
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
*/

import React from 'react';
import { BaseEdge, getBezierPath } from '@xyflow/react';
import { BaseEdge, getSmoothStepPath } from '@xyflow/react';
import { useEuiTheme } from '@elastic/eui';
import type { Color } from '@kbn/cloud-security-posture-common/types/graph/latest';
import type { EdgeProps } from '../types';
import { getMarker } from './styles';
import type { EdgeProps, EdgeViewModel } from '../types';
import { getShapeHandlePosition } from './utils';
import { getMarkerStart, getMarkerEnd } from './markers';

type EdgeColor = EdgeViewModel['color'];

export function DefaultEdge({
id,
Expand All @@ -25,22 +26,18 @@ export function DefaultEdge({
data,
}: EdgeProps) {
const { euiTheme } = useEuiTheme();
const color: Color = data?.color ?? 'primary';
const color: EdgeColor = data?.color ?? 'primary';

const [edgePath] = getBezierPath({
const [edgePath] = getSmoothStepPath({
// sourceX and targetX are adjusted to account for the shape handle position
sourceX: sourceX - getShapeHandlePosition(data?.sourceShape),
sourceY,
sourcePosition,
targetX: targetX + getShapeHandlePosition(data?.targetShape),
targetY,
targetPosition,
curvature:
0.1 *
(data?.sourceShape === 'group' ||
(data?.sourceShape === 'label' && data?.targetShape === 'group')
? -1 // We flip direction when the edge is between parent node to child nodes (groups always contain children in our graph)
: 1),
borderRadius: 15,
offset: 0,
});

return (
Expand All @@ -50,12 +47,19 @@ export function DefaultEdge({
style={{
stroke: euiTheme.colors[color],
}}
css={{
strokeDasharray: '2,2',
}}
css={
(!data?.type || data?.type === 'dashed') && {
strokeDasharray: '2,2',
}
}
markerStart={
data?.sourceShape !== 'label' && data?.sourceShape !== 'group'
? getMarkerStart(color)
: undefined
}
markerEnd={
data?.targetShape !== 'label' && data?.targetShape !== 'group'
? getMarker(color)
? getMarkerEnd(color)
: undefined
}
/>
Expand Down
Loading

0 comments on commit 576f3c5

Please sign in to comment.