def get(self, request, *args, **kwargs): rule_id = request.GET.get('rule_id', None) groups = {} # 参数不全 if rule_id is None: return self.render_json_response({'state': False, "rules_num": 0}) rules = Rules(load_all=True).id_rule_map if rule_id in (u"", u"所有"): # 所有规则 return self.render_json_response({ 'state': True, 'strategy_groups': groups, "rules_num": len(rules.keys()) }) rule_obj = rules.get(rule_id) if not rule_obj: return self.render_json_response({'state': False, "rules_num": 0}) group_list = rule_obj.strategy_group_list for strategy_group in group_list: group_name = strategy_group[3] groups[rule_id] = group_name break return self.render_json_response({ 'state': True, 'strategy_groups': groups, "rules_num": len(rules.keys()) })
class HitLogDetailTable(tables.Table): time = columns.Column(verbose_name=_(u'触碰时间')) rule_id = columns.Column(verbose_name=_(u'规则名称'), orderable=False) group_name = columns.Column(verbose_name=_(u'策略原子组名称'), orderable=False) user_id = columns.Column(verbose_name=_(u'用户ID'), orderable=False) control = columns.Column(verbose_name=_(u'管控原子'), orderable=False) req_body = columns.Column(verbose_name=_(u'请求体'), orderable=False) hit_number = columns.Column(verbose_name=_(u'是否首次命中'), orderable=False) class Meta: attrs = {'class': 'table table-striped table-hover'} def before_render(self, request): self.rules = Rules(load_all=True) def render_time(self, value): return value.strftime('%Y-%m-%d %H:%M:%S') def render_rule_id(self, value): return self.rules.get_rule_name(str(value)) def render_control(self, value): return CONTROL_MAP.get(value, value) def render_hit_number(self, value): return u'-' if value == 0 else u'是' if value == 1 else u'否' def render_passed_users(self, value): return u'-' if value == 0 else value
class HitLogDetailTable(tables.Table): time = columns.Column(verbose_name=_('Hit_time')) rule_id = columns.Column(verbose_name=_('RuleName'), orderable=False) group_name = columns.Column(verbose_name=_('PolicyGroupNameCall'), orderable=False) user_id = columns.Column(verbose_name=_('UserID'), orderable=False) control = columns.Column(verbose_name=_('Projectmanagement'), orderable=False) req_body = columns.Column(verbose_name=_('RequestBody'), orderable=False) hit_number = columns.Column( verbose_name=_('Whether to hit for the first time'), orderable=False) class Meta: attrs = {'class': 'table table-striped table-hover'} def __init__(self, *args, **kwargs): self.rules = Rules(load_all=True) def before_render(self, request): pass @staticmethod def render_time(value): return value.strftime('%Y-%m-%d %H:%M:%S') def render_rule_id(self, value): return self.rules.get_rule_name(str(value)) @staticmethod def render_control(value): return CONTROL_MAP.get(value, value) @staticmethod def render_hit_number(value): return '-' if value == 0 else _('is') if value == 1 else _('Whether') @staticmethod def render_passed_users(value): return '-' if value == 0 else value
def before_render(self, request): self.rules = Rules(load_all=True)
def _get_all_rule_id_and_names(self): rule_id_and_names = Rules(load_all=True).get_all_rule_id_and_name() rule_id_and_names.insert(0, ('', u'所有规则')) return rule_id_and_names
def _get_all_strategy_groups(self): strategy_names = Rules(load_all=True).get_all_group_uuid_and_name() strategy_names.insert(0, ('', u'所有策略原子组')) return strategy_names
def _get_all_rule_id_and_names(): rule_id_and_names = Rules(load_all=True).get_all_rule_id_and_name() rule_id_and_names.insert(0, ('', _('All_Rules'))) return rule_id_and_names
def _get_all_strategy_groups(): strategy_names = Rules(load_all=True).get_all_group_uuid_and_name() strategy_names.insert(0, ('', _('All_Policy_Group'))) return strategy_names
def _build_rule_choices(cls): return Rules().get_all_rule_uuid_and_name()
#!/usr/bin/env python3 # coding: utf-8 from django.utils.translation import gettext_lazy as _ from aswan import settings from server.base import Response from risk_models.exceptions import RuleNotExistsException from risk_models.rule import calculate_rule, Rules, AccessCount rules = Rules(auto_refresh=True) ac = AccessCount(auto_persist=True) def query_handler(req_body): rule_id = req_body.get('rule_id') result, ec, error = None, 0, None try: if settings.DEBUG: assert rule_id rule_id = str(rule_id) control, weight = calculate_rule(rule_id, req_body, rules=rules, ac=ac) result = {'control': control, 'weight': weight} except AssertionError: error = _('must contain rule_id') ec = 100 except RuleNotExistsException: error = _('rule_id does not exist or is offline') ec = 101
def __init__(self, *args, **kwargs): self.rules = Rules(load_all=True)