2022-04-12 09:45:10 +00:00
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
from common.drf.serializers import BulkModelSerializer
|
2022-06-28 12:12:55 +00:00
|
|
|
|
from acls.serializers.rules import ip_group_child_validator, ip_group_help_text
|
2022-04-12 09:45:10 +00:00
|
|
|
|
from ..models import Endpoint, EndpointRule
|
|
|
|
|
|
|
|
|
|
__all__ = ['EndpointSerializer', 'EndpointRuleSerializer']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class EndpointSerializer(BulkModelSerializer):
|
2022-07-13 08:29:05 +00:00
|
|
|
|
# 解决 luna 处理繁琐的问题,oracle_port 返回匹配到的端口
|
|
|
|
|
oracle_port = serializers.SerializerMethodField(label=_('Oracle port'))
|
2022-04-12 09:45:10 +00:00
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = Endpoint
|
|
|
|
|
fields_mini = ['id', 'name']
|
|
|
|
|
fields_small = [
|
|
|
|
|
'host',
|
|
|
|
|
'https_port', 'http_port', 'ssh_port',
|
|
|
|
|
'rdp_port', 'mysql_port', 'mariadb_port',
|
2022-05-12 06:47:35 +00:00
|
|
|
|
'postgresql_port', 'redis_port',
|
2022-07-13 08:29:05 +00:00
|
|
|
|
'oracle_11g_port', 'oracle_12c_port',
|
|
|
|
|
'oracle_port',
|
2022-04-12 09:45:10 +00:00
|
|
|
|
]
|
|
|
|
|
fields = fields_mini + fields_small + [
|
|
|
|
|
'comment', 'date_created', 'date_updated', 'created_by'
|
|
|
|
|
]
|
|
|
|
|
extra_kwargs = {
|
|
|
|
|
'https_port': {'default': 443},
|
|
|
|
|
'http_port': {'default': 80},
|
|
|
|
|
'ssh_port': {'default': 2222},
|
|
|
|
|
'rdp_port': {'default': 3389},
|
|
|
|
|
'mysql_port': {'default': 33060},
|
|
|
|
|
'mariadb_port': {'default': 33061},
|
|
|
|
|
'postgresql_port': {'default': 54320},
|
2022-05-12 06:47:35 +00:00
|
|
|
|
'redis_port': {'default': 63790},
|
2022-07-13 08:29:05 +00:00
|
|
|
|
'oracle_11g_port': {'default': 15211},
|
|
|
|
|
'oracle_12c_port': {'default': 15212},
|
2022-04-12 09:45:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-13 08:29:05 +00:00
|
|
|
|
def get_oracle_port(self, obj: Endpoint):
|
|
|
|
|
view = self.context.get('view')
|
|
|
|
|
if not view or view.action not in ['smart']:
|
|
|
|
|
return 0
|
|
|
|
|
return obj.get_port(view.target_protocol)
|
|
|
|
|
|
2022-04-12 09:45:10 +00:00
|
|
|
|
|
|
|
|
|
class EndpointRuleSerializer(BulkModelSerializer):
|
2022-06-28 12:12:55 +00:00
|
|
|
|
_ip_group_help_text = '{} <br> {}'.format(
|
|
|
|
|
ip_group_help_text,
|
|
|
|
|
_('If asset IP addresses under different endpoints conflict, use asset labels')
|
|
|
|
|
)
|
2022-04-12 09:45:10 +00:00
|
|
|
|
ip_group = serializers.ListField(
|
2022-06-28 12:12:55 +00:00
|
|
|
|
default=['*'], label=_('IP'), help_text=_ip_group_help_text,
|
2022-04-12 09:45:10 +00:00
|
|
|
|
child=serializers.CharField(max_length=1024, validators=[ip_group_child_validator])
|
|
|
|
|
)
|
|
|
|
|
endpoint_display = serializers.ReadOnlyField(source='endpoint.name', label=_('Endpoint'))
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = EndpointRule
|
|
|
|
|
fields_mini = ['id', 'name']
|
|
|
|
|
fields_small = fields_mini + ['ip_group', 'priority']
|
|
|
|
|
fields_fk = ['endpoint', 'endpoint_display']
|
|
|
|
|
fields = fields_mini + fields_small + fields_fk + [
|
|
|
|
|
'comment', 'date_created', 'date_updated', 'created_by'
|
|
|
|
|
]
|
|
|
|
|
extra_kwargs = {
|
|
|
|
|
}
|