2022-10-27 07:47:05 +00:00
|
|
|
|
import time
|
2022-10-27 09:20:31 +00:00
|
|
|
|
from datetime import timedelta
|
2022-04-13 12:24:56 +00:00
|
|
|
|
from django.utils import timezone
|
2021-10-18 10:41:41 +00:00
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2019-02-28 09:58:53 +00:00
|
|
|
|
from django.conf import settings
|
2022-07-11 10:09:06 +00:00
|
|
|
|
from orgs.mixins.models import OrgModelMixin
|
2019-02-28 09:58:53 +00:00
|
|
|
|
|
2022-07-17 06:28:55 +00:00
|
|
|
|
from django.db import models
|
2022-07-11 10:09:06 +00:00
|
|
|
|
from common.utils import lazyproperty
|
|
|
|
|
from common.utils.timezone import as_current_tz
|
2022-10-27 09:20:31 +00:00
|
|
|
|
from common.db.models import JMSBaseModel
|
2022-10-27 12:01:50 +00:00
|
|
|
|
from assets.const import Protocol
|
2022-02-17 12:13:31 +00:00
|
|
|
|
|
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
|
def date_expired_default():
|
|
|
|
|
return timezone.now() + timedelta(seconds=settings.CONNECTION_TOKEN_EXPIRATION)
|
|
|
|
|
|
|
|
|
|
|
2022-07-17 06:28:55 +00:00
|
|
|
|
class ConnectionToken(OrgModelMixin, JMSBaseModel):
|
2022-07-11 10:09:06 +00:00
|
|
|
|
user = models.ForeignKey(
|
2022-10-27 07:47:05 +00:00
|
|
|
|
'users.User', on_delete=models.SET_NULL, null=True, blank=True,
|
|
|
|
|
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-10-27 12:01:50 +00:00
|
|
|
|
protocol = models.CharField(
|
|
|
|
|
choices=Protocol.choices, max_length=16, default=Protocol.ssh, verbose_name=_("Protocol")
|
|
|
|
|
)
|
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"))
|
|
|
|
|
account_username = models.CharField(max_length=128, default='', verbose_name=_("Account"))
|
2022-10-27 07:47:05 +00:00
|
|
|
|
secret = models.CharField(max_length=64, default='', verbose_name=_("Secret"))
|
2022-10-27 12:01:50 +00:00
|
|
|
|
date_expired = models.DateTimeField(
|
|
|
|
|
default=date_expired_default, verbose_name=_("Date expired")
|
|
|
|
|
)
|
2022-02-17 12:13:31 +00:00
|
|
|
|
|
|
|
|
|
class Meta:
|
2022-07-11 10:09:06 +00:00
|
|
|
|
ordering = ('-date_expired',)
|
2022-03-02 12:48:43 +00:00
|
|
|
|
verbose_name = _('Connection token')
|
2022-03-10 03:25:33 +00:00
|
|
|
|
permissions = [
|
|
|
|
|
('view_connectiontokensecret', _('Can view connection token secret'))
|
|
|
|
|
]
|
2022-03-02 12:48:43 +00:00
|
|
|
|
|
2022-10-28 07:01:17 +00:00
|
|
|
|
@property
|
|
|
|
|
def is_valid(self):
|
|
|
|
|
return not self.is_expired
|
|
|
|
|
|
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-10-27 07:47:05 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def get_default_date_expired(cls):
|
|
|
|
|
return date_expired_default()
|
|
|
|
|
|
|
|
|
|
def expire(self):
|
|
|
|
|
self.date_expired = timezone.now()
|
|
|
|
|
self.save()
|
2022-07-11 10:09:06 +00:00
|
|
|
|
|
|
|
|
|
def renewal(self):
|
|
|
|
|
""" 续期 Token,将来支持用户自定义创建 token 后,续期策略要修改 """
|
|
|
|
|
self.date_expired = self.get_default_date_expired()
|
|
|
|
|
self.save()
|
|
|
|
|
|
2022-10-27 07:47:05 +00:00
|
|
|
|
# actions 和 expired_at 在 check_valid() 中赋值
|
|
|
|
|
actions = expire_at = None
|
2022-07-11 10:09:06 +00:00
|
|
|
|
|
2022-10-28 07:58:05 +00:00
|
|
|
|
def check_permission(self):
|
2022-10-27 07:47:05 +00:00
|
|
|
|
from perms.utils.account import PermAccountUtil
|
2022-07-11 10:09:06 +00:00
|
|
|
|
if self.is_expired:
|
|
|
|
|
is_valid = False
|
|
|
|
|
error = _('Connection token expired at: {}').format(as_current_tz(self.date_expired))
|
|
|
|
|
return is_valid, error
|
2022-10-28 07:01:17 +00:00
|
|
|
|
if not self.user or not self.user.is_valid:
|
2022-10-26 08:09:07 +00:00
|
|
|
|
is_valid = False
|
2022-10-28 07:01:17 +00:00
|
|
|
|
error = _('No user or invalid user')
|
2022-10-26 08:09:07 +00:00
|
|
|
|
return is_valid, error
|
2022-11-08 06:30:07 +00:00
|
|
|
|
if not self.asset or not self.asset.is_active:
|
2022-10-26 08:09:07 +00:00
|
|
|
|
is_valid = False
|
2022-10-28 07:01:17 +00:00
|
|
|
|
error = _('No asset or inactive asset')
|
2022-10-26 08:09:07 +00:00
|
|
|
|
return is_valid, error
|
2022-10-28 07:58:05 +00:00
|
|
|
|
if not self.account_username:
|
2022-10-27 07:47:05 +00:00
|
|
|
|
is_valid = False
|
2022-10-28 07:01:17 +00:00
|
|
|
|
error = _('No account')
|
2022-10-27 07:47:05 +00:00
|
|
|
|
return is_valid, error
|
2022-10-31 10:47:12 +00:00
|
|
|
|
actions, expire_at = PermAccountUtil().validate_permission(
|
2022-10-28 07:58:05 +00:00
|
|
|
|
self.user, self.asset, self.account_username
|
|
|
|
|
)
|
2022-10-27 07:47:05 +00:00
|
|
|
|
if not actions or expire_at < time.time():
|
2022-10-26 08:09:07 +00:00
|
|
|
|
is_valid = False
|
|
|
|
|
error = _('User has no permission to access asset or permission expired')
|
|
|
|
|
return is_valid, error
|
|
|
|
|
self.actions = actions
|
2022-10-27 07:47:05 +00:00
|
|
|
|
self.expire_at = expire_at
|
2022-10-31 10:47:12 +00:00
|
|
|
|
is_valid, error = True, ''
|
|
|
|
|
return is_valid, error
|
2022-07-11 10:09:06 +00:00
|
|
|
|
|
2022-10-28 07:58:05 +00:00
|
|
|
|
@lazyproperty
|
|
|
|
|
def account(self):
|
|
|
|
|
if not self.asset:
|
|
|
|
|
return None
|
|
|
|
|
account = self.asset.accounts.filter(username=self.account_username).first()
|
|
|
|
|
return account
|
|
|
|
|
|
2022-07-11 10:09:06 +00:00
|
|
|
|
@lazyproperty
|
|
|
|
|
def domain(self):
|
2022-10-27 07:47:05 +00:00
|
|
|
|
domain = self.asset.domain if self.asset else None
|
2022-07-11 10:09:06 +00:00
|
|
|
|
return domain
|
|
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
|
def gateway(self):
|
|
|
|
|
from assets.models import Domain
|
|
|
|
|
if not self.domain:
|
|
|
|
|
return
|
|
|
|
|
self.domain: Domain
|
|
|
|
|
return self.domain.random_gateway()
|
|
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
|
def cmd_filter_rules(self):
|
|
|
|
|
from assets.models import CommandFilterRule
|
|
|
|
|
kwargs = {
|
|
|
|
|
'user_id': self.user.id,
|
2022-10-27 07:47:05 +00:00
|
|
|
|
'account': self.account,
|
2022-07-11 10:09:06 +00:00
|
|
|
|
}
|
|
|
|
|
if self.asset:
|
|
|
|
|
kwargs['asset_id'] = self.asset.id
|
|
|
|
|
rules = CommandFilterRule.get_queryset(**kwargs)
|
|
|
|
|
return rules
|
|
|
|
|
|
2022-03-02 12:48:43 +00:00
|
|
|
|
|
|
|
|
|
class SuperConnectionToken(ConnectionToken):
|
|
|
|
|
class Meta:
|
|
|
|
|
proxy = True
|
|
|
|
|
verbose_name = _("Super connection token")
|