Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: hook profile download queries optimise. #1520

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 43 additions & 17 deletions dongtai_protocol/views/hook_profilesv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from dongtai_protocol.api_schema import DongTaiParameter
from dongtai_protocol.views.hook_profiles import HookProfilesEndPoint, JAVA, convert_strategy
from django.db.models import Q
from collections import defaultdict

logger = logging.getLogger("django")

Expand All @@ -29,28 +30,53 @@ def get_profiles(user=None, language_id=JAVA):
hook_types = IastStrategyModel.objects.filter(
Q(state__in=['enable'],
user_id__in=set([1, user.id]) if user else [1])).order_by('id')
allstrategies = list(
HookStrategy.objects.filter(
(Q(hooktype__in=hook_types_a) | Q(strategy__in=hook_types))
& Q(enable=const.HOOK_TYPE_ENABLE, language_id=language_id)).
values().all())

sink_strategies_dict = defaultdict(list)
other_strategies_dict = defaultdict(list)
for strategy in allstrategies:
if strategy['type'] == 4:
sink_strategies_dict[strategy['strategy_id']].append(strategy)
else:
other_strategies_dict[strategy['hooktype_id']].append(strategy)

for hook_type in list(hook_types) + list(hook_types_a):
strategy_details = list()
if isinstance(hook_type, IastStrategyModel):
hook_type = convert_strategy(hook_type)
strategies = hook_type.strategies.filter(
type__in=(1, 2, 3)
if not isinstance(hook_type, IastStrategyModel) else [4],
enable=const.HOOK_TYPE_ENABLE,
language_id=language_id).values()
strategies = sink_strategies_dict[hook_type.id]
else:
strategies = other_strategies_dict[hook_type.id]

for strategy in strategies:
profiles.append({
'type': hook_type.type,
'vul_type': hook_type.value,
"source": strategy.get("source"),
"target": strategy.get("target"),
"signature": strategy.get("value"),
"inherit": strategy.get("inherit"),
"ignore_blacklist": strategy.get("ignore_blacklist"),
"ignore_internal": strategy.get("ignore_internal"),
"tags": strategy.get("tags"),
"untags": strategy.get("untags"),
"command": strategy.get("command"),
"stack_blacklist": strategy.get("stack_blacklist"),
'type':
hook_type.type,
'vul_type':
hook_type.value,
"source":
strategy.get("source"),
"target":
strategy.get("target"),
"signature":
strategy.get("value"),
"inherit":
strategy.get("inherit"),
"ignore_blacklist":
strategy.get("ignore_blacklist"),
"ignore_internal":
strategy.get("ignore_internal"),
"tags":
strategy.get("tags"),
"untags":
strategy.get("untags"),
"command":
strategy.get("command"),
"stack_blacklist":
strategy.get("stack_blacklist"),
})
return profiles