2017-10-06 23:37:22 +00:00
|
|
|
# -*- mode: python; coding: utf-8 -*-
|
2017-01-15 23:01:18 +00:00
|
|
|
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
|
|
|
|
# se veut agnostique au réseau considéré, de manière à être installable en
|
|
|
|
# quelques clics.
|
|
|
|
#
|
|
|
|
# Copyright © 2017 Gabriel Détraz
|
2019-09-29 14:02:28 +00:00
|
|
|
# Copyright © 2017 Lara Kermarec
|
2017-01-15 23:01:18 +00:00
|
|
|
# Copyright © 2017 Augustin Lemesle
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
2018-04-13 22:48:44 +00:00
|
|
|
# Augustin Lemesle
|
2018-04-14 18:19:02 +00:00
|
|
|
"""machines.serializers
|
|
|
|
Serializers for the Machines app
|
|
|
|
"""
|
|
|
|
|
2016-07-18 21:22:59 +00:00
|
|
|
from rest_framework import serializers
|
2018-09-24 17:31:23 +00:00
|
|
|
|
2017-10-16 01:02:33 +00:00
|
|
|
from machines.models import (
|
|
|
|
Interface,
|
|
|
|
IpType,
|
|
|
|
Extension,
|
|
|
|
IpList,
|
|
|
|
Domain,
|
2017-11-15 14:17:22 +00:00
|
|
|
Txt,
|
2017-10-16 01:02:33 +00:00
|
|
|
Mx,
|
2017-11-16 01:33:57 +00:00
|
|
|
Srv,
|
2017-10-16 01:02:33 +00:00
|
|
|
Service_link,
|
|
|
|
Ns,
|
2018-01-30 06:14:28 +00:00
|
|
|
OuverturePort,
|
2019-11-04 16:55:03 +00:00
|
|
|
Ipv6List,
|
2017-10-16 01:02:33 +00:00
|
|
|
)
|
|
|
|
|
2016-07-18 21:22:59 +00:00
|
|
|
|
2016-11-16 18:30:48 +00:00
|
|
|
class IpTypeField(serializers.RelatedField):
|
2018-04-14 18:19:02 +00:00
|
|
|
""" Serializer for an IpType object field """
|
|
|
|
|
2016-11-16 18:30:48 +00:00
|
|
|
def to_representation(self, value):
|
|
|
|
return value.type
|
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
def to_internal_value(self, data):
|
|
|
|
pass
|
|
|
|
|
2017-10-16 01:02:33 +00:00
|
|
|
|
2016-11-16 18:30:48 +00:00
|
|
|
class IpListSerializer(serializers.ModelSerializer):
|
2018-04-14 18:19:02 +00:00
|
|
|
""" Serializer for an Ipv4List obejct using the IpType serialization """
|
|
|
|
|
2016-11-16 18:30:48 +00:00
|
|
|
ip_type = IpTypeField(read_only=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = IpList
|
2019-11-04 16:55:03 +00:00
|
|
|
fields = ("ipv4", "ip_type")
|
2016-11-16 18:30:48 +00:00
|
|
|
|
2017-10-16 01:02:33 +00:00
|
|
|
|
2018-01-30 06:14:28 +00:00
|
|
|
class Ipv6ListSerializer(serializers.ModelSerializer):
|
2018-04-14 18:19:02 +00:00
|
|
|
""" Serializer for an Ipv6List object """
|
|
|
|
|
2018-01-30 06:14:28 +00:00
|
|
|
class Meta:
|
|
|
|
model = Ipv6List
|
2019-11-04 16:55:03 +00:00
|
|
|
fields = ("ipv6", "slaac_ip")
|
2018-01-30 06:14:28 +00:00
|
|
|
|
|
|
|
|
2016-11-16 18:30:48 +00:00
|
|
|
class InterfaceSerializer(serializers.ModelSerializer):
|
2018-04-14 18:19:02 +00:00
|
|
|
""" Serializer for an Interface object. Use SerializerMethodField
|
|
|
|
to get ForeignKey values """
|
|
|
|
|
2016-11-16 18:30:48 +00:00
|
|
|
ipv4 = IpListSerializer(read_only=True)
|
2018-04-14 18:19:02 +00:00
|
|
|
# TODO : use serializer.RelatedField to avoid duplicate code
|
2019-11-04 16:55:03 +00:00
|
|
|
mac_address = serializers.SerializerMethodField("get_macaddress")
|
|
|
|
domain = serializers.SerializerMethodField("get_dns")
|
|
|
|
extension = serializers.SerializerMethodField("get_interface_extension")
|
2016-12-19 22:59:54 +00:00
|
|
|
|
2016-07-18 21:22:59 +00:00
|
|
|
class Meta:
|
|
|
|
model = Interface
|
2019-11-04 16:55:03 +00:00
|
|
|
fields = ("ipv4", "mac_address", "domain", "extension")
|
2016-12-24 19:04:53 +00:00
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_dns(obj):
|
|
|
|
""" The name of the associated DNS object """
|
2016-12-26 17:00:43 +00:00
|
|
|
return obj.domain.name
|
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_interface_extension(obj):
|
|
|
|
""" The name of the associated Interface object """
|
2017-08-06 18:00:29 +00:00
|
|
|
return obj.domain.extension.name
|
2016-11-16 18:30:48 +00:00
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_macaddress(obj):
|
|
|
|
""" The string representation of the associated MAC address """
|
2016-12-19 22:59:54 +00:00
|
|
|
return str(obj.mac_address)
|
|
|
|
|
2017-10-16 01:02:33 +00:00
|
|
|
|
2017-10-03 02:42:49 +00:00
|
|
|
class FullInterfaceSerializer(serializers.ModelSerializer):
|
2018-04-14 18:19:02 +00:00
|
|
|
""" Serializer for an Interface obejct. Use SerializerMethodField
|
|
|
|
to get ForeignKey values """
|
|
|
|
|
2017-10-03 02:42:49 +00:00
|
|
|
ipv4 = IpListSerializer(read_only=True)
|
2018-01-30 06:14:28 +00:00
|
|
|
ipv6 = Ipv6ListSerializer(read_only=True, many=True)
|
2018-04-14 18:19:02 +00:00
|
|
|
# TODO : use serializer.RelatedField to avoid duplicate code
|
2019-11-04 16:55:03 +00:00
|
|
|
mac_address = serializers.SerializerMethodField("get_macaddress")
|
|
|
|
domain = serializers.SerializerMethodField("get_dns")
|
|
|
|
extension = serializers.SerializerMethodField("get_interface_extension")
|
2017-10-03 02:42:49 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Interface
|
2019-11-04 16:55:03 +00:00
|
|
|
fields = ("ipv4", "ipv6", "mac_address", "domain", "extension")
|
2017-10-03 02:42:49 +00:00
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_dns(obj):
|
|
|
|
""" The name of the associated DNS object """
|
2017-10-03 02:42:49 +00:00
|
|
|
return obj.domain.name
|
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_interface_extension(obj):
|
|
|
|
""" The name of the associated Extension object """
|
2017-10-03 02:42:49 +00:00
|
|
|
return obj.domain.extension.name
|
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_macaddress(obj):
|
|
|
|
""" The string representation of the associated MAC address """
|
2017-10-03 02:42:49 +00:00
|
|
|
return str(obj.mac_address)
|
|
|
|
|
2017-10-16 01:02:33 +00:00
|
|
|
|
2016-11-16 18:30:48 +00:00
|
|
|
class ExtensionNameField(serializers.RelatedField):
|
2018-04-14 18:19:02 +00:00
|
|
|
""" Serializer for Extension object field """
|
|
|
|
|
2016-11-18 19:16:40 +00:00
|
|
|
def to_representation(self, value):
|
2016-12-19 19:55:35 +00:00
|
|
|
return value.name
|
2016-07-19 19:43:24 +00:00
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
def to_internal_value(self, data):
|
|
|
|
pass
|
|
|
|
|
2017-10-16 01:02:33 +00:00
|
|
|
|
2016-11-20 03:39:10 +00:00
|
|
|
class TypeSerializer(serializers.ModelSerializer):
|
2018-04-14 18:19:02 +00:00
|
|
|
""" Serializer for an IpType object. Use SerializerMethodField to
|
|
|
|
get ForeignKey values. Infos about the general port policy is added """
|
|
|
|
|
2016-11-20 03:39:10 +00:00
|
|
|
extension = ExtensionNameField(read_only=True)
|
2019-11-04 16:55:03 +00:00
|
|
|
ouverture_ports_tcp_in = serializers.SerializerMethodField(
|
|
|
|
"get_port_policy_input_tcp"
|
|
|
|
)
|
|
|
|
ouverture_ports_tcp_out = serializers.SerializerMethodField(
|
|
|
|
"get_port_policy_output_tcp"
|
|
|
|
)
|
|
|
|
ouverture_ports_udp_in = serializers.SerializerMethodField(
|
|
|
|
"get_port_policy_input_udp"
|
|
|
|
)
|
|
|
|
ouverture_ports_udp_out = serializers.SerializerMethodField(
|
|
|
|
"get_port_policy_output_udp"
|
|
|
|
)
|
2017-10-03 17:07:53 +00:00
|
|
|
|
2016-11-20 03:39:10 +00:00
|
|
|
class Meta:
|
|
|
|
model = IpType
|
2019-11-04 16:55:03 +00:00
|
|
|
fields = (
|
|
|
|
"type",
|
|
|
|
"extension",
|
|
|
|
"domaine_ip_start",
|
|
|
|
"domaine_ip_stop",
|
|
|
|
"prefix_v6",
|
|
|
|
"ouverture_ports_tcp_in",
|
|
|
|
"ouverture_ports_tcp_out",
|
|
|
|
"ouverture_ports_udp_in",
|
|
|
|
"ouverture_ports_udp_out",
|
|
|
|
)
|
2017-10-03 17:07:53 +00:00
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_port_policy(obj, protocole, io):
|
|
|
|
""" Generic utility function to get the policy for a given
|
|
|
|
port, protocole and IN or OUT """
|
2017-10-12 21:54:50 +00:00
|
|
|
if obj.ouverture_ports is None:
|
2017-10-03 17:07:53 +00:00
|
|
|
return []
|
2017-10-16 01:02:33 +00:00
|
|
|
return map(
|
|
|
|
str,
|
2019-11-04 16:55:03 +00:00
|
|
|
obj.ouverture_ports.ouvertureport_set.filter(protocole=protocole).filter(
|
|
|
|
io=io
|
|
|
|
),
|
2017-10-16 01:02:33 +00:00
|
|
|
)
|
2017-10-03 17:07:53 +00:00
|
|
|
|
|
|
|
def get_port_policy_input_tcp(self, obj):
|
2017-10-16 01:02:33 +00:00
|
|
|
"""Renvoie la liste des ports ouverts en entrée tcp"""
|
2017-10-03 17:07:53 +00:00
|
|
|
return self.get_port_policy(obj, OuverturePort.TCP, OuverturePort.IN)
|
|
|
|
|
|
|
|
def get_port_policy_output_tcp(self, obj):
|
2017-10-16 01:02:33 +00:00
|
|
|
"""Renvoie la liste des ports ouverts en sortie tcp"""
|
2017-10-03 17:07:53 +00:00
|
|
|
return self.get_port_policy(obj, OuverturePort.TCP, OuverturePort.OUT)
|
|
|
|
|
|
|
|
def get_port_policy_input_udp(self, obj):
|
2017-10-16 01:02:33 +00:00
|
|
|
"""Renvoie la liste des ports ouverts en entrée udp"""
|
2017-10-03 17:07:53 +00:00
|
|
|
return self.get_port_policy(obj, OuverturePort.UDP, OuverturePort.IN)
|
|
|
|
|
|
|
|
def get_port_policy_output_udp(self, obj):
|
2017-10-16 01:02:33 +00:00
|
|
|
"""Renvoie la liste des ports ouverts en sortie udp"""
|
2017-10-03 17:07:53 +00:00
|
|
|
return self.get_port_policy(obj, OuverturePort.UDP, OuverturePort.OUT)
|
2016-11-20 03:39:10 +00:00
|
|
|
|
2017-10-16 01:02:33 +00:00
|
|
|
|
2016-11-20 04:12:36 +00:00
|
|
|
class ExtensionSerializer(serializers.ModelSerializer):
|
2017-10-16 01:02:33 +00:00
|
|
|
"""Serialisation d'une extension : origin_ip et la zone sont
|
|
|
|
des foreign_key donc evalués en get_..."""
|
2019-11-04 16:55:03 +00:00
|
|
|
|
|
|
|
origin = serializers.SerializerMethodField("get_origin_ip")
|
|
|
|
zone_entry = serializers.SerializerMethodField("get_zone_name")
|
|
|
|
soa = serializers.SerializerMethodField("get_soa_data")
|
2016-11-20 04:12:36 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Extension
|
2019-11-04 16:55:03 +00:00
|
|
|
fields = ("name", "origin", "origin_v6", "zone_entry", "soa")
|
2016-11-20 04:12:36 +00:00
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_origin_ip(obj):
|
|
|
|
""" The IP of the associated origin for the zone """
|
|
|
|
return obj.origin.ipv4
|
2016-11-20 04:12:36 +00:00
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_zone_name(obj):
|
|
|
|
""" The name of the associated zone """
|
2017-09-06 09:07:36 +00:00
|
|
|
return str(obj.dns_entry)
|
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_soa_data(obj):
|
|
|
|
""" The representation of the associated SOA """
|
2019-11-04 16:55:03 +00:00
|
|
|
return {"mail": obj.soa.dns_soa_mail, "param": obj.soa.dns_soa_param}
|
2017-10-20 00:28:47 +00:00
|
|
|
|
2017-10-16 01:02:33 +00:00
|
|
|
|
2016-11-20 03:29:16 +00:00
|
|
|
class MxSerializer(serializers.ModelSerializer):
|
2017-10-16 01:02:33 +00:00
|
|
|
"""Serialisation d'un MX, evaluation du nom, de la zone
|
|
|
|
et du serveur cible, etant des foreign_key"""
|
2019-11-04 16:55:03 +00:00
|
|
|
|
|
|
|
name = serializers.SerializerMethodField("get_entry_name")
|
|
|
|
zone = serializers.SerializerMethodField("get_zone_name")
|
|
|
|
mx_entry = serializers.SerializerMethodField("get_mx_name")
|
2016-11-16 18:30:48 +00:00
|
|
|
|
2016-11-12 15:12:24 +00:00
|
|
|
class Meta:
|
2016-11-20 03:29:16 +00:00
|
|
|
model = Mx
|
2019-11-04 16:55:03 +00:00
|
|
|
fields = ("zone", "priority", "name", "mx_entry")
|
2016-11-20 03:29:16 +00:00
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_entry_name(obj):
|
|
|
|
""" The name of the DNS MX entry """
|
2016-12-26 23:48:32 +00:00
|
|
|
return str(obj.name)
|
2016-11-20 03:29:16 +00:00
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_zone_name(obj):
|
|
|
|
""" The name of the associated zone of the MX record """
|
2016-11-20 03:29:16 +00:00
|
|
|
return obj.zone.name
|
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_mx_name(obj):
|
|
|
|
""" The string representation of the entry to add to the DNS """
|
2017-09-06 09:07:36 +00:00
|
|
|
return str(obj.dns_entry)
|
|
|
|
|
2017-10-16 01:02:33 +00:00
|
|
|
|
2017-11-15 14:17:22 +00:00
|
|
|
class TxtSerializer(serializers.ModelSerializer):
|
2017-10-16 01:02:33 +00:00
|
|
|
"""Serialisation d'un txt : zone cible et l'entrée txt
|
|
|
|
sont evaluées à part"""
|
2019-11-04 16:55:03 +00:00
|
|
|
|
|
|
|
zone = serializers.SerializerMethodField("get_zone_name")
|
|
|
|
txt_entry = serializers.SerializerMethodField("get_txt_name")
|
2017-09-05 16:18:41 +00:00
|
|
|
|
|
|
|
class Meta:
|
2017-11-15 14:17:22 +00:00
|
|
|
model = Txt
|
2019-11-04 16:55:03 +00:00
|
|
|
fields = ("zone", "txt_entry", "field1", "field2")
|
2017-09-05 16:18:41 +00:00
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_zone_name(obj):
|
|
|
|
""" The name of the associated zone """
|
2017-09-05 16:18:41 +00:00
|
|
|
return str(obj.zone.name)
|
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_txt_name(obj):
|
|
|
|
""" The string representation of the entry to add to the DNS """
|
2017-09-05 16:18:41 +00:00
|
|
|
return str(obj.dns_entry)
|
2017-11-16 01:33:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SrvSerializer(serializers.ModelSerializer):
|
|
|
|
"""Serialisation d'un srv : zone cible et l'entrée txt"""
|
2019-11-04 16:55:03 +00:00
|
|
|
|
|
|
|
extension = serializers.SerializerMethodField("get_extension_name")
|
|
|
|
srv_entry = serializers.SerializerMethodField("get_srv_name")
|
2017-11-16 01:33:57 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Srv
|
|
|
|
fields = (
|
2019-11-04 16:55:03 +00:00
|
|
|
"service",
|
|
|
|
"protocole",
|
|
|
|
"extension",
|
|
|
|
"ttl",
|
|
|
|
"priority",
|
|
|
|
"weight",
|
|
|
|
"port",
|
|
|
|
"target",
|
|
|
|
"srv_entry",
|
2017-11-16 01:33:57 +00:00
|
|
|
)
|
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_extension_name(obj):
|
|
|
|
""" The name of the associated extension """
|
2017-11-16 01:33:57 +00:00
|
|
|
return str(obj.extension.name)
|
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_srv_name(obj):
|
|
|
|
""" The string representation of the entry to add to the DNS """
|
2017-11-16 01:33:57 +00:00
|
|
|
return str(obj.dns_entry)
|
2017-09-05 16:18:41 +00:00
|
|
|
|
2017-10-16 01:02:33 +00:00
|
|
|
|
2016-11-20 03:29:16 +00:00
|
|
|
class NsSerializer(serializers.ModelSerializer):
|
2017-10-16 01:02:33 +00:00
|
|
|
"""Serialisation d'un NS : la zone, l'entrée ns complète et le serveur
|
|
|
|
ns sont évalués à part"""
|
2019-11-04 16:55:03 +00:00
|
|
|
|
|
|
|
zone = serializers.SerializerMethodField("get_zone_name")
|
|
|
|
ns = serializers.SerializerMethodField("get_domain_name")
|
|
|
|
ns_entry = serializers.SerializerMethodField("get_text_name")
|
2016-07-18 21:22:59 +00:00
|
|
|
|
2016-11-18 17:15:57 +00:00
|
|
|
class Meta:
|
2016-11-20 03:29:16 +00:00
|
|
|
model = Ns
|
2019-11-04 16:55:03 +00:00
|
|
|
fields = ("zone", "ns", "ns_entry")
|
2016-11-20 03:29:16 +00:00
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_zone_name(obj):
|
|
|
|
""" The name of the associated zone """
|
2016-11-20 03:29:16 +00:00
|
|
|
return obj.zone.name
|
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_domain_name(obj):
|
|
|
|
""" The name of the associated NS target """
|
2016-12-26 23:48:32 +00:00
|
|
|
return str(obj.ns)
|
2016-11-18 17:15:57 +00:00
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_text_name(obj):
|
|
|
|
""" The string representation of the entry to add to the DNS """
|
2017-09-06 09:07:36 +00:00
|
|
|
return str(obj.dns_entry)
|
|
|
|
|
2017-10-16 01:02:33 +00:00
|
|
|
|
2016-12-24 19:04:53 +00:00
|
|
|
class DomainSerializer(serializers.ModelSerializer):
|
2017-10-16 01:02:33 +00:00
|
|
|
"""Serialisation d'un domain, extension, cname sont des foreign_key,
|
|
|
|
et l'entrée complète, sont évalués à part"""
|
2019-11-04 16:55:03 +00:00
|
|
|
|
|
|
|
extension = serializers.SerializerMethodField("get_zone_name")
|
|
|
|
cname = serializers.SerializerMethodField("get_alias_name")
|
|
|
|
cname_entry = serializers.SerializerMethodField("get_cname_name")
|
2016-07-18 21:22:59 +00:00
|
|
|
|
2016-11-18 17:15:57 +00:00
|
|
|
class Meta:
|
2016-12-24 19:04:53 +00:00
|
|
|
model = Domain
|
2019-11-04 16:55:03 +00:00
|
|
|
fields = ("name", "extension", "cname", "cname_entry")
|
2016-11-20 03:29:16 +00:00
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_zone_name(obj):
|
|
|
|
""" The name of the associated zone """
|
2016-12-25 23:01:48 +00:00
|
|
|
return obj.extension.name
|
2016-11-20 03:29:16 +00:00
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_alias_name(obj):
|
|
|
|
""" The name of the associated alias """
|
2016-12-26 23:48:32 +00:00
|
|
|
return str(obj.cname)
|
2016-12-24 19:04:53 +00:00
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_cname_name(obj):
|
|
|
|
""" The name of the associated CNAME target """
|
2017-09-06 09:07:36 +00:00
|
|
|
return str(obj.dns_entry)
|
|
|
|
|
2017-10-16 01:02:33 +00:00
|
|
|
|
2017-08-08 17:40:30 +00:00
|
|
|
class ServiceServersSerializer(serializers.ModelSerializer):
|
2017-10-16 01:02:33 +00:00
|
|
|
"""Evaluation d'un Service, et serialisation"""
|
2019-11-04 16:55:03 +00:00
|
|
|
|
|
|
|
server = serializers.SerializerMethodField("get_server_name")
|
|
|
|
service = serializers.SerializerMethodField("get_service_name")
|
|
|
|
need_regen = serializers.SerializerMethodField("get_regen_status")
|
2017-08-08 17:40:30 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Service_link
|
2019-11-04 16:55:03 +00:00
|
|
|
fields = ("server", "service", "need_regen")
|
2017-08-08 17:40:30 +00:00
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_server_name(obj):
|
|
|
|
""" The name of the associated server """
|
2017-08-08 17:40:30 +00:00
|
|
|
return str(obj.server.domain.name)
|
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_service_name(obj):
|
|
|
|
""" The name of the service name """
|
2017-08-08 17:40:30 +00:00
|
|
|
return str(obj.service)
|
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_regen_status(obj):
|
|
|
|
""" The string representation of the regen status """
|
2018-05-26 21:08:03 +00:00
|
|
|
return obj.need_regen
|
2017-10-03 16:27:06 +00:00
|
|
|
|
2017-10-16 01:02:33 +00:00
|
|
|
|
2017-10-04 20:03:26 +00:00
|
|
|
class OuverturePortsSerializer(serializers.Serializer):
|
2017-10-16 01:02:33 +00:00
|
|
|
"""Serialisation de l'ouverture des ports"""
|
2019-11-04 16:55:03 +00:00
|
|
|
|
2017-10-04 20:03:26 +00:00
|
|
|
ipv4 = serializers.SerializerMethodField()
|
|
|
|
ipv6 = serializers.SerializerMethodField()
|
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
def create(self, validated_data):
|
|
|
|
""" Creates a new object based on the un-serialized data.
|
|
|
|
Used to implement an abstract inherited method """
|
|
|
|
pass
|
|
|
|
|
|
|
|
def update(self, instance, validated_data):
|
|
|
|
""" Updates an object based on the un-serialized data.
|
|
|
|
Used to implement an abstract inherited method """
|
|
|
|
pass
|
|
|
|
|
|
|
|
@staticmethod
|
2017-10-04 20:03:26 +00:00
|
|
|
def get_ipv4():
|
2018-04-14 18:19:02 +00:00
|
|
|
""" The representation of the policy for the IPv4 addresses """
|
2018-04-13 22:48:44 +00:00
|
|
|
return {
|
|
|
|
i.ipv4.ipv4: {
|
|
|
|
"tcp_in": [j.tcp_ports_in() for j in i.port_lists.all()],
|
2018-09-24 17:31:23 +00:00
|
|
|
"tcp_out": [j.tcp_ports_out() for j in i.port_lists.all()],
|
2018-04-13 22:48:44 +00:00
|
|
|
"udp_in": [j.udp_ports_in() for j in i.port_lists.all()],
|
|
|
|
"udp_out": [j.udp_ports_out() for j in i.port_lists.all()],
|
2017-10-16 01:02:33 +00:00
|
|
|
}
|
2019-11-04 16:55:03 +00:00
|
|
|
for i in Interface.objects.all()
|
|
|
|
if i.ipv4
|
2017-10-04 20:03:26 +00:00
|
|
|
}
|
2017-10-16 01:02:33 +00:00
|
|
|
|
2018-04-14 18:19:02 +00:00
|
|
|
@staticmethod
|
2017-10-04 20:03:26 +00:00
|
|
|
def get_ipv6():
|
2018-04-14 18:19:02 +00:00
|
|
|
""" The representation of the policy for the IPv6 addresses """
|
2018-04-13 22:48:44 +00:00
|
|
|
return {
|
|
|
|
i.ipv6: {
|
|
|
|
"tcp_in": [j.tcp_ports_in() for j in i.port_lists.all()],
|
2018-09-24 17:31:23 +00:00
|
|
|
"tcp_out": [j.tcp_ports_out() for j in i.port_lists.all()],
|
2018-04-13 22:48:44 +00:00
|
|
|
"udp_in": [j.udp_ports_in() for j in i.port_lists.all()],
|
|
|
|
"udp_out": [j.udp_ports_out() for j in i.port_lists.all()],
|
2017-10-16 01:02:33 +00:00
|
|
|
}
|
2019-11-04 16:55:03 +00:00
|
|
|
for i in Interface.objects.all()
|
|
|
|
if i.ipv6
|
2017-10-04 20:03:26 +00:00
|
|
|
}
|