pref: 修改 perm token

pull/9075/head
ibuler 2022-11-14 14:44:18 +08:00
parent 4f135bc349
commit 8e1312e8ce
5 changed files with 32 additions and 12 deletions

View File

@ -282,9 +282,6 @@ class ConnectionTokenViewSet(ExtraActionApiMixin, RootOrgViewMixin, JMSModelView
raise PermissionDenied(error) raise PermissionDenied(error)
# SuperConnectionToken
class SuperConnectionTokenViewSet(ConnectionTokenViewSet): class SuperConnectionTokenViewSet(ConnectionTokenViewSet):
serializer_classes = { serializer_classes = {
'default': SuperConnectionTokenSerializer, 'default': SuperConnectionTokenSerializer,

View File

@ -1,14 +1,13 @@
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers from rest_framework import serializers
from django.utils.translation import ugettext_lazy as _ from assets.models import Asset, Gateway, Domain, CommandFilterRule, Account, Platform
from orgs.mixins.serializers import OrgResourceModelSerializerMixin
from authentication.models import ConnectionToken from authentication.models import ConnectionToken
from common.utils import pretty_string from common.utils import pretty_string
from common.utils.random import random_string from common.utils.random import random_string
from assets.models import Asset, Gateway, Domain, CommandFilterRule, Account from orgs.mixins.serializers import OrgResourceModelSerializerMixin
from users.models import User
from perms.serializers.permission import ActionChoicesField from perms.serializers.permission import ActionChoicesField
from users.models import User
__all__ = [ __all__ = [
'ConnectionTokenSerializer', 'ConnectionTokenSecretSerializer', 'ConnectionTokenSerializer', 'ConnectionTokenSecretSerializer',
@ -86,7 +85,6 @@ class ConnectionTokenDisplaySerializer(ConnectionTokenSerializer):
class SuperConnectionTokenSerializer(ConnectionTokenSerializer): class SuperConnectionTokenSerializer(ConnectionTokenSerializer):
class Meta(ConnectionTokenSerializer.Meta): class Meta(ConnectionTokenSerializer.Meta):
read_only_fields = [ read_only_fields = [
'validity', 'user_display', 'system_user_display', 'validity', 'user_display', 'system_user_display',
@ -104,6 +102,7 @@ class SuperConnectionTokenSerializer(ConnectionTokenSerializer):
class ConnectionTokenUserSerializer(serializers.ModelSerializer): class ConnectionTokenUserSerializer(serializers.ModelSerializer):
""" User """ """ User """
class Meta: class Meta:
model = User model = User
fields = ['id', 'name', 'username', 'email'] fields = ['id', 'name', 'username', 'email']
@ -111,6 +110,7 @@ class ConnectionTokenUserSerializer(serializers.ModelSerializer):
class ConnectionTokenAssetSerializer(serializers.ModelSerializer): class ConnectionTokenAssetSerializer(serializers.ModelSerializer):
""" Asset """ """ Asset """
class Meta: class Meta:
model = Asset model = Asset
fields = ['id', 'name', 'address', 'protocols', 'org_id'] fields = ['id', 'name', 'address', 'protocols', 'org_id']
@ -118,6 +118,7 @@ class ConnectionTokenAssetSerializer(serializers.ModelSerializer):
class ConnectionTokenAccountSerializer(serializers.ModelSerializer): class ConnectionTokenAccountSerializer(serializers.ModelSerializer):
""" Account """ """ Account """
class Meta: class Meta:
model = Account model = Account
fields = [ fields = [
@ -127,6 +128,7 @@ class ConnectionTokenAccountSerializer(serializers.ModelSerializer):
class ConnectionTokenGatewaySerializer(serializers.ModelSerializer): class ConnectionTokenGatewaySerializer(serializers.ModelSerializer):
""" Gateway """ """ Gateway """
class Meta: class Meta:
model = Gateway model = Gateway
fields = ['id', 'ip', 'port', 'username', 'password', 'private_key'] fields = ['id', 'ip', 'port', 'username', 'password', 'private_key']
@ -143,6 +145,7 @@ class ConnectionTokenDomainSerializer(serializers.ModelSerializer):
class ConnectionTokenCmdFilterRuleSerializer(serializers.ModelSerializer): class ConnectionTokenCmdFilterRuleSerializer(serializers.ModelSerializer):
""" Command filter rule """ """ Command filter rule """
class Meta: class Meta:
model = CommandFilterRule model = CommandFilterRule
fields = [ fields = [
@ -151,12 +154,18 @@ class ConnectionTokenCmdFilterRuleSerializer(serializers.ModelSerializer):
] ]
class ConnectionTokenPlatform(serializers.ModelSerializer):
class Meta:
model = Platform
fields = ['id', 'name', 'org_id']
class ConnectionTokenSecretSerializer(OrgResourceModelSerializerMixin): class ConnectionTokenSecretSerializer(OrgResourceModelSerializerMixin):
user = ConnectionTokenUserSerializer(read_only=True) user = ConnectionTokenUserSerializer(read_only=True)
asset = ConnectionTokenAssetSerializer(read_only=True) asset = ConnectionTokenAssetSerializer(read_only=True)
platform = ConnectionTokenPlatform(read_only=True)
account = ConnectionTokenAccountSerializer(read_only=True) account = ConnectionTokenAccountSerializer(read_only=True)
gateway = ConnectionTokenGatewaySerializer(read_only=True) gateway = ConnectionTokenGatewaySerializer(read_only=True)
domain = ConnectionTokenDomainSerializer(read_only=True)
cmd_filter_rules = ConnectionTokenCmdFilterRuleSerializer(many=True) cmd_filter_rules = ConnectionTokenCmdFilterRuleSerializer(many=True)
actions = ActionChoicesField() actions = ActionChoicesField()
expire_at = serializers.IntegerField() expire_at = serializers.IntegerField()

View File

@ -27,6 +27,11 @@ class ActionChoices(BitChoices):
(_("Clipboard"), [cls.copy, cls.paste]), (_("Clipboard"), [cls.copy, cls.paste]),
) )
@classmethod
def has_perm(cls, action_name, total):
action_value = getattr(cls, action_name)
return action_value & total == action_value
class SpecialAccount(models.TextChoices): class SpecialAccount(models.TextChoices):
ALL = "@ALL", "All" ALL = "@ALL", "All"

View File

@ -3,12 +3,19 @@ from django.utils.translation import gettext_lazy as _
class PermToken(models.Model): class PermToken(models.Model):
"""
1. 用完失效
2. 仅用于授权不用于认证
3. redis 就行
4. 有效期 5 分钟
"""
user = models.ForeignKey('users.User', on_delete=models.CASCADE, verbose_name=_('User')) user = models.ForeignKey('users.User', on_delete=models.CASCADE, verbose_name=_('User'))
asset = models.ForeignKey('assets.Asset', on_delete=models.CASCADE, verbose_name=_('Asset')) asset = models.ForeignKey('assets.Asset', on_delete=models.CASCADE, verbose_name=_('Asset'))
account = models.CharField(max_length=128, verbose_name=_('Account')) account = models.CharField(max_length=128, verbose_name=_('Account'))
secret = models.CharField(max_length=1024, verbose_name=_('Secret')) secret = models.CharField(max_length=1024, verbose_name=_('Secret'))
protocol = models.CharField(max_length=32, verbose_name=_('Protocol')) protocol = models.CharField(max_length=32, verbose_name=_('Protocol'))
connect_method = models.CharField(max_length=32, verbose_name=_('Connect method')) connect_method = models.CharField(max_length=32, verbose_name=_('Connect method'))
actions = models.IntegerField(verbose_name=_('Actions'))
class Meta: class Meta:
abstract = True abstract = True

View File

@ -1,4 +1,3 @@
import time
from collections import defaultdict from collections import defaultdict
from assets.models import Account from assets.models import Account
@ -9,6 +8,7 @@ __all__ = ['PermAccountUtil']
class PermAccountUtil(AssetPermissionUtil): class PermAccountUtil(AssetPermissionUtil):
""" 资产授权账号相关的工具 """ """ 资产授权账号相关的工具 """
@staticmethod @staticmethod
def get_permed_accounts_from_perms(perms, user, asset): def get_permed_accounts_from_perms(perms, user, asset):
alias_action_bit_mapper = defaultdict(int) alias_action_bit_mapper = defaultdict(int)
@ -75,7 +75,9 @@ class PermAccountUtil(AssetPermissionUtil):
return accounts return accounts
def validate_permission(self, user, asset, account_username): def validate_permission(self, user, asset, account_username):
""" 校验用户有某个资产下某个账号名的权限 """ """ 校验用户有某个资产下某个账号名的权限
:param account_username: 可能是 @USER @INPUT
"""
permed_accounts = self.get_permed_accounts_for_user(user, asset) permed_accounts = self.get_permed_accounts_for_user(user, asset)
accounts_mapper = {account.username: account for account in permed_accounts} accounts_mapper = {account.username: account for account in permed_accounts}