diff --git a/apps/common/api.py b/apps/common/api.py index e09cf9726..51416fa3b 100644 --- a/apps/common/api.py +++ b/apps/common/api.py @@ -1,6 +1,9 @@ # -*- coding: utf-8 -*- # + +import os import json +import jms_storage from rest_framework.views import Response, APIView from ldap3 import Server, Connection @@ -8,8 +11,9 @@ from django.core.mail import get_connection, send_mail from django.utils.translation import ugettext_lazy as _ from django.conf import settings -from .permissions import IsOrgAdmin +from .permissions import IsOrgAdmin, IsSuperUser from .serializers import MailTestSerializer, LDAPTestSerializer +from .models import Setting class MailTestingAPI(APIView): @@ -85,6 +89,79 @@ class LDAPTestingAPI(APIView): return Response({"error": str(serializer.errors)}, status=401) +class ReplayStorageCreateAPI(APIView): + permission_classes = (IsSuperUser,) + + def post(self, request): + storage_data = request.data + + if storage_data.get('TYPE') == 'ceph': + port = storage_data.get('PORT') + if port.isdigit(): + storage_data['PORT'] = int(storage_data.get('PORT')) + + storage_name = storage_data.pop('NAME') + data = {storage_name: storage_data} + + if not self.is_valid(storage_data): + return Response({"error": _("Error: Account invalid")}, status=401) + + Setting.save_storage('TERMINAL_REPLAY_STORAGE', data) + return Response({"msg": _('Create succeed')}, status=200) + + @staticmethod + def is_valid(storage_data): + if storage_data.get('TYPE') == 'server': + return True + storage = jms_storage.get_object_storage(storage_data) + target = 'tests.py' + src = os.path.join(settings.BASE_DIR, 'common', target) + return storage.is_valid(src, target) + + +class ReplayStorageDeleteAPI(APIView): + permission_classes = (IsSuperUser,) + + def post(self, request): + storage_name = str(request.data.get('name')) + Setting.delete_storage('TERMINAL_REPLAY_STORAGE', storage_name) + return Response({"msg": _('Delete succeed')}, status=200) + + +class CommandStorageCreateAPI(APIView): + permission_classes = (IsSuperUser,) + + def post(self, request): + storage_data = request.data + storage_name = storage_data.pop('NAME') + data = {storage_name: storage_data} + if not self.is_valid(storage_data): + return Response({"error": _("Error: Account invalid")}, status=401) + + Setting.save_storage('TERMINAL_COMMAND_STORAGE', data) + return Response({"msg": _('Create succeed')}, status=200) + + @staticmethod + def is_valid(storage_data): + if storage_data.get('TYPE') == 'server': + return True + try: + storage = jms_storage.get_log_storage(storage_data) + except Exception: + return False + + return storage.ping() + + +class CommandStorageDeleteAPI(APIView): + permission_classes = (IsSuperUser,) + + def post(self, request): + storage_name = str(request.data.get('name')) + Setting.delete_storage('TERMINAL_COMMAND_STORAGE', storage_name) + return Response({"msg": _('Delete succeed')}, status=200) + + class DjangoSettingsAPI(APIView): def get(self, request): if not settings.DEBUG: diff --git a/apps/common/forms.py b/apps/common/forms.py index 610e1a83e..10ac4ec17 100644 --- a/apps/common/forms.py +++ b/apps/common/forms.py @@ -135,32 +135,24 @@ class TerminalSettingForm(BaseForm): ('hostname', _('Hostname')), ('ip', _('IP')), ) - TERMINAL_ASSET_LIST_SORT_BY = forms.ChoiceField( - choices=SORT_BY_CHOICES, initial='hostname', label=_("List sort by") - ) - TERMINAL_HEARTBEAT_INTERVAL = forms.IntegerField( - initial=5, label=_("Heartbeat interval"), help_text=_("Units: seconds") - ) TERMINAL_PASSWORD_AUTH = forms.BooleanField( initial=True, required=False, label=_("Password auth") ) TERMINAL_PUBLIC_KEY_AUTH = forms.BooleanField( initial=True, required=False, label=_("Public key auth") ) - TERMINAL_COMMAND_STORAGE = FormEncryptDictField( - label=_("Command storage"), help_text=_( - "Set terminal storage setting, `default` is the using as default," - "You can set other storage and some terminal using" - ) + TERMINAL_HEARTBEAT_INTERVAL = forms.IntegerField( + initial=5, label=_("Heartbeat interval"), help_text=_("Units: seconds") ) - TERMINAL_REPLAY_STORAGE = FormEncryptDictField( - label=_("Replay storage"), help_text=_( - "Set replay storage setting, `default` is the using as default," - "You can set other storage and some terminal using" - ) + TERMINAL_ASSET_LIST_SORT_BY = forms.ChoiceField( + choices=SORT_BY_CHOICES, initial='hostname', label=_("List sort by") ) +class TerminalCommandStorage(BaseForm): + pass + + class SecuritySettingForm(BaseForm): # MFA global setting SECURITY_MFA_AUTH = forms.BooleanField( diff --git a/apps/common/models.py b/apps/common/models.py index 61f5512c9..812d491a9 100644 --- a/apps/common/models.py +++ b/apps/common/models.py @@ -67,6 +67,30 @@ class Setting(models.Model): except json.JSONDecodeError as e: raise ValueError("Json dump error: {}".format(str(e))) + @classmethod + def save_storage(cls, name, data): + obj = cls.objects.filter(name=name).first() + if not obj: + obj = cls() + obj.name = name + obj.encrypted = True + obj.cleaned_value = data + else: + value = obj.cleaned_value + value.update(data) + obj.cleaned_value = value + obj.save() + return obj + + @classmethod + def delete_storage(cls, name, storage_name): + obj = cls.objects.get(name=name) + value = obj.cleaned_value + value.pop(storage_name, '') + obj.cleaned_value = value + obj.save() + return True + @classmethod def refresh_all_settings(cls): try: diff --git a/apps/common/tasks.py b/apps/common/tasks.py index bfb005511..00420bc8b 100644 --- a/apps/common/tasks.py +++ b/apps/common/tasks.py @@ -3,6 +3,7 @@ from django.conf import settings from celery import shared_task from .utils import get_logger from .models import Setting +from common.models import common_settings logger = get_logger(__file__) @@ -28,7 +29,7 @@ def send_mail_async(*args, **kwargs): if len(args) == 3: args = list(args) - args[0] = settings.EMAIL_SUBJECT_PREFIX + args[0] + args[0] = common_settings.EMAIL_SUBJECT_PREFIX + args[0] args.insert(2, settings.EMAIL_HOST_USER) args = tuple(args) diff --git a/apps/common/templates/common/basic_setting.html b/apps/common/templates/common/basic_setting.html index 9c9258e33..17c8057bc 100644 --- a/apps/common/templates/common/basic_setting.html +++ b/apps/common/templates/common/basic_setting.html @@ -75,32 +75,6 @@ {% block custom_foot_js %} {% endblock %} diff --git a/apps/common/templates/common/command_storage_create.html b/apps/common/templates/common/command_storage_create.html new file mode 100644 index 000000000..a3a83f2e7 --- /dev/null +++ b/apps/common/templates/common/command_storage_create.html @@ -0,0 +1,176 @@ +{#{% extends 'base.html' %}#} +{% extends '_base_create_update.html' %} +{% load static %} +{% load bootstrap3 %} +{% load i18n %} +{% load common_tags %} + +{% block content %} +
+{% endblock %} + +{% block custom_foot_js %} + +{% endblock %} diff --git a/apps/common/templates/common/replay_storage_create.html b/apps/common/templates/common/replay_storage_create.html new file mode 100644 index 000000000..6382d27e1 --- /dev/null +++ b/apps/common/templates/common/replay_storage_create.html @@ -0,0 +1,248 @@ +{#{% extends 'base.html' %}#} +{% extends '_base_create_update.html' %} +{% load static %} +{% load bootstrap3 %} +{% load i18n %} +{% load common_tags %} + +{% block content %} + +{% endblock %} + +{% block custom_foot_js %} + +{% endblock %} diff --git a/apps/common/templates/common/terminal_setting.html b/apps/common/templates/common/terminal_setting.html index 320f628b0..0435e48e9 100644 --- a/apps/common/templates/common/terminal_setting.html +++ b/apps/common/templates/common/terminal_setting.html @@ -63,6 +63,14 @@ {% endif %} {% endfor %} +{% trans 'Name' %} | {% trans 'Type' %} | +{% trans 'Action' %} |
---|---|---|
{{ name }} | {{ setting.TYPE }} | +{% trans 'Delete' %} |