summaryrefslogtreecommitdiff
path: root/designate/objects/rrdata_txt.py
blob: 654094ef0eb4950c3f2daf7cfad315a7666dac1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Copyright (c) 2014 Rackspace Hosting
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
from designate.objects import base
from designate.objects import fields
from designate.objects.record import Record
from designate.objects.record import RecordList


@base.DesignateRegistry.register
class TXT(Record):
    """
    TXT Resource Record Type
    Defined in: RFC1035
    """
    fields = {
        'txt_data': fields.TxtField()
    }

    def _to_string(self):
        return self.txt_data

    @staticmethod
    def _is_wrapped_in_double_quotes(value):
        return value.startswith('"') and value.endswith('"')

    @staticmethod
    def _is_missing_double_quote(value):
        return ((value.startswith('"') and not value.endswith('"')) or
                (not value.startswith('"') and value.endswith('"')))

    def _validate_record_single_string(self, value):
        if len(value) > 255:
            raise ValueError(
                'Any TXT record string exceeding 255 characters has to be '
                'split.'
            )

        if self._is_missing_double_quote(value):
            raise ValueError(
                'TXT record is missing a double quote either at beginning '
                'or at end.'
            )

        if not self._is_wrapped_in_double_quotes(value):
            # value with spaces should be quoted as per RFC1035 5.1
            for element in value:
                if element.isspace():
                    raise ValueError(
                        'Empty spaces are not allowed in TXT record, '
                        'unless wrapped in double quotes.'
                    )
        else:
            # quotes within value should be escaped with backslash
            strip_value = value.strip('"')
            for index, char in enumerate(strip_value):
                if char == '"':
                    if strip_value[index - 1] != "\\":
                        raise ValueError(
                            'Quotation marks should be escaped with backslash.'
                        )

    def _from_string(self, value):
        if len(value) > 255:
            # expecting record containing multiple strings as
            # per rfc7208 3.3 and rfc1035 3.3.14
            stripped_value = value.strip('"')
            if (not self._is_wrapped_in_double_quotes(value) and
                    '" "' not in stripped_value):
                raise ValueError(
                    'TXT record strings over 255 characters have to be split '
                    'into multiple strings wrapped in double quotes.'
                )

            record_strings = stripped_value.split('" "')
            for record_string in record_strings:
                # add back the delimiting quotes after
                # strip and split for each string
                record_string = '"{}"'.format(record_string)
                # further validate each string individually
                self._validate_record_single_string(value=record_string)
        else:
            # validate single TXT record string
            self._validate_record_single_string(value=value)

        self.txt_data = value

    # The record type is defined in the RFC. This will be used when the record
    # is sent by mini-dns.
    RECORD_TYPE = 16


@base.DesignateRegistry.register
class TXTList(RecordList):

    LIST_ITEM_TYPE = TXT
    fields = {
        'objects': fields.ListOfObjectsField('TXT'),
    }