-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsa_maker.py
executable file
·507 lines (415 loc) · 19 KB
/
sa_maker.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#!/usr/bin/env python3
import json
import os
import sys
from copy import copy
import click
from loguru import logger
from utils import misc
from utils.google import Google
############################################################
# INIT
############################################################
# Globals
cfg = None
google = None
# Click
@click.group(help='service_account_maker')
@click.version_option('0.0.1', prog_name='service_account_maker')
@click.option('-v', '--verbose', count=True, default=0, help='Adjust the logging level')
@click.option(
'--config-path',
envvar='SA_MAKER_CONFIG_PATH',
type=click.Path(file_okay=True, dir_okay=False),
help='Configuration filepath',
show_default=True,
default=os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), "config.json")
)
@click.option(
'--log-path',
envvar='SA_MAKER_LOG_PATH',
type=click.Path(file_okay=True, dir_okay=False),
help='Log filepath',
show_default=True,
default=os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), "activity.log")
)
@click.option(
'--token-path',
envvar='SA_MAKER_TOKEN_PATH',
type=click.Path(file_okay=True, dir_okay=False),
help='Token filepath',
show_default=True,
default=os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), "token.json")
)
def app(verbose, config_path, log_path, token_path):
global cfg, google
# Ensure paths are full paths
if not config_path.startswith(os.path.sep):
config_path = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), config_path)
if not log_path.startswith(os.path.sep):
log_path = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), log_path)
if not token_path.startswith(os.path.sep):
token_path = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), token_path)
# Load config
from utils.config import Config
cfg = Config(config_path=config_path).cfg
# Load logger
log_levels = {0: 'INFO', 1: 'DEBUG', 2: 'TRACE'}
log_level = log_levels[verbose] if verbose in log_levels else 'TRACE'
config_logger = {
'handlers': [
{'sink': sys.stdout, 'backtrace': True if verbose >= 2 else False, 'level': log_level},
{'sink': log_path,
'rotation': '30 days',
'retention': '7 days',
'enqueue': True,
'backtrace': True if verbose >= 2 else False,
'level': log_level}
]
}
logger.configure(**config_logger)
# Load google
google = Google(cfg.client_id, cfg.client_secret, cfg.project_name, token_path)
# Display params
logger.info("%s = %r" % ("LOG_PATH".ljust(12), log_path))
logger.info("%s = %r" % ("LOG_LEVEL".ljust(12), log_level))
logger.info("")
return
############################################################
# CLICK FUNCTIONS
############################################################
@app.command(help='Authorize Google Account')
def authorize():
global google, cfg
logger.debug(f"client_id: {cfg.client_id!r}")
logger.debug(f"client_secret: {cfg.client_secret!r}")
# Provide authorization link
logger.info("Visit the link below and paste the authorization code")
logger.info(google.get_auth_link())
logger.info("Enter authorization code: ")
auth_code = input()
logger.debug(f"auth_code: {auth_code!r}")
# Exchange authorization code
token = google.exchange_code(auth_code)
if not token or 'access_token' not in token:
logger.error("Failed exchanging authorization code for an access token....")
sys.exit(1)
else:
logger.info(f"Exchanged authorization code for an access token:\n\n{json.dumps(token, indent=2)}\n")
sys.exit(0)
@app.command(help='Retrieve existing groups')
def list_groups():
global google, cfg
# retrieve groups
logger.info("Retrieving existing groups...")
success, groups = google.get_groups()
if success:
logger.info(f"Existing groups:\n{json.dumps(groups, indent=2)}")
sys.exit(0)
else:
logger.error(f"Failed to retrieve groups:\n{groups}")
sys.exit(1)
@app.command(help='Create group')
@click.option('--name', '-n', required=True, help='Name of the group')
@click.option('--domain', '-d', required=True, help='Domain of the G Suite account')
def create_group(name, domain):
global google, cfg
# create group
logger.info(f"Creating group named: {name} - {name}@{domain}")
success, group = google.create_group(name, domain)
if success:
logger.info(f"Created group {name!r}:\n{group}")
sys.exit(0)
else:
logger.error(f"Failed to create group {name!r}:\n{group}")
sys.exit(1)
@app.command(help='Remove a group')
@click.option('--name', '-n', required=True, help='Name of the group')
@click.option('--domain', '-d', required=True, help='Domain of the G Suite account')
def remove_group(name, domain):
global google, cfg
# retrieve group id
success, groups = google.get_groups()
if not success:
logger.error(f"Unable to retrieve existing groups:\n{groups}")
sys.exit(1)
group_id = misc.get_group_id(groups, name, f'{name}@{domain}')
if not group_id:
logger.error(f"Failed to determine group_id of group with name {name!r}")
sys.exit(1)
# remove group
logger.info(f"Removing group: {name} - {name}@{domain}")
success, resp = google.delete_group(group_id)
if success:
logger.info(f"Deleted group!")
sys.exit(0)
else:
logger.error(f"Failed removing group {name!r} - {name}@{domain}:\n{resp}")
sys.exit(1)
@app.command(help='Set users for a group')
@click.option('--name', '-n', required=True, help='Name of the existing group')
@click.option('--key-prefix', '-k', required=True, help='Name prefix of service accounts')
def set_group_users(name, key_prefix):
global google, cfg
# validate the service key folder exists
service_key_folder = os.path.join(cfg.service_account_folder, key_prefix)
if not os.path.exists(service_key_folder):
logger.error(f"The service key folder did not exist at: {service_key_folder}")
sys.exit(1)
# retrieve service key users to share teamdrive access with
service_key_users = misc.get_service_account_users(service_key_folder)
if service_key_users is None:
logger.error(f"Failed to determine the service key user(s) to add to group: {name}")
sys.exit(1)
# retrieve group id
success, groups = google.get_groups()
if not success:
logger.error(f"Unable to retrieve existing groups:\n{groups}")
sys.exit(1)
group_id = misc.get_group_id(groups, name)
if not group_id:
logger.error(f"Failed to determine group_id of group with name {name!r}")
sys.exit(1)
# retrieve group members
success, group_members = google.get_group_users(group_id)
if not success:
logger.error(f"Failed retrieving users in group with name {name!r}:\n{group_members}")
sys.exit(1)
# remove users that are already a member
if 'members' in group_members:
for member in group_members['members']:
if member['email'] in service_key_users:
service_key_users.remove(member['email'])
if not len(service_key_users):
logger.info(f"There were no service key users to add to group with name {name!r}")
sys.exit(0)
# add user to group
logger.info(
f"Adding {len(service_key_users)} users to {name!r} group, user(s): {service_key_users}")
for service_key_user in service_key_users:
success, resp = google.set_group_user(group_id, service_key_user)
if success:
logger.info(f"Added user to {name!r} group: {service_key_user}")
else:
logger.error(f"Failed adding user to {name!r} group for user {service_key_user!r}:\n{resp}")
sys.exit(1)
sys.exit(0)
@app.command(help='Lists users for a group')
@click.option('--name', '-n', required=True, help='Name of the group')
def list_group_users(name):
global google, cfg
# retrieve the group id
success, groups = google.get_groups()
if not success:
logger.error(f"Unable to retrieve existing groups:\n{groups}")
sys.exit(1)
group_id = misc.get_group_id(groups, name)
if not group_id:
logger.error(f"Failed to determine group_id of group with name {name!r}")
sys.exit(1)
# get group members
success, group_members = google.get_group_users(group_id)
if success:
logger.info(f"Existing users on group {name!r}:\n{json.dumps(group_members, indent=2)}")
sys.exit(0)
else:
logger.error(f"Failed retrieving users in group with name {name!r}:\n{group_members}")
sys.exit(1)
@app.command(help='Retrieve existing service accounts')
def list_accounts():
global google, cfg
# retrieve service accounts
logger.info("Retrieving existing service accounts...")
success, service_accounts = google.get_service_accounts()
if success:
logger.info(f"Existing service accounts:\n{json.dumps(service_accounts, indent=2)}")
sys.exit(0)
else:
logger.error(f"Failed to retrieve service accounts:\n{service_accounts}")
sys.exit(1)
@app.command(help='Create service accounts')
@click.option('--name', '-n', required=True, help='Name prefix for service accounts')
@click.option('--amount', '-a', default=1, required=False, help='Amount of service accounts to create')
def create_accounts(name, amount=1):
global google, cfg
service_key_folder = os.path.join(cfg.service_account_folder, name)
# does service key subfolder exist?
if not os.path.exists(service_key_folder):
logger.debug(f"Creating service key path: {service_key_folder!r}")
if os.makedirs(service_key_folder, exist_ok=True):
logger.info(f"Created service key path: {service_key_folder!r}")
# count amount of service files that exist already in this folder
starting_account_number = misc.get_starting_account_number(service_key_folder)
if not starting_account_number:
logger.error(f"Failed to determining the account number to start from....")
sys.exit(1)
for account_number in range(starting_account_number, starting_account_number + amount):
account_name = f'{name}{account_number:06d}'
# create the service account
success, service_account = google.create_service_account(account_name)
if success and (isinstance(service_account, dict) and 'email' in service_account and
'uniqueId' in service_account):
account_email = service_account['email']
logger.info(f"Created service account: {account_email!r}")
# create key for new service account
success, service_key = google.create_service_account_key(account_email)
if success and (isinstance(service_key, dict) and 'privateKeyData' in service_key):
service_key_path = os.path.join(service_key_folder, f'{account_number}.json')
if misc.dump_service_file(service_key_path, service_key):
logger.info(f"Created service key for account {account_email!r}: {service_key_path}")
else:
logger.error(f"Created service key for account, but failed to dump it to: {service_key_path}")
sys.exit(1)
else:
logger.error(f"Failed to create service key for account {account_email!r}:\n{service_key}\n")
sys.exit(1)
else:
logger.error(f"Failed to create service account {account_name!r}:\n{service_account}\n")
sys.exit(1)
@app.command(help='Retrieve existing teamdrives')
def list_teamdrives():
global google, cfg
success, teamdrives = google.get_teamdrives()
if success:
logger.info(f'Existing teamdrives:\n{json.dumps(teamdrives, indent=2)}')
sys.exit(0)
else:
logger.error(f'Failed to retrieve teamdrives:\n{teamdrives}')
sys.exit(1)
@app.command(help='Create teamdrive')
@click.option('--name', '-n', required=True, help='Name of the new teamdrive')
def create_teamdrive(name):
global google, cfg
success, teamdrive = google.create_teamdrive(name)
if success:
logger.info(f'Created teamdrive {name!r}:\n{teamdrive}')
sys.exit(0)
else:
logger.error(f'Failed to create teamdrive {name!r}:\n{teamdrive}')
sys.exit(1)
@app.command(help='Set users for a teamdrive')
@click.option('--name', '-n', required=True, help='Name of the existing teamdrive')
@click.option('--key-prefix', '-k', required=True, help='Name prefix of service accounts')
def set_teamdrive_users(name, key_prefix):
global google, cfg
# validate the service key folder exists
service_key_folder = os.path.join(cfg.service_account_folder, key_prefix)
if not os.path.exists(service_key_folder):
logger.error(f"The service key folder did not exist at: {service_key_folder}")
sys.exit(1)
# retrieve service key users to share teamdrive access with
service_key_users = misc.get_service_account_users(service_key_folder)
if service_key_users is None:
logger.error(f"Failed to determine the service key user(s) to share with teamdrive: {name}")
sys.exit(1)
# retrieve teamdrive id
success, teamdrives = google.get_teamdrives()
if not success:
logger.error(f"Unable to retrieve existing teamdrives:\n{teamdrives}")
sys.exit(1)
teamdrive_id = misc.get_teamdrive_id(teamdrives, name)
if not teamdrive_id:
logger.error(f"Failed to determine teamdrive_id of teamdrive with name {name!r}")
sys.exit(1)
logger.info(
f"Sharing access to {name!r} teamdrive for {len(service_key_users)} service key user(s): {service_key_users}")
# share access to teamdrive
for service_key_user in service_key_users:
success, resp = google.set_teamdrive_share_user(teamdrive_id, service_key_user)
if success:
logger.info(f"Shared access to {name!r} teamdrive for user: {service_key_user}")
else:
logger.error(f"Failed sharing access to {name!r} teamdrive for user {service_key_user!r}:\n{resp}")
sys.exit(1)
sys.exit(0)
@app.command(help='Lists users for a teamdrive')
@click.option('--name', '-n', required=True, help='Name of the teamdrive')
def list_teamdrive_users(name):
global google, cfg
# retrieve the teamdrive id
success, teamdrives = google.get_teamdrives()
if not success:
logger.error(f"Unable to retrieve existing teamdrives:\n{teamdrives}")
sys.exit(1)
teamdrive_id = misc.get_teamdrive_id(teamdrives, name)
if not teamdrive_id:
logger.error(f"Failed to determine teamdrive_id of teamdrive with name {name!r}")
sys.exit(1)
# get permissions (users) on the teamdrive
success, teamdrive_permissions = google.get_teamdrive_permissions(teamdrive_id)
if not success:
logger.error(f"Unable to retrieve existing permissions on teamdrive {name!r}:\n{teamdrive_permissions}")
sys.exit(1)
elif 'permissions' not in teamdrive_permissions:
logger.error(
f"Unexpected response when retrieving existing permission(s) on teamdrive {name!r}:\n"
f"{teamdrive_permissions}")
sys.exit(1)
# remove permissions that are already deleted
for permission in copy(teamdrive_permissions['permissions']):
if permission['deleted']:
# this permission is already deleted, lets remove it
teamdrive_permissions['permissions'].remove(permission)
logger.info(f"Existing users on teamdrive {name!r}:\n{json.dumps(teamdrive_permissions, indent=2)}")
sys.exit(0)
@app.command(help='Remove users from a teamdrive')
@click.option('--name', '-n', required=True, help='Name of the teamdrive')
@click.option('--email', '-e', required=False, default='ALL', show_default=True, help='Email of user to remove')
@click.option('--keep-emails', '-k', required=False, multiple=True, help='Email of users to keep')
@click.option('--service-accounts-only', '-sao', is_flag=True, required=False, default=False, show_default=True,
help='Only remove service accounts')
def remove_teamdrive_users(name, email, keep_emails, service_accounts_only):
global google, cfg
if email == 'ALL' and (not len(keep_emails) and not service_accounts_only):
logger.error(f"You must specify an email to keep when removing access to all users (your teamdrive owner)")
sys.exit(1)
# retrieve the teamdrive id
success, teamdrives = google.get_teamdrives()
if not success:
logger.error(f"Unable to retrieve existing teamdrives:\n{teamdrives}")
sys.exit(1)
teamdrive_id = misc.get_teamdrive_id(teamdrives, name)
if not teamdrive_id:
logger.error(f"Failed to determine teamdrive_id of teamdrive with name {name!r}")
sys.exit(1)
# get permissions (users) on the teamdrive
success, teamdrive_permissions = google.get_teamdrive_permissions(teamdrive_id)
if not success:
logger.error(f"Unable to retrieve existing permissions on teamdrive {name!r}:\n{teamdrive_permissions}")
sys.exit(1)
elif 'permissions' not in teamdrive_permissions:
logger.error(
f"Unexpected response when retrieving existing permission(s) on teamdrive {name!r}:\n"
f"{teamdrive_permissions}")
sys.exit(1)
# remove permissions that are already deleted
for permission in copy(teamdrive_permissions['permissions']):
if permission['deleted']:
# this permission is already deleted, lets remove it
teamdrive_permissions['permissions'].remove(permission)
logger.info(f"Found {len(teamdrive_permissions['permissions'])} permissions for teamdrive {name!r}")
# loop emails removing their access
for permission in teamdrive_permissions['permissions']:
# only go further (remove a user) if email was not supplied, or this permission email matches
if email != 'ALL' and permission['emailAddress'].lower() != email.lower():
continue
# is this in service accounts only mode
if service_accounts_only and not permission['emailAddress'].lower().endswith('.iam.gserviceaccount.com'):
continue
# is this a safe email?
if misc.is_safe_email(keep_emails, permission['emailAddress']):
logger.info(f"Keeping permissions on teamdrive {name!r} for: {permission['emailAddress']}")
continue
success, resp = google.delete_teamdrive_share_user(teamdrive_id, permission['id'])
if success:
logger.info(f"Removed permissions on teamdrive {name!r} for: {permission['emailAddress']}")
else:
logger.error(f"Unexpected response when removing permissions on teamdrive {name!r} for "
f"{permission['emailAddress']!r}:\n{resp}")
sys.exit(1)
sys.exit(0)
############################################################
# MAIN
############################################################
if __name__ == "__main__":
app()