-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathusers.py
More file actions
587 lines (501 loc) · 16.7 KB
/
users.py
File metadata and controls
587 lines (501 loc) · 16.7 KB
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
from functools import lru_cache
from pathlib import Path
from typing import Optional
import click
from boltons.iterutils import bucketize
from pydantic import Field
from requests import HTTPError
from rich.panel import Panel
from rich.progress import track
from _incydr_cli import console
from _incydr_cli import logging_options
from _incydr_cli import render
from _incydr_cli.cmds.models import UserCSV
from _incydr_cli.cmds.models import UserJSON
from _incydr_cli.cmds.options.output_options import columns_option
from _incydr_cli.cmds.options.output_options import input_format_option
from _incydr_cli.cmds.options.output_options import single_format_option
from _incydr_cli.cmds.options.output_options import SingleFormat
from _incydr_cli.cmds.options.output_options import table_format_option
from _incydr_cli.cmds.options.output_options import TableFormat
from _incydr_cli.cmds.options.utils import user_lookup_callback
from _incydr_cli.cmds.utils import deprecation_warning
from _incydr_cli.cmds.utils import user_lookup
from _incydr_cli.core import IncydrCommand
from _incydr_cli.core import IncydrGroup
from _incydr_cli.file_readers import AutoDecodedFile
from _incydr_sdk.agents.models import Agent
from _incydr_sdk.core.client import Client
from _incydr_sdk.devices.models import Device
from _incydr_sdk.users.client import RoleNotFoundError
from _incydr_sdk.users.client import RoleProcessingError
from _incydr_sdk.users.client import UserNotAssignedRoleError
from _incydr_sdk.users.models import Role
from _incydr_sdk.users.models import User
from _incydr_sdk.users.models import UserRole
from _incydr_sdk.utils import model_as_card
user_arg = click.argument("user", callback=user_lookup_callback)
@click.group(cls=IncydrGroup)
@logging_options
def users():
"""View and manage users and user roles."""
@users.command("list", cls=IncydrCommand)
@click.option(
"--active/--inactive",
default=None,
help="Filter by active or inactive users. Defaults to returning both when when neither option is passed.",
)
@click.option(
"--blocked/--unblocked",
default=None,
help="Filter by blocked or unblocked users. Defaults to returning both when when neither option is passed.",
)
@click.option("--username", default=None)
@table_format_option
@columns_option
@logging_options
def list_(
active: Optional[bool],
blocked: Optional[bool],
username: Optional[str],
format_: TableFormat,
columns: Optional[str],
):
"""
List users.
"""
client = Client()
users_ = client.users.v1.iter_all(active=active, blocked=blocked, username=username)
if format_ == TableFormat.csv:
render.csv(User, users_, columns=columns, flat=True)
elif format_ == TableFormat.table:
columns = columns or [
"user_id",
"username",
"first_name",
"last_name",
"org_guid",
"org_name",
"notes",
"active",
"blocked",
]
render.table(User, users_, columns=columns, flat=False)
else:
printed = False
if format_ == TableFormat.json_pretty:
for item in users_:
printed = True
console.print_json(item.json())
else:
for item in users_:
printed = True
click.echo(item.json())
if not printed:
console.print("No results found.")
@users.command(cls=IncydrCommand)
@click.argument("user") # don't need username callback, handled by client method
@single_format_option
@logging_options
def show(
user,
format_: SingleFormat,
):
"""
Show details for a user.
Accepts a user ID or a username. Performs an additional lookup if username is passed.
"""
client = Client()
user = client.users.v1.get_user(user)
if format_ == SingleFormat.rich and client.settings.use_rich:
console.print(Panel.fit(model_as_card(user), title=f"User {user.username}"))
elif format_ == SingleFormat.json_pretty:
console.print_json(user.json())
else:
click.echo(user.json())
@users.command(cls=IncydrCommand)
@user_arg
@table_format_option
@columns_option
@logging_options
def list_devices(
user,
format_: TableFormat,
columns: Optional[str],
):
"""
DEPRECATED - use list-agents instead.
"""
deprecation_warning("DEPRECATED. Use list-agents instead.")
client = Client()
devices = client.users.v1.get_devices(user).devices
if format_ == TableFormat.csv:
render.csv(Device, devices, columns=columns, flat=True)
elif format_ == TableFormat.table:
columns = columns or [
"device_id",
"name",
"os_hostname",
"status",
"active",
"blocked",
"alert_state",
"org_guid",
"login_date",
]
render.table(Device, devices, columns=columns, flat=False)
elif format_ == TableFormat.json_pretty:
for item in devices:
console.print_json(item.json())
else:
for item in devices:
click.echo(item.json())
@users.command(cls=IncydrCommand)
@user_arg
@table_format_option
@columns_option
@logging_options
def list_agents(
user,
format_: TableFormat,
columns: Optional[str],
):
"""
List agents associated with a particular user.
"""
client = Client()
if "@" in user:
user = client.users.v1.get_user(user).user_id
devices = list(client.agents.v1.iter_all(user_id=user))
if format_ == TableFormat.csv:
render.csv(Agent, devices, columns=columns, flat=True)
elif format_ == TableFormat.table:
columns = columns or [
"agent_id",
"name",
"os_hostname",
"active",
"agent_type",
"agent_health_issue_types",
"last_connected",
]
render.table(Agent, devices, columns=columns, flat=False)
elif format_ == TableFormat.json_pretty:
for item in devices:
console.print_json(item.json())
else:
for item in devices:
click.echo(item.json())
@users.command("list-roles", cls=IncydrCommand)
@user_arg
@table_format_option
@columns_option
@logging_options
def list_user_roles(
user: str,
format_: TableFormat,
columns: Optional[str],
):
"""
List roles associated with a particular user.
"""
client = Client()
roles = client.users.v1.list_user_roles(user)
if format_ == TableFormat.csv:
render.csv(UserRole, roles, columns=columns, flat=True)
elif format_ == TableFormat.table:
render.table(UserRole, roles, columns=columns, flat=False)
elif format_ == TableFormat.json_pretty:
for item in roles:
console.print_json(item.json())
else:
for item in roles:
click.echo(item.json())
@users.command(cls=IncydrCommand)
@user_arg
@click.argument("roles")
@click.option("--add", "update_method", flag_value="add", default=None)
@click.option("--remove", "update_method", flag_value="remove", default=None)
@logging_options
def update_roles(
user,
roles: str,
update_method: str,
):
"""
Update roles associated with a particular user.
Usage:
users update-roles USER ROLES
USER is the user ID or username of the user whose roles will be updated.
Performs an additional lookup if username is passed.
ROLES is a comma-delimited list of role IDs and/or role names to replace that user's roles.
Use the `--remove` flag to remove the specified role(s) from a user's existing roles.
Alternatively, use the `--add` flag to assign additional roles to a user's existing roles.
"""
client = Client()
if update_method == "add":
update_func = client.users.v1.add_roles
elif update_method == "remove":
update_func = client.users.v1.remove_roles
else:
update_func = client.users.v1.update_roles
try:
update_func(user, [r.strip() for r in roles.split(",")])
except RoleNotFoundError as e:
raise e
except UserNotAssignedRoleError as e:
raise e
console.print(f"Roles successfully updated for user '{user}'")
@users.command(cls=IncydrCommand)
@user_arg
@logging_options
def activate(
user,
):
"""
Activate a user.
"""
client = Client()
client.users.v1.activate(user)
console.print(f"User '{user}' successfully activated.")
@users.command(cls=IncydrCommand)
@user_arg
@logging_options
def deactivate(
user,
):
"""
Deactivate a user.
"""
client = Client()
client.users.v1.deactivate(user)
console.print(f"User '{user}' successfully deactivated.")
@users.command(cls=IncydrCommand)
@user_arg
@click.argument("org_guid")
@logging_options
def move(
user,
org_guid,
):
"""
Move a user to a specified organization.
"""
client = Client()
client.users.v1.move(user, org_guid)
console.print(f"User '{user}' successfully moved to org '{org_guid}'.")
@users.group("roles", cls=IncydrGroup)
def roles_():
"""View available roles."""
@roles_.command("show", cls=IncydrCommand)
@single_format_option
@click.argument("role")
@logging_options
def show_role(
role,
format_: SingleFormat,
):
"""
Show details for a single role, specified by role name or role ID.
"""
client = Client()
role = client.users.v1.get_role(role)
if format_ == SingleFormat.rich and client.settings.use_rich:
console.print(Panel.fit(model_as_card(role), title=role.role_name))
elif format_ == SingleFormat.json_pretty:
console.print_json(role.json())
else:
click.echo(role.json())
@roles_.command("list", cls=IncydrCommand)
@table_format_option
@logging_options
def list_roles(
format_: TableFormat,
):
"""
List all available roles that can be assigned by the current user.
"""
client = Client()
roles = client.users.v1.list_roles()
if format_ == TableFormat.csv:
render.csv(Role, roles, flat=True)
elif format_ == TableFormat.table:
render.table(Role, roles, flat=False)
elif format_ == TableFormat.json_pretty:
for item in roles:
console.print_json(item.json())
else:
for item in roles:
click.echo(item.json())
@users.command(cls=IncydrCommand)
@click.argument("file", type=AutoDecodedFile())
@input_format_option
@click.option("--add", "update_method", flag_value="add", default=None)
@click.option("--remove", "update_method", flag_value="remove", default=None)
@logging_options
def bulk_update_roles(
file,
update_method: str,
format_: Optional[str],
):
"""
Bulk update roles associated with multiple users from a file.
By default, the provided roles will replace the specified user's existing roles.
Use the `--add` flag or the `--remove` flag to add or remove roles, respectively, from a user's existing roles.
Takes a single arg `FILE` which specifies the path to the file (use "-" to read from stdin).
File format can either be CSV or [JSON Lines format](https://jsonlines.org) (Default is CSV).
Requires the following CSV columns or JSON keys:
* `user` - User ID or username of the user whose roles will be updated. Performs an additional lookup if username is passed.
* `role` - Role ID and/or role name (case-sensitive) to assign to the new user.
Example:
The following command will add roles to users as specified in a JSON-lines formatted file:
incydr users bulk-update-roles path/to/file.json --add --format json-lines
"""
client = Client()
@lru_cache()
def resolve_username(user):
if user is None:
return
elif "@" in user:
return user_lookup(client, user)
else: # assume user_id
return user
class RoleUpdateCSV(UserCSV):
role: str = Field(
csv_aliases=["role", "role_id", "role_name", "roleId", "roleName"]
)
class RoleUpdateJSON(UserJSON):
role: str = Field(alias="roleId")
if format_ == "csv":
roles_file = RoleUpdateCSV.parse_csv(file)
else:
roles_file = RoleUpdateJSON.parse_json_lines(file)
if update_method == "add":
update_func = client.users.v1.add_roles
elif update_method == "remove":
update_func = client.users.v1.remove_roles
else:
update_func = client.users.v1.update_roles
# group updates by user
buckets = bucketize(
roles_file,
key=lambda role_update: role_update.user,
value_transform=lambda role_update: role_update.role,
)
for bucket in buckets:
user = bucket
roles = buckets[bucket]
try:
user = resolve_username(user)
update_func(user, roles)
console.print(f"Successfully updated roles for user {user}.'")
except RoleProcessingError as err:
console.print(
f"[red]Error! {str(err)} [/red]",
highlight=False,
)
@users.command(cls=IncydrCommand)
@click.argument("file", type=click.File())
@input_format_option
@logging_options
def bulk_activate(file: Path, format_: str):
"""
Bulk activate users.
Takes a single arg `FILE` which specifies the path to the file (use "-" to read from stdin).
File format can either be CSV or [JSON Lines format](https://jsonlines.org) (Default is CSV).
Requires a single `user` column or field that contains either the user IDs or the usernames of the users to be activated.
"""
client = Client()
@lru_cache()
def resolve_username(user):
if user is None:
return
elif "@" in user:
return user_lookup(client, user)
else: # assume user_id
return user
if format_ == "csv":
models = UserCSV.parse_csv(file)
else:
models = UserJSON.parse_json_lines(file)
try:
for row in track(
models,
description="Activating users...",
transient=True,
):
client.users.v1.activate(resolve_username(row.user))
except HTTPError as err:
console.print(f"[red]Error:[/red] {err.response.text}")
@users.command(cls=IncydrCommand)
@click.argument("file", type=click.File())
@input_format_option
@logging_options
def bulk_deactivate(file: Path, format_: str):
"""
Bulk deactivate users.
Takes a single arg `FILE` which specifies the path to the file (use "-" to read from stdin).
File format can either be CSV or [JSON Lines format](https://jsonlines.org) (Default is CSV).
Requires a single `user` column or field that contains either the user IDs or the usernames of the users to be deactivated.
"""
client = Client()
@lru_cache()
def resolve_username(user):
if user is None:
return
elif "@" in user:
return user_lookup(client, user)
else: # assume user_id
return user
if format_ == "csv":
models = UserCSV.parse_csv(file)
else:
models = UserJSON.parse_json_lines(file)
try:
for row in track(
models,
description="Deactivating users...",
transient=True,
):
client.users.v1.deactivate(resolve_username(row.user))
except HTTPError as err:
console.print(f"[red]Error:[/red] {err.response.text}")
@users.command(cls=IncydrCommand)
@click.argument("file", type=click.File())
@input_format_option
@logging_options
def bulk_move(file: Path, format_: str):
"""
Bulk move multiple users to specified organizations.
Takes a single arg `FILE` which specifies the path to the file (use "-" to read from stdin).
File format can either be CSV or [JSON Lines format](https://jsonlines.org) (Default is CSV).
Requires the following columns:
* `user` - User ID or username of the user who will be moved to the new organization. Performs an additional lookup if username is passed.
* `org_guid` - GUID for the user's new organization.
"""
client = Client()
class UserMoveCSV(UserCSV):
org_guid: str = Field(csv_aliases=["org_guid", "orgGuid", "org"])
class UserMoveJSON(UserJSON):
org_guid: str = Field(alias="orgGuid")
@lru_cache()
def resolve_username(user):
if user is None:
return
elif "@" in user:
return user_lookup(client, user)
else: # assume user_id
return user
if format_ == "csv":
models = UserMoveCSV.parse_csv(file)
else:
models = UserMoveJSON.parse_json_lines(file)
try:
for row in track(
models,
description="Moving users...",
transient=True,
):
client.users.v1.move(resolve_username(row.user), row.org_guid)
except HTTPError as err:
console.print(f"[red]Error:[/red] {err.response.text}")