def wrapper(*args, **kwargs): if "owner" in kwargs: _val = kwargs.get("owner") if isinstance(_val, basestring): _owner = User.objects.get_by_natural_key(username=_val) kwargs["owner"] = _owner if "user" in kwargs: _val = kwargs.get("user") if isinstance(_val, basestring): _user = User.objects.get_by_natural_key(username=_val) kwargs["user"] = _user if "zone" in kwargs: _val = kwargs.get("zone") if isinstance(_val, basestring): _zone = ZoneModel.get_zone_by_name(name=_val) kwargs["zone"] = _zone if "zone_name" in kwargs: _val = kwargs["zone_name"] if isinstance(_val, basestring): _zone = ZoneModel.get_zone_by_name(name=_val) kwargs["zone_name"] = _zone return func(*args, **kwargs)
def handle(self, *args, **options): path = options.get('path') cfg_type = options.get('type') zone = options.get('zone') clear = options.get('clear') == 'clear' cfg_model = get_cfg_model_by_type(cfg_type) fields = FIELDS.get(cfg_type, ()) if not cfg_model: raise ValueError('unknown type: %s' % cfg_type) elif not fields: raise ValueError('unsupported type: %s' % cfg_type) if clear: cfg_model.objects.filter(zone__name=zone).delete() Validator = get_validator_by_model(cfg_model) with open(path) as fp: reader = csv.reader(fp) for row in reader: data = dict(zip(fields, row)) zone = ZoneModel.get_zone_by_name(zone) data.update(dict(zone=zone)) validator = Validator(data=data) if validator.is_valid(): item = cfg_model(**data) item.save()
def get_all_notify_groups_by_name(cls, zone, owner, name, deleted=False): user_model = User.objects.get(username=owner) zone = ZoneModel.get_zone_by_name(zone) return cls.objects.filter(deleted=deleted, zone=zone, user=user_model, name=name).order_by('-create_datetime')
def create(self, uuid, nfm_id, nfg_id, name, phone, email, zone, owner, tel_verify=False, email_verify=False): try: notify_group_record = NotifyGroupModel.get_notify_group_by_id( nfg_id) zone_model = ZoneModel.get_zone_by_name(zone) user_model = User.objects.get(username=owner) _nfm_model = NotifyMemberModel(uuid=uuid, nfm_id=nfm_id, name=name, phone=phone, email=email, zone=zone_model, user=user_model, tel_verify=tel_verify, email_verify=email_verify, notify_group=notify_group_record) _nfm_model.save() return _nfm_model, None except Exception as exp: return None, exp
def get_all_apps(cls, owner, zone): """ 获取当前zone当前用户下app状态 :param owner: :param zone: :return: """ user_model = AccountService.get_by_owner(owner).user zone_model = ZoneModel.get_zone_by_name(zone) all_app = AppStoreModel.objects.filter(app_zone=zone_model).all() app_users = AppUserModel.objects.filter(app_users__in=(user_model, ), app_app__app_zone=zone_model).all() apps_installed = list() for app in app_users: apps_installed.extend(app.app_app.all()) dict_apps = defaultdict(dict) for app in all_app: dict_one_app = dict() if app in apps_installed: dict_one_app["installed"] = True app_user = app_users.filter(app_app=app).first() dict_one_app["status"] = app_user.app_status dict_one_app["version"] = app.app_version dict_one_app["publisher"] = app.app_publisher if app.app_name == "mq": dict_one_app["mq_host"] = KAFKA_MQ_HOST dict_apps[app.app_name] = dict_one_app continue dict_one_app["installed"] = False dict_one_app["status"] = None dict_one_app["version"] = app.app_version dict_one_app["publisher"] = app.app_publisher dict_apps[app.app_name] = dict_one_app return dict_apps
def get_backup_instance_image_info(image_uuid, zone, logger): platform = None system = None image_name = None image_info = {} if not image_uuid: return image_info zone = ZoneModel.get_zone_by_name(zone) try: image = ImageModel.get_image_by_uuid(image_uuid, zone) if image is not None: platform = image.platform system = image.system image_name = image.image_name else: backups = InstanceBackupModel.objects.filter(uuid=image_uuid) if backups and len(backups) > 0: platform = backups[0].platform system = backups[0].system image_name = backups[0].image_name else: logger.error("cannot find instance backup or image " "with uuid " + image_uuid) # image_info.update({"platform": platform, "system": system}) if platform: image_info.update({"platform": platform}) if system: image_info.update({"system": system}) if image_name: image_info.update(({"image_name": image_name})) except Exception as exp: logger.error(str(exp)) return image_info
def get_strategy_by_uuid(cls, uuid, zone, deleted=False): zone_record = ZoneModel.get_zone_by_name(zone) if cls.strategy_exists_by_uuid(uuid=uuid, zone=zone, deleted=deleted): return cls.objects.get(uuid=uuid, zone=zone_record, deleted=deleted) return None
def notify_member_in_group_by_name(cls, zone, nfg_id, name, deleted=False): zone = ZoneModel.get_zone_by_name(zone) notify_group = NotifyGroupModel.get_notify_group_by_id(nfg_id) return cls.objects.filter(notify_group=notify_group, zone=zone, name=name, deleted=deleted).exists()
def create_waf_service(payload): """ 新建waf站点 发送新建请求 存入数据库 :param payload: :return: """ owner = payload.pop("owner") zone = payload.pop("zone") domain = payload.get("domain") payload["ips"] = [item.encode() for item in payload.pop("ips")] smc_ip, smc_port = get_smc_info() create_code, create_msg = create_waf_site(smc_ip, smc_port, payload) if create_code: return console_response(code=1, msg=create_msg) user = AccountService.get_by_owner(owner).user zone = ZoneModel.get_zone_by_name(zone) try: WafServiceModel(waf_domain=domain, user=user, zone=zone).save() action_record = dict( domain=domain ) return console_response(action_record=action_record) except Exception as exc: logger.error("save domain error, %s", exc) return console_response(code=1, msg=exc.message)
def create(self, title, content, departments, users, username, zone): try: msgid = make_random_id('msg', NoticeModel.get_exists_by_id) zone = ZoneModel.get_zone_by_name(zone) author = username msg = NoticeModel(msgid=msgid, title=title, content=content, author=author, zone=zone) msg.save() for department_id in departments: if Department.objects.filter( department_id=department_id).exists(): department = Department.objects.get( department_id=department_id) msg.departments.add(department) for name in users: if User.objects.filter(username=name): u = User.objects.get(username=name) msg.users.add(u) msg.save() return msg, None except Exception as exp: logger.error("cannot create data , %s" % exp.message) return None, exp
def create_risk_vulnera(cls, alarm_type=None, alarm_id=None, desc_params='[]', server_uuid=None, server_ip=None, intranet_ip=None, systime=None, gen_time=None, template=None, file_path=None, zone=None): try: zone = ZoneModel.get_zone_by_name(zone) cls.objects.create(alarm_type=alarm_type, alarm_id=alarm_id, desc_params=desc_params, server_uuid=server_uuid, server_ip=server_ip, intranet_ip=intranet_ip, systime=systime, gen_time=gen_time, template=template, file_path=file_path, zone=zone) return True except Exception as exp: logger.error(msg=exp) return False
def create_safe_event(cls, alarm_type=None, alarm_id=None, desc_params='[]', server_uuid=None, server_ip=None, intranet_ip=None, systime=None, gen_time=None, attack_type='-', attack_event=None, attacker_ip=None, zone=None): try: zone = ZoneModel.get_zone_by_name(zone) cls.objects.create(alarm_type=alarm_type, alarm_id=alarm_id, desc_params=desc_params, server_uuid=server_uuid, server_ip=server_ip, intranet_ip=intranet_ip, systime=systime, gen_time=gen_time, attack_type=attack_type, attack_event=attack_event, attacker_ip=attacker_ip, zone=zone) return True except Exception as exp: logger.error(msg=exp) return False
def save_security_group_rule(resp, payload): """ Save create security group rule status """ if resp.get("code") != 0: return None, SaveDataError("Create security group failed") uuid = resp["data"]["ret_set"][0]["id"] sgr_id = payload.get("sgr_id") sg_uuid = payload.get("security_group_id") port_range_min = payload.get("port_range_min") port_range_max = payload.get("port_range_max") remote_ip_prefix = payload.get("remote_ip_prefix") protocol = payload.get("protocol") priority = payload.get("priority") direction = payload.get("direction") remote_group_id = payload.get("remote_group_id") zone = payload.get("zone") zone_record = ZoneModel.get_zone_by_name(zone) _security_group = SecurityGroupModel.get_security_by_uuid(uuid=sg_uuid, zone=zone_record) _security_group_rule_ins, err = SecurityGroupRuleModel. \ objects.create(uuid, sgr_id, _security_group, protocol, priority, direction, port_range_min, port_range_max, remote_ip_prefix, remote_group_id) return _security_group_rule_ins, err
def get_notify_group_by_uuid(cls, uuid, zone, deleted=False): zone_record = ZoneModel.get_zone_by_name(zone) if cls.notify_group_exists_by_uuid(uuid, zone, deleted): return cls.objects.get(uuid=uuid, zone=zone_record, deleted=deleted) else: return None
def get_config_by_uuid(cls, uuid, zone, deleted=False): try: zone_record = ZoneModel.get_zone_by_name(zone) return cls.objects.get(uuid=uuid, zone=zone_record, deleted=deleted) except cls.DoesNotExist: logger.info("cannot find config with uuid %s, %s" % (uuid, "doesn't exist")) return None
def restore_disk_backup(payload): resource_id = payload.pop("resource_id", None) backup_id = payload.pop("backup_id") action = payload.pop("action") version = payload.pop("version") backup_info = DiskBackupModel.get_backup_by_id(backup_id=backup_id) backup_uuid = backup_info.uuid if resource_id is not None: try: resource_uuid = DisksModel.get_disk_by_id(disk_id=resource_id).uuid except Exception: error_info = "cannot find disk with disk_id " + resource_id return console_response(BackupErrorCode.RESTORE_RESOURCE_NOT_FOUND, error_info) else: payload.update({ "action": "DescribeDiskBackup", "backup_id": backup_uuid }) # resp = api.get(payload=payload, timeout=10) resp = api.get(payload=payload) if resp.get("code") != 0: return console_response(CommonErrorCode.REQUEST_API_ERROR, resp.get("msg")) elif resp["data"]["total_count"] <= 0: return console_response( BackupErrorCode.ASSOCIATE_DISK_NOT_FOUND, "the disk related to the backup cannot " "be found, may be it has already " "been deleted") resource_uuid = resp["data"]["ret_set"][0]["volume_id"] zone_record = ZoneModel.get_zone_by_name(payload["zone"]) resource_record = DisksModel.get_disk_by_uuid(resource_uuid, zone_record) if resource_record: resource_id = resource_record.disk_id payload.update({ "action": action, "version": version, "disk_uuid": resource_uuid, "backup_id": backup_uuid }) # resp = api.get(payload=payload, timeout=10) resp = api.get(payload=payload) msg = resp.get("msg") if msg is None: msg = "Success" code = 0 if resp.get("code") != 0: code = CommonErrorCode.REQUEST_API_ERROR # source_backup_id is for record return console_response(code, msg, 1, [{ "disk_id": resource_id, "backup_id": backup_id }], {"source_backup_id": resource_id})
def save_security_group_version_2(uuid, sg_id, name, zone, owner): try: zone_record = ZoneModel.get_zone_by_name(zone) user_record = User.objects.get(username=owner) sg_record, err = SecurityGroupModel.objects. \ create(uuid, sg_id, name, zone_record, user_record) return sg_record, err except Exception as exp: return None, "save security group failed, {}".format(exp)
def get_rds_backup_by_uuid(cls, uuid, zone, deleted=False): try: zone_record = ZoneModel.get_zone_by_name(zone) return cls.objects.get(uuid=uuid, related_rds__zone=zone_record, deleted=deleted) except cls.DoesNotExist: logger.info("cannot find rds backup with uuid {}, {}".format( uuid, "doesn't exist")) return None
def describe_disk_backups(payload): backup_id = payload.get("backup_id", None) resource_id = payload.pop("resource_id", None) backup_status = payload.pop("status", None) owner = payload.get("owner") zone = payload.get("zone") zone_record = ZoneModel.get_zone_by_name(zone) availability_zone = payload.get('availability_zone') search_key = payload.get('search_key') if backup_id is not None: try: backup_uuid = DiskBackupModel.get_backup_by_id( backup_id=backup_id).uuid payload.update({"backup_id": backup_uuid}) except Exception: err_msg = "cannot find backup whose backup id is " + backup_id return console_response(BackupErrorCode.BACKUP_NOT_FOUND, err_msg) elif resource_id is not None: try: disk_uuid = DisksModel.objects.get(disk_id=resource_id).uuid payload.update({"disk_uuid": disk_uuid}) except Exception: err_msg = "cannot find disk uuid whose disk id is " \ + resource_id return console_response(BackupErrorCode.ASSOCIATE_DISK_NOT_FOUND, err_msg) # resp = api.get(payload=payload, timeout=10) resp = api.get(payload=payload) if resp["code"] == 0: ret_set = resp["data"].get("ret_set", []) resp_data = filter_disk_backup_info( ret_set, backup_status, owner, zone_record, availability_zone=availability_zone) if resp_data is None: return console_response( 1, "The uuid do not found, maybe the " "disk has been deleted") result_list = [] if search_key: for resp_item in resp_data: for item in resp_item.values(): if search_key in str(item): if resp_item not in result_list: result_list.append(resp_item) else: result_list = resp_data result = console_response(0, "Success", len(result_list), result_list) else: result = console_response(CommonErrorCode.REQUEST_API_ERROR, resp.get("msg")) return result
def list_msgs(payload): zone = payload.get('zone') zone = ZoneModel.get_zone_by_name(zone) page_index = payload.get('page_index') page_size = payload.get('page_size') msgs = NoticeModel.objects.filter(zone=zone).all() total_count = len(msgs) msgs = msgs.order_by('-commit_time')[(page_index - 1) * page_size:page_index * page_size] data = DescribeNoticeSerializer(msgs, many=True).data return console_response(total_count=total_count, ret_set=data)
def list(cls, payload): data = payload.get('data') owner = data.get('owner') zone = data.get('zone') zone_model = ZoneModel.get_zone_by_name(zone) account = AccountService.get_by_owner(owner) jumper_instance_set = InstancesModel.get_instances_by_owner( owner, zone).filter(role="jumpserver", deleted=1, destroyed=0) jumper_detail_list, total_count = InstanceService.render_with_detail( jumper_instance_set, account, zone_model) return console_response(code=0, ret_set=jumper_detail_list)
def create(self, db_version_id, db_type, db_version, zone): try: zone_record = ZoneModel.get_zone_by_name(zone) db_version_record = RdsDBVersionModel( db_version_id=db_version_id, db_type=db_type, db_version=db_version, zone=zone_record ) db_version_record.save() return db_version_record, None except Exception as exp: return None, exp
def create(self, ticket_id, applicant, approve, zone, **kwargs): try: zone = ZoneModel.get_zone_by_name(zone) kwargs.update({'zone': zone}) params = self.build_model_params(**kwargs) with transaction.atomic(): item = self.model(**params) item.save() CfgRecordModel.objects.create(item, ticket_id, applicant, approve, zone) return item, None except Exception as exp: logger.error("cannot save the new data to database, %s" % exp.message) return None, exp
def create(self, uuid, nfg_id, name, zone, owner): try: zone_model = ZoneModel.get_zone_by_name(zone) user_model = User.objects.get(username=owner) _nfg_model = NotifyGroupModel(uuid=uuid, nfg_id=nfg_id, name=name, zone=zone_model, user=user_model) _nfg_model.save() return _nfg_model, None except Exception as exp: return None, exp
def describe_security_group(payload): """ Describe security group(s) """ # version = payload.get("version") sg_id_param = payload.pop("sg_id", None) zone_name = payload.get("zone") user_name = payload.get("owner") # the default security group count is three, if not, generate them if SecurityGroupModel. \ default_security_group_count(user_name, zone_name) < 3: default_sg_id = make_default_sg_id(is_jumper=True) resp = add_security_group(user_name, zone_name, JUMPER_DEFAULT_SECURITY_GROUP_RULES, default_sg_id, make_security_group_rule_id, save_security_group_version_2, save_security_group_rule_version_2, "堡垒机默认安全组") if resp: return resp default_sg_id = make_default_sg_id() resp = add_security_group(user_name, zone_name, WEB_DEFAULT_SECURITY_GROUP_RULES, default_sg_id, make_security_group_rule_id, save_security_group_version_2, save_security_group_rule_version_2, "Web默认安全组") if resp: return resp if sg_id_param is not None: security_group = SecurityGroupModel.get_security_by_id( sg_id=sg_id_param) uuid = security_group.uuid payload.update({"uuid": uuid}) # resp = api.get(payload=payload, timeout=10) resp = api.get(payload=payload) if resp.get("code") != 0: return console_response(CommonErrorCode.REQUEST_API_ERROR, resp.get("msg")) sg_set = resp["data"].get("ret_set", []) zone_record = ZoneModel.get_zone_by_name(zone_name) sg_list = filter_needed_security_info(sg_set, zone_record, user_name, sg_id_param, payload) sorted_sg_list = sorted(sg_list, key=lambda x: (x['type'], x['name'])) resp["data"]["ret_set"] = sorted_sg_list resp["data"]["total_count"] = len(sorted_sg_list) return console_response(resp.get("code"), "Success", len(sorted_sg_list), sorted_sg_list)
def format_data_list(item, data_list, instance, payload): name_dict = {} item = str(item) new_data_list = [] if item.startswith("CPU") or item.startswith("MEMORY"): new_data_list = [None] elif item.startswith("NET"): instance = dict(instance) net_info = instance.get("addresses") for net_name, net_detail in net_info.items(): nets = list(net_detail) for net in nets: net = dict(net) ip_addr = net.get("addr") mac_addr = net.get("OS-EXT-IPS-MAC:mac_addr") addr_type = net.get("OS-EXT-IPS:type") if ip_addr in data_list and addr_type.strip() == "fixed": new_data_list.append(mac_addr) name_dict.update({mac_addr: ip_addr}) new_data_list = set(new_data_list) elif item.startswith("SYS_DISK"): new_data_list = ["vda"] elif item.startswith("DATA_DISK"): instance = dict(instance) attached_disks = instance.get("os-extended-volumes:volumes_attached") for disk in attached_disks: disk = dict(disk) disk_uuid = disk.get("id") disk_info = DisksModel.get_disk_by_uuid( disk_uuid, ZoneModel.get_zone_by_name(payload.get("zone"))) disk_id = disk_info.disk_id if disk_uuid in data_list: new_data_list.append(disk_uuid) name_dict.update({disk_uuid: disk_id}) elif item.startswith("PUBLIC_IP"): instance = dict(instance) float_ip_info = instance.get("addresses") for net_name, net_detail in float_ip_info.items(): nets = list(net_detail) for net in nets: net = dict(net) ip_addr = net.get("addr") addr_type = net.get("OS-EXT-IPS:type") if ip_addr in data_list and addr_type.strip() == "floating": new_data_list.append(ip_addr) name_dict.update({ip_addr: ip_addr}) new_data_list = set(new_data_list) return new_data_list, name_dict
def create(cls, quota_type, capacity, owner, zone, used=0): try: user = User.objects.get_by_natural_key(username=owner) zone = ZoneModel.get_zone_by_name(name=zone) quota = cls( quota_type=quota_type, capacity=capacity, used=used, user=user, zone=zone ) quota.save() return quota, None except Exception as exp: return None, exp
def get_disks_info(volumes_attached, zone): zone_record = ZoneModel.get_zone_by_name(zone) disks = [] for volume in volumes_attached: disk_uuid = volume["id"] disk_obj = DisksModel.get_disk_by_uuid(uuid=disk_uuid, zone=zone_record) if disk_obj: disk = {} disk["disk_id"] = disk_obj.disk_id disk["disk_name"] = disk_obj.name disk["create_datetime"] =\ datetime_to_timestamp(disk_obj.create_datetime) disks.append(disk) return disks
def create(self, uuid, alm_id, name, resource_type, period, zone, owner): try: zone_model = ZoneModel.get_zone_by_name(zone) user_model = User.objects.get(username=owner) alm_model = StrategyModel(uuid=uuid, alm_id=alm_id, name=name, resource_type=resource_type, period=period, zone=zone_model, user=user_model) alm_model.save() return alm_model, None except Exception as exp: return None, exp
def post(self, request, *args, **kwargs): form = DescribeInstanceGroupSerializer(data=request.data) if not form.is_valid(): return Response( console_response(CommonErrorCode.PARAMETER_ERROR, form.errors)) owner = form.validated_data['owner'] zone_name = form.validated_data['zone'] account = AccountService.get_by_owner(owner) zone = ZoneModel.get_zone_by_name(zone_name) groups = InstanceGroupService.mget_by_account(account, zone) groups = [_.to_dict() for _ in groups] return old_restful_response(0, "succ", len(groups), groups)