Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Azure
GitHub Repository: Azure/Azure-Sentinel-Notebooks
Path: blob/master/scenario-notebooks/UserSecurityMetadata/GraphVis.py
3253 views
1
# -------------------------------------------------------------------------
2
# Copyright (c) Microsoft Corporation. All rights reserved.
3
# Licensed under the MIT License. See License.txt in the project root for
4
# license information.
5
# --------------------------------------------------------------------------
6
from pathlib import Path
7
from string import Template
8
from NodeEdge import Node, Edge
9
import jsonpickle
10
11
12
class GraphVis:
13
def __init__(self):
14
self.nodes = set()
15
self.edges = set()
16
17
def addEdge(self, fromNode, toNode, label):
18
if fromNode.group != "User" and fromNode.group != "Group" and fromNode.group != "AzureSubscription" and fromNode.group != "ServicePrincipal":
19
raise Exception("Error: Unsupported node type - " +
20
jsonpickle.encode(fromNode, unpicklable=False, make_refs=False))
21
if toNode.group != "User" and toNode.group != "Group" and toNode.group != "AzureSubscription" and toNode.group != "ServicePrincipal":
22
raise Exception("Error: Unsupported node type - " +
23
jsonpickle.encode(toNode, unpicklable=False, make_refs=False))
24
self.nodes.add(fromNode)
25
self.nodes.add(toNode)
26
edge = Edge(fromNode.id, toNode.id, label)
27
self.edges.add(edge)
28
29
def getHtml(self):
30
htmlTemplate = Template(Path('./graph.html.template').read_text())
31
nodesJson = jsonpickle.encode(
32
self.nodes, unpicklable=False, make_refs=False)
33
edgesJson = jsonpickle.encode(
34
self.edges, unpicklable=False, make_refs=False).replace("from_", "from")
35
return htmlTemplate.substitute(NODES=nodesJson, EDGES=edgesJson)
36
37