2018-12-10 02:11:54 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from rest_framework import viewsets
|
2019-08-21 12:27:21 +00:00
|
|
|
from rest_framework.exceptions import ValidationError
|
2019-02-13 07:55:11 +00:00
|
|
|
from django.db import transaction
|
2020-08-16 15:08:58 +00:00
|
|
|
from django.db.models import Q
|
2019-08-21 12:27:21 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
2019-06-11 05:30:45 +00:00
|
|
|
from django.conf import settings
|
2018-12-10 02:11:54 +00:00
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
from assets.models import Asset, Node
|
2019-08-21 12:27:21 +00:00
|
|
|
from orgs.mixins.api import RootOrgViewMixin
|
2018-12-10 02:11:54 +00:00
|
|
|
from common.permissions import IsValidUser
|
2022-03-14 12:31:05 +00:00
|
|
|
from rbac.permissions import RBACPermission
|
2018-12-10 02:11:54 +00:00
|
|
|
from ..models import CommandExecution
|
|
|
|
from ..serializers import CommandExecutionSerializer
|
|
|
|
from ..tasks import run_command_execution
|
|
|
|
|
|
|
|
|
2019-08-01 09:10:02 +00:00
|
|
|
class CommandExecutionViewSet(RootOrgViewMixin, viewsets.ModelViewSet):
|
2018-12-10 02:11:54 +00:00
|
|
|
serializer_class = CommandExecutionSerializer
|
2022-03-14 12:31:05 +00:00
|
|
|
permission_classes = (RBACPermission,)
|
2018-12-10 02:11:54 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
2022-03-14 12:31:05 +00:00
|
|
|
return CommandExecution.objects.filter(user_id=str(self.request.user.id))
|
2018-12-10 02:11:54 +00:00
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
def check_hosts(self, serializer):
|
|
|
|
data = serializer.validated_data
|
|
|
|
assets = data["hosts"]
|
|
|
|
system_user = data["run_as"]
|
2020-08-16 15:08:58 +00:00
|
|
|
user = self.request.user
|
|
|
|
|
|
|
|
q = Q(granted_by_permissions__system_users__id=system_user.id) & (
|
|
|
|
Q(granted_by_permissions__users=user) |
|
|
|
|
Q(granted_by_permissions__user_groups__users=user)
|
|
|
|
)
|
|
|
|
|
|
|
|
permed_assets = set()
|
2022-03-14 12:31:05 +00:00
|
|
|
permed_assets.update(Asset.objects.filter(id__in=[a.id for a in assets]).filter(q).distinct())
|
2020-08-16 15:08:58 +00:00
|
|
|
node_keys = Node.objects.filter(q).distinct().values_list('key', flat=True)
|
|
|
|
|
|
|
|
nodes_assets_q = Q()
|
|
|
|
for _key in node_keys:
|
|
|
|
nodes_assets_q |= Q(nodes__key__startswith=f'{_key}:')
|
|
|
|
nodes_assets_q |= Q(nodes__key=_key)
|
|
|
|
|
|
|
|
permed_assets.update(
|
|
|
|
Asset.objects.filter(
|
|
|
|
id__in=[a.id for a in assets]
|
|
|
|
).filter(
|
|
|
|
nodes_assets_q
|
|
|
|
).distinct()
|
|
|
|
)
|
|
|
|
|
2019-09-17 04:34:47 +00:00
|
|
|
invalid_assets = set(assets) - set(permed_assets)
|
|
|
|
if invalid_assets:
|
2019-08-21 12:27:21 +00:00
|
|
|
msg = _("Not has host {} permission").format(
|
2019-09-17 04:34:47 +00:00
|
|
|
[str(a.id) for a in invalid_assets]
|
2019-08-21 12:27:21 +00:00
|
|
|
)
|
|
|
|
raise ValidationError({"hosts": msg})
|
|
|
|
|
2019-06-11 05:30:45 +00:00
|
|
|
def check_permissions(self, request):
|
2022-04-02 07:51:23 +00:00
|
|
|
if not settings.SECURITY_COMMAND_EXECUTION:
|
2019-06-11 05:30:45 +00:00
|
|
|
return self.permission_denied(request, "Command execution disabled")
|
|
|
|
return super().check_permissions(request)
|
|
|
|
|
2018-12-10 02:11:54 +00:00
|
|
|
def perform_create(self, serializer):
|
2019-08-21 12:27:21 +00:00
|
|
|
self.check_hosts(serializer)
|
2018-12-10 02:11:54 +00:00
|
|
|
instance = serializer.save()
|
|
|
|
instance.user = self.request.user
|
|
|
|
instance.save()
|
2019-08-21 12:27:21 +00:00
|
|
|
cols = self.request.query_params.get("cols", '80')
|
|
|
|
rows = self.request.query_params.get("rows", '24')
|
2019-02-13 07:55:11 +00:00
|
|
|
transaction.on_commit(lambda: run_command_execution.apply_async(
|
2019-08-21 12:27:21 +00:00
|
|
|
args=(instance.id,), kwargs={"cols": cols, "rows": rows},
|
|
|
|
task_id=str(instance.id)
|
2019-02-13 07:55:11 +00:00
|
|
|
))
|