2018-02-06 10:32:02 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
2018-09-03 03:24:25 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
2018-02-06 10:32:02 +00:00
|
|
|
from rest_framework.response import Response
|
2018-07-23 04:55:13 +00:00
|
|
|
|
2018-02-06 10:32:02 +00:00
|
|
|
from common.utils import get_logger
|
2021-05-31 07:01:14 +00:00
|
|
|
from common.permissions import IsOrgAdmin, IsOrgAdminOrAppUser, IsValidUser
|
2019-08-21 12:27:21 +00:00
|
|
|
from orgs.mixins.api import OrgBulkModelViewSet
|
2019-10-18 07:05:45 +00:00
|
|
|
from orgs.mixins import generics
|
2021-06-07 02:40:07 +00:00
|
|
|
from orgs.utils import tmp_to_root_org
|
2018-09-03 03:24:25 +00:00
|
|
|
from ..models import SystemUser, Asset
|
2018-02-06 10:32:02 +00:00
|
|
|
from .. import serializers
|
2021-05-31 07:01:14 +00:00
|
|
|
from ..serializers import SystemUserWithAuthInfoSerializer, SystemUserTempAuthSerializer
|
2019-08-21 12:27:21 +00:00
|
|
|
from ..tasks import (
|
|
|
|
push_system_user_to_assets_manual, test_system_user_connectivity_manual,
|
2020-12-08 09:29:22 +00:00
|
|
|
push_system_user_to_assets
|
2019-08-21 12:27:21 +00:00
|
|
|
)
|
2018-02-06 10:32:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
__all__ = [
|
2019-03-18 02:15:33 +00:00
|
|
|
'SystemUserViewSet', 'SystemUserAuthInfoApi', 'SystemUserAssetAuthInfoApi',
|
2020-11-09 11:17:00 +00:00
|
|
|
'SystemUserCommandFilterRuleListApi', 'SystemUserTaskApi', 'SystemUserAssetsListView',
|
2021-05-31 07:01:14 +00:00
|
|
|
'SystemUserTempAuthInfoApi', 'SystemUserAppAuthInfoApi',
|
2018-02-06 10:32:02 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-07-04 07:36:57 +00:00
|
|
|
class SystemUserViewSet(OrgBulkModelViewSet):
|
2018-02-06 10:32:02 +00:00
|
|
|
"""
|
|
|
|
System user api set, for add,delete,update,list,retrieve resource
|
|
|
|
"""
|
2019-10-18 07:05:45 +00:00
|
|
|
model = SystemUser
|
2021-01-07 11:05:55 +00:00
|
|
|
filterset_fields = {
|
|
|
|
'name': ['exact'],
|
|
|
|
'username': ['exact'],
|
2021-07-08 06:23:18 +00:00
|
|
|
'protocol': ['exact', 'in'],
|
|
|
|
'type': ['exact', 'in'],
|
2021-01-07 11:05:55 +00:00
|
|
|
}
|
2021-01-07 02:53:10 +00:00
|
|
|
search_fields = filterset_fields
|
2018-02-06 10:32:02 +00:00
|
|
|
serializer_class = serializers.SystemUserSerializer
|
2020-03-12 08:24:38 +00:00
|
|
|
serializer_classes = {
|
|
|
|
'default': serializers.SystemUserSerializer,
|
|
|
|
}
|
2018-07-23 04:55:13 +00:00
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
2018-10-31 07:31:09 +00:00
|
|
|
|
2018-02-06 10:32:02 +00:00
|
|
|
|
2018-05-17 03:39:04 +00:00
|
|
|
class SystemUserAuthInfoApi(generics.RetrieveUpdateDestroyAPIView):
|
2018-02-06 10:32:02 +00:00
|
|
|
"""
|
|
|
|
Get system user auth info
|
|
|
|
"""
|
2019-10-18 07:05:45 +00:00
|
|
|
model = SystemUser
|
2018-07-23 04:55:13 +00:00
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
2020-03-12 08:24:38 +00:00
|
|
|
serializer_class = SystemUserWithAuthInfoSerializer
|
2018-02-06 10:32:02 +00:00
|
|
|
|
2018-05-17 03:39:04 +00:00
|
|
|
def destroy(self, request, *args, **kwargs):
|
|
|
|
instance = self.get_object()
|
|
|
|
instance.clear_auth()
|
|
|
|
return Response(status=204)
|
|
|
|
|
2018-02-06 10:32:02 +00:00
|
|
|
|
2021-05-31 07:01:14 +00:00
|
|
|
class SystemUserTempAuthInfoApi(generics.CreateAPIView):
|
|
|
|
model = SystemUser
|
|
|
|
permission_classes = (IsValidUser,)
|
|
|
|
serializer_class = SystemUserTempAuthSerializer
|
|
|
|
|
|
|
|
def create(self, request, *args, **kwargs):
|
|
|
|
serializer = super().get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
pk = kwargs.get('pk')
|
|
|
|
user = self.request.user
|
2021-06-07 02:40:07 +00:00
|
|
|
data = serializer.validated_data
|
2021-05-31 07:01:14 +00:00
|
|
|
instance_id = data.get('instance_id')
|
2021-06-07 02:40:07 +00:00
|
|
|
|
|
|
|
with tmp_to_root_org():
|
|
|
|
instance = get_object_or_404(SystemUser, pk=pk)
|
|
|
|
instance.set_temp_auth(instance_id, user, data)
|
2021-05-31 07:01:14 +00:00
|
|
|
return Response(serializer.data, status=201)
|
|
|
|
|
|
|
|
|
2019-03-18 02:15:33 +00:00
|
|
|
class SystemUserAssetAuthInfoApi(generics.RetrieveAPIView):
|
|
|
|
"""
|
|
|
|
Get system user with asset auth info
|
|
|
|
"""
|
2019-10-18 07:05:45 +00:00
|
|
|
model = SystemUser
|
2020-03-12 08:24:38 +00:00
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
|
|
|
serializer_class = SystemUserWithAuthInfoSerializer
|
|
|
|
|
2021-05-31 07:01:14 +00:00
|
|
|
def get_object(self):
|
|
|
|
instance = super().get_object()
|
|
|
|
asset_id = self.kwargs.get('asset_id')
|
|
|
|
user_id = self.request.query_params.get("user_id")
|
|
|
|
username = self.request.query_params.get("username")
|
|
|
|
instance.load_asset_more_auth(asset_id=asset_id, user_id=user_id, username=username)
|
|
|
|
return instance
|
|
|
|
|
|
|
|
|
|
|
|
class SystemUserAppAuthInfoApi(generics.RetrieveAPIView):
|
|
|
|
"""
|
|
|
|
Get system user with asset auth info
|
|
|
|
"""
|
|
|
|
model = SystemUser
|
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
|
|
|
serializer_class = SystemUserWithAuthInfoSerializer
|
2019-03-18 02:15:33 +00:00
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
instance = super().get_object()
|
2021-05-31 07:01:14 +00:00
|
|
|
app_id = self.kwargs.get('app_id')
|
|
|
|
user_id = self.request.query_params.get("user_id")
|
|
|
|
if user_id:
|
|
|
|
instance.load_app_more_auth(app_id, user_id)
|
|
|
|
return instance
|
2018-02-06 10:32:02 +00:00
|
|
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
class SystemUserTaskApi(generics.CreateAPIView):
|
2018-07-23 04:55:13 +00:00
|
|
|
permission_classes = (IsOrgAdmin,)
|
2020-03-12 08:24:38 +00:00
|
|
|
serializer_class = serializers.SystemUserTaskSerializer
|
|
|
|
|
2021-03-08 02:08:51 +00:00
|
|
|
def do_push(self, system_user, asset_ids=None):
|
|
|
|
if asset_ids is None:
|
2020-03-12 08:24:38 +00:00
|
|
|
task = push_system_user_to_assets_manual.delay(system_user)
|
|
|
|
else:
|
|
|
|
username = self.request.query_params.get('username')
|
2020-12-08 09:29:22 +00:00
|
|
|
task = push_system_user_to_assets.delay(
|
2021-03-08 02:08:51 +00:00
|
|
|
system_user.id, asset_ids, username=username
|
2020-03-12 08:24:38 +00:00
|
|
|
)
|
|
|
|
return task
|
|
|
|
|
|
|
|
@staticmethod
|
2021-05-19 09:59:15 +00:00
|
|
|
def do_test(system_user, asset_ids):
|
|
|
|
task = test_system_user_connectivity_manual.delay(system_user, asset_ids)
|
2020-03-12 08:24:38 +00:00
|
|
|
return task
|
2018-09-03 03:24:25 +00:00
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
pk = self.kwargs.get('pk')
|
|
|
|
return get_object_or_404(SystemUser, pk=pk)
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
def perform_create(self, serializer):
|
|
|
|
action = serializer.validated_data["action"]
|
|
|
|
asset = serializer.validated_data.get('asset')
|
2021-05-19 09:59:15 +00:00
|
|
|
|
|
|
|
if asset:
|
|
|
|
assets = [asset]
|
|
|
|
else:
|
|
|
|
assets = serializer.validated_data.get('assets') or []
|
|
|
|
|
|
|
|
asset_ids = [asset.id for asset in assets]
|
|
|
|
asset_ids = asset_ids if asset_ids else None
|
2020-12-08 09:29:22 +00:00
|
|
|
|
2018-09-03 03:24:25 +00:00
|
|
|
system_user = self.get_object()
|
2020-03-12 08:24:38 +00:00
|
|
|
if action == 'push':
|
2021-03-08 02:08:51 +00:00
|
|
|
task = self.do_push(system_user, asset_ids)
|
2020-03-12 08:24:38 +00:00
|
|
|
else:
|
2021-05-19 09:59:15 +00:00
|
|
|
task = self.do_test(system_user, asset_ids)
|
2020-03-12 08:24:38 +00:00
|
|
|
data = getattr(serializer, '_data', {})
|
|
|
|
data["task"] = task.id
|
|
|
|
setattr(serializer, '_data', data)
|
2018-10-10 07:37:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SystemUserCommandFilterRuleListApi(generics.ListAPIView):
|
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
|
|
|
|
|
|
|
def get_serializer_class(self):
|
|
|
|
from ..serializers import CommandFilterRuleSerializer
|
|
|
|
return CommandFilterRuleSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
pk = self.kwargs.get('pk', None)
|
|
|
|
system_user = get_object_or_404(SystemUser, pk=pk)
|
|
|
|
return system_user.cmd_filter_rules
|
2020-11-09 11:17:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SystemUserAssetsListView(generics.ListAPIView):
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
|
|
|
serializer_class = serializers.AssetSimpleSerializer
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = ("hostname", "ip")
|
|
|
|
search_fields = filterset_fields
|
2020-11-09 11:17:00 +00:00
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
pk = self.kwargs.get('pk')
|
|
|
|
return get_object_or_404(SystemUser, pk=pk)
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
system_user = self.get_object()
|
|
|
|
return system_user.get_all_assets()
|