Commit 17181db8 authored by ibuler's avatar ibuler

[Feature] 支持es存储

parents 41f1c3f7 67001dd9
......@@ -78,9 +78,6 @@ class BasicSettingForm(BaseForm):
max_length=1024, label=_("Email Subject Prefix"),
initial="[Jumpserver] "
)
AUTH_LDAP = forms.BooleanField(
label=_("Enable LDAP Auth"), initial=False, required=False
)
class EmailSettingForm(BaseForm):
......
......@@ -28,8 +28,6 @@ class BasicSettingView(AdminUserRequiredMixin, TemplateView):
form = self.form_class(request.POST)
if form.is_valid():
form.save()
if "AUTH_LDAP" in form.cleaned_data:
ldap_auth_enable.send(form.cleaned_data["AUTH_LDAP"])
msg = _("Update setting successfully, please restart program")
messages.success(request, msg)
return redirect('settings:basic-setting')
......@@ -82,6 +80,8 @@ class LDAPSettingView(AdminUserRequiredMixin, TemplateView):
form = self.form_class(request.POST)
if form.is_valid():
form.save()
if "AUTH_LDAP" in form.cleaned_data:
ldap_auth_enable.send(form.cleaned_data["AUTH_LDAP"])
msg = _("Update setting successfully, please restart program")
messages.success(request, msg)
return redirect('settings:ldap-setting')
......
......@@ -391,6 +391,7 @@ TERMINAL_COMMAND_STORAGE = {
# }
}
# Django bootstrap3 setting, more see http://django-bootstrap3.readthedocs.io/en/latest/settings.html
BOOTSTRAP3 = {
'horizontal_label_class': 'col-md-2',
......
This diff is collapsed.
# -*- coding: utf-8 -*-
#
from collections import OrderedDict
import copy
import logging
import os
import uuid
......@@ -21,7 +20,8 @@ from .serializers import TerminalSerializer, StatusSerializer, \
SessionSerializer, TaskSerializer, ReplaySerializer
from .hands import IsSuperUserOrAppUser, IsAppUser, \
IsSuperUserOrAppUserOrUserReadonly
from .backends import get_terminal_command_store, SessionCommandSerializer
from .backends import get_command_store, get_multi_command_store, \
SessionCommandSerializer
logger = logging.getLogger(__file__)
......@@ -196,7 +196,8 @@ class CommandViewSet(viewsets.ViewSet):
}
"""
command_store = get_terminal_command_store()
command_store = get_command_store()
multi_command_storage = get_multi_command_store()
serializer_class = SessionCommandSerializer
permission_classes = (IsSuperUserOrAppUser,)
......@@ -217,7 +218,7 @@ class CommandViewSet(viewsets.ViewSet):
return Response({"msg": msg}, status=401)
def list(self, request, *args, **kwargs):
queryset = list(self.command_store.all())
queryset = self.multi_command_storage.filter()
serializer = self.serializer_class(queryset, many=True)
return Response(serializer.data)
......
......@@ -3,7 +3,7 @@ from django.conf import settings
from .command.serializers import SessionCommandSerializer
TYPE_ENGINE_MAPPING = {
'elasticsearch': 'terminal.backends.command.db',
'elasticsearch': 'terminal.backends.command.es',
}
......@@ -31,4 +31,10 @@ def get_terminal_command_store():
return storage_list
def get_multi_command_store():
from .command.multi import CommandStore
storage_list = get_terminal_command_store().values()
storage = CommandStore(storage_list)
return storage
......@@ -73,7 +73,7 @@ class CommandStore(CommandBase):
session=session,
)
queryset = self.model.objects.filter(**filter_kwargs)
return queryset
return [command.to_dict() for command in queryset]
def count(self, date_from=None, date_to=None,
user=None, asset=None, system_user=None,
......
# -*- coding: utf-8 -*-
#
from jms_es_sdk import ESStore
from .base import CommandBase
class CommandStore(CommandBase, ESStore):
def __init__(self, params):
hosts = params.get('HOSTS', ['http://localhost'])
ESStore.__init__(self, hosts=hosts)
def save(self, command):
return ESStore.save(self, command)
def bulk_save(self, commands):
return ESStore.bulk_save(self, commands)
def filter(self, date_from=None, date_to=None,
user=None, asset=None, system_user=None,
input=None, session=None):
data = ESStore.filter(
self, date_from=date_from, date_to=date_to,
user=user, asset=asset, system_user=system_user,
input=input, session=session
)
return [item["_source"] for item in data["hits"] if item]
def count(self, date_from=None, date_to=None,
user=None, asset=None, system_user=None,
input=None, session=None):
amount = ESStore.count(
self, date_from=date_from, date_to=date_to,
user=user, asset=asset, system_user=system_user,
input=input, session=session
)
return amount
......@@ -33,5 +33,11 @@ class AbstractSessionCommand(models.Model):
commands.append(command)
return commands
def to_dict(self):
d = {}
for field in self._meta.fields:
d[field.name] = getattr(self, field.name)
return d
def __str__(self):
return self.input
# -*- coding: utf-8 -*-
#
from .base import CommandBase
class CommandStore(CommandBase):
def __init__(self, storage_list):
self.storage_list = storage_list
def filter(self, **kwargs):
queryset = []
for storage in self.storage_list:
queryset.extend(storage.filter(**kwargs))
return sorted(queryset, key=lambda command: command["timestamp"])
def count(self, **kwargs):
amount = 0
for storage in self.storage_list:
amount += storage.count(**kwargs)
return amount
def save(self, command):
pass
def bulk_save(self, commands):
pass
\ No newline at end of file
......@@ -11,10 +11,11 @@ from .backends.command.models import AbstractSessionCommand
def get_all_command_storage():
storage_choices = []
# storage_choices = []
from common.models import Setting
Setting.refresh_all_settings()
for k, v in settings.TERMINAL_COMMAND_STORAGE.items():
storage_choices.append((k, k))
return storage_choices
yield (k, k)
class Terminal(models.Model):
......
......@@ -5,7 +5,7 @@ from django.utils import timezone
from rest_framework import serializers
from .models import Terminal, Status, Session, Task
from .backends import get_terminal_command_store
from .backends import get_multi_command_store
class TerminalSerializer(serializers.ModelSerializer):
......@@ -43,14 +43,14 @@ class TerminalSerializer(serializers.ModelSerializer):
class SessionSerializer(serializers.ModelSerializer):
command_amount = serializers.SerializerMethodField()
command_store = get_terminal_command_store()
command_store = get_multi_command_store()
class Meta:
model = Session
fields = '__all__'
def get_command_amount(self, obj):
return len(self.command_store.filter(session=obj.session))
return self.command_store.count(session=str(obj.id))
class StatusSerializer(serializers.ModelSerializer):
......
# ~*~ coding: utf-8 ~*~
from django import template
from ..backends import get_terminal_command_store
from ..backends import get_multi_command_store
register = template.Library()
command_store_dict = get_terminal_command_store()
command_store = get_multi_command_store()
@register.filter
def get_session_command_amount(session_id):
amount = 0
for name, store in command_store_dict.items():
amount += store.count(session=str(session_id))
return amount
return command_store.count(session=session_id)
......@@ -9,10 +9,10 @@ from django.utils.translation import ugettext as _
from common.mixins import DatetimeSearchMixin
from ..models import Command
from .. import utils
from ..backends import get_terminal_command_store
from ..backends import get_multi_command_store
__all__ = ['CommandListView']
command_store_list = get_terminal_command_store()
common_storage = get_multi_command_store()
class CommandListView(DatetimeSearchMixin, ListView):
......@@ -39,10 +39,7 @@ class CommandListView(DatetimeSearchMixin, ListView):
filter_kwargs['system_user'] = self.system_user
if self.command:
filter_kwargs['input'] = self.command
queryset = []
for store in command_store_list:
queryset.extend(store.filter(**filter_kwargs))
queryset = sorted(queryset, key=lambda c: c.timestamp, reverse=True)
queryset = common_storage.filter(**filter_kwargs)
return queryset
def get_context_data(self, **kwargs):
......
......@@ -10,7 +10,7 @@ from django.conf import settings
from users.utils import AdminUserRequiredMixin
from common.mixins import DatetimeSearchMixin
from ..models import Session, Command, Terminal
from ..backends import get_terminal_command_store
from ..backends import get_multi_command_store
from .. import utils
......@@ -19,7 +19,7 @@ __all__ = [
'SessionDetailView',
]
command_store = get_terminal_command_store()
command_store = get_multi_command_store()
class SessionListView(AdminUserRequiredMixin, DatetimeSearchMixin, ListView):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment