[node-patches] Change in ovirt-node[master]: Make FQDN validator abide by RFC1035

rbarry at redhat.com rbarry at redhat.com
Tue Jun 11 23:05:01 UTC 2013


Ryan Barry has uploaded a new change for review.

Change subject: Make FQDN validator abide by RFC1035
......................................................................

Make FQDN validator abide by RFC1035

Check for the length of each field as well as the entire
hostname to make sure it's not too long

Change-Id: I747a29c1a15b601bf1eea364b76e56818078e80b
Signed-off-by: Ryan Barry <rbarry at redhat.com>
---
M src/ovirt/node/valid.py
1 file changed, 40 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/83/15583/1

diff --git a/src/ovirt/node/valid.py b/src/ovirt/node/valid.py
index d9928b0..8418748 100644
--- a/src/ovirt/node/valid.py
+++ b/src/ovirt/node/valid.py
@@ -279,6 +279,46 @@
     pattern = ("^(([0-9]\.)?([a-z]|[a-z][a-z0-9\-]*[a-z0-9])\.)*" +
                "([a-z]|[a-z][a-z0-9\-]*[a-z0-9])$", re.I)
 
+    def validate(self, value):
+        FQDNLength()(value)
+        return super(FQDN, self).validate(value)
+
+
+class FQDNLength(Validator):
+    """Matches a FQDN and ensures that fields are 63 character or less
+    per level
+
+    >>> FQDNLength().validate(r'1234567890123456789012345678901234567890123456\
+78901234567890123.com')
+    True
+    >>> FQDNLength().validate('1234567890123456789012345678901234567890123456\
+789012345678901234.com')
+    False
+    """
+
+    description = "a field less than 255 characters"
+
+    def validate(self, value):
+        is_valid = True
+        fields = {k: value.split(".")[k] for k in range(len(value.split(".")))}
+        for k, v in fields.items():
+            #Each field must be less than 64 octets
+            valid = True if len(v) < 64 else False
+            if valid is False:
+                self.description = "less than 64 characters in the {index}" + \
+                                   "section".format(index=self.ordinal(k))
+                is_valid = False
+        #Can't be longer than 255 octets total
+        if len(value) > 255:
+            is_valid = False
+        return is_valid
+
+    def ordinal(self, n):
+        if 10 <= n % 100 < 20:
+            return str(n) + 'th'
+        else:
+            return str(n) + {1: 'st', 2: 'nd', 3: 'rd'}.get(n % 10, "th")
+
 
 class IPv4Address(Validator):
     """Matches IPv4 addresses


-- 
To view, visit http://gerrit.ovirt.org/15583
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I747a29c1a15b601bf1eea364b76e56818078e80b
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-node
Gerrit-Branch: master
Gerrit-Owner: Ryan Barry <rbarry at redhat.com>



More information about the node-patches mailing list