serializers.py 11 KB
Newer Older
1
# -*- mode: python; coding: utf-8 -*-
lhark's avatar
lhark committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 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
# Copyright © 2017  Goulven Kermarec
# 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.

Dalahro's avatar
Dalahro committed
24 25
#Augustin Lemesle

Dalahro's avatar
Dalahro committed
26
from rest_framework import serializers
Gabriel Detraz's avatar
Gabriel Detraz committed
27 28 29 30 31 32 33
from machines.models import (
    Interface,
    IpType,
    Extension,
    IpList,
    MachineType,
    Domain,
Gabriel Detraz's avatar
Gabriel Detraz committed
34
    Txt,
Gabriel Detraz's avatar
Gabriel Detraz committed
35
    Mx,
Gabriel Detraz's avatar
Fix #52  
Gabriel Detraz committed
36
    Srv,
Gabriel Detraz's avatar
Gabriel Detraz committed
37 38 39
    Service_link,
    Ns,
    OuverturePortList,
40 41
    OuverturePort,
    Ipv6List
Gabriel Detraz's avatar
Gabriel Detraz committed
42 43
)

Dalahro's avatar
Dalahro committed
44

Dalahro's avatar
Dalahro committed
45
class IpTypeField(serializers.RelatedField):
Gabriel Detraz's avatar
Gabriel Detraz committed
46
    """Serialisation d'une iptype, renvoie son evaluation str"""
Dalahro's avatar
Dalahro committed
47 48 49
    def to_representation(self, value):
        return value.type

Gabriel Detraz's avatar
Gabriel Detraz committed
50

Dalahro's avatar
Dalahro committed
51
class IpListSerializer(serializers.ModelSerializer):
Gabriel Detraz's avatar
Gabriel Detraz committed
52 53
    """Serialisation d'une iplist, ip_type etant une foreign_key,
    on evalue sa methode str"""
Dalahro's avatar
Dalahro committed
54 55 56 57 58 59
    ip_type = IpTypeField(read_only=True)

    class Meta:
        model = IpList
        fields = ('ipv4', 'ip_type')

Gabriel Detraz's avatar
Gabriel Detraz committed
60

61 62 63 64 65 66
class Ipv6ListSerializer(serializers.ModelSerializer):
    class Meta:
        model = Ipv6List
        fields = ('ipv6', 'slaac_ip')


Dalahro's avatar
Dalahro committed
67
class InterfaceSerializer(serializers.ModelSerializer):
Gabriel Detraz's avatar
Gabriel Detraz committed
68 69 70
    """Serialisation d'une interface, ipv4, domain et extension sont
    des foreign_key, on les override et on les evalue avec des fonctions
    get_..."""
Dalahro's avatar
Dalahro committed
71
    ipv4 = IpListSerializer(read_only=True)
Gabriel Detraz's avatar
Gabriel Detraz committed
72
    mac_address = serializers.SerializerMethodField('get_macaddress')
chirac's avatar
chirac committed
73
    domain = serializers.SerializerMethodField('get_dns')
Mael Kervella's avatar
Mael Kervella committed
74
    extension = serializers.SerializerMethodField('get_interface_extension')
Gabriel Detraz's avatar
Gabriel Detraz committed
75

Dalahro's avatar
Dalahro committed
76 77
    class Meta:
        model = Interface
Gabriel Detraz's avatar
Gabriel Detraz committed
78
        fields = ('ipv4', 'mac_address', 'domain', 'extension')
chirac's avatar
chirac committed
79 80

    def get_dns(self, obj):
Gabriel Detraz's avatar
Gabriel Detraz committed
81 82
        return obj.domain.name

Mael Kervella's avatar
Mael Kervella committed
83
    def get_interface_extension(self, obj):
84
        return obj.domain.extension.name
Dalahro's avatar
Dalahro committed
85

Gabriel Detraz's avatar
Gabriel Detraz committed
86 87 88
    def get_macaddress(self, obj):
        return str(obj.mac_address)

Gabriel Detraz's avatar
Gabriel Detraz committed
89

Gabriel Detraz's avatar
Gabriel Detraz committed
90
class FullInterfaceSerializer(serializers.ModelSerializer):
91
    """Serialisation complete d'une interface avec les ipv6 en plus"""
Gabriel Detraz's avatar
Gabriel Detraz committed
92
    ipv4 = IpListSerializer(read_only=True)
93
    ipv6 = Ipv6ListSerializer(read_only=True, many=True)
Gabriel Detraz's avatar
Gabriel Detraz committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    mac_address = serializers.SerializerMethodField('get_macaddress')
    domain = serializers.SerializerMethodField('get_dns')
    extension = serializers.SerializerMethodField('get_interface_extension')

    class Meta:
        model = Interface
        fields = ('ipv4', 'ipv6', 'mac_address', 'domain', 'extension')

    def get_dns(self, obj):
        return obj.domain.name

    def get_interface_extension(self, obj):
        return obj.domain.extension.name

    def get_macaddress(self, obj):
        return str(obj.mac_address)

Gabriel Detraz's avatar
Gabriel Detraz committed
111

Dalahro's avatar
Dalahro committed
112
class ExtensionNameField(serializers.RelatedField):
Gabriel Detraz's avatar
Gabriel Detraz committed
113
    """Evaluation str d'un objet extension (.example.org)"""
114
    def to_representation(self, value):
Mael Kervella's avatar
Mael Kervella committed
115
        return value.name
Dalahro's avatar
Dalahro committed
116

Gabriel Detraz's avatar
Gabriel Detraz committed
117

root's avatar
root committed
118
class TypeSerializer(serializers.ModelSerializer):
Gabriel Detraz's avatar
Gabriel Detraz committed
119 120 121
    """Serialisation d'un iptype : extension et la liste des
    ouvertures de port son evalués en get_... etant des
    foreign_key ou des relations manytomany"""
root's avatar
root committed
122
    extension = ExtensionNameField(read_only=True)
Gabriel Detraz's avatar
Gabriel Detraz committed
123 124 125 126 127 128 129 130
    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')
131

root's avatar
root committed
132 133
    class Meta:
        model = IpType
Gabriel Detraz's avatar
Gabriel Detraz committed
134
        fields = ('type', 'extension', 'domaine_ip_start', 'domaine_ip_stop',
Gabriel Detraz's avatar
Gabriel Detraz committed
135
                  'prefix_v6',
Gabriel Detraz's avatar
Gabriel Detraz committed
136 137
                  'ouverture_ports_tcp_in', 'ouverture_ports_tcp_out',
                  'ouverture_ports_udp_in', 'ouverture_ports_udp_out',)
138 139

    def get_port_policy(self, obj, protocole, io):
140
        if obj.ouverture_ports is None:
141
            return []
Gabriel Detraz's avatar
Gabriel Detraz committed
142 143 144 145 146 147
        return map(
            str,
            obj.ouverture_ports.ouvertureport_set.filter(
                protocole=protocole
            ).filter(io=io)
        )
148 149

    def get_port_policy_input_tcp(self, obj):
Gabriel Detraz's avatar
Gabriel Detraz committed
150
        """Renvoie la liste des ports ouverts en entrée tcp"""
151 152 153
        return self.get_port_policy(obj, OuverturePort.TCP, OuverturePort.IN)

    def get_port_policy_output_tcp(self, obj):
Gabriel Detraz's avatar
Gabriel Detraz committed
154
        """Renvoie la liste des ports ouverts en sortie tcp"""
155 156 157
        return self.get_port_policy(obj, OuverturePort.TCP, OuverturePort.OUT)

    def get_port_policy_input_udp(self, obj):
Gabriel Detraz's avatar
Gabriel Detraz committed
158
        """Renvoie la liste des ports ouverts en entrée udp"""
159 160 161
        return self.get_port_policy(obj, OuverturePort.UDP, OuverturePort.IN)

    def get_port_policy_output_udp(self, obj):
Gabriel Detraz's avatar
Gabriel Detraz committed
162
        """Renvoie la liste des ports ouverts en sortie udp"""
163
        return self.get_port_policy(obj, OuverturePort.UDP, OuverturePort.OUT)
root's avatar
root committed
164

Gabriel Detraz's avatar
Gabriel Detraz committed
165

166
class ExtensionSerializer(serializers.ModelSerializer):
Gabriel Detraz's avatar
Gabriel Detraz committed
167 168
    """Serialisation d'une extension : origin_ip et la zone sont
    des foreign_key donc evalués en get_..."""
169
    origin = serializers.SerializerMethodField('get_origin_ip')
170
    zone_entry = serializers.SerializerMethodField('get_zone_name')
171
    soa = serializers.SerializerMethodField('get_soa_data')
172 173 174

    class Meta:
        model = Extension
175
        fields = ('name', 'origin', 'origin_v6', 'zone_entry', 'soa')
176 177 178 179

    def get_origin_ip(self, obj):
        return obj.origin.ipv4

180 181 182
    def get_zone_name(self, obj):
        return str(obj.dns_entry)

183 184 185
    def get_soa_data(self, obj):
        return { 'mail': obj.soa.dns_soa_mail, 'param': obj.soa.dns_soa_param }

Gabriel Detraz's avatar
Gabriel Detraz committed
186

187
class MxSerializer(serializers.ModelSerializer):
Gabriel Detraz's avatar
Gabriel Detraz committed
188 189
    """Serialisation d'un MX, evaluation du nom, de la zone
    et du serveur cible, etant des foreign_key"""
190
    name = serializers.SerializerMethodField('get_entry_name')
191
    zone = serializers.SerializerMethodField('get_zone_name')
192
    mx_entry = serializers.SerializerMethodField('get_mx_name')
Dalahro's avatar
Dalahro committed
193

Dalahro's avatar
Dalahro committed
194
    class Meta:
195
        model = Mx
196
        fields = ('zone', 'priority', 'name', 'mx_entry')
197

198
    def get_entry_name(self, obj):
Mael Kervella's avatar
Mael Kervella committed
199
        return str(obj.name)
200 201 202 203

    def get_zone_name(self, obj):
        return obj.zone.name

204 205 206
    def get_mx_name(self, obj):
        return str(obj.dns_entry)

Gabriel Detraz's avatar
Gabriel Detraz committed
207

Gabriel Detraz's avatar
Gabriel Detraz committed
208
class TxtSerializer(serializers.ModelSerializer):
Gabriel Detraz's avatar
Gabriel Detraz committed
209 210
    """Serialisation d'un txt : zone cible et l'entrée txt
    sont evaluées à part"""
Gabriel Detraz's avatar
Gabriel Detraz committed
211
    zone = serializers.SerializerMethodField('get_zone_name')
Gabriel Detraz's avatar
Gabriel Detraz committed
212
    txt_entry = serializers.SerializerMethodField('get_txt_name')
Gabriel Detraz's avatar
Gabriel Detraz committed
213 214

    class Meta:
Gabriel Detraz's avatar
Gabriel Detraz committed
215 216
        model = Txt
        fields = ('zone', 'txt_entry', 'field1', 'field2')
Gabriel Detraz's avatar
Gabriel Detraz committed
217 218 219 220

    def get_zone_name(self, obj):
        return str(obj.zone.name)

Gabriel Detraz's avatar
Gabriel Detraz committed
221
    def get_txt_name(self, obj):
Gabriel Detraz's avatar
Gabriel Detraz committed
222
        return str(obj.dns_entry)
Gabriel Detraz's avatar
Fix #52  
Gabriel Detraz committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248


class SrvSerializer(serializers.ModelSerializer):
    """Serialisation d'un srv : zone cible et l'entrée txt"""
    extension = serializers.SerializerMethodField('get_extension_name')
    srv_entry = serializers.SerializerMethodField('get_srv_name')

    class Meta:
        model = Srv
        fields = (
            'service',
            'protocole',
            'extension',
            'ttl',
            'priority',
            'weight',
            'port',
            'target',
            'srv_entry'
        )

    def get_extension_name(self, obj):
        return str(obj.extension.name)

    def get_srv_name(self, obj):
        return str(obj.dns_entry)
Gabriel Detraz's avatar
Gabriel Detraz committed
249

Gabriel Detraz's avatar
Gabriel Detraz committed
250

251
class NsSerializer(serializers.ModelSerializer):
Gabriel Detraz's avatar
Gabriel Detraz committed
252 253
    """Serialisation d'un NS : la zone, l'entrée ns complète et le serveur
    ns sont évalués à part"""
254
    zone = serializers.SerializerMethodField('get_zone_name')
Mael Kervella's avatar
Mael Kervella committed
255
    ns = serializers.SerializerMethodField('get_domain_name')
Gabriel Detraz's avatar
Gabriel Detraz committed
256
    ns_entry = serializers.SerializerMethodField('get_text_name')
Dalahro's avatar
Dalahro committed
257

258
    class Meta:
259
        model = Ns
260
        fields = ('zone', 'ns', 'ns_entry')
261 262 263 264

    def get_zone_name(self, obj):
        return obj.zone.name

Mael Kervella's avatar
Mael Kervella committed
265 266
    def get_domain_name(self, obj):
        return str(obj.ns)
267

268 269 270
    def get_text_name(self, obj):
        return str(obj.dns_entry)

Gabriel Detraz's avatar
Gabriel Detraz committed
271

chirac's avatar
chirac committed
272
class DomainSerializer(serializers.ModelSerializer):
Gabriel Detraz's avatar
Gabriel Detraz committed
273 274
    """Serialisation d'un domain, extension, cname sont des foreign_key,
    et l'entrée complète, sont évalués à part"""
275
    extension = serializers.SerializerMethodField('get_zone_name')
276
    cname = serializers.SerializerMethodField('get_alias_name')
Gabriel Detraz's avatar
Gabriel Detraz committed
277
    cname_entry = serializers.SerializerMethodField('get_cname_name')
Dalahro's avatar
Dalahro committed
278

279
    class Meta:
chirac's avatar
chirac committed
280
        model = Domain
281
        fields = ('name', 'extension', 'cname', 'cname_entry')
282 283

    def get_zone_name(self, obj):
chirac's avatar
chirac committed
284
        return obj.extension.name
285

286
    def get_alias_name(self, obj):
Mael Kervella's avatar
Mael Kervella committed
287
        return str(obj.cname)
chirac's avatar
chirac committed
288

289 290 291
    def get_cname_name(self, obj):
        return str(obj.dns_entry)

Gabriel Detraz's avatar
Gabriel Detraz committed
292

293
class ServiceServersSerializer(serializers.ModelSerializer):
Gabriel Detraz's avatar
Gabriel Detraz committed
294
    """Evaluation d'un Service, et serialisation"""
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
    server = serializers.SerializerMethodField('get_server_name')
    service = serializers.SerializerMethodField('get_service_name')
    need_regen = serializers.SerializerMethodField('get_regen_status')

    class Meta:
        model = Service_link
        fields = ('server', 'service', 'need_regen')

    def get_server_name(self, obj):
        return str(obj.server.domain.name)

    def get_service_name(self, obj):
        return str(obj.service)

    def get_regen_status(self, obj):
        return obj.need_regen()
311

Gabriel Detraz's avatar
Gabriel Detraz committed
312

313
class OuverturePortsSerializer(serializers.Serializer):
Gabriel Detraz's avatar
Gabriel Detraz committed
314
    """Serialisation de l'ouverture des ports"""
315 316 317 318 319
    ipv4 = serializers.SerializerMethodField()
    ipv6 = serializers.SerializerMethodField()

    def get_ipv4():
        return {i.ipv4.ipv4:
Gabriel Detraz's avatar
Gabriel Detraz committed
320 321 322 323 324 325 326
            {
                "tcp_in":[j.tcp_ports_in() for j in i.port_lists.all()],
                "tcp_out":[j.tcp_ports_out()for j in i.port_lists.all()],
                "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()],
            }
                for i in Interface.objects.all() if i.ipv4
327
        }
Gabriel Detraz's avatar
Gabriel Detraz committed
328

329 330
    def get_ipv6():
        return {i.ipv6:
Gabriel Detraz's avatar
Gabriel Detraz committed
331 332 333 334 335 336 337
            {
                "tcp_in":[j.tcp_ports_in() for j in i.port_lists.all()],
                "tcp_out":[j.tcp_ports_out()for j in i.port_lists.all()],
                "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()],
            }
                for i in Interface.objects.all() if i.ipv6
338
        }