Browse code

Adding validation for hostname/domainname in the installer

* Empty hostname or domainname are not allowed
* Hostname or domainname should not start or end with '-'
* Hostname should start with alpha char
* Hostname should be less than 64 chars

Change-Id: I91defb26e0ed8e1abea46f46195117a86e320c81
Reviewed-on: http://photon-jenkins.eng.vmware.com/803
Reviewed-by: Xiaolin Li <xiaolinl@vmware.com>
Tested-by: jenkins-photon <wangnan2015@hotmail.com>
(cherry picked from commit 4c700ec828564db714d7ec1a8f1c128e69aa1a12)
Reviewed-on: http://photon-jenkins.eng.vmware.com/900
Reviewed-by: suezzelur <anishs@vmware.com>
Tested-by: suezzelur <anishs@vmware.com>

mbassiouny authored on 2016/05/04 08:59:11
Showing 1 changed files
... ...
@@ -87,10 +87,22 @@ class IsoInstaller(object):
87 87
         raise Exception("Can not mount the cd")
88 88
 
89 89
     def validate_hostname(self, hostname):
90
-        error_msg = "It should start with alpha char and ends with alpha-numeric char"
90
+        error_empty = "Empty hostname or domain is not allowed"
91
+        error_dash = "Hostname or domain should not start or end with '-'"
92
+        error_hostname = "Hostname should start with alpha char and <= 64 chars"
93
+
91 94
         if (hostname == None or len(hostname) == 0):
92
-            return False, error_msg
93
-        return (ord(hostname[0]) in self.alpha_chars) and (hostname[-1] not in ['.', '-']), error_msg
95
+            return False, error_empty
96
+
97
+        fields = hostname.split('.')
98
+        for field in fields:
99
+            if len(field) == 0:
100
+                return False, error_empty
101
+            if field[0] == '-' or field[-1] == '-':
102
+                return False, error_dash
103
+
104
+        machinename = fields[0]
105
+        return (len(machinename) <= 64) and (ord(machinename[0]) in self.alpha_chars), error_hostname
94 106
 
95 107
     def validate_ostree_url_input(self, ostree_repo_url):
96 108
         if not ostree_repo_url: