Skip to content

Commit

Permalink
Merge pull request #715 from Lingghh/dev/nodetag_add_processrelation
Browse files Browse the repository at this point in the history
✨增加标签关联工具进程能力
  • Loading branch information
cyw3 authored Dec 8, 2022
2 parents ef2d8d5 + df87829 commit f2784b0
Show file tree
Hide file tree
Showing 11 changed files with 301 additions and 79 deletions.
23 changes: 16 additions & 7 deletions server/projects/main/apps/nodemgr/api_urls/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,22 @@

# 前缀 /api/v2/
urlpatterns = [
path("tags/", v2.ExecTagListAPIView.as_view(), name="apiv2_tag_list"),
path("tags/<int:tag_id>/", v2.ExecTagDetailAPIView.as_view(), name="apiv1_tag_detail"),
path("nodes/", v2.NodeListAPIView.as_view(), name="apiv2_node_list"),
path("nodes/options/", v2.NodeOptionAPIView.as_view(), name="apiv2_node_option_list"),
path("nodes/<int:node_id>/", v2.NodeDetailAPIView.as_view(), name="apiv2_node_detail"),
path("nodes/<int:node_id>/processes/", v2.NodeProcessesAPIView.as_view(), name="apiv2_node_processes"),
path("nodes/<int:node_id>/tasks/", v2.NodeTaskListAPIView.as_view(), name="apiv2_node_task_list"),
path("tags/", v2.ExecTagListAPIView.as_view(),
name="apiv2_tag_list"),
path("tags/<int:tag_id>/", v2.ExecTagDetailAPIView.as_view(),
name="apiv2_tag_detail"),
path("tags/<int:tag_id>/processes/", v2.TagProcessesAPIView.as_view(),
name="apiv2_tag_processes"),
path("nodes/", v2.NodeListAPIView.as_view(),
name="apiv2_node_list"),
path("nodes/options/", v2.NodeOptionAPIView.as_view(),
name="apiv2_node_option_list"),
path("nodes/<int:node_id>/", v2.NodeDetailAPIView.as_view(),
name="apiv2_node_detail"),
path("nodes/<int:node_id>/processes/", v2.NodeProcessesAPIView.as_view(),
name="apiv2_node_processes"),
path("nodes/<int:node_id>/tasks/", v2.NodeTaskListAPIView.as_view(),
name="apiv2_node_task_list"),
path("nodes/processes/", v2.AllProcessesAPIView.as_view(),
name="apiv2_all_processes"),
path("nodes/processes/batchupdate/", v2.NodeProcessesBatchUpdateAPIView.as_view(),
Expand Down
6 changes: 4 additions & 2 deletions server/projects/main/apps/nodemgr/api_urls/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

# 前缀 /api/v3/orgs/<org_sid>/nodes/
urlpatterns = [
path("tags/", v3.OrgExecTagListView.as_view(), name="apiv3_tag_list"),
path("tags/<int:tag_id>/", v3.OrgExecTagDetailView.as_view(), name="apiv3_tag_detail"),
path("tags/", v3.OrgExecTagListAPIView.as_view(), name="apiv3_tag_list"),
path("tags/<int:tag_id>/", v3.OrgExecTagDetailAPIView.as_view(), name="apiv3_tag_detail"),
path("tags/<int:tag_id>/processes/", v3.OrgExecTagProcessesAPIView.as_view(),
name="apiv3_tag_processes"),

path("", v3.OrgNodeListAPIView.as_view(), name="apiv3_node_list"),
path("<int:node_id>/", v3.OrgNodeDetailAPIView.as_view(), name="apiv3_node_detail"),
Expand Down
4 changes: 2 additions & 2 deletions server/projects/main/apps/nodemgr/apis/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
from django_filters.rest_framework.backends import DjangoFilterBackend
from rest_framework import exceptions
from rest_framework import generics, status
from rest_framework.exceptions import ParseError, PermissionDenied
from rest_framework.filters import OrderingFilter
from rest_framework.generics import get_object_or_404
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.exceptions import PermissionDenied, ParseError

# 项目内 import
from apps.authen.backends import TCANodeTokenBackend
Expand Down Expand Up @@ -61,7 +61,7 @@ class NodeHeartBeatApiView(APIView):

def post(self, request, node_id):
node = get_object_or_404(models.Node, id=node_id)
logger.debug("[Node: %s][User: %s] upload node heart" % (node_id, request.user))
logger.debug("[Node: %s][User: %s] upload node heart" % (node, request.user))
if request.user != node.manager:
raise exceptions.PermissionDenied("非节点管理员无权操作")
if node.enabled == models.Node.EnabledEnum.DISABLED:
Expand Down
56 changes: 36 additions & 20 deletions server/projects/main/apps/nodemgr/apis/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from apps.nodemgr import filters, models
from apps.nodemgr.core import NodeManager
from apps.nodemgr.serializers import base as serializers
from apps.scan_conf.models import ToolProcessRelation
from util.permissions import IsSuperUserOrReadOnly

logger = logging.getLogger(__name__)
Expand All @@ -44,6 +43,8 @@ class ExecTagListAPIView(generics.ListCreateAPIView):
"""
permission_classes = [IsSuperUserOrReadOnly]
serializer_class = serializers.ExecTagSerializer
filter_backends = (DjangoFilterBackend,)
filterset_class = filters.TagFilter
queryset = models.ExecTag.objects.all()


Expand Down Expand Up @@ -156,19 +157,9 @@ def get_node(self, request, **kwargs):

def get(self, request, **kwargs):
node = self.get_node(request, **kwargs)
result = {}
for tool_process in ToolProcessRelation.objects.all():
processes = result.get(tool_process.checktool.name, {})
processes.update({tool_process.process.name: {"supported": False}})
result.update({tool_process.checktool.name: processes})
for node_tool_process in models.NodeToolProcessRelation.objects.filter(node=node):
try:
result[node_tool_process.checktool.name][node_tool_process.process.name]["supported"] = True
result[node_tool_process.checktool.name][node_tool_process.process.name]["id"] = node_tool_process.id
except Exception as e:
logger.exception("[Tool: %s][Process: %s] err: %s" % (
node_tool_process.checktool.name, node_tool_process.process.name, e))
return Response(result)
all_processes = NodeManager.get_all_processes()
all_processes = NodeManager.get_support_process_relations(all_processes, node)
return Response(all_processes)

def put(self, request, **kwargs):
node = self.get_node(request, **kwargs)
Expand Down Expand Up @@ -214,9 +205,34 @@ class AllProcessesAPIView(generics.GenericAPIView):
"""

def get(self, request, **kwargs):
result = {}
for tool_process in ToolProcessRelation.objects.all():
processes = result.get(tool_process.checktool.name, {})
processes.update({tool_process.process.name: {"supported": False}})
result.update({tool_process.checktool.name: processes})
return Response(result)
all_processes = NodeManager.get_all_processes()
return Response(all_processes)


class TagProcessesAPIView(APIView):
"""
### GET
应用场景:获取标签进程配置情况
### PUT
应用场景:修改标签进程配置,参数为get所得参数格式,按需修改supported的值为true or false即可
"""
permission_classes = [IsAdminUser]

def get_tag(self, request, **kwargs):
"""获取标签
"""
tag_id = kwargs["tag_id"]
return get_object_or_404(models.ExecTag, id=tag_id)

def get(self, request, **kwargs):
tag = self.get_tag(request, **kwargs)
all_processes = NodeManager.get_all_processes()
all_processes = NodeManager.get_support_process_relations(all_processes, tag)
return Response(all_processes)

def put(self, request, **kwargs):
tag = self.get_tag(request, **kwargs)
data = request.data
NodeManager.update_tag_processes(tag, data)
return Response(data)
60 changes: 54 additions & 6 deletions server/projects/main/apps/nodemgr/apis/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# ==============================================================================

"""
nodemgr - v2 apis
nodemgr - v3 apis
"""

# python 原生import
Expand All @@ -16,18 +16,21 @@
from django.db.models import Q
from django.shortcuts import get_object_or_404
from rest_framework import generics
from rest_framework.exceptions import NotFound
from rest_framework.response import Response

# 项目内 import
from apps.authen.permissions import OrganizationDefaultPermission
from apps.nodemgr import models
from apps.nodemgr.apis import v2 as api_v2
from apps.nodemgr.core import NodeManager
from apps.nodemgr.permissions import OrganizationNodeAdminPermission
from apps.nodemgr.serializers import v3 as serializers_v3

logger = logging.getLogger(__name__)


class OrgExecTagListView(generics.ListCreateAPIView):
class OrgExecTagListAPIView(api_v2.ExecTagListAPIView):
"""标签列表
### GET
Expand All @@ -44,8 +47,8 @@ def get_queryset(self):
return models.ExecTag.objects.filter(Q(org_sid=org_sid) | Q(org_sid__isnull=True))


class OrgExecTagDetailView(generics.RetrieveUpdateAPIView):
"""标签详情
class OrgExecTagDetailAPIView(generics.RetrieveUpdateAPIView):
"""团队标签详情
### GET
应用场景:获取标签详情
Expand All @@ -62,7 +65,45 @@ def get_object(self):
return get_object_or_404(models.ExecTag, id=tag_id, org_sid=org_sid)


class OrgExecTagProcessesAPIView(api_v2.TagProcessesAPIView):
"""团队标签进程列表
### GET
应用场景:获取指定标签的进程列表
### PUT
应用场景:修改标签进程配置,参数为get所得参数格式,按需修改supported的值为true or false即可
"""
permission_classes = [OrganizationDefaultPermission]

def get_tag(self, request, **kwargs):
"""获取标签
"""
org_sid = self.kwargs["org_sid"]
tag_id = kwargs["tag_id"]
tag = get_object_or_404(models.ExecTag, id=tag_id)
if tag.org_sid == org_sid or tag.org_sid is None:
return tag
else:
raise NotFound({"cd_error": "Tag-%s标签不存在" % tag_id})

def put(self, request, **kwargs):
"""修改标签进程配置
"""
tag = self.get_tag(request, **kwargs)
if tag.org_sid is None:
raise NotFound({"cd_error": "Tag-%s为公共标签,不能修改" % tag.display_name})
data = request.data
NodeManager.update_tag_processes(tag, data)
return Response(data)


class OrgNodeListAPIView(api_v2.NodeListAPIView):
"""指定团队的节点列表
### GET
应用场景:获取指定团队的节点列表
"""
permission_classes = [OrganizationDefaultPermission]

def get_queryset(self):
Expand All @@ -71,6 +112,11 @@ def get_queryset(self):


class OrgNodeDetailAPIView(api_v2.NodeDetailAPIView):
"""指定团队的节点详情
### GET
应用场景:获取指定团队的节点详情
"""
permission_classes = [OrganizationNodeAdminPermission]
serializer_class = serializers_v3.OrgNodeSerializer

Expand All @@ -81,7 +127,8 @@ def get_object(self):


class OrgNodeProcessesAPIView(api_v2.NodeProcessesAPIView):
"""
"""指定团队的节点进程列表
### GET
应用场景:获取节点进程配置情况
Expand All @@ -99,7 +146,8 @@ def get_node(self, request, **kwargs):


class OrgNodeTaskListAPIView(api_v2.NodeTaskListAPIView):
"""
"""指定团队的节点任务列表
### GET
应用场景:获取节点任务列表
"""
Expand Down
Loading

0 comments on commit f2784b0

Please sign in to comment.