def add_process(req_user): ''' Add a process to the registry''' try: if not req_user["admin"]: raise AuthorizationError payload = request.get_json() if not payload: raise InvalidRequest("Invalid payload.") validate_process(payload) process_id = payload["process_id"] description = payload["description"] process_type = payload["type"] args = payload["args"] if process_type == "operation": git_uri = payload["git_uri"] git_ref = payload["git_ref"] git_dir = payload["git_dir"] else: git_uri = None git_ref = None git_dir = None does_exist = Process.query.filter_by(process_id=process_id).first() if does_exist: raise InvalidRequest("The process already exists.") process = Process(user_id=req_user["id"], process_id=process_id, description=description, git_uri=git_uri, git_ref=git_ref, git_dir=git_dir, process_type=process_type, args=args) DB.session.add(process) DB.session.commit() return parse_response(200, data={"process_id": process.id}) except (AuthorizationError, ValidationError, InvalidRequest) as exp: return parse_response(exp.code, str(exp)) except ValueError as exp: return parse_response(400, "Invalid payload.") except OperationalError as exp: return parse_response(503, "The service is currently unavailable.")
def login_user(): ''' Check credentials and send auth token ''' try: username = request.authorization["username"] password = request.authorization["password"] if not username or not password: raise InvalidRequest("Missing username or password.") user = User.query.filter_by(username=username).first() if not user: raise AuthenticationError("User does not exist.") if not BCRYPT.check_password_hash(user.password, password): raise AuthenticationError("Username or Password wrong.") auth_token = user.encode_auth_token(user.id) return parse_response(200, data={ "token": auth_token.decode(), "user_id": user.id }) except (InvalidRequest, AuthenticationError) as exp: return parse_response(exp.code, str(exp)) except BadRequest as exp: return parse_response(400, "Could not parse payload.") except OperationalError as exp: return parse_response(503, "The service is currently unavailable.")
def add_user(req_uid): ''' Add a user to the database. ''' try: if not is_admin(req_uid): raise AuthorizationError payload = request.get_json() if not payload: raise InvalidRequest("Invalid payload.") validate_user(payload) username = payload.get("username") email = payload.get("email") admin = payload.get("admin") password = payload.get("password") ex_user = User.query.filter_by(username=username).first() ex_email = User.query.filter_by(email=email).first() if ex_user or ex_email: raise InvalidRequest("The username or e-mail already exists.") new_user = User(username=username, password=password, email=email, admin=admin) DB.session.add(new_user) DB.session.commit() return parse_response(201, "{0} successfully created.".format(username)) except (AuthorizationError, ValidationError, InvalidRequest) as exp: return parse_response(exp.code, str(exp)) except (exc.IntegrityError, ValueError): return parse_response(400, "Invalid payload.") except OperationalError as exp: return parse_response(503, "The service is currently unavailable.")
def alter_process(req_user, process_id): ''' Alter values of process in namespace''' try: if not req_user["admin"]: raise AuthorizationError payload = request.get_json() if not payload: raise InvalidRequest("Invalid payload.") validate_process(payload) process = Process.query.filter_by(process_id=process_id).first() if not process: raise InvalidRequest("Process does not exist.") process.process_id = payload["process_id"] process.description = payload["description"] process.process_type = payload["type"] process.args = payload["args"] if payload["type"] == "operation": process.git_uri = payload["git_uri"] process.git_ref = payload["git_ref"] process.git_dir = payload["git_dir"] DB.session.commit() return parse_response( 200, "Process {0} sucesssfully altered.".format(process_id)) except (InvalidRequest, AuthorizationError) as exp: return parse_response(exp.code, str(exp)) except OperationalError as exp: return parse_response(503, "The service is currently unavailable.")
def get_process_details(process_id): ''' Detailed information about specific process that includes sensitive information ''' # TODO: Authentification / service message broker try: process_id = process_id.lower() if process_id == "min_time": process_id = "min-time" process = Process.query.filter_by(process_id=process_id).first() if not process: raise InvalidRequest("Process does not exist.") return parse_response(200, data=process.get_dict()) except InvalidRequest as exp: return parse_response(exp.code, str(exp)) except OperationalError as exp: return parse_response(503, "The service is currently unavailable.")
def delete_process(req_user, process_id): ''' Delete process ''' try: if not req_user["admin"]: raise AuthorizationError process = Process.query.filter_by(process_id=process_id).first() if not process: raise InvalidRequest("Process does not exist.") DB.session.delete(process) DB.session.commit() return parse_response( 200, "Process {0} sucesssfully deleted.".format(process_id)) except (InvalidRequest, AuthorizationError) as exp: return parse_response(exp.code, str(exp)) except OperationalError as exp: return parse_response(503, "The service is currently unavailable.")
def change_user(req_uid): ''' Change users details ''' try: if not is_admin(req_uid): raise AuthorizationError payload = request.get_json() if not payload: raise InvalidRequest("Invalid payload.") validate_user(payload) username = payload.get("username") email = payload.get("email") admin = payload.get("admin") password = payload.get("password") ex_user = User.query.filter_by(username=username).first() ex_user.username = username ex_user.email = email ex_user.password = User.generate_hash(password) ex_user.admin = admin DB.session.commit() return parse_response(201, "{0} successfully created.".format(username)) except (AuthorizationError, ValidationError, InvalidRequest) as exp: return parse_response(exp.code, str(exp)) except (exc.IntegrityError, ValueError): return parse_response(400, "Invalid payload.") except OperationalError as exp: return parse_response(503, "The service is currently unavailable.")