jumpserver/apps/assets/serializers.py

237 lines
7.2 KiB
Python
Raw Normal View History

2016-10-22 12:21:36 +00:00
# -*- coding: utf-8 -*-
from django.core.cache import cache
2017-03-15 16:19:47 +00:00
from rest_framework import viewsets, serializers, generics
2017-12-12 04:19:45 +00:00
from rest_framework_bulk.serializers import BulkListSerializer
2016-10-22 12:21:36 +00:00
2017-12-12 04:19:45 +00:00
from common.mixins import BulkSerializerMixin
2017-12-10 16:29:25 +00:00
from .models import AssetGroup, Asset, Cluster, AdminUser, SystemUser
from .tasks import SYSTEM_USER_CONN_CACHE_KEY_PREFIX, ADMIN_USER_CONN_CACHE_KEY_PREFIX
2016-11-01 09:21:16 +00:00
2017-01-06 12:34:24 +00:00
class AssetGroupSerializer(BulkSerializerMixin, serializers.ModelSerializer):
2016-11-10 14:06:23 +00:00
assets_amount = serializers.SerializerMethodField()
2017-01-07 14:34:12 +00:00
assets = serializers.PrimaryKeyRelatedField(many=True, queryset=Asset.objects.all())
2016-11-10 14:06:23 +00:00
2016-11-06 13:29:04 +00:00
class Meta:
model = AssetGroup
2017-01-07 14:34:12 +00:00
list_serializer_class = BulkListSerializer
fields = ['id', 'name', 'comment', 'assets_amount', 'assets']
2016-11-06 13:29:04 +00:00
2016-11-10 14:06:23 +00:00
@staticmethod
def get_assets_amount(obj):
return obj.assets.count()
2016-12-29 11:17:00 +00:00
2016-12-15 11:55:15 +00:00
class AssetUpdateGroupSerializer(serializers.ModelSerializer):
groups = serializers.PrimaryKeyRelatedField(many=True, queryset=AssetGroup.objects.all())
class Meta:
model = Asset
fields = ['id', 'groups']
2016-12-29 11:17:00 +00:00
2016-12-15 11:55:15 +00:00
class AssetUpdateSystemUserSerializer(serializers.ModelSerializer):
system_users = serializers.PrimaryKeyRelatedField(many=True, queryset=SystemUser.objects.all())
class Meta:
model = Asset
fields = ['id', 'system_users']
2016-11-06 13:29:04 +00:00
2016-12-29 11:17:00 +00:00
2017-01-06 12:34:24 +00:00
class AssetGroupUpdateSerializer(serializers.ModelSerializer):
2017-01-07 14:34:12 +00:00
"""update the asset group, and add or delete the asset to the group"""
assets = serializers.PrimaryKeyRelatedField(many=True, queryset=Asset.objects.all())
class Meta:
model = AssetGroup
fields = ['id', 'assets']
2017-01-06 12:34:24 +00:00
class AssetGroupUpdateSystemUserSerializer(serializers.ModelSerializer):
2017-01-07 14:34:12 +00:00
system_users = serializers.PrimaryKeyRelatedField(many=True, queryset=SystemUser.objects.all())
class Meta:
model = AssetGroup
fields = ['id', 'system_users']
2017-01-06 12:34:24 +00:00
2017-12-07 08:25:50 +00:00
class ClusterUpdateAssetsSerializer(serializers.ModelSerializer):
2017-01-07 14:34:12 +00:00
assets = serializers.PrimaryKeyRelatedField(many=True, queryset=Asset.objects.all())
class Meta:
2017-12-07 08:25:50 +00:00
model = Cluster
2017-01-07 14:34:12 +00:00
fields = ['id', 'assets']
2017-01-06 12:34:24 +00:00
2016-11-06 13:29:04 +00:00
class AdminUserSerializer(serializers.ModelSerializer):
assets_amount = serializers.SerializerMethodField()
2017-12-10 16:29:25 +00:00
unreachable_amount = serializers.SerializerMethodField()
2016-11-06 13:29:04 +00:00
class Meta:
model = AdminUser
fields = '__all__'
2016-11-06 13:29:04 +00:00
2017-12-10 16:29:25 +00:00
@staticmethod
def get_unreachable_amount(obj):
data = cache.get(ADMIN_USER_CONN_CACHE_KEY_PREFIX + obj.name)
if data:
return len(data.get('dark'))
else:
return 'Unknown'
@staticmethod
def get_assets_amount(obj):
amount = 0
clusters = obj.cluster_set.all()
for cluster in clusters:
amount += len(cluster.assets.all())
return amount
2016-11-06 14:45:26 +00:00
2016-11-06 13:29:04 +00:00
class SystemUserSerializer(serializers.ModelSerializer):
2017-12-10 16:29:25 +00:00
unreachable_amount = serializers.SerializerMethodField()
assets_amount = serializers.SerializerMethodField()
2017-12-10 16:29:25 +00:00
2016-11-06 13:29:04 +00:00
class Meta:
model = SystemUser
2016-11-10 14:06:23 +00:00
exclude = ('_password', '_private_key', '_public_key')
2016-11-06 13:29:04 +00:00
2017-12-10 16:29:25 +00:00
@staticmethod
def get_unreachable_amount(obj):
data = cache.get(SYSTEM_USER_CONN_CACHE_KEY_PREFIX + obj.name)
if data:
return len(data.get('dark'))
else:
return "Unknown"
@staticmethod
def get_assets_amount(obj):
amount = 0
for cluster in obj.cluster.all():
amount += cluster.assets.all().count()
return amount
2016-11-06 14:45:26 +00:00
2016-11-06 13:29:04 +00:00
2017-01-17 08:34:47 +00:00
class AssetSystemUserSerializer(serializers.ModelSerializer):
class Meta:
model = SystemUser
fields = ('id', 'name', 'username', 'priority', 'protocol', 'comment',)
2017-01-17 08:34:47 +00:00
2017-01-06 12:34:24 +00:00
class SystemUserUpdateAssetsSerializer(serializers.ModelSerializer):
2017-01-07 14:34:12 +00:00
assets = serializers.PrimaryKeyRelatedField(many=True, queryset=Asset.objects.all())
class Meta:
model = SystemUser
fields = ['id', 'assets']
2017-01-06 12:34:24 +00:00
class SystemUserUpdateAssetGroupSerializer(serializers.ModelSerializer):
2017-01-07 14:34:12 +00:00
asset_groups = serializers.PrimaryKeyRelatedField(many=True, queryset=AssetGroup.objects.all())
class Meta:
model = SystemUser
fields = ['id', 'asset_groups']
2016-12-29 11:17:00 +00:00
class SystemUserSimpleSerializer(serializers.ModelSerializer):
class Meta:
model = SystemUser
fields = ('id', 'name', 'username')
2016-11-10 14:06:23 +00:00
class AssetSerializer(BulkSerializerMixin, serializers.ModelSerializer):
class Meta(object):
model = Asset
list_serializer_class = BulkListSerializer
fields = '__all__'
2017-12-12 04:19:45 +00:00
validators = [] # If not set to [], partial bulk update will be error
2016-11-27 06:37:50 +00:00
def get_field_names(self, declared_fields, info):
2017-12-12 04:19:45 +00:00
fields = super().get_field_names(declared_fields, info)
fields.extend([
'get_type_display', 'get_env_display',
'hardware_info', 'is_connective',
])
2016-11-27 06:37:50 +00:00
return fields
2016-11-10 14:06:23 +00:00
class AssetGrantedSerializer(serializers.ModelSerializer):
2017-01-17 08:34:47 +00:00
system_users_granted = AssetSystemUserSerializer(many=True, read_only=True)
2016-11-10 14:06:23 +00:00
is_inherited = serializers.SerializerMethodField()
system_users_join = serializers.SerializerMethodField()
class Meta(object):
model = Asset
fields = ("id", "hostname", "ip", "port", "system_users_granted",
"is_inherited", "is_active", "system_users_join", "comment")
2016-11-10 14:06:23 +00:00
@staticmethod
def get_is_inherited(obj):
if getattr(obj, 'inherited', ''):
return True
else:
return False
@staticmethod
def get_system_users_join(obj):
2017-12-10 16:29:25 +00:00
return ', '.join([system_user.username for system_user in obj.system_users_granted])
class MyAssetGrantedSerializer(AssetGrantedSerializer):
"""Remove ip and port from asset for security"""
class Meta(object):
model = Asset
fields = ("id", "hostname", "system_users_granted", "is_inherited",
"is_active", "system_users_join", "comment")
2016-11-10 14:06:23 +00:00
2017-12-07 08:25:50 +00:00
class ClusterSerializer(BulkSerializerMixin, serializers.ModelSerializer):
2016-11-06 13:29:04 +00:00
assets_amount = serializers.SerializerMethodField()
admin_user_name = serializers.SerializerMethodField()
2017-01-07 14:34:12 +00:00
assets = serializers.PrimaryKeyRelatedField(many=True, queryset=Asset.objects.all())
2016-11-06 13:29:04 +00:00
class Meta:
2017-12-07 08:25:50 +00:00
model = Cluster
fields = '__all__'
2016-11-06 13:29:04 +00:00
@staticmethod
def get_assets_amount(obj):
return obj.assets.count()
@staticmethod
def get_admin_user_name(obj):
2017-12-13 09:21:08 +00:00
try:
return obj.admin_user.name
except AttributeError:
return ''
2016-11-06 13:29:04 +00:00
2017-11-14 01:44:16 +00:00
class AssetGroupGrantedSerializer(BulkSerializerMixin, serializers.ModelSerializer):
assets_granted = AssetGrantedSerializer(many=True, read_only=True)
assets_amount = serializers.SerializerMethodField()
class Meta:
model = AssetGroup
list_serializer_class = BulkListSerializer
fields = '__all__'
@staticmethod
def get_assets_amount(obj):
return len(obj.assets_granted)
class MyAssetGroupGrantedSerializer(serializers.ModelSerializer):
assets_granted = MyAssetGrantedSerializer(many=True, read_only=True)
assets_amount = serializers.SerializerMethodField()
class Meta:
model = AssetGroup
list_serializer_class = BulkListSerializer
fields = '__all__'
@staticmethod
def get_assets_amount(obj):
return len(obj.assets_granted)