2018-01-11 12:10:27 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2018-10-23 11:22:18 +00:00
|
|
|
|
|
|
|
import os
|
2018-01-12 07:43:26 +00:00
|
|
|
import json
|
2018-10-23 11:22:18 +00:00
|
|
|
import jms_storage
|
2018-01-12 07:43:26 +00:00
|
|
|
|
2018-04-02 05:19:31 +00:00
|
|
|
from rest_framework.views import Response, APIView
|
2018-01-12 07:43:26 +00:00
|
|
|
from ldap3 import Server, Connection
|
2018-01-11 12:10:27 +00:00
|
|
|
from django.core.mail import get_connection, send_mail
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2018-01-12 07:43:26 +00:00
|
|
|
from django.conf import settings
|
2018-01-11 12:10:27 +00:00
|
|
|
|
2018-10-27 18:17:46 +00:00
|
|
|
from .permissions import IsOrgAdmin, IsSuperUser
|
2018-01-12 07:43:26 +00:00
|
|
|
from .serializers import MailTestSerializer, LDAPTestSerializer
|
2018-10-23 11:22:18 +00:00
|
|
|
from .models import Setting
|
2018-01-11 12:10:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MailTestingAPI(APIView):
|
2018-07-23 04:55:13 +00:00
|
|
|
permission_classes = (IsOrgAdmin,)
|
2018-01-11 12:10:27 +00:00
|
|
|
serializer_class = MailTestSerializer
|
|
|
|
success_message = _("Test mail sent to {}, please check")
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
serializer = self.serializer_class(data=request.data)
|
|
|
|
if serializer.is_valid():
|
|
|
|
email_host_user = serializer.validated_data["EMAIL_HOST_USER"]
|
2018-05-29 10:58:56 +00:00
|
|
|
for k, v in serializer.validated_data.items():
|
|
|
|
if k.startswith('EMAIL'):
|
|
|
|
setattr(settings, k, v)
|
2018-01-11 12:10:27 +00:00
|
|
|
try:
|
2018-05-29 10:58:56 +00:00
|
|
|
subject = "Test"
|
|
|
|
message = "Test smtp setting"
|
|
|
|
send_mail(subject, message, email_host_user, [email_host_user])
|
2018-01-11 12:10:27 +00:00
|
|
|
except Exception as e:
|
|
|
|
return Response({"error": str(e)}, status=401)
|
|
|
|
|
|
|
|
return Response({"msg": self.success_message.format(email_host_user)})
|
2018-01-12 07:43:26 +00:00
|
|
|
else:
|
|
|
|
return Response({"error": str(serializer.errors)}, status=401)
|
|
|
|
|
|
|
|
|
|
|
|
class LDAPTestingAPI(APIView):
|
2018-07-23 04:55:13 +00:00
|
|
|
permission_classes = (IsOrgAdmin,)
|
2018-01-12 07:43:26 +00:00
|
|
|
serializer_class = LDAPTestSerializer
|
|
|
|
success_message = _("Test ldap success")
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
serializer = self.serializer_class(data=request.data)
|
|
|
|
if serializer.is_valid():
|
|
|
|
host = serializer.validated_data["AUTH_LDAP_SERVER_URI"]
|
|
|
|
bind_dn = serializer.validated_data["AUTH_LDAP_BIND_DN"]
|
|
|
|
password = serializer.validated_data["AUTH_LDAP_BIND_PASSWORD"]
|
|
|
|
use_ssl = serializer.validated_data.get("AUTH_LDAP_START_TLS", False)
|
2018-08-07 10:11:46 +00:00
|
|
|
search_ougroup = serializer.validated_data["AUTH_LDAP_SEARCH_OU"]
|
2018-01-12 07:43:26 +00:00
|
|
|
search_filter = serializer.validated_data["AUTH_LDAP_SEARCH_FILTER"]
|
|
|
|
attr_map = serializer.validated_data["AUTH_LDAP_USER_ATTR_MAP"]
|
|
|
|
|
|
|
|
try:
|
|
|
|
attr_map = json.loads(attr_map)
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
return Response({"error": "AUTH_LDAP_USER_ATTR_MAP not valid"}, status=401)
|
|
|
|
|
|
|
|
server = Server(host, use_ssl=use_ssl)
|
|
|
|
conn = Connection(server, bind_dn, password)
|
|
|
|
try:
|
|
|
|
conn.bind()
|
|
|
|
except Exception as e:
|
|
|
|
return Response({"error": str(e)}, status=401)
|
|
|
|
|
|
|
|
users = []
|
2018-08-07 10:11:46 +00:00
|
|
|
for search_ou in str(search_ougroup).split("|"):
|
|
|
|
ok = conn.search(search_ou, search_filter % ({"user": "*"}),
|
|
|
|
attributes=list(attr_map.values()))
|
|
|
|
if not ok:
|
|
|
|
return Response({"error": _("Search no entry matched in ou {}").format(search_ou)}, status=401)
|
|
|
|
|
|
|
|
for entry in conn.entries:
|
|
|
|
user = {}
|
|
|
|
for attr, mapping in attr_map.items():
|
|
|
|
if hasattr(entry, mapping):
|
|
|
|
user[attr] = getattr(entry, mapping)
|
|
|
|
users.append(user)
|
2018-01-12 07:43:26 +00:00
|
|
|
if len(users) > 0:
|
2018-02-01 04:37:45 +00:00
|
|
|
return Response({"msg": _("Match {} s users").format(len(users))})
|
2018-01-12 07:43:26 +00:00
|
|
|
else:
|
|
|
|
return Response({"error": "Have user but attr mapping error"}, status=401)
|
|
|
|
else:
|
|
|
|
return Response({"error": str(serializer.errors)}, status=401)
|
|
|
|
|
|
|
|
|
2018-10-23 11:22:18 +00:00
|
|
|
class ReplayStorageCreateAPI(APIView):
|
2018-10-27 18:17:46 +00:00
|
|
|
permission_classes = (IsSuperUser,)
|
2018-10-23 11:22:18 +00:00
|
|
|
|
|
|
|
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):
|
2018-12-26 04:57:59 +00:00
|
|
|
return Response({
|
|
|
|
"error": _("Error: Account invalid (Please make sure the "
|
|
|
|
"information such as Access key or Secret key is correct)")},
|
|
|
|
status=401
|
|
|
|
)
|
2018-10-23 11:22:18 +00:00
|
|
|
|
|
|
|
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)
|
2018-10-23 12:41:01 +00:00
|
|
|
return storage.is_valid(src, target)
|
2018-10-23 11:22:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ReplayStorageDeleteAPI(APIView):
|
2018-10-27 18:17:46 +00:00
|
|
|
permission_classes = (IsSuperUser,)
|
2018-10-23 11:22:18 +00:00
|
|
|
|
|
|
|
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):
|
2018-10-27 18:17:46 +00:00
|
|
|
permission_classes = (IsSuperUser,)
|
2018-10-23 11:22:18 +00:00
|
|
|
|
|
|
|
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):
|
2018-12-26 04:57:59 +00:00
|
|
|
return Response(
|
|
|
|
{"error": _("Error: Account invalid (Please make sure the "
|
|
|
|
"information such as Access key or Secret key is correct)")},
|
|
|
|
status=401
|
|
|
|
)
|
2018-10-23 11:22:18 +00:00
|
|
|
|
|
|
|
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
|
2018-10-24 02:29:40 +00:00
|
|
|
try:
|
|
|
|
storage = jms_storage.get_log_storage(storage_data)
|
|
|
|
except Exception:
|
|
|
|
return False
|
|
|
|
|
2018-10-23 11:22:18 +00:00
|
|
|
return storage.ping()
|
|
|
|
|
|
|
|
|
|
|
|
class CommandStorageDeleteAPI(APIView):
|
2018-10-27 18:17:46 +00:00
|
|
|
permission_classes = (IsSuperUser,)
|
2018-10-23 11:22:18 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2018-01-12 07:43:26 +00:00
|
|
|
class DjangoSettingsAPI(APIView):
|
|
|
|
def get(self, request):
|
2018-07-25 03:21:12 +00:00
|
|
|
if not settings.DEBUG:
|
|
|
|
return Response("Not in debug mode")
|
|
|
|
|
|
|
|
data = {}
|
2018-11-22 04:27:27 +00:00
|
|
|
for i in [settings, getattr(settings, '_wrapped')]:
|
|
|
|
if not i:
|
|
|
|
continue
|
|
|
|
for k, v in i.__dict__.items():
|
|
|
|
if k and k.isupper():
|
|
|
|
try:
|
|
|
|
json.dumps(v)
|
|
|
|
data[k] = v
|
|
|
|
except (json.JSONDecodeError, TypeError):
|
|
|
|
data[k] = str(v)
|
2018-07-25 03:21:12 +00:00
|
|
|
return Response(data)
|
2018-04-01 15:45:37 +00:00
|
|
|
|
|
|
|
|
2018-04-02 07:54:49 +00:00
|
|
|
|