Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Azure
GitHub Repository: Azure/Azure-Sentinel-Notebooks
Path: blob/master/scenario-notebooks/UserSecurityMetadata/Entities.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 validate_email import validate_email
7
from Utils import executeProcess
8
from NodeEdge import Node, DrawableNode
9
from functools import lru_cache
10
11
12
class User(DrawableNode):
13
14
def __init__(self, name, email, objectId):
15
self.name = name
16
self.email = email
17
self.objectId = objectId
18
19
def getNode(self):
20
return Node(self.objectId, self.name, "User")
21
22
@staticmethod
23
@lru_cache(maxsize=100)
24
def getUserById(userId):
25
rawoutput = executeProcess(
26
f'az ad user show --id {userId} --query [displayName,mail,objectId] --output tsv'.split(' '))
27
output = rawoutput.split('\n')
28
if len(output) != 3:
29
raise Exception(
30
f'Unable to get AAD User with Id - {userId}. Error - {rawoutput}')
31
else:
32
user = User(output[0], output[1], output[2])
33
return user
34
35
@staticmethod
36
@lru_cache(maxsize=100)
37
def getUserByEmail(userEmail):
38
rawoutput = executeProcess(
39
f'az ad user list --filter startswith(mail,\'{userEmail}\') --query [0].{{Name:displayName,Email:mail,ObjectId:objectId}} --output tsv'.split(' '))
40
output = rawoutput.split('\t')
41
if len(output) != 3:
42
raise Exception(
43
f'Not found - User with email - {userEmail}.')
44
else:
45
user = User(output[0], output[1], output[2])
46
return user
47
48
@staticmethod
49
def getUserByIdOrEmail(userIdOrEmail):
50
try:
51
return User.getUserById(userIdOrEmail)
52
except:
53
isvalidEmail = validate_email(userIdOrEmail)
54
if isvalidEmail:
55
return User.getUserByEmail(userIdOrEmail)
56
raise
57
58
59
class Group(DrawableNode):
60
def __init__(self, name, email, groupId):
61
self.name = name
62
self.email = email
63
self.groupId = groupId
64
65
def getNode(self):
66
return Node(self.groupId, self.name, "Group")
67
68
@staticmethod
69
@lru_cache(maxsize=100)
70
def getGroupById(groupId):
71
rawoutput = executeProcess(
72
f'az ad group show --group {groupId} --query [displayName,mail,objectId] --output tsv'.split(' '))
73
output = rawoutput.split('\n')
74
if len(output) != 3:
75
raise Exception(
76
f'Unable to get AAD Group with Id - {groupId}. Error - {rawoutput}')
77
else:
78
group = Group(output[0], output[1], output[2])
79
return group
80
81
82
class ServicePrincipal(DrawableNode):
83
def __init__(self, name, objectId):
84
self.name = name
85
self.objectId = objectId
86
87
def getNode(self):
88
return Node(self.objectId, self.name, "ServicePrincipal")
89
90
@staticmethod
91
@lru_cache(maxsize=100)
92
def getServicePrincipalById(objectId):
93
rawoutput = executeProcess(
94
f'az ad sp show --id {objectId} --query [displayName,objectId] --output tsv'.split(' '))
95
output = rawoutput.split('\n')
96
if len(output) != 2:
97
raise Exception(
98
f'Unable to get AAD ServicePrincipal with Id - {objectId}. Error - {rawoutput}')
99
else:
100
sp = ServicePrincipal(output[0], output[1])
101
return sp
102
103
104
class Subscription(DrawableNode):
105
106
def __init__(self, name, subId):
107
self.name = name
108
self.subId = subId
109
110
def getNode(self):
111
return Node(self.subId, self.name, "AzureSubscription")
112
113