forked from sysdiglabs/sysdig-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_team_mgmt_extended.py
executable file
·265 lines (215 loc) · 8.82 KB
/
user_team_mgmt_extended.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env python
#
# This example shows the different aspects of user/team management.
#
import sys
from sdcclient import SdcClient
#
# Parse arguments
#
if len(sys.argv) != 4:
print(('usage: %s <sysdig-token> team-prefix user-name' % sys.argv[0]))
print('You can find your token at https://app.sysdigcloud.com/#/settings/user')
sys.exit(1)
sdc_token = sys.argv[1]
#
# Instantiate the SDC client
#
sdclient = SdcClient(sdc_token, sdc_url='https://app.sysdigcloud.com')
team_prefix = sys.argv[2]
user_email_parts = sys.argv[3].split('@')
user_email_prefix = user_email_parts[0]
user_email_domain = user_email_parts[1]
#
# Create test users
#
# All users initially are part of default team.
#
admin = user_email_prefix + '+team_mgmt-admin' + '@' + user_email_domain
userA = user_email_prefix + '+team_mgmt-a' + '@' + user_email_domain
userB = user_email_prefix + '+team_mgmt-b' + '@' + user_email_domain
teamA = team_prefix + 'A'
teamB = team_prefix + 'B'
print('Creating test users...')
try:
ok, res = sdclient.create_user_invite(admin, first_name='TestUser', last_name='Admin', system_role='ROLE_CUSTOMER')
if not ok:
print(('-- User creation failed:', res, '. Exiting.'))
sys.exit(1)
else:
print(('-- User \'', admin, '\' created successfully.'))
ok, res = sdclient.create_user_invite(userA, first_name='TestUser', last_name='Alpha')
if not ok:
print(('-- User creation failed:', res, '. Exiting.'))
sys.exit(1)
else:
print(('-- User \'', userA, '\' created successfully.'))
ok, res = sdclient.create_user_invite(userB, first_name='TestUser', last_name='Beta')
if not ok:
print(('-- User creation failed:', res, '. Exiting.'))
sys.exit(1)
else:
print(('-- User \'', userB, '\' created successfully.'))
#
# Create test teams
#
# Possible failures on Team creation might include having reached the
# max limit on Teams for this customer account or if the Team by that
# name already exists. Since a previous successful run of this test
# would have deleted the Team by the same name, and we need to be able
# to configure Teams for this test to pass, we'll treat both types of
# error as a genuine fail of the test.
#
print('Creating test teams...')
ok, res = sdclient.create_team(teamA)
if not ok:
print(('-- Team creation failed:', res, '. Exiting.'))
sys.exit(1)
else:
print(('-- Team \'', teamA, '\' created successfully.'))
ok, res = sdclient.create_team(teamB)
if not ok:
print(('-- Team creation failed:', res, '. Exiting.'))
sys.exit(1)
else:
print(('-- Team \'', teamB, '\' created successfully.'))
#
# Membership manipulation
#
# Admins are part of all teams and their membership cannot be edited.
#
print('Membership manipulation...')
ok, res = sdclient.list_memberships(teamA)
if not ok:
print(('-- Unable to fetch team memberships:', res, '. Exiting.'))
sys.exit(1)
elif admin not in list(res.keys()):
print(('-- Admin should be part of all teams!', 'Exiting.'))
sys.exit(1)
elif userA in list(res.keys()) or userB in list(res.keys()):
print(('-- Users ', userA, ' and ', userB, ' should not be part of team ', teamA, '!', 'Exiting.'))
sys.exit(1)
ok, res = sdclient.list_memberships(teamB)
if not ok:
print(('-- Unable to fetch team memberships:', res, '. Exiting.'))
sys.exit(1)
elif admin not in list(res.keys()):
print(('-- Admin should be part of all teams!', 'Exiting.'))
sys.exit(1)
elif userA in list(res.keys()) or userB in list(res.keys()):
print(('-- Users ', userA, ' and ', userB, ' should not be part of team ', teamB, '!', 'Exiting.'))
sys.exit(1)
#
# Create team memberships
#
print('-- Create team memberships')
# Manipulate with teamA
ok, res = sdclient.save_memberships(teamA, {userA: 'ROLE_TEAM_EDIT'})
if not ok:
print(('-- Unable to add ', userA, ' to ', teamA, ' due to: ', res, '. Exiting.'))
sys.exit(1)
ok, res = sdclient.list_memberships(teamA)
if not ok:
print(('-- Unable to fetch team memberships:', res, '. Exiting.'))
sys.exit(1)
elif userA not in list(res.keys()) or admin not in list(res.keys()):
print(('-- Users ', userA, ' and ', admin, ' should be part of team ', teamA, '!', 'Exiting.'))
sys.exit(1)
# Manipulate with teamB
ok, res = sdclient.save_memberships(teamB, {userA: 'ROLE_TEAM_MANAGER', userB: 'ROLE_TEAM_READ'})
if not ok:
print(('-- Unable to add ', userA, ' and ', userB, ' to ', teamB, ' due to: ', res, '. Exiting.'))
sys.exit(1)
ok, res = sdclient.list_memberships(teamB)
if not ok:
print(('-- Unable to fetch team memberships:', res, '. Exiting.'))
sys.exit(1)
elif userA not in list(res.keys()) or userB not in list(res.keys()) or admin not in list(res.keys()):
print(('-- Users ', userA, ', ', userB, ' and ', admin, ' should be part of team ', teamB, '!', 'Exiting.'))
sys.exit(1)
# Update team memberships
print('-- Update team memberships')
# Add new or update existing memberships
ok, res = sdclient.save_memberships(teamA, {userA: 'ROLE_TEAM_READ', userB: 'ROLE_TEAM_EDIT'})
if not ok:
print(('-- Unable to modify membership for ', userA, ' and to add ', userB, ' to ', teamA, ' due to: ', res,
'. Exiting.'))
sys.exit(1)
ok, res = sdclient.list_memberships(teamA)
if not ok:
print(('-- Unable to fetch team memberships:', res, '. Exiting.'))
sys.exit(1)
elif userA not in list(res.keys()) or userB not in list(res.keys()) or admin not in list(res.keys()):
print(('-- Users ', userA, ', ', userB, ' and ', admin, ' should be part of team ', teamA, '!', 'Exiting.'))
sys.exit(1)
elif res[userA] != 'ROLE_TEAM_READ' or res[userB] != 'ROLE_TEAM_EDIT':
print(('-- Users ', userA, ' and ', userB, ' should have appropriate roles assigned for team ', teamA, '!',
'Exiting.'))
sys.exit(1)
# Remove team memberships
print('-- Remove team memberships')
ok, res = sdclient.remove_memberships(teamA, [userB])
if not ok:
print(('-- Unable to remove membership for ', userB, ' from team', teamA, ' due to: ', res, '. Exiting.'))
sys.exit(1)
ok, res = sdclient.list_memberships(teamA)
if not ok:
print(('-- Unable to fetch team memberships:', res, '. Exiting.'))
sys.exit(1)
elif userB in list(res.keys()):
print(('-- User ', userB, ' should not be part of team ', teamA, '!', 'Exiting.'))
sys.exit(1)
# Admin user cannot be removed from any team
ok, res = sdclient.remove_memberships(teamB, [admin, userA])
if not ok:
print(('-- Unable to remove membership for ', userB, ' from team', teamA, ' due to: ', res, '. Exiting.'))
sys.exit(1)
ok, res = sdclient.list_memberships(teamB)
if not ok:
print(('-- Unable to fetch team memberships:', res, '. Exiting.'))
sys.exit(1)
elif userA in list(res.keys()):
print(('-- User ', userA, ' should not be part of team ', teamB, '!', 'Exiting.'))
sys.exit(1)
elif admin not in list(res.keys()):
print(('-- User ', admin, ' should be always part of all teams!', 'Exiting.'))
sys.exit(1)
finally:
#
# Clean-up
#
print('Cleaning up...')
print('-- Deleting test teams.')
try:
ok, res = sdclient.delete_team(teamA)
if not ok:
print(('-- Team \'', teamA, '\' deletion failed: ', res))
except Exception as exception:
print(('-- Team \'', teamA, '\' deletion failed: ', exception))
try:
ok, res = sdclient.delete_team(teamB)
if not ok:
print(('-- Team \'', teamB, '\' deletion failed: ', res))
except Exception as exception:
print(('-- Team \'', teamB, '\' deletion failed: ', exception))
print('-- Deleting test users.')
try:
ok, res = sdclient.delete_user(admin)
if not ok:
print(('-- User \'', admin, '\' deletion failed: ', res))
except Exception as exception:
print(('-- User \'', admin, '\' deletion failed: ', exception))
try:
ok, res = sdclient.delete_user(userA)
if not ok:
print(('-- User \'', userA, '\' deletion failed: ', res))
except Exception as exception:
print(('-- User \'', userA, '\' deletion failed: ', exception))
try:
ok, res = sdclient.delete_user(userB)
if not ok:
print(('-- User \'', userB, '\' deletion failed: ', res))
except Exception as exception:
print(('-- User \'', userB, '\' deletion failed: ', exception))
print('All done successfully!!!')
sys.exit(0)