@infrascan/shared-types 0.2.4 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @infrascan/shared-types
2
2
 
3
+ ## 0.4.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#77](https://github.com/infrascan/infrascan/pull/77) [`21d4ecf`](https://github.com/infrascan/infrascan/commit/21d4ecf4b7fec31f4ac7b2cc5857aa5d2b725075) Thanks [@lfarrel6](https://github.com/lfarrel6)! - Add in-memory graph model to decouple the infrascan packages from a single graphing library, and to make graph manipulation easier
8
+
9
+ ## 0.3.0
10
+
11
+ ### Minor Changes
12
+
13
+ - [#75](https://github.com/infrascan/infrascan/pull/75) [`4634235`](https://github.com/infrascan/infrascan/commit/4634235d61bd6bd817c6fb9e62add778218b69b6) Thanks [@lfarrel6](https://github.com/lfarrel6)! - Add graph model interface to shared types package
14
+
3
15
  ## 0.2.4
4
16
 
5
17
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infrascan/shared-types",
3
- "version": "0.2.4",
3
+ "version": "0.4.0",
4
4
  "description": "Shared types for multiple infrascan packages",
5
5
  "main": "src/index.ts",
6
6
  "scripts": {
package/src/api.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { AwsCredentialIdentityProvider, RetryStrategy, RetryStrategyV2 } from "@aws-sdk/types";
2
2
 
3
- import type { GraphEdge, GraphNode } from "./graph";
3
+ import type { SelectedEdge, SelectedNode } from "./graph";
4
4
  import type { BaseEdgeResolver } from "./config";
5
5
 
6
6
  /**
@@ -85,9 +85,9 @@ type GetterFn<T, P extends Provider> = (
85
85
  type GetNodeFn<T extends Provider> = (
86
86
  stateConnector: Connector,
87
87
  context: ProviderContextMap[T],
88
- ) => Promise<GraphNode[]>;
88
+ ) => Promise<SelectedNode[]>;
89
89
 
90
- type GetEdgeFn = (stateConnector: Connector) => Promise<GraphEdge[]>;
90
+ type GetEdgeFn = (stateConnector: Connector) => Promise<SelectedEdge[]>;
91
91
 
92
92
  export type Provider = "aws";
93
93
 
@@ -106,15 +106,6 @@ export type ProviderContext<T extends Provider> = {
106
106
  context: ProviderContextMap[T];
107
107
  };
108
108
 
109
- type FormatNodeFn<P extends Provider> = (
110
- node: GraphNode,
111
- context: ProviderContextMap[P],
112
- ) => GraphNode;
113
- type FormatEdgeFn<P extends Provider> = (
114
- node: GraphEdge,
115
- context: ProviderContextMap[P],
116
- ) => GraphEdge;
117
-
118
109
  export type EntityRoleData = {
119
110
  roleArn: string;
120
111
  executor: string;
@@ -131,9 +122,7 @@ export interface ServiceModule<T, P extends Provider> {
131
122
  getters: GetterFn<T, P>[];
132
123
  nodes?: string[];
133
124
  getNodes?: GetNodeFn<P>;
134
- formatNode?: FormatNodeFn<P>;
135
125
  edges?: BaseEdgeResolver[];
136
126
  getEdges?: GetEdgeFn;
137
- formatEdge?: FormatEdgeFn<P>;
138
127
  getIamRoles?: GetIamRoleFn;
139
128
  }
package/src/graph.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * A node returned from a state selector before its been formatted for a graphing library
3
3
  */
4
- export type SelectedNode = {
4
+ export interface SelectedNode {
5
5
  id: string;
6
6
  name?: string;
7
7
  parent?: string;
@@ -9,62 +9,48 @@ export type SelectedNode = {
9
9
  rawState?: any;
10
10
  };
11
11
 
12
- /**
13
- * A node on the graph
14
- */
15
- export type GraphNode = {
16
- group: "nodes";
17
- id: string;
18
- data: {
19
- id: string;
20
- type: string;
21
- /**
22
- * Parent node (account, region etc)
23
- */
24
- parent?: string;
25
- name?: string;
26
- service?: string;
27
- };
28
- /* eslint-disable @typescript-eslint/no-explicit-any */
29
- metadata?: any;
30
- };
12
+ export interface SelectedEdgeTarget {
13
+ name: string,
14
+ target: string
15
+ }
31
16
 
32
- /**
33
- * An edge connecting two nodes within a graph
34
- */
35
- export type GraphEdge = {
36
- group: "edges";
37
- /**
38
- * Unique ID for the edge
39
- */
40
- id?: string;
41
- data: {
42
- id: string;
43
- name: string;
44
- /**
45
- * Source Node
46
- */
47
- source: string;
48
- /**
49
- * Target Node
50
- */
51
- target: string;
52
- type: string;
53
- };
54
- metadata?: {
55
- label: string;
56
- roleArn?: string;
57
- /* eslint-disable @typescript-eslint/no-explicit-any */
58
- statement?: any;
59
- };
17
+ export interface SelectedEdge {
18
+ source: string;
19
+ target: string;
20
+ metadata?: Record<string, unknown>
60
21
  };
61
22
 
62
- /**
63
- * Generic type for elements in a graph
64
- */
65
- export type GraphElement = GraphNode | GraphEdge;
23
+ export interface Node {
24
+ id: string,
25
+ name: string,
26
+ metadata: Record<string, unknown>;
27
+ service?: string,
28
+ type?: string,
29
+ parent?: string | Node,
30
+ children?: Record<string, Node>,
31
+ incomingEdges: Record<string, Edge<Node>>,
32
+ outgoingEdges: Record<string, Edge<Node>>,
33
+ }
66
34
 
67
- export type EdgeTarget = {
68
- name: string;
69
- target: string;
35
+ export interface Edge<T extends string | Node> {
36
+ id: string,
37
+ name?: string,
38
+ metadata: Record<string, unknown>,
39
+ source: T,
40
+ target: T
41
+ }
42
+
43
+ export interface Graph {
44
+ nodes: Node[],
45
+ edges: Edge<Node>[],
46
+ addNode: (node: Pick<Node, "id" | "name" | "metadata" | "parent" | "service" | "type">) => void,
47
+ addEdge: (edge: Pick<Edge<string>, "name" | "source" | "target" | "metadata">) => void,
48
+ addChild: (parent: string, child: string) => void,
49
+ getNode: (id: string) => Node | undefined,
50
+ mapNodesById: (id: string, mapperFn: (node: Node) => Node[]) => void,
51
+ removeEdge: (id: string) => Edge<Node>,
52
+ removeNode: (id: string) => Node,
53
+ serialize: () => string
70
54
  };
55
+
56
+ export type GraphSerializer<T> = (graph: Graph) => T;
package/src/index.ts CHANGED
@@ -2,3 +2,4 @@ export * from "./api";
2
2
  export * from "./config";
3
3
  export * from "./graph";
4
4
  export * from "./scan";
5
+ export * from "./plugins";
package/src/plugins.ts ADDED
@@ -0,0 +1,42 @@
1
+ import { Graph } from "./graph";
2
+ import { AwsContext } from "./api";
3
+
4
+ // All supported events to trigger a plugin
5
+ export type GraphPluginEvents = "onServiceComplete" | "onRegionComplete" | "onAccountComplete" | "onGraphComplete";
6
+
7
+ /**
8
+ * A plugin which is called everytime a service's nodes have been added to the graph.
9
+ * Receives the current graph state, the service ID, and the current AWS Context.
10
+ */
11
+ export type OnServiceCompletePlugin = (graph: Graph, service: string, context: AwsContext) => void | Promise<void>;
12
+
13
+ /**
14
+ * A plugin which is called everytime a region's nodes have been added to the graph.
15
+ * Receives the current graph state, and the current AWS Context.
16
+ */
17
+ export type OnRegionCompletePlugin = (graph: Graph, context: AwsContext) => void | Promise<void>;
18
+
19
+ /**
20
+ * A plugin which is called everytime an account's nodes have been added to the graph.
21
+ * Receives the current graph state, and the current AWS Context.
22
+ */
23
+ export type OnAccountCompletePlugin = (graph: Graph, context: AwsContext) => void | Promise<void>;
24
+
25
+ /**
26
+ * A plugin which is called when the entire graph has been generated.
27
+ * Receives the entire graph state.
28
+ */
29
+ export type OnGraphCompletePlugin = (graph: Graph) => void | Promise<void>;
30
+
31
+ export type PluginEventHandlerTypes = {
32
+ "onServiceComplete": OnServiceCompletePlugin,
33
+ "onRegionComplete": OnRegionCompletePlugin,
34
+ "onAccountComplete": OnAccountCompletePlugin,
35
+ "onGraphComplete": OnGraphCompletePlugin,
36
+ };
37
+
38
+ export interface GraphPlugin<E extends GraphPluginEvents> {
39
+ id: string,
40
+ event: E,
41
+ handler: PluginEventHandlerTypes[E]
42
+ }