2022-12-07 07:09:01 +00:00
|
|
|
|
import base64
|
|
|
|
|
import json
|
2022-10-27 09:20:31 +00:00
|
|
|
|
from datetime import timedelta
|
2022-11-28 07:01:16 +00:00
|
|
|
|
|
|
|
|
|
from django.conf import settings
|
2022-12-07 07:09:01 +00:00
|
|
|
|
from django.core.cache import cache
|
2022-11-28 07:01:16 +00:00
|
|
|
|
from django.db import models
|
2022-04-13 12:24:56 +00:00
|
|
|
|
from django.utils import timezone
|
2023-07-24 03:52:25 +00:00
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-11-22 13:54:40 +00:00
|
|
|
|
from rest_framework.exceptions import PermissionDenied
|
2019-02-28 09:58:53 +00:00
|
|
|
|
|
2023-08-08 02:16:23 +00:00
|
|
|
|
from accounts.models import VirtualAccount
|
2022-11-28 07:01:16 +00:00
|
|
|
|
from assets.const import Protocol
|
2023-04-12 03:43:06 +00:00
|
|
|
|
from assets.const.host import GATEWAY_NAME
|
2023-02-14 08:53:10 +00:00
|
|
|
|
from common.db.fields import EncryptTextField
|
2023-02-10 11:40:35 +00:00
|
|
|
|
from common.exceptions import JMSException
|
|
|
|
|
from common.utils import lazyproperty, pretty_string, bulk_get
|
2022-07-11 10:09:06 +00:00
|
|
|
|
from common.utils.timezone import as_current_tz
|
2022-12-20 12:23:42 +00:00
|
|
|
|
from orgs.mixins.models import JMSOrgBaseModel
|
2023-06-12 10:20:30 +00:00
|
|
|
|
from orgs.utils import tmp_to_org
|
2023-12-05 08:52:11 +00:00
|
|
|
|
from terminal.models import Applet, VirtualApp
|
2022-02-17 12:13:31 +00:00
|
|
|
|
|
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
|
def date_expired_default():
|
2023-06-19 11:57:01 +00:00
|
|
|
|
return timezone.now() + timedelta(seconds=settings.CONNECTION_TOKEN_ONETIME_EXPIRATION)
|
2022-07-11 10:09:06 +00:00
|
|
|
|
|
|
|
|
|
|
2022-12-20 12:23:42 +00:00
|
|
|
|
class ConnectionToken(JMSOrgBaseModel):
|
2022-11-25 15:09:55 +00:00
|
|
|
|
value = models.CharField(max_length=64, default='', verbose_name=_("Value"))
|
2022-07-11 10:09:06 +00:00
|
|
|
|
user = models.ForeignKey(
|
2022-11-28 07:01:16 +00:00
|
|
|
|
'users.User', on_delete=models.SET_NULL, null=True, blank=True,
|
2022-10-27 07:47:05 +00:00
|
|
|
|
related_name='connection_tokens', verbose_name=_('User')
|
2022-07-11 10:09:06 +00:00
|
|
|
|
)
|
|
|
|
|
asset = models.ForeignKey(
|
2022-10-27 07:47:05 +00:00
|
|
|
|
'assets.Asset', on_delete=models.SET_NULL, null=True, blank=True,
|
|
|
|
|
related_name='connection_tokens', verbose_name=_('Asset'),
|
2022-07-11 10:09:06 +00:00
|
|
|
|
)
|
2022-12-05 04:42:15 +00:00
|
|
|
|
account = models.CharField(max_length=128, verbose_name=_("Account name")) # 登录账号Name
|
2022-12-05 03:06:50 +00:00
|
|
|
|
input_username = models.CharField(max_length=128, default='', blank=True, verbose_name=_("Input username"))
|
2023-02-14 08:53:10 +00:00
|
|
|
|
input_secret = EncryptTextField(max_length=64, default='', blank=True, verbose_name=_("Input secret"))
|
2022-12-05 03:06:50 +00:00
|
|
|
|
protocol = models.CharField(max_length=16, default=Protocol.ssh, verbose_name=_("Protocol"))
|
2022-11-28 14:58:43 +00:00
|
|
|
|
connect_method = models.CharField(max_length=32, verbose_name=_("Connect method"))
|
2023-06-05 09:27:44 +00:00
|
|
|
|
connect_options = models.JSONField(default=dict, verbose_name=_("Connect options"))
|
2022-10-28 07:01:17 +00:00
|
|
|
|
user_display = models.CharField(max_length=128, default='', verbose_name=_("User display"))
|
|
|
|
|
asset_display = models.CharField(max_length=128, default='', verbose_name=_("Asset display"))
|
2023-05-09 05:43:46 +00:00
|
|
|
|
is_reusable = models.BooleanField(default=False, verbose_name=_("Reusable"))
|
2022-12-05 03:06:50 +00:00
|
|
|
|
date_expired = models.DateTimeField(default=date_expired_default, verbose_name=_("Date expired"))
|
2023-01-16 11:02:09 +00:00
|
|
|
|
from_ticket = models.OneToOneField(
|
|
|
|
|
'tickets.ApplyLoginAssetTicket', related_name='connection_token',
|
|
|
|
|
on_delete=models.SET_NULL, null=True, blank=True,
|
|
|
|
|
verbose_name=_('From ticket')
|
|
|
|
|
)
|
|
|
|
|
is_active = models.BooleanField(default=True, verbose_name=_("Active"))
|
2022-02-17 12:13:31 +00:00
|
|
|
|
|
|
|
|
|
class Meta:
|
2022-07-11 10:09:06 +00:00
|
|
|
|
ordering = ('-date_expired',)
|
2022-03-10 03:25:33 +00:00
|
|
|
|
permissions = [
|
2023-07-13 08:00:24 +00:00
|
|
|
|
('expire_connectiontoken', _('Can expire connection token')),
|
|
|
|
|
('reuse_connectiontoken', _('Can reuse connection token')),
|
2022-03-10 03:25:33 +00:00
|
|
|
|
]
|
2023-07-13 08:00:24 +00:00
|
|
|
|
verbose_name = _('Connection token')
|
2022-03-02 12:48:43 +00:00
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
|
@property
|
|
|
|
|
def is_expired(self):
|
|
|
|
|
return self.date_expired < timezone.now()
|
|
|
|
|
|
2022-07-12 10:27:07 +00:00
|
|
|
|
@property
|
|
|
|
|
def expire_time(self):
|
|
|
|
|
interval = self.date_expired - timezone.now()
|
|
|
|
|
seconds = interval.total_seconds()
|
|
|
|
|
if seconds < 0:
|
|
|
|
|
seconds = 0
|
|
|
|
|
return int(seconds)
|
|
|
|
|
|
2022-11-22 13:54:40 +00:00
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
|
self.asset_display = pretty_string(self.asset, max_length=128)
|
|
|
|
|
self.user_display = pretty_string(self.user, max_length=128)
|
|
|
|
|
return super().save(*args, **kwargs)
|
2022-10-27 07:47:05 +00:00
|
|
|
|
|
|
|
|
|
def expire(self):
|
|
|
|
|
self.date_expired = timezone.now()
|
2023-05-09 05:43:46 +00:00
|
|
|
|
self.save(update_fields=['date_expired'])
|
2022-07-11 10:09:06 +00:00
|
|
|
|
|
2023-07-13 08:00:24 +00:00
|
|
|
|
def set_reusable(self, is_reusable):
|
2024-04-07 04:11:44 +00:00
|
|
|
|
if not settings.CONNECTION_TOKEN_REUSABLE:
|
|
|
|
|
return
|
2023-07-13 08:00:24 +00:00
|
|
|
|
self.is_reusable = is_reusable
|
|
|
|
|
if self.is_reusable:
|
|
|
|
|
seconds = settings.CONNECTION_TOKEN_REUSABLE_EXPIRATION
|
|
|
|
|
else:
|
|
|
|
|
seconds = settings.CONNECTION_TOKEN_ONETIME_EXPIRATION
|
2024-04-07 04:11:44 +00:00
|
|
|
|
|
|
|
|
|
self.date_expired = self.date_created + timedelta(seconds=seconds)
|
2023-07-13 08:00:24 +00:00
|
|
|
|
self.save(update_fields=['is_reusable', 'date_expired'])
|
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
|
def renewal(self):
|
|
|
|
|
""" 续期 Token,将来支持用户自定义创建 token 后,续期策略要修改 """
|
2022-11-22 13:54:40 +00:00
|
|
|
|
self.date_expired = date_expired_default()
|
2022-07-11 10:09:06 +00:00
|
|
|
|
self.save()
|
|
|
|
|
|
2022-11-23 08:11:17 +00:00
|
|
|
|
@lazyproperty
|
|
|
|
|
def permed_account(self):
|
2023-10-26 11:19:09 +00:00
|
|
|
|
from perms.utils import PermAssetDetailUtil
|
|
|
|
|
permed_account = PermAssetDetailUtil(self.user, self.asset) \
|
|
|
|
|
.validate_permission(self.account, self.protocol)
|
2022-11-23 08:11:17 +00:00
|
|
|
|
return permed_account
|
|
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
|
def actions(self):
|
|
|
|
|
return self.permed_account.actions
|
|
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
|
def expire_at(self):
|
|
|
|
|
return self.permed_account.date_expired.timestamp()
|
2022-07-11 10:09:06 +00:00
|
|
|
|
|
2022-11-23 08:11:17 +00:00
|
|
|
|
def is_valid(self):
|
2023-01-16 11:02:09 +00:00
|
|
|
|
if not self.is_active:
|
|
|
|
|
error = _('Connection token inactive')
|
|
|
|
|
raise PermissionDenied(error)
|
2023-10-26 11:19:09 +00:00
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
|
if self.is_expired:
|
|
|
|
|
error = _('Connection token expired at: {}').format(as_current_tz(self.date_expired))
|
2022-11-23 08:11:17 +00:00
|
|
|
|
raise PermissionDenied(error)
|
2022-10-28 07:01:17 +00:00
|
|
|
|
if not self.user or not self.user.is_valid:
|
|
|
|
|
error = _('No user or invalid user')
|
2022-11-23 08:11:17 +00:00
|
|
|
|
raise PermissionDenied(error)
|
2022-11-08 06:30:07 +00:00
|
|
|
|
if not self.asset or not self.asset.is_active:
|
2022-10-28 07:01:17 +00:00
|
|
|
|
error = _('No asset or inactive asset')
|
2023-05-09 05:43:46 +00:00
|
|
|
|
raise PermissionDenied(error)
|
2022-12-05 04:42:15 +00:00
|
|
|
|
if not self.account:
|
2022-10-28 07:01:17 +00:00
|
|
|
|
error = _('No account')
|
2022-11-23 08:11:17 +00:00
|
|
|
|
raise PermissionDenied(error)
|
2022-11-22 13:54:40 +00:00
|
|
|
|
|
2022-12-07 07:09:01 +00:00
|
|
|
|
if timezone.now() - self.date_created < timedelta(seconds=60):
|
|
|
|
|
return True, None
|
|
|
|
|
|
2022-11-23 08:11:17 +00:00
|
|
|
|
if not self.permed_account or not self.permed_account.actions:
|
2022-11-22 13:54:40 +00:00
|
|
|
|
msg = 'user `{}` not has asset `{}` permission for login `{}`'.format(
|
2022-12-05 04:42:15 +00:00
|
|
|
|
self.user, self.asset, self.account
|
2022-11-22 13:54:40 +00:00
|
|
|
|
)
|
|
|
|
|
raise PermissionDenied(msg)
|
|
|
|
|
|
2022-11-23 08:11:17 +00:00
|
|
|
|
if self.permed_account.date_expired < timezone.now():
|
2022-11-22 13:54:40 +00:00
|
|
|
|
raise PermissionDenied('Expired')
|
2022-11-23 08:11:17 +00:00
|
|
|
|
return True
|
2022-07-11 10:09:06 +00:00
|
|
|
|
|
2022-10-28 07:58:05 +00:00
|
|
|
|
@lazyproperty
|
2022-11-22 13:54:40 +00:00
|
|
|
|
def platform(self):
|
|
|
|
|
return self.asset.platform
|
|
|
|
|
|
2022-12-07 07:09:01 +00:00
|
|
|
|
@lazyproperty
|
|
|
|
|
def connect_method_object(self):
|
|
|
|
|
from common.utils import get_request_os
|
|
|
|
|
from jumpserver.utils import get_current_request
|
|
|
|
|
from terminal.connect_methods import ConnectMethodUtil
|
|
|
|
|
|
|
|
|
|
request = get_current_request()
|
|
|
|
|
os = get_request_os(request) if request else 'windows'
|
|
|
|
|
method = ConnectMethodUtil.get_connect_method(
|
|
|
|
|
self.connect_method, protocol=self.protocol, os=os
|
|
|
|
|
)
|
|
|
|
|
return method
|
|
|
|
|
|
|
|
|
|
def get_remote_app_option(self):
|
|
|
|
|
cmdline = {
|
|
|
|
|
'app_name': self.connect_method,
|
|
|
|
|
'user_id': str(self.user.id),
|
|
|
|
|
'asset_id': str(self.asset.id),
|
|
|
|
|
'token_id': str(self.id)
|
|
|
|
|
}
|
|
|
|
|
cmdline_b64 = base64.b64encode(json.dumps(cmdline).encode()).decode()
|
|
|
|
|
app = '||tinker'
|
|
|
|
|
options = {
|
|
|
|
|
'remoteapplicationmode:i': '1',
|
|
|
|
|
'remoteapplicationprogram:s': app,
|
|
|
|
|
'remoteapplicationname:s': app,
|
|
|
|
|
'alternate shell:s': app,
|
|
|
|
|
'remoteapplicationcmdline:s': cmdline_b64,
|
2023-05-18 09:51:50 +00:00
|
|
|
|
'disableconnectionsharing:i': '1',
|
2022-12-07 07:09:01 +00:00
|
|
|
|
}
|
|
|
|
|
return options
|
|
|
|
|
|
2023-12-05 08:52:11 +00:00
|
|
|
|
def get_virtual_app_option(self):
|
|
|
|
|
method = self.connect_method_object
|
|
|
|
|
if not method or method.get('type') != 'virtual_app' or method.get('disabled', False):
|
|
|
|
|
return None
|
|
|
|
|
virtual_app = VirtualApp.objects.filter(name=method.get('value')).first()
|
|
|
|
|
if not virtual_app:
|
|
|
|
|
return None
|
|
|
|
|
return virtual_app
|
|
|
|
|
|
2022-12-07 07:09:01 +00:00
|
|
|
|
def get_applet_option(self):
|
|
|
|
|
method = self.connect_method_object
|
|
|
|
|
if not method or method.get('type') != 'applet' or method.get('disabled', False):
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
applet = Applet.objects.filter(name=method.get('value')).first()
|
|
|
|
|
if not applet:
|
|
|
|
|
return None
|
|
|
|
|
|
2023-06-20 05:54:53 +00:00
|
|
|
|
host_account = applet.select_host_account(self.user, self.asset)
|
2022-12-07 07:09:01 +00:00
|
|
|
|
if not host_account:
|
2023-02-10 11:40:35 +00:00
|
|
|
|
raise JMSException({'error': 'No host account available'})
|
2022-12-07 07:09:01 +00:00
|
|
|
|
|
2023-12-14 09:49:31 +00:00
|
|
|
|
host, account, lock_key = bulk_get(host_account, ('host', 'account', 'lock_key'))
|
2023-08-14 07:46:46 +00:00
|
|
|
|
gateway = host.domain.select_gateway() if host.domain else None
|
2024-04-22 08:29:29 +00:00
|
|
|
|
platform = host.platform
|
2022-12-07 07:09:01 +00:00
|
|
|
|
|
|
|
|
|
data = {
|
2023-12-14 09:49:31 +00:00
|
|
|
|
'id': lock_key,
|
2022-12-07 07:09:01 +00:00
|
|
|
|
'applet': applet,
|
|
|
|
|
'host': host,
|
|
|
|
|
'gateway': gateway,
|
2024-04-22 08:29:29 +00:00
|
|
|
|
'platform': platform,
|
2022-12-07 07:09:01 +00:00
|
|
|
|
'account': account,
|
|
|
|
|
'remote_app_option': self.get_remote_app_option()
|
|
|
|
|
}
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2023-12-14 09:49:31 +00:00
|
|
|
|
def release_applet_account(lock_key):
|
2022-12-07 07:09:01 +00:00
|
|
|
|
if lock_key:
|
|
|
|
|
cache.delete(lock_key)
|
2023-02-10 11:40:35 +00:00
|
|
|
|
return True
|
2022-12-07 07:09:01 +00:00
|
|
|
|
|
2022-11-22 13:54:40 +00:00
|
|
|
|
@lazyproperty
|
2022-12-05 04:42:15 +00:00
|
|
|
|
def account_object(self):
|
2022-10-28 07:58:05 +00:00
|
|
|
|
if not self.asset:
|
|
|
|
|
return None
|
2022-11-22 13:54:40 +00:00
|
|
|
|
|
2023-06-27 03:23:56 +00:00
|
|
|
|
if self.account.startswith('@'):
|
2023-08-08 02:16:23 +00:00
|
|
|
|
account = VirtualAccount.get_special_account(
|
|
|
|
|
self.account, self.user, self.asset, input_username=self.input_username,
|
|
|
|
|
input_secret=self.input_secret, from_permed=False
|
|
|
|
|
)
|
2022-11-22 13:54:40 +00:00
|
|
|
|
else:
|
2023-06-27 03:23:56 +00:00
|
|
|
|
account = self.asset.accounts.filter(name=self.account).first()
|
2023-06-27 07:22:18 +00:00
|
|
|
|
if not account.secret and self.input_secret:
|
|
|
|
|
account.secret = self.input_secret
|
2023-06-27 03:23:56 +00:00
|
|
|
|
return account
|
2022-10-28 07:58:05 +00:00
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
|
@lazyproperty
|
|
|
|
|
def domain(self):
|
2023-02-16 08:51:42 +00:00
|
|
|
|
if not self.asset.platform.domain_enabled:
|
|
|
|
|
return
|
2023-04-12 03:43:06 +00:00
|
|
|
|
if self.asset.platform.name == GATEWAY_NAME:
|
|
|
|
|
return
|
|
|
|
|
domain = self.asset.domain if self.asset.domain else None
|
2022-07-11 10:09:06 +00:00
|
|
|
|
return domain
|
|
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
|
def gateway(self):
|
2023-04-12 03:43:06 +00:00
|
|
|
|
if not self.asset or not self.domain:
|
2022-07-11 10:09:06 +00:00
|
|
|
|
return
|
2023-02-16 08:51:42 +00:00
|
|
|
|
return self.asset.gateway
|
2022-07-11 10:09:06 +00:00
|
|
|
|
|
|
|
|
|
@lazyproperty
|
2022-12-05 05:27:51 +00:00
|
|
|
|
def command_filter_acls(self):
|
2022-12-02 09:48:44 +00:00
|
|
|
|
from acls.models import CommandFilterACL
|
2022-07-11 10:09:06 +00:00
|
|
|
|
kwargs = {
|
2022-12-03 16:04:39 +00:00
|
|
|
|
'user': self.user,
|
|
|
|
|
'asset': self.asset,
|
2023-06-12 10:28:04 +00:00
|
|
|
|
'account': self.account_object,
|
2022-07-11 10:09:06 +00:00
|
|
|
|
}
|
2023-06-12 10:20:30 +00:00
|
|
|
|
with tmp_to_org(self.asset.org_id):
|
|
|
|
|
acls = CommandFilterACL.filter_queryset(**kwargs).valid()
|
2022-12-05 05:27:51 +00:00
|
|
|
|
return acls
|
2022-07-11 10:09:06 +00:00
|
|
|
|
|
2022-03-02 12:48:43 +00:00
|
|
|
|
|
|
|
|
|
class SuperConnectionToken(ConnectionToken):
|
|
|
|
|
class Meta:
|
|
|
|
|
proxy = True
|
2023-07-13 08:00:24 +00:00
|
|
|
|
permissions = [
|
|
|
|
|
('view_superconnectiontokensecret', _('Can view super connection token secret'))
|
|
|
|
|
]
|
2022-03-02 12:48:43 +00:00
|
|
|
|
verbose_name = _("Super connection token")
|