jumpserver/apps/accounts/api/automations/check_account.py

154 lines
5.3 KiB
Python
Raw Normal View History

2024-10-22 09:30:20 +00:00
# -*- coding: utf-8 -*-
#
2024-11-04 10:34:35 +00:00
from django.db.models import Q, Count
2024-12-10 07:44:16 +00:00
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.utils import timezone
2025-01-08 11:13:13 +00:00
from rest_framework.decorators import action
from rest_framework.exceptions import MethodNotAllowed
2024-11-27 11:33:58 +00:00
from rest_framework.response import Response
2024-10-22 09:30:20 +00:00
from accounts import serializers
from accounts.const import AutomationTypes
2024-12-03 09:49:04 +00:00
from accounts.models import (
CheckAccountAutomation,
AccountRisk,
RiskChoice,
CheckAccountEngine,
2024-12-10 07:44:16 +00:00
AutomationExecution,
2024-12-03 09:49:04 +00:00
)
2024-12-10 07:44:16 +00:00
from assets.models import Asset
2024-11-14 11:00:29 +00:00
from common.api import JMSModelViewSet
2024-12-03 09:49:04 +00:00
from common.utils import many_get
2024-10-22 09:30:20 +00:00
from orgs.mixins.api import OrgBulkModelViewSet
from .base import AutomationExecutionViewSet
__all__ = [
2024-12-03 09:49:04 +00:00
"CheckAccountAutomationViewSet",
"CheckAccountExecutionViewSet",
"AccountRiskViewSet",
"CheckAccountEngineViewSet",
2024-10-22 09:30:20 +00:00
]
2024-11-27 11:33:58 +00:00
from ...risk_handlers import RiskHandler
2024-10-22 09:30:20 +00:00
2024-11-13 08:09:07 +00:00
class CheckAccountAutomationViewSet(OrgBulkModelViewSet):
2024-11-14 11:00:29 +00:00
model = CheckAccountAutomation
2024-12-03 09:49:04 +00:00
filterset_fields = ("name",)
2024-10-22 09:30:20 +00:00
search_fields = filterset_fields
2024-11-14 11:00:29 +00:00
serializer_class = serializers.CheckAccountAutomationSerializer
2024-10-22 09:30:20 +00:00
class CheckAccountExecutionViewSet(AutomationExecutionViewSet):
rbac_perms = (
2024-11-14 11:00:29 +00:00
("list", "accounts.view_checkaccountexecution"),
("retrieve", "accounts.view_checkaccountsexecution"),
("create", "accounts.add_checkaccountexecution"),
2024-12-10 07:44:16 +00:00
("adhoc", "accounts.add_checkaccountexecution"),
2024-11-18 03:22:46 +00:00
("report", "accounts.view_checkaccountsexecution"),
2024-10-22 09:30:20 +00:00
)
2024-12-03 09:49:04 +00:00
ordering = ("-date_created",)
2024-11-13 08:09:07 +00:00
tp = AutomationTypes.check_account
2024-10-22 09:30:20 +00:00
def get_queryset(self):
queryset = super().get_queryset()
queryset = queryset.filter(automation__type=self.tp)
return queryset
2024-12-10 07:44:16 +00:00
@action(methods=["get"], detail=False, url_path="adhoc")
def adhoc(self, request, *args, **kwargs):
asset_id = request.query_params.get("asset_id")
if not asset_id:
return Response(status=400, data={"asset_id": "This field is required."})
get_object_or_404(Asset, pk=asset_id)
execution = AutomationExecution()
execution.snapshot = {
"assets": [asset_id],
"nodes": [],
"type": AutomationTypes.check_account,
"engines": ["check_account_secret"],
"name": "Check asset risk: {} {}".format(asset_id, timezone.now()),
}
execution.save()
execution.start()
report = execution.manager.gen_report()
return HttpResponse(report)
2024-10-22 09:30:20 +00:00
class AccountRiskViewSet(OrgBulkModelViewSet):
model = AccountRisk
2024-12-03 09:49:04 +00:00
search_fields = ("username", "asset")
filterset_fields = ("risk", "status", "asset")
2024-10-22 09:30:20 +00:00
serializer_classes = {
2024-12-03 09:49:04 +00:00
"default": serializers.AccountRiskSerializer,
"assets": serializers.AssetRiskSerializer,
"handle": serializers.HandleRiskSerializer,
2024-10-22 09:30:20 +00:00
}
2024-12-03 09:49:04 +00:00
ordering_fields = ("asset", "risk", "status", "username", "date_created")
ordering = ("status", "asset", "date_created")
2024-10-22 09:30:20 +00:00
rbac_perms = {
2024-12-03 09:49:04 +00:00
"sync_accounts": "assets.add_accountrisk",
"assets": "accounts.view_accountrisk",
"handle": "accounts.change_accountrisk",
2024-10-22 09:30:20 +00:00
}
2024-11-27 11:33:58 +00:00
def update(self, request, *args, **kwargs):
2024-12-03 09:49:04 +00:00
raise MethodNotAllowed("PUT")
2024-11-27 11:33:58 +00:00
def create(self, request, *args, **kwargs):
2024-12-03 09:49:04 +00:00
raise MethodNotAllowed("POST")
2024-11-04 10:34:35 +00:00
2024-12-03 09:49:04 +00:00
@action(methods=["get"], detail=False, url_path="assets")
2024-11-04 10:34:35 +00:00
def assets(self, request, *args, **kwargs):
annotations = {
2024-12-03 09:49:04 +00:00
f"{risk[0]}_count": Count("id", filter=Q(risk=risk[0]))
2024-11-04 10:34:35 +00:00
for risk in RiskChoice.choices
}
queryset = (
2024-12-03 09:49:04 +00:00
AccountRisk.objects.select_related(
"asset", "asset__platform"
) # 使用 select_related 来优化 asset 和 asset__platform 的查询
.values(
"asset__id", "asset__name", "asset__address", "asset__platform__name"
) # 添加需要的字段
.annotate(risk_total=Count("id")) # 计算风险总数
2024-11-04 10:34:35 +00:00
.annotate(**annotations) # 使用上面定义的 annotations 进行计数
)
return self.get_paginated_response_from_queryset(queryset)
2024-10-22 09:30:20 +00:00
2024-12-03 09:49:04 +00:00
@action(methods=["post"], detail=False, url_path="handle")
2024-11-27 11:33:58 +00:00
def handle(self, request, *args, **kwargs):
2024-12-03 09:49:04 +00:00
s = self.get_serializer(data=request.data)
s.is_valid(raise_exception=True)
2024-11-27 11:33:58 +00:00
2024-12-10 07:44:16 +00:00
asset, username, act, risk = many_get(
s.validated_data, ("asset", "username", "action", "risk")
)
2024-12-03 09:49:04 +00:00
handler = RiskHandler(asset=asset, username=username, request=self.request)
2024-11-27 11:33:58 +00:00
data = handler.handle(act, risk)
if not data:
2024-12-03 09:49:04 +00:00
data = {"message": "Success"}
2025-01-14 10:13:17 +00:00
s = serializers.AccountRiskSerializer(instance=data)
return Response(data=s.data)
2024-11-27 11:33:58 +00:00
2024-11-01 10:49:03 +00:00
2024-11-14 11:00:29 +00:00
class CheckAccountEngineViewSet(JMSModelViewSet):
2024-12-03 09:49:04 +00:00
search_fields = ("name",)
2024-11-14 11:00:29 +00:00
serializer_class = serializers.CheckAccountEngineSerializer
2024-11-01 10:49:03 +00:00
2025-01-21 09:04:27 +00:00
perm_model = CheckAccountEngine
2024-12-03 09:49:04 +00:00
2024-11-01 10:49:03 +00:00
def get_queryset(self):
2025-01-21 09:04:27 +00:00
return CheckAccountEngine.get_default_engines()
def filter_queryset(self, queryset: list):
search = self.request.GET.get('search')
if search is not None:
queryset = [
item for item in queryset
if search in item['name']
]
return queryset