def _edit(user: UserAccessMeta, auth: AuthManager, args: Namespace, add_user: bool): if user: value = getattr(args, "admin_access", None) if value is not None: setattr(user, "admin_access", to_bool(value)) value = getattr(args, "write_groups", None) if value is not None: setattr(user, "write_groups", value) # make sure that the read groups contain the write groups as well value = getattr(args, "read_groups", None) value = list(set(value + user.write_groups)) if value is not None else list( set(user.read_groups + user.write_groups)) setattr(user, "read_groups", value) if auth.edit_user(access=user, password=args.password, hashed=False, add=add_user): _format_user(auth.get_user(user=args.username), multi=False) elif add_user: print("[Error] Could not add user") else: print("[Error] No changes were applied") else: print("[Error] User not found")
def info_user(auth: AuthManager, args: Namespace): if args.username: users = [auth.get_user(user=args.username)] else: users = auth.get_users() first = True for user in users: _format_user(user, multi=not first) first = False
def verify(auth: AuthManager, args: Namespace): pswd = getpass.getpass("[{}] password:"******"[Error] Invalid username or password")
def edit(auth: AuthManager, args: Namespace): if args.reset_password: password = get_password_input(args.username, args.ignore) if not password: return setattr(args, "password", password) else: setattr(args, "password", None) _edit(user=auth.get_user(user=args.username), auth=auth, args=args, add_user=False)
def setup_app(logging_level: str = "debug", credentials_config: str = DEFAULT_CREDENTIALS_CONFIG, auth_config: str = DEFAULT_AUTH_CONFIG, worker_config: str = DEFAULT_WORKER_CONFIG) -> falcon.API: logging.basicConfig(level=parse_logging_lvl(logging_level), format='[%(asctime)s] [%(levelname)s] %(message)s') file_controller = FileController(config_file=credentials_config) worker_manager = WorkerManager(config_file=worker_config, controller=file_controller) auth = AuthManager(config_file=auth_config) app = falcon.API(middleware=[RequireJSON(), CORSComponent(), AuthMiddleware(auth=auth, public_paths={"/api/auth"}), RequireAuthGroups(controller=file_controller)]) app.add_route('/api/auth', AuthRoute(auth=auth)) app.add_route('/api/user', UserEdit(auth=auth)) app.add_route('/api/user/groups', GroupsInfo(auth=auth)) app.add_route('/api/user/info', UserInfo(auth=auth)) app.add_route('/api/workers', WorkersEdit(worker_manager=worker_manager)) app.add_route('/api/workers/status', WorkersStatusRoute(worker_manager=worker_manager)) app.add_route('/api/workers/queue', WorkerQueue(controller=file_controller, worker_manager=worker_manager)) app.add_route('/api/list', StoreList(controller=file_controller)) app.add_route('/api/{store_id}/list', ProjectList(controller=file_controller)) app.add_route('/api/{store_id}/create', ProjectCreate(controller=file_controller)) app.add_route('/api/{store_id}/{project_id}/version', ProjectVersion(controller=file_controller)) app.add_route('/api/{store_id}/{project_id}/list', AssetList(controller=file_controller)) app.add_route('/api/{store_id}/{project_id}/create', AssetCreate(controller=file_controller)) app.add_route('/api/{store_id}/{project_id}/projectMeta', ProjectMetaEdit(controller=file_controller)) app.add_route('/api/{store_id}/{project_id}/projectAccessMeta', ProjectAccessMetaEdit(controller=file_controller)) # had to redo the routes because the falcon parser cannot parse routes with the same prefix app.add_route('/api/{store_id}/{project_id}/object/{object_id}', ObjectEdit(controller=file_controller)) app.add_route('/api/{store_id}/{project_id}/object/{object_id}/info', ObjectInfo(controller=file_controller)) app.add_route('/api/{store_id}/{project_id}/files/{asset_id}', FileList(controller=file_controller)) app.add_route('/api/{store_id}/{project_id}/meta/{asset_id}', AssetMetaEdit(controller=file_controller)) app.add_route('/api/{store_id}/{project_id}/upload/{asset_id}', AssetUpload(controller=file_controller)) app.add_route('/api/{store_id}/{project_id}/tags/{asset_id}', TagEdit(controller=file_controller)) app.add_error_handler(Exception, handle_exceptions) atexit.register(auth.close) atexit.register(worker_manager.close) return app
def remove(auth: AuthManager, args: Namespace): if auth.remove_user(user=args.username): print("User removed successfully") else: print("[Error] User not found")
def main(): parser = argparse.ArgumentParser( prog="am-cli", description="A commandline interface for the OVE Asset Manager.") parser.add_argument("--config", default=DEFAULT_AUTH_CONFIG, type=str, help="Auth config file") subparsers = parser.add_subparsers() parser_user = subparsers.add_parser("user", help="User commands") subparsers_user = parser_user.add_subparsers() parser_user_add = subparsers_user.add_parser("verify", help="Verify user password") parser_user_add.add_argument("username", type=str, help="Username to verify") parser_user_add.set_defaults(function=user.verify) parser_user_add = subparsers_user.add_parser("add", help="Add user") parser_user_add.add_argument("username", type=str, help="Username to add") parser_user_add.add_argument("--read", dest="read_groups", type=str, nargs="+", default=[], help="Read access groups") parser_user_add.add_argument("--write", dest="write_groups", type=str, nargs="+", default=[], help="Write access groups") parser_user_add.add_argument('--admin', dest='admin_access', action='store_const', const=True, default=False, help='Admin access') parser_user_add.add_argument('--ignore', dest='ignore', action='store_const', const=True, default=False, help='Ignore validation') parser_user_add.set_defaults(function=user.add) parser_user_edit = subparsers_user.add_parser("edit", help="Edit user") parser_user_edit.add_argument("username", type=str, help="Username to edit") parser_user_edit.add_argument("--password", dest='reset_password', action='store_const', const=True, default=False, help="Password") parser_user_edit_read = parser_user_edit.add_mutually_exclusive_group() parser_user_edit_read.add_argument("--read", dest="read_groups", type=str, nargs="+", help="Read access groups") parser_user_edit_read.add_argument("--noread", dest="read_groups", action='store_const', const=[], help="Remove all read access groups") parser_user_edit_write = parser_user_edit.add_mutually_exclusive_group() parser_user_edit_write.add_argument("--write", dest="write_groups", type=str, nargs="+", help="Write access groups") parser_user_edit_write.add_argument("--nowrite", dest="write_groups", action='store_const', const=[], help="Remove all write access groups") parser_user_edit_admin = parser_user_edit.add_mutually_exclusive_group() parser_user_edit_admin.add_argument('--admin', dest='admin_access', action='store_const', const=True, help='Grant Admin access') parser_user_edit_admin.add_argument('--noadmin', dest='admin_access', action='store_const', const=False, help='Remove Admin access') parser_user_edit.add_argument('--ignore', dest='ignore', action='store_const', const=True, default=False, help='Ignore validation') parser_user_edit.set_defaults(function=user.edit) parser_user_remove = subparsers_user.add_parser("remove", help="Remove user") parser_user_remove.add_argument("username", type=str, help="Username to remove") parser_user_remove.set_defaults(function=user.remove) parser_user_info = subparsers_user.add_parser( "info", help="List permissions for specific user, or all users") parser_user_info.add_argument("username", type=str, nargs='?', default='', help="Username to list info about") parser_user_info.set_defaults(function=user.info_user) args = parser.parse_args() if getattr(args, "function", None): try: auth = AuthManager(config_file=args.config) args.function(auth, args) except: print("[Error]", sys.exc_info()[1]) else: parser.print_usage()