class IncidentUpdatesNotify(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __incident_update = None __task = None __notification = None __subscriber = None __correlation_id = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__incident_update = IncidentUpdateModule() self.__task = Task_Module() self.__notification = NotificationModule() self.__subscriber = SubscriberModule() self.__logger = self.__helpers.get_logger(__name__) self.__form.add_validator(ExtraRules()) @allow_if_authenticated def post(self, request, incident_id, update_id): self.__correlation_id = request.META["X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__user_id = request.user.id task = self.__task.delay("incident_update", { "incident_update_id": update_id, "user_id": self.__user_id }, self.__user_id) result = False if task: result = self.__notification.create_notification({ "highlight": "Incident Update", "notification": "notifying subscribers with the incident update", "url": "#", "type": NotificationModule.PENDING, "delivered": False, "user_id": self.__user_id, "task_id": task.id }) if task and result: return JsonResponse(self.__response.send_private_success([{ "type": "success", "message": _("Notification delivery started successfully.") }], {}, self.__correlation_id)) else: return JsonResponse(self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while starting delivery.") }], {}, self.__correlation_id))
class Tag_Image_By_Id(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __host_id = None __host_module = None __task_module = None __notification_module = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__host_module = Host_Module() self.__task_module = Task_Module() self.__notification_module = Notification_Module() self.__logger = self.__helpers.get_logger(__name__) def post(self, request, host_id): self.__user_id = request.user.id self.__host_id = host_id self.__request.set_request(request) request_data = self.__request.get_request_data("post", { "long_id": "", "repository": "", "tag": "", "force": "" }) self.__form.add_inputs({ 'long_id': { 'value': request_data["long_id"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'repository': { 'value': request_data["repository"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'tag': { 'value': request_data["tag"], 'sanitize': { 'strip': {} }, 'validate': {} }, 'force': { 'value': request_data["force"], 'sanitize': { 'strip': {} }, 'validate': {} } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse( self.__response.send_private_failure( self.__form.get_errors(with_type=True))) if not self.__host_module.user_owns(self.__host_id, self.__user_id): return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Invalid Request.") }])) _long_id = self.__form.get_input_value("long_id") _repository = self.__form.get_input_value("repository") _tag = self.__form.get_input_value("tag") _force = self.__form.get_input_value("force") task = self.__task_module.delay( "tag_image_by_id", { "host_id": self.__host_id, "long_id": _long_id, "repository": _repository, "tag": _tag, "force": _force }, self.__user_id) if task: self.__notification_module.create_notification({ "highlight": "", "notification": _("Tag docker image as %s:%s") % (_repository, _tag), "url": "#", "type": Notification_Module.PENDING, "delivered": False, "user_id": self.__user_id, "host_id": self.__host_id, "task_id": task.id }) return JsonResponse( self.__response.send_private_success([{ "type": "success", "message": _("Request is in progress!") }])) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while creating request.") }]))
class Prune_All_Unused_Images(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __host_id = None __host_module = None __task_module = None __notification_module = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__host_module = Host_Module() self.__task_module = Task_Module() self.__notification_module = Notification_Module() self.__logger = self.__helpers.get_logger(__name__) def post(self, request, host_id): self.__user_id = request.user.id self.__host_id = host_id if not self.__host_module.user_owns(self.__host_id, self.__user_id): return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Invalid Request.") }])) task = self.__task_module.delay("prune_all_unused_images", {"host_id": self.__host_id}, self.__user_id) if task: self.__notification_module.create_notification({ "highlight": "", "notification": _("prune all unused docker images"), "url": "#", "type": Notification_Module.PENDING, "delivered": False, "user_id": self.__user_id, "host_id": self.__host_id, "task_id": task.id }) return JsonResponse( self.__response.send_private_success([{ "type": "success", "message": _("Request is in progress!") }])) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while creating request.") }]))
class Pull_Image(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __host_id = None __host_module = None __task_module = None __notification_module = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__host_module = Host_Module() self.__task_module = Task_Module() self.__notification_module = Notification_Module() self.__logger = self.__helpers.get_logger(__name__) def post(self, request, host_id): self.__user_id = request.user.id self.__host_id = host_id self.__request.set_request(request) request_data = self.__request.get_request_data("post", {"image_name": ""}) self.__form.add_inputs({ 'image_name': { 'value': request_data["image_name"], 'sanitize': { 'strip': {} }, 'validate': { 'not_empty': { 'error': _('Error! docker image is required!') }, 'length_between': { 'param': [1, 100], 'error': _('Error! a valid docker image is required!') } } } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse( self.__response.send_private_failure( self.__form.get_errors(with_type=True))) if not self.__host_module.user_owns(self.__host_id, self.__user_id): return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Invalid Request.") }])) _image_name = self.__form.get_input_value("image_name") if ":" not in _image_name: _image_name = "%s:latest" % _image_name task = self.__task_module.delay("pull_image", { "host_id": self.__host_id, "image_name": _image_name }, self.__user_id) if task: self.__notification_module.create_notification({ "highlight": "", "notification": "pulling docker image %s" % _image_name, "url": "#", "type": Notification_Module.PENDING, "delivered": False, "user_id": self.__user_id, "host_id": self.__host_id, "task_id": task.id }) return JsonResponse( self.__response.send_private_success([{ "type": "success", "message": _("Request is in progress!") }])) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while creating request.") }]))
class Build_Image(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __host_id = None __host_module = None __task_module = None __notification_module = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__host_module = Host_Module() self.__task_module = Task_Module() self.__notification_module = Notification_Module() self.__logger = self.__helpers.get_logger(__name__) def post(self, request, host_id): self.__user_id = request.user.id self.__host_id = host_id self.__request.set_request(request) request_data = self.__request.get_request_data("post", { "tag": "", "dockerfile": "", "rm": "", "nocache": "" }) self.__form.add_inputs({ 'tag': { 'value': request_data["tag"], 'sanitize': {}, 'validate': {} }, 'dockerfile': { 'value': request_data["dockerfile"], 'sanitize': {}, 'validate': {} }, 'rm': { 'value': request_data["rm"], 'sanitize': {}, 'validate': {} }, 'nocache': { 'value': request_data["nocache"], 'sanitize': {}, 'validate': {} } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse( self.__response.send_private_failure( self.__form.get_errors(with_type=True))) if not self.__host_module.user_owns(self.__host_id, self.__user_id): return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Invalid Request.") }])) _tag = self.__form.get_input_value("tag") _fileobj = self.__form.get_input_value("dockerfile") _rm = bool(self.__form.get_input_value("rm") == "1") _nocache = bool(self.__form.get_input_value("nocache") == "1") task = self.__task_module.delay( "build_image", { "host_id": self.__host_id, "fileobj": _fileobj, "tag": _tag, "rm": _rm, "nocache": _nocache }, self.__user_id) if task: self.__notification_module.create_notification({ "highlight": "", "notification": "building docker image %s" % _tag, "url": "#", "type": Notification_Module.PENDING, "delivered": False, "user_id": self.__user_id, "host_id": self.__host_id, "task_id": task.id }) return JsonResponse( self.__response.send_private_success([{ "type": "success", "message": _("Request is in progress!") }])) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while creating request.") }]))
class Install(View): __request = None __response = None __helpers = None __form = None __install = None __logger = None __notification = None __correlation_id = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__form = Form() self.__install = InstallModule() self.__notification = NotificationModule() self.__logger = self.__helpers.get_logger(__name__) self.__form.add_validator(ExtraRules()) @stop_request_if_installed def post(self, request): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" if self.__install.is_installed(): return JsonResponse( self.__response.send_private_failure( [{ "type": "error", "message": _("Error! Application is already installed.") }], {}, self.__correlation_id)) self.__request.set_request(request) request_data = self.__request.get_request_data( "post", { "app_name": "", "app_email": "", "app_url": "", "admin_username": "", "admin_email": "", "admin_password": "" }) self.__form.add_inputs({ 'app_name': { 'value': request_data["app_name"], 'sanitize': { 'escape': {}, 'strip': {} }, 'validate': { 'alpha_numeric': { 'error': _('Error! Application name must be alpha numeric.') }, 'length_between': { 'param': [2, 30], 'error': _('Error! Application name must be 2 to 30 characters long.' ) } } }, 'app_email': { 'value': request_data["app_email"], 'sanitize': { 'escape': {}, 'strip': {} }, 'validate': { 'sv_email': { 'error': _('Error! Application email is invalid.') } } }, 'app_url': { 'value': request_data["app_url"], 'sanitize': { 'escape': {}, 'strip': {} }, 'validate': { 'sv_url': { 'error': _('Error! Application url is invalid.') } } }, 'admin_username': { 'value': request_data["admin_username"], 'sanitize': { 'escape': {}, 'strip': {} }, 'validate': { 'alpha_numeric': { 'error': _('Error! Username must be alpha numeric.') }, 'length_between': { 'param': [4, 10], 'error': _('Error! Username must be 5 to 10 characters long.') } } }, 'admin_email': { 'value': request_data["admin_email"], 'sanitize': { 'escape': {}, 'strip': {} }, 'validate': { 'sv_email': { 'error': _('Error! Admin email is invalid.') } } }, 'admin_password': { 'value': request_data["admin_password"], 'validate': { 'sv_password': { 'error': _('Error! Password must contain at least uppercase letter, lowercase letter, numbers and special character.' ) }, 'length_between': { 'param': [7, 20], 'error': _('Error! Password length must be from 8 to 20 characters.' ) } } } }) self.__form.process() if not self.__form.is_passed(): return JsonResponse( self.__response.send_errors_failure(self.__form.get_errors(), {}, self.__correlation_id)) self.__install.set_app_data(self.__form.get_sinput("app_name"), self.__form.get_sinput("app_email"), self.__form.get_sinput("app_url")) self.__install.set_admin_data(self.__form.get_sinput("admin_username"), self.__form.get_sinput("admin_email"), self.__form.get_sinput("admin_password")) try: user_id = self.__install.install() except Exception as exception: self.__logger.error( _("Internal server error during installation: %(exception)s {'correlationId':'%(correlationId)s'}" ) % { "exception": exception, "correlationId": self.__correlation_id }) if user_id: self.__notification.create_notification({ "highlight": _('Installation'), "notification": _('Silverback installed successfully'), "url": "#", "type": NotificationModule.MESSAGE, "delivered": False, "user_id": user_id, "task_id": None }) return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("Application installed successfully.") }], {}, self.__correlation_id)) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong during installing.") }], {}, self.__correlation_id))
class Install(View, Controller): """Install Private Endpoint Controller""" def __init__(self): self.__install = InstallModule() self.__notification = NotificationModule() @stop_request_if_installed def post(self, request): if self.__install.is_installed(): return self.json([{ "type": "error", "message": _("Error! Application is already installed.") }]) request_data = self.get_request_data( request, "post", { "app_name": "", "app_email": "", "app_url": "", "admin_username": "", "admin_email": "", "admin_password": "" }) self.form().add_inputs({ 'app_name': { 'value': request_data["app_name"], 'sanitize': { 'strip': {} }, 'validate': { 'alpha_numeric': { 'error': _('Error! Application name must be alpha numeric.') }, 'length_between': { 'param': [2, 30], 'error': _('Error! Application name must be 2 to 30 characters long.' ) } } }, 'app_email': { 'value': request_data["app_email"], 'sanitize': { 'strip': {} }, 'validate': { 'sv_email': { 'error': _('Error! Application email is invalid.') } } }, 'app_url': { 'value': request_data["app_url"], 'sanitize': { 'strip': {} }, 'validate': { 'sv_url': { 'error': _('Error! Application url is invalid.') } } }, 'admin_username': { 'value': request_data["admin_username"], 'sanitize': { 'strip': {} }, 'validate': { 'alpha_numeric': { 'error': _('Error! Username must be alpha numeric.') }, 'length_between': { 'param': [4, 10], 'error': _('Error! Username must be 5 to 10 characters long.') } } }, 'admin_email': { 'value': request_data["admin_email"], 'sanitize': { 'strip': {} }, 'validate': { 'sv_email': { 'error': _('Error! Admin email is invalid.') } } }, 'admin_password': { 'value': request_data["admin_password"], 'validate': { 'sv_password': { 'error': _('Error! Password must contain at least uppercase letter, lowercase letter, numbers and special character.' ) }, 'length_between': { 'param': [7, 20], 'error': _('Error! Password length must be from 8 to 20 characters.' ) } } } }) self.form().process() if not self.form().is_passed(): return self.json(self.form().get_errors()) self.__install.set_app_data(self.form().get_sinput("app_name"), self.form().get_sinput("app_email"), self.form().get_sinput("app_url")) self.__install.set_admin_data(self.form().get_sinput("admin_username"), self.form().get_sinput("admin_email"), self.form().get_sinput("admin_password")) try: user_id = self.__install.install() except Exception as exception: self.logger().error( _("Internal server error during installation: %(exception)s") % {"exception": exception}) if user_id: self.__notification.create_notification({ "highlight": _('Installation'), "notification": _('Silverback installed successfully'), "url": "#", "type": NotificationModule.MESSAGE, "delivered": False, "user_id": user_id, "task_id": None }) return self.json([{ "type": "success", "message": _("Application installed successfully.") }]) else: return self.json([{ "type": "error", "message": _("Error! Something went wrong during installation.") }])