|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | + |
| 4 | +import sys |
| 5 | +from cbapi.response.models import User, Team, SensorGroup |
| 6 | +from cbapi.example_helpers import build_cli_parser, get_cb_response_object, get_object_by_name_or_id |
| 7 | +from cbapi.errors import ServerError |
| 8 | +import logging |
| 9 | +import getpass |
| 10 | +from cbapi.response.rest_api import get_api_token |
| 11 | + |
| 12 | + |
| 13 | +log = logging.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +def list_users(cb, parser, args): |
| 17 | + format_string = "{:16s} {:30s} {:30s} {:40s}" |
| 18 | + print(format_string.format("Username", "Name", "Email address", "Teams")) |
| 19 | + print(format_string.format("-"*16, "-"*30, "-"*30, "-"*40)) |
| 20 | + |
| 21 | + for u in cb.select(User): |
| 22 | + print(format_string.format(u.username, " ".join([u.first_name, u.last_name]), u.email, |
| 23 | + ", ".join([t['name'] for t in u.teams]))) |
| 24 | + |
| 25 | + |
| 26 | +def list_teams(cb, parser, args): |
| 27 | + for t in cb.select(Team): |
| 28 | + print("Team {0} (id {1}):".format(t.name, t.id)) |
| 29 | + for ga in t.group_access: |
| 30 | + print(" {0} on Sensor Group \"{1}\"".format(ga["access_category"], ga["group_name"])) |
| 31 | + |
| 32 | + |
| 33 | +def add_team(cb, parser, args): |
| 34 | + t = cb.create(Team) |
| 35 | + t.name = args.name |
| 36 | + |
| 37 | + for sg in args.administrator or []: |
| 38 | + if isinstance(t, int): |
| 39 | + sg = cb.select(SensorGroup, sg) |
| 40 | + else: |
| 41 | + sg = cb.select(SensorGroup).where("name:{0}".format(sg)).first() |
| 42 | + |
| 43 | + t.add_administrator_access(sg) |
| 44 | + |
| 45 | + for sg in args.viewer or []: |
| 46 | + if isinstance(t, int): |
| 47 | + sg = cb.select(SensorGroup, sg) |
| 48 | + else: |
| 49 | + sg = cb.select(SensorGroup).where("name:{0}".format(sg)).first() |
| 50 | + |
| 51 | + t.add_viewer_access(sg) |
| 52 | + |
| 53 | + try: |
| 54 | + t.save() |
| 55 | + except ServerError as se: |
| 56 | + print("Could not add team: {0:s}".format(str(se))) |
| 57 | + except Exception as e: |
| 58 | + print("Could not add team: {0:s}".format(str(e))) |
| 59 | + else: |
| 60 | + log.debug("team data: {0:s}".format(str(t))) |
| 61 | + print("Added team {0}.".format(t.name)) |
| 62 | + |
| 63 | + |
| 64 | +def add_user(cb, parser, args): |
| 65 | + u = cb.create(User) |
| 66 | + u.username = args.username |
| 67 | + u.first_name = args.first_name |
| 68 | + u.last_name = args.last_name |
| 69 | + u.email = args.email |
| 70 | + u.teams = [] |
| 71 | + u.global_admin = args.global_admin |
| 72 | + |
| 73 | + log.debug("Adding user: {0:s}".format(u.username)) |
| 74 | + |
| 75 | + if not args.password: |
| 76 | + passwords_dont_match = True |
| 77 | + while passwords_dont_match: |
| 78 | + pw1 = getpass.getpass("New password for {0}: ".format(u.username)) |
| 79 | + pw2 = getpass.getpass("Re-enter password: ") |
| 80 | + if pw1 == pw2: |
| 81 | + passwords_dont_match = False |
| 82 | + else: |
| 83 | + print("Passwords don't match; try again") |
| 84 | + |
| 85 | + u.password = pw1 |
| 86 | + else: |
| 87 | + u.password = args.password |
| 88 | + |
| 89 | + for t in args.team or []: |
| 90 | + if isinstance(t, int): |
| 91 | + t = cb.select(Team, t) |
| 92 | + else: |
| 93 | + t = cb.select(Team).where("name:{0}".format(t)).first() |
| 94 | + |
| 95 | + u.add_team(t) |
| 96 | + |
| 97 | + try: |
| 98 | + u.save() |
| 99 | + except ServerError as se: |
| 100 | + print("Could not add user: {0:s}".format(str(se))) |
| 101 | + except Exception as e: |
| 102 | + print("Could not add user: {0:s}".format(str(e))) |
| 103 | + else: |
| 104 | + log.debug("user data: {0:s}".format(str(u))) |
| 105 | + print("Added user {0}.".format(u.username)) |
| 106 | + |
| 107 | + |
| 108 | +def get_api_key(cb, parser, args): |
| 109 | + if not args.password: |
| 110 | + password = getpass.getpass("Password for {0}: ".format(args.username)) |
| 111 | + else: |
| 112 | + password = args.password |
| 113 | + |
| 114 | + print("API token for user {0}: {1}".format(args.username, get_api_token(cb.credentials.url, |
| 115 | + args.username, password, |
| 116 | + verify=cb.credentials.ssl_verify))) |
| 117 | + |
| 118 | + |
| 119 | +def delete_user(cb, parser, args): |
| 120 | + user = cb.select(User, args.username) |
| 121 | + try: |
| 122 | + user.delete() |
| 123 | + except Exception as e: |
| 124 | + print("Could not delete user {0:s}: {1:s}".format(args.username, str(e))) |
| 125 | + else: |
| 126 | + print("Deleted user {0:s}".format(args.username)) |
| 127 | + |
| 128 | + |
| 129 | +def main(): |
| 130 | + parser = build_cli_parser() |
| 131 | + commands = parser.add_subparsers(help="User commands", dest="command_name") |
| 132 | + |
| 133 | + list_command = commands.add_parser("list", help="List all configured users") |
| 134 | + list_teams_command = commands.add_parser("list-teams", help="List all configured user teams") |
| 135 | + |
| 136 | + add_command = commands.add_parser("add", help="Add new user") |
| 137 | + add_command.add_argument("-u", "--username", help="New user's username", required=True) |
| 138 | + add_command.add_argument("-f", "--first-name", help="First name", required=True) |
| 139 | + add_command.add_argument("-l", "--last-name", help="Last name", required=True) |
| 140 | + add_command.add_argument("-p", "--password", help="Password - if not specified, prompt at runtime", required=False) |
| 141 | + add_command.add_argument("-e", "--email", help="Email address", required=True) |
| 142 | + add_command.add_argument("-A", "--global-admin", help="Make new user global admin", default=False, |
| 143 | + action="store_true") |
| 144 | + add_command.add_argument("-t", "--team", help="Add new user to this team (can specify multiple teams)", |
| 145 | + action="append", metavar="TEAM-NAME") |
| 146 | + |
| 147 | + add_team_command = commands.add_parser("add-team", help="Add new team") |
| 148 | + add_team_command.add_argument("-N", "--name", help="Name of the new team") |
| 149 | + add_team_command.add_argument("-A", "--administrator", help="Add administrator rights to the given sensor group", |
| 150 | + metavar="SENSOR-GROUP", action="append") |
| 151 | + add_team_command.add_argument("-V", "--viewer", help="Add viewer rights to the given sensor group", |
| 152 | + metavar="SENSOR-GROUP", action="append") |
| 153 | + |
| 154 | + get_api_key_command = commands.add_parser("get-api-key", help="Get API key for user") |
| 155 | + get_api_key_command.add_argument("-u", "--username", help="Username", required=True) |
| 156 | + get_api_key_command.add_argument("-p", "--password", help="Password - if not specified, prompt at runtime", |
| 157 | + required=False) |
| 158 | + |
| 159 | + del_command = commands.add_parser("delete", help="Delete user") |
| 160 | + del_user_specifier = del_command.add_mutually_exclusive_group(required=True) |
| 161 | + del_user_specifier.add_argument("-u", "--username", help="Name of user to delete.") |
| 162 | + |
| 163 | + args = parser.parse_args() |
| 164 | + cb = get_cb_response_object(args) |
| 165 | + |
| 166 | + if args.command_name == "list": |
| 167 | + return list_users(cb, parser, args) |
| 168 | + elif args.command_name == "list-teams": |
| 169 | + return list_teams(cb, parser, args) |
| 170 | + elif args.command_name == "get-api-key": |
| 171 | + return get_api_key(cb, parser, args) |
| 172 | + elif args.command_name == "add": |
| 173 | + return add_user(cb, parser, args) |
| 174 | + elif args.command_name == "add-team": |
| 175 | + return add_team(cb, parser, args) |
| 176 | + elif args.command_name == "delete": |
| 177 | + return delete_user(cb, parser, args) |
| 178 | + |
| 179 | + |
| 180 | +if __name__ == "__main__": |
| 181 | + sys.exit(main()) |
0 commit comments