-
Notifications
You must be signed in to change notification settings - Fork 1
/
tool_modifygroups.py
121 lines (99 loc) · 3.47 KB
/
tool_modifygroups.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import os
import json
import typing
import logging
from microsoft.submaintenance import AzIdentities
from microsoft.submaintenance.utils import(
Configuration,
AzLoginUtils
)
from microsoft.submaintenance.utils.grouputils import (
UserAssignment,
GroupConfiguration
)
# MCIReaderTest
# MCIExternalReaderTest
#------------------------------------------
# Set up logging
#------------------------------------------
if os.path.exists("./identityassignment.log"):
os.remove("./identityassignment.log")
logging.basicConfig(
filename="./identityassignment.log",
encoding="utf-8",
format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.DEBUG,
datefmt='%Y-%m-%d %H:%M:%S'
)
# --------------------------------------------------------------
# Application code
# --------------------------------------------------------------
# Load up the groups
CREDENTIALS_FILE = "./credentials.json"
CONFIGURATION_FILE = "./configuration.json"
config = "./groupconfiguration.json"
#------------------------------------------
# Ensure we have an identity to use, for
# this you need az login with rights to the
# sub you have configured.
#------------------------------------------
try:
AzLoginUtils.validate_login(CREDENTIALS_FILE)
except Exception as ex:
print(str(ex))
quit()
# Load configuration and create instance of identities and load groups
configuration = Configuration(CONFIGURATION_FILE)
az_identities = AzIdentities()
group_config = GroupConfiguration(az_identities, config)
# Get the users
user_assignments: typing.List[UserAssignment] = []
sub_role_summary = az_identities.get_role_summary(group_config.subscription)
for rs in sub_role_summary:
user_assignments.append(UserAssignment(az_identities, rs, sub_role_summary[rs]))
print("Separate users from all assignements")
users = [x for x in user_assignments if x.type == "User"]
# DEBUG
"""
roles = {}
for user in users:
for assignment in user.assignments:
if assignment["role"] not in roles:
roles[assignment["role"]] = {}
if assignment["scope"] not in roles[assignment["role"]]:
roles[assignment["role"]][assignment["scope"]] = 0
roles[assignment["role"]][assignment["scope"]] += 1
with open("assignments_by_scope.json", "w") as output_file:
output_file.writelines(json.dumps(roles, indent=4))
quit()
"""
# DEBUG
# For each role, get the users that might be associated with it
failed_loads: typing.Dict[str, typing.List[UserAssignment]] = {}
for role in group_config.roles:
failed_users = []
info_msg = "Check users for role definition : {}".format(role.name)
logging.info(info_msg)
print(info_msg)
current = 1
total_adds = 0
for user in users:
print(role.name, ": {} of {}".format(current, len(users)))
current += 1
if user.supports_role_definition(role.name, role.scopes):
if user.get_oid() is None:
failed_users.append(user)
else:
# role.add_user_to_role_group(user)
print("Add {} to {} with {}".format(user.name, role.name, role.scopes))
total_adds += 1
failed_users = [x.name for x in failed_users]
failed_user_message = "Role: {}\nFailed User:\n {}".format(
role.name,
json.dumps(failed_users, indent=4)
)
logging.info("Added {} users for role definition {} to groups".format(
total_adds,
role.name
))
logging.warning(failed_user_message)