diff options
Diffstat (limited to 'designate/objects/rrdata_txt.py')
-rw-r--r-- | designate/objects/rrdata_txt.py | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/designate/objects/rrdata_txt.py b/designate/objects/rrdata_txt.py index 44e13fff..9fcec720 100644 --- a/designate/objects/rrdata_txt.py +++ b/designate/objects/rrdata_txt.py @@ -26,14 +26,23 @@ class TXT(Record): Defined in: RFC1035 """ fields = { - 'txt_data': fields.TxtField(maxLength=255) + 'txt_data': fields.TxtField() } def _to_string(self): return self.txt_data - def _from_string(self, value): - if (not value.startswith('"') and not value.endswith('"')): + @staticmethod + def _is_wrapped_in_double_quotes(value): + return value.startswith('"') and value.endswith('"') + + def _validate_record_single_string(self, value): + if len(value) > 255: + err = ("Any TXT record string exceeding " + "255 characters has to be split.") + raise InvalidObject(err) + + 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(): @@ -50,6 +59,29 @@ class TXT(Record): "backslash.") raise InvalidObject(err) + 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): + err = ("TXT record strings over 255 characters " + "have to be split into multiple strings " + "wrapped in double quotes.") + raise InvalidObject(err) + + 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 |