Path: blob/master/scenario-notebooks/UserSecurityMetadata/Entities.py
3253 views
# -------------------------------------------------------------------------1# Copyright (c) Microsoft Corporation. All rights reserved.2# Licensed under the MIT License. See License.txt in the project root for3# license information.4# --------------------------------------------------------------------------5from validate_email import validate_email6from Utils import executeProcess7from NodeEdge import Node, DrawableNode8from functools import lru_cache91011class User(DrawableNode):1213def __init__(self, name, email, objectId):14self.name = name15self.email = email16self.objectId = objectId1718def getNode(self):19return Node(self.objectId, self.name, "User")2021@staticmethod22@lru_cache(maxsize=100)23def getUserById(userId):24rawoutput = executeProcess(25f'az ad user show --id {userId} --query [displayName,mail,objectId] --output tsv'.split(' '))26output = rawoutput.split('\n')27if len(output) != 3:28raise Exception(29f'Unable to get AAD User with Id - {userId}. Error - {rawoutput}')30else:31user = User(output[0], output[1], output[2])32return user3334@staticmethod35@lru_cache(maxsize=100)36def getUserByEmail(userEmail):37rawoutput = executeProcess(38f'az ad user list --filter startswith(mail,\'{userEmail}\') --query [0].{{Name:displayName,Email:mail,ObjectId:objectId}} --output tsv'.split(' '))39output = rawoutput.split('\t')40if len(output) != 3:41raise Exception(42f'Not found - User with email - {userEmail}.')43else:44user = User(output[0], output[1], output[2])45return user4647@staticmethod48def getUserByIdOrEmail(userIdOrEmail):49try:50return User.getUserById(userIdOrEmail)51except:52isvalidEmail = validate_email(userIdOrEmail)53if isvalidEmail:54return User.getUserByEmail(userIdOrEmail)55raise565758class Group(DrawableNode):59def __init__(self, name, email, groupId):60self.name = name61self.email = email62self.groupId = groupId6364def getNode(self):65return Node(self.groupId, self.name, "Group")6667@staticmethod68@lru_cache(maxsize=100)69def getGroupById(groupId):70rawoutput = executeProcess(71f'az ad group show --group {groupId} --query [displayName,mail,objectId] --output tsv'.split(' '))72output = rawoutput.split('\n')73if len(output) != 3:74raise Exception(75f'Unable to get AAD Group with Id - {groupId}. Error - {rawoutput}')76else:77group = Group(output[0], output[1], output[2])78return group798081class ServicePrincipal(DrawableNode):82def __init__(self, name, objectId):83self.name = name84self.objectId = objectId8586def getNode(self):87return Node(self.objectId, self.name, "ServicePrincipal")8889@staticmethod90@lru_cache(maxsize=100)91def getServicePrincipalById(objectId):92rawoutput = executeProcess(93f'az ad sp show --id {objectId} --query [displayName,objectId] --output tsv'.split(' '))94output = rawoutput.split('\n')95if len(output) != 2:96raise Exception(97f'Unable to get AAD ServicePrincipal with Id - {objectId}. Error - {rawoutput}')98else:99sp = ServicePrincipal(output[0], output[1])100return sp101102103class Subscription(DrawableNode):104105def __init__(self, name, subId):106self.name = name107self.subId = subId108109def getNode(self):110return Node(self.subId, self.name, "AzureSubscription")111112113