2022-07-20 05:23:43 +00:00
|
|
|
|
import abc
|
2021-11-09 09:10:29 +00:00
|
|
|
|
import os
|
2022-07-11 10:09:06 +00:00
|
|
|
|
import json
|
2021-09-24 07:31:25 +00:00
|
|
|
|
import base64
|
2022-07-11 10:09:06 +00:00
|
|
|
|
import urllib.parse
|
2021-02-24 07:31:22 +00:00
|
|
|
|
from django.http import HttpResponse
|
2022-07-11 10:09:06 +00:00
|
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
|
from rest_framework.exceptions import PermissionDenied
|
|
|
|
|
from rest_framework.decorators import action
|
2019-02-27 12:55:28 +00:00
|
|
|
|
from rest_framework.response import Response
|
2022-07-11 10:09:06 +00:00
|
|
|
|
from rest_framework import status
|
2021-08-02 08:18:34 +00:00
|
|
|
|
from rest_framework.request import Request
|
2022-07-11 10:09:06 +00:00
|
|
|
|
|
|
|
|
|
from common.drf.api import JMSModelViewSet
|
2021-08-03 02:57:57 +00:00
|
|
|
|
from common.http import is_true
|
2022-07-11 10:09:06 +00:00
|
|
|
|
from orgs.mixins.api import RootOrgViewMixin
|
2022-08-17 03:54:18 +00:00
|
|
|
|
from perms.models import Action
|
2022-04-12 09:45:10 +00:00
|
|
|
|
from terminal.models import EndpointRule
|
2021-02-24 07:31:22 +00:00
|
|
|
|
from ..serializers import (
|
2022-07-20 05:18:56 +00:00
|
|
|
|
ConnectionTokenSerializer, ConnectionTokenSecretSerializer,
|
|
|
|
|
SuperConnectionTokenSerializer, ConnectionTokenDisplaySerializer,
|
2021-02-24 07:31:22 +00:00
|
|
|
|
)
|
2022-07-11 10:09:06 +00:00
|
|
|
|
from ..models import ConnectionToken
|
2019-02-27 12:55:28 +00:00
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
|
__all__ = ['ConnectionTokenViewSet', 'SuperConnectionTokenViewSet']
|
2022-02-17 12:13:31 +00:00
|
|
|
|
|
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
|
class ConnectionTokenMixin:
|
2021-08-02 08:18:34 +00:00
|
|
|
|
request: Request
|
2022-07-11 10:09:06 +00:00
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def check_token_valid(token: ConnectionToken):
|
|
|
|
|
is_valid, error = token.check_valid()
|
|
|
|
|
if not is_valid:
|
|
|
|
|
raise PermissionDenied(error)
|
|
|
|
|
|
2022-07-20 05:23:43 +00:00
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def get_request_resource_user(self, serializer):
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
def get_request_resources(self, serializer):
|
|
|
|
|
user = self.get_request_resource_user(serializer)
|
2022-07-11 10:09:06 +00:00
|
|
|
|
asset = serializer.validated_data.get('asset')
|
|
|
|
|
application = serializer.validated_data.get('application')
|
|
|
|
|
system_user = serializer.validated_data.get('system_user')
|
|
|
|
|
return user, asset, application, system_user
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def check_user_has_resource_permission(user, asset, application, system_user):
|
|
|
|
|
from perms.utils.asset import has_asset_system_permission
|
|
|
|
|
|
|
|
|
|
if asset and not has_asset_system_permission(user, asset, system_user):
|
|
|
|
|
error = f'User not has this asset and system user permission: ' \
|
|
|
|
|
f'user={user.id} system_user={system_user.id} asset={asset.id}'
|
|
|
|
|
raise PermissionDenied(error)
|
|
|
|
|
|
2022-04-12 09:45:10 +00:00
|
|
|
|
def get_smart_endpoint(self, protocol, asset=None, application=None):
|
|
|
|
|
if asset:
|
|
|
|
|
target_ip = asset.get_target_ip()
|
|
|
|
|
elif application:
|
|
|
|
|
target_ip = application.get_target_ip()
|
|
|
|
|
else:
|
|
|
|
|
target_ip = ''
|
|
|
|
|
endpoint = EndpointRule.match_endpoint(target_ip, protocol, self.request)
|
|
|
|
|
return endpoint
|
2021-02-23 06:37:42 +00:00
|
|
|
|
|
2021-11-09 09:10:29 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
def parse_env_bool(env_key, env_default, true_value, false_value):
|
|
|
|
|
return true_value if is_true(os.getenv(env_key, env_default)) else false_value
|
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
|
def get_client_protocol_data(self, token: ConnectionToken):
|
|
|
|
|
from assets.models import SystemUser
|
|
|
|
|
protocol = token.system_user.protocol
|
|
|
|
|
username = token.user.username
|
|
|
|
|
rdp_config = ssh_token = ''
|
|
|
|
|
if protocol == SystemUser.Protocol.rdp:
|
|
|
|
|
filename, rdp_config = self.get_rdp_file_info(token)
|
|
|
|
|
elif protocol == SystemUser.Protocol.ssh:
|
|
|
|
|
filename, ssh_token = self.get_ssh_token(token)
|
|
|
|
|
else:
|
|
|
|
|
raise ValueError('Protocol not support: {}'.format(protocol))
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
"filename": filename,
|
|
|
|
|
"protocol": protocol,
|
|
|
|
|
"username": username,
|
|
|
|
|
"token": ssh_token,
|
|
|
|
|
"config": rdp_config
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def get_rdp_file_info(self, token: ConnectionToken):
|
|
|
|
|
rdp_options = {
|
2021-02-24 07:31:22 +00:00
|
|
|
|
'full address:s': '',
|
|
|
|
|
'username:s': '',
|
2021-08-03 02:57:57 +00:00
|
|
|
|
# 'screen mode id:i': '1',
|
2021-06-08 04:45:36 +00:00
|
|
|
|
# 'desktopwidth:i': '1280',
|
|
|
|
|
# 'desktopheight:i': '800',
|
2021-07-16 06:15:10 +00:00
|
|
|
|
'use multimon:i': '0',
|
2021-04-29 06:18:52 +00:00
|
|
|
|
'session bpp:i': '32',
|
2021-02-24 07:31:22 +00:00
|
|
|
|
'audiomode:i': '0',
|
|
|
|
|
'disable wallpaper:i': '0',
|
|
|
|
|
'disable full window drag:i': '0',
|
|
|
|
|
'disable menu anims:i': '0',
|
|
|
|
|
'disable themes:i': '0',
|
|
|
|
|
'alternate shell:s': '',
|
|
|
|
|
'shell working directory:s': '',
|
|
|
|
|
'authentication level:i': '2',
|
|
|
|
|
'connect to console:i': '0',
|
|
|
|
|
'disable cursor setting:i': '0',
|
|
|
|
|
'allow font smoothing:i': '1',
|
|
|
|
|
'allow desktop composition:i': '1',
|
|
|
|
|
'redirectprinters:i': '0',
|
|
|
|
|
'prompt for credentials on client:i': '0',
|
|
|
|
|
'autoreconnection enabled:i': '1',
|
|
|
|
|
'bookmarktype:i': '3',
|
|
|
|
|
'use redirection server name:i': '0',
|
2022-03-25 02:43:48 +00:00
|
|
|
|
'smart sizing:i': '1',
|
2022-04-24 09:00:48 +00:00
|
|
|
|
# 'drivestoredirect:s': '*',
|
2021-06-09 02:39:03 +00:00
|
|
|
|
# 'domain:s': ''
|
2021-02-24 07:31:22 +00:00
|
|
|
|
# 'alternate shell:s:': '||MySQLWorkbench',
|
|
|
|
|
# 'remoteapplicationname:s': 'Firefox',
|
|
|
|
|
# 'remoteapplicationcmdline:s': '',
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-16 03:32:25 +00:00
|
|
|
|
# 设置磁盘挂载
|
2022-07-11 10:09:06 +00:00
|
|
|
|
drives_redirect = is_true(self.request.query_params.get('drives_redirect'))
|
2022-02-16 07:48:28 +00:00
|
|
|
|
if drives_redirect:
|
2022-07-11 10:09:06 +00:00
|
|
|
|
actions = Action.choices_to_value(token.actions)
|
2022-02-16 07:48:28 +00:00
|
|
|
|
if actions & Action.UPDOWNLOAD == Action.UPDOWNLOAD:
|
2022-07-11 10:09:06 +00:00
|
|
|
|
rdp_options['drivestoredirect:s'] = '*'
|
2021-10-07 07:30:39 +00:00
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
|
# 设置全屏
|
|
|
|
|
full_screen = is_true(self.request.query_params.get('full_screen'))
|
|
|
|
|
rdp_options['screen mode id:i'] = '2' if full_screen else '1'
|
2021-11-16 03:32:25 +00:00
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
|
# 设置 RDP Server 地址
|
2022-04-12 09:45:10 +00:00
|
|
|
|
endpoint = self.get_smart_endpoint(
|
2022-07-11 10:09:06 +00:00
|
|
|
|
protocol='rdp', asset=token.asset, application=token.application
|
2022-04-12 09:45:10 +00:00
|
|
|
|
)
|
2022-07-11 10:09:06 +00:00
|
|
|
|
rdp_options['full address:s'] = f'{endpoint.host}:{endpoint.rdp_port}'
|
2021-11-09 09:10:29 +00:00
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
|
# 设置用户名
|
|
|
|
|
rdp_options['username:s'] = '{}|{}'.format(token.user.username, str(token.id))
|
|
|
|
|
if token.system_user.ad_domain:
|
|
|
|
|
rdp_options['domain:s'] = token.system_user.ad_domain
|
2021-11-09 09:10:29 +00:00
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
|
# 设置宽高
|
|
|
|
|
height = self.request.query_params.get('height')
|
|
|
|
|
width = self.request.query_params.get('width')
|
|
|
|
|
if width and height:
|
|
|
|
|
rdp_options['desktopwidth:i'] = width
|
|
|
|
|
rdp_options['desktopheight:i'] = height
|
|
|
|
|
rdp_options['winposstr:s:'] = f'0,1,0,0,{width},{height}'
|
|
|
|
|
|
|
|
|
|
# 设置其他选项
|
|
|
|
|
rdp_options['session bpp:i'] = os.getenv('JUMPSERVER_COLOR_DEPTH', '32')
|
|
|
|
|
rdp_options['audiomode:i'] = self.parse_env_bool('JUMPSERVER_DISABLE_AUDIO', 'false', '2', '0')
|
|
|
|
|
|
|
|
|
|
if token.asset:
|
2022-08-11 07:45:03 +00:00
|
|
|
|
name = token.asset.name
|
2022-07-11 10:09:06 +00:00
|
|
|
|
elif token.application and token.application.category_remote_app:
|
|
|
|
|
app = '||jmservisor'
|
|
|
|
|
name = token.application.name
|
|
|
|
|
rdp_options['remoteapplicationmode:i'] = '1'
|
|
|
|
|
rdp_options['alternate shell:s'] = app
|
|
|
|
|
rdp_options['remoteapplicationprogram:s'] = app
|
|
|
|
|
rdp_options['remoteapplicationname:s'] = name
|
2021-06-17 05:20:34 +00:00
|
|
|
|
else:
|
|
|
|
|
name = '*'
|
2022-08-10 10:17:59 +00:00
|
|
|
|
prefix_name = f'{token.user.username}-{name}'
|
|
|
|
|
filename = self.get_connect_filename(prefix_name)
|
2022-07-11 10:09:06 +00:00
|
|
|
|
|
2021-09-24 07:31:25 +00:00
|
|
|
|
content = ''
|
2022-07-11 10:09:06 +00:00
|
|
|
|
for k, v in rdp_options.items():
|
2021-09-24 07:31:25 +00:00
|
|
|
|
content += f'{k}:{v}\n'
|
2021-07-27 05:35:25 +00:00
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
|
return filename, content
|
|
|
|
|
|
2022-08-10 10:17:59 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
def get_connect_filename(prefix_name):
|
|
|
|
|
prefix_name = prefix_name.replace('/', '_')
|
|
|
|
|
prefix_name = prefix_name.replace('\\', '_')
|
|
|
|
|
prefix_name = prefix_name.replace('.', '_')
|
|
|
|
|
filename = f'{prefix_name}-jumpserver'
|
|
|
|
|
filename = urllib.parse.quote(filename)
|
|
|
|
|
return filename
|
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
|
def get_ssh_token(self, token: ConnectionToken):
|
|
|
|
|
if token.asset:
|
2022-08-11 07:45:03 +00:00
|
|
|
|
name = token.asset.name
|
2022-07-11 10:09:06 +00:00
|
|
|
|
elif token.application:
|
|
|
|
|
name = token.application.name
|
2022-03-28 11:52:48 +00:00
|
|
|
|
else:
|
|
|
|
|
name = '*'
|
2022-08-10 10:17:59 +00:00
|
|
|
|
prefix_name = f'{token.user.username}-{name}'
|
|
|
|
|
filename = self.get_connect_filename(prefix_name)
|
2022-03-28 11:52:48 +00:00
|
|
|
|
|
2022-04-12 09:45:10 +00:00
|
|
|
|
endpoint = self.get_smart_endpoint(
|
2022-07-11 10:09:06 +00:00
|
|
|
|
protocol='ssh', asset=token.asset, application=token.application
|
2022-04-12 09:45:10 +00:00
|
|
|
|
)
|
2022-07-11 10:09:06 +00:00
|
|
|
|
data = {
|
2022-04-12 09:45:10 +00:00
|
|
|
|
'ip': endpoint.host,
|
2022-04-13 11:48:31 +00:00
|
|
|
|
'port': str(endpoint.ssh_port),
|
2022-07-11 10:09:06 +00:00
|
|
|
|
'username': 'JMS-{}'.format(str(token.id)),
|
|
|
|
|
'password': token.secret
|
2022-03-28 11:52:48 +00:00
|
|
|
|
}
|
2022-07-11 10:09:06 +00:00
|
|
|
|
token = json.dumps(data)
|
|
|
|
|
return filename, token
|
2022-03-28 11:52:48 +00:00
|
|
|
|
|
2021-09-24 07:31:25 +00:00
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
|
class ConnectionTokenViewSet(ConnectionTokenMixin, RootOrgViewMixin, JMSModelViewSet):
|
|
|
|
|
filterset_fields = (
|
2022-08-19 10:49:00 +00:00
|
|
|
|
'type', 'user_display', 'asset_display'
|
2022-07-11 10:09:06 +00:00
|
|
|
|
)
|
|
|
|
|
search_fields = filterset_fields
|
|
|
|
|
serializer_classes = {
|
|
|
|
|
'default': ConnectionTokenSerializer,
|
2022-07-11 11:34:08 +00:00
|
|
|
|
'list': ConnectionTokenDisplaySerializer,
|
|
|
|
|
'retrieve': ConnectionTokenDisplaySerializer,
|
2022-07-11 10:09:06 +00:00
|
|
|
|
'get_secret_detail': ConnectionTokenSecretSerializer,
|
|
|
|
|
}
|
|
|
|
|
rbac_perms = {
|
|
|
|
|
'retrieve': 'authentication.view_connectiontoken',
|
|
|
|
|
'create': 'authentication.add_connectiontoken',
|
|
|
|
|
'expire': 'authentication.add_connectiontoken',
|
|
|
|
|
'get_secret_detail': 'authentication.view_connectiontokensecret',
|
|
|
|
|
'get_rdp_file': 'authentication.add_connectiontoken',
|
|
|
|
|
'get_client_protocol_url': 'authentication.add_connectiontoken',
|
|
|
|
|
}
|
2022-07-19 07:57:02 +00:00
|
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
|
return ConnectionToken.objects.filter(user=self.request.user)
|
2021-09-24 07:31:25 +00:00
|
|
|
|
|
2022-07-20 05:23:43 +00:00
|
|
|
|
def get_request_resource_user(self, serializer):
|
|
|
|
|
return self.request.user
|
|
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
|
if self.request.user.is_service_account:
|
|
|
|
|
# TODO: 组件获取 token 详情,将来放在 Super-connection-token API 中
|
|
|
|
|
obj = get_object_or_404(ConnectionToken, pk=self.kwargs.get('pk'))
|
|
|
|
|
else:
|
|
|
|
|
obj = super(ConnectionTokenViewSet, self).get_object()
|
|
|
|
|
return obj
|
2021-09-24 07:31:25 +00:00
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
|
def create_connection_token(self):
|
|
|
|
|
data = self.request.query_params if self.request.method == 'GET' else self.request.data
|
2021-08-02 08:18:34 +00:00
|
|
|
|
serializer = self.get_serializer(data=data)
|
|
|
|
|
serializer.is_valid(raise_exception=True)
|
2022-07-11 10:09:06 +00:00
|
|
|
|
self.perform_create(serializer)
|
|
|
|
|
token: ConnectionToken = serializer.instance
|
|
|
|
|
return token
|
2021-11-16 03:32:25 +00:00
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
|
def perform_create(self, serializer):
|
|
|
|
|
user, asset, application, system_user = self.get_request_resources(serializer)
|
|
|
|
|
self.check_user_has_resource_permission(user, asset, application, system_user)
|
|
|
|
|
return super(ConnectionTokenViewSet, self).perform_create(serializer)
|
|
|
|
|
|
|
|
|
|
@action(methods=['POST'], detail=False, url_path='secret-info/detail')
|
|
|
|
|
def get_secret_detail(self, request, *args, **kwargs):
|
|
|
|
|
# 非常重要的 api,在逻辑层再判断一下,双重保险
|
|
|
|
|
perm_required = 'authentication.view_connectiontokensecret'
|
|
|
|
|
if not request.user.has_perm(perm_required):
|
|
|
|
|
raise PermissionDenied('Not allow to view secret')
|
|
|
|
|
token_id = request.data.get('token') or ''
|
|
|
|
|
token = get_object_or_404(ConnectionToken, pk=token_id)
|
|
|
|
|
self.check_token_valid(token)
|
|
|
|
|
token.load_system_user_auth()
|
|
|
|
|
serializer = self.get_serializer(instance=token)
|
|
|
|
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
2021-08-02 08:18:34 +00:00
|
|
|
|
|
2022-04-24 09:00:48 +00:00
|
|
|
|
@action(methods=['POST', 'GET'], detail=False, url_path='rdp/file')
|
|
|
|
|
def get_rdp_file(self, request, *args, **kwargs):
|
2022-07-11 10:09:06 +00:00
|
|
|
|
token = self.create_connection_token()
|
|
|
|
|
self.check_token_valid(token)
|
|
|
|
|
filename, content = self.get_rdp_file_info(token)
|
|
|
|
|
filename = '{}.rdp'.format(filename)
|
|
|
|
|
response = HttpResponse(content, content_type='application/octet-stream')
|
2022-04-24 09:00:48 +00:00
|
|
|
|
response['Content-Disposition'] = 'attachment; filename*=UTF-8\'\'%s' % filename
|
|
|
|
|
return response
|
|
|
|
|
|
2022-02-17 12:13:31 +00:00
|
|
|
|
@action(methods=['POST', 'GET'], detail=False, url_path='client-url')
|
2021-08-02 08:18:34 +00:00
|
|
|
|
def get_client_protocol_url(self, request, *args, **kwargs):
|
2022-07-11 10:09:06 +00:00
|
|
|
|
token = self.create_connection_token()
|
|
|
|
|
self.check_token_valid(token)
|
2021-11-16 03:32:25 +00:00
|
|
|
|
try:
|
2022-07-11 10:09:06 +00:00
|
|
|
|
protocol_data = self.get_client_protocol_data(token)
|
2021-11-16 03:32:25 +00:00
|
|
|
|
except ValueError as e:
|
2022-07-11 10:09:06 +00:00
|
|
|
|
return Response(data={'error': str(e)}, status=status.HTTP_400_BAD_REQUEST)
|
2021-11-16 03:32:25 +00:00
|
|
|
|
protocol_data = json.dumps(protocol_data).encode()
|
|
|
|
|
protocol_data = base64.b64encode(protocol_data).decode()
|
2021-08-02 08:18:34 +00:00
|
|
|
|
data = {
|
2022-07-11 10:09:06 +00:00
|
|
|
|
'url': 'jms://{}'.format(protocol_data)
|
2021-08-02 08:18:34 +00:00
|
|
|
|
}
|
|
|
|
|
return Response(data=data)
|
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
|
@action(methods=['PATCH'], detail=True)
|
|
|
|
|
def expire(self, request, *args, **kwargs):
|
|
|
|
|
instance = self.get_object()
|
|
|
|
|
instance.expire()
|
|
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT)
|
2022-05-31 08:20:33 +00:00
|
|
|
|
|
2022-03-30 03:08:50 +00:00
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
|
class SuperConnectionTokenViewSet(ConnectionTokenViewSet):
|
2022-04-24 09:00:48 +00:00
|
|
|
|
serializer_classes = {
|
|
|
|
|
'default': SuperConnectionTokenSerializer,
|
|
|
|
|
}
|
|
|
|
|
rbac_perms = {
|
|
|
|
|
'create': 'authentication.add_superconnectiontoken',
|
|
|
|
|
'renewal': 'authentication.add_superconnectiontoken'
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-20 05:23:43 +00:00
|
|
|
|
def get_request_resource_user(self, serializer):
|
|
|
|
|
return serializer.validated_data.get('user')
|
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
|
@action(methods=['PATCH'], detail=False)
|
2022-04-24 09:00:48 +00:00
|
|
|
|
def renewal(self, request, *args, **kwargs):
|
2022-07-11 10:09:06 +00:00
|
|
|
|
from common.utils.timezone import as_current_tz
|
|
|
|
|
|
|
|
|
|
token_id = request.data.get('token') or ''
|
|
|
|
|
token = get_object_or_404(ConnectionToken, pk=token_id)
|
|
|
|
|
date_expired = as_current_tz(token.date_expired)
|
|
|
|
|
if token.is_expired:
|
|
|
|
|
raise PermissionDenied('Token is expired at: {}'.format(date_expired))
|
|
|
|
|
token.renewal()
|
|
|
|
|
data = {
|
|
|
|
|
'ok': True,
|
|
|
|
|
'msg': f'Token is renewed, date expired: {date_expired}'
|
|
|
|
|
}
|
|
|
|
|
return Response(data=data, status=status.HTTP_200_OK)
|