def update(self, instance, validated_data): password = validated_data.get('password', None) if password != instance.password: pc = prpcrypt() validated_data['password'] = pc.encrypt(password) self.Meta.model.objects.filter(id=instance.id).update(**validated_data) return instance
class SetEncryptMixins(object): pc = prpcrypt() parameter = 'password' def create(self, validated_data): password = validated_data.get(self.parameter) validated_data[self.parameter] = self.pc.encrypt(password) return super(SetEncryptMixins, self).create(validated_data) def update(self, instance, validated_data): password = validated_data.get(self.parameter) if password != instance.password: validated_data[self.parameter] = self.pc.encrypt(password) return super(SetEncryptMixins, self).update(instance, validated_data)
class CheckConn(InceptionConn): pc = prpcrypt() conf = configparser.ConfigParser() file_path = settings.INCEPTION_SETTINGS.get('file_path') def check(self, request): res = {'status': 0, 'data': ''} request_data = request.data check_type = request_data.get('check_type') if check_type == 'inception_conn': sub_cmd = "inception get variables" cmd = self.get_cmd(sub_cmd) else: params = {} if check_type == 'inception_backup': self.conf.read(self.file_path) password = self.conf.get('inception', 'inception_remote_system_password') request_data['password'] = password params = request_data elif check_type == 'update_target_db': db_id = request_data.get('id') instance = Dbconf.objects.get(id=db_id) params = { 'host': instance.host, 'port': instance.port, 'user': instance.user, 'password': self.pc.decrypt(instance.password) } elif check_type == 'create_target_db': params = request_data cmd = self.get_mysql_conn(params) popen = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) lines = popen.stdout.readlines() last_item = lines[-1].decode('gbk') if self.error_tag in last_item.lower(): res['status'] = -1 res['data'] = last_item return res
def create(self, validated_data): password = validated_data.get('password', None) pc = prpcrypt() validated_data['password'] = pc.encrypt(password) instance = self.Meta.model.objects.create(**validated_data) return instance
def get_db_addr(self, user, password, host, port, actiontype): pc = prpcrypt() password = pc.decrypt(password) dbaddr = '--user={}; --password={}; --host={}; --port={}; {};'.format(user, password, host, port, actiontype) return dbaddr
def update(self, instance, validated_data): password = validated_data.get('password') if password != instance.password: pc = prpcrypt() validated_data['password'] = pc.encrypt(password) return super(DbSerializer, self).update(instance, validated_data)
def create(self, validated_data): password = validated_data.get('password') pc = prpcrypt() validated_data['password'] = pc.encrypt(password) return super(DbSerializer, self).create(validated_data)
class ActionMixins(AppellationMixins): pc = prpcrypt() type_select_tag = 'select' action_type_execute = '--enable-execute' action_type_check = '--enable-check' def get_reject_step(self, instance): user = self.request.user if self.has_flow(instance): if user.is_superuser: return 1 if instance.commiter == user.username else 2 else: role = user.role return self.reject_steps.get(role) @staticmethod def get_current_step(instance): steps = instance.workorder.step_set.all() current = 0 for step in steps: if step.status not in [-1, 0]: current += 1 return current @property def is_manual_review(self): instance = Strategy.objects.first() if not instance: instance = Strategy.objects.create() return instance.is_manual_review def get_db_addr(self, user, password, host, port, actiontype): password = self.pc.decrypt(password) dbaddr = '--user={}; --password={}; --host={}; --port={}; {};'.format( user, password, host, port, actiontype) return dbaddr def has_flow(self, instance): return instance.is_manual_review == True and instance.env == self.env_prd def replace_remark(self, sqlobj): username = self.request.user.username uri = self.request.META['PATH_INFO'].split('/')[-2] if username != sqlobj.treater: sqlobj.remark += ' [' + username + self.action_desc_map.get( uri) + ']' if sqlobj.workorder.status == True: steps = sqlobj.workorder.step_set.all() step_obj_second = steps[1] if not (self.request.user == step_obj_second.user and uri == 'reject'): step_obj = steps[0] step_obj.user = self.request.user step_obj.save() sqlobj.save() def check_execute_sql(self, db_id, sql_content, action_type): dbobj = Dbconf.objects.get(id=db_id) db_addr = self.get_db_addr(dbobj.user, dbobj.password, dbobj.host, dbobj.port, action_type) sql_review = Inception(sql_content, dbobj.name).inception_handle(db_addr) result, status = sql_review.get('result'), sql_review.get('status') if status == -1 or len(result) == 1: raise ParseError({self.connect_error: result}) success_sqls = [] exception_sqls = [] for sql_result in result: error_message = sql_result[4] if error_message == 'None' or re.findall('Warning', error_message): success_sqls.append(sql_result) else: exception_sqls.append(error_message) if exception_sqls and action_type == self.action_type_check: raise ParseError({self.exception_sqls: exception_sqls}) return (success_sqls, exception_sqls, json.dumps(result)) def mail(self, sqlobj, mailtype): if sqlobj.env == self.env_prd: username = self.request.user.username treater = sqlobj.treater commiter = sqlobj.commiter mailto_users = [treater, commiter] mailto_users = list(set(mailto_users)) mailto_list = [ u.email for u in User.objects.filter(username__in=mailto_users) ] send_mail.delay(mailto_list, username, sqlobj.id, sqlobj.remark, mailtype, sqlobj.sql_content, sqlobj.db.name)