Alex Lourie has uploaded a new change for review.

Change subject: packaging: setup: add validation for DC and SD names
......................................................................

packaging: setup: add validation for DC and SD names

Change-Id: I1e133b874a1c9ce6bafb2a9f2d18c0400de06e05
Bug-Url: https://bugzilla.redhat.com/1031993
Signed-off-by: Alex Lourie <[email protected]>
---
M src/plugins/ovirt-hosted-engine-setup/storage/storage.py
1 file changed, 80 insertions(+), 5 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-hosted-engine-setup 
refs/changes/75/21775/1

diff --git a/src/plugins/ovirt-hosted-engine-setup/storage/storage.py 
b/src/plugins/ovirt-hosted-engine-setup/storage/storage.py
index 8e4b4f3..84880ac 100644
--- a/src/plugins/ovirt-hosted-engine-setup/storage/storage.py
+++ b/src/plugins/ovirt-hosted-engine-setup/storage/storage.py
@@ -24,6 +24,7 @@
 
 import glob
 import os
+import re
 import uuid
 import gettext
 import stat
@@ -58,6 +59,14 @@
 
     DATA_DOMAIN = 1
     UMOUNT_TRIES = 10
+
+    _RE_NOT_ALPHANUMERIC = re.compile(r"[^-\w]")
+    _NOT_VALID_NAME_MSG = _(
+        'It can only consist of alphanumeric '
+        'characters (that is, letters, numbers, '
+        'and signs "-" and "_"). All other characters '
+        'are not valid in the name.'
+    )
 
     def __init__(self, context):
         super(Plugin, self).__init__(context=context)
@@ -309,6 +318,14 @@
                     'or specify another location'
                 )
             )
+
+    def _validName(self, name):
+        if (
+            name is None or
+            self._RE_NOT_ALPHANUMERIC.search(name)
+        ):
+            return False
+        return True
 
     def _validateDomain(self, connection, domain_type):
         path = tempfile.mkdtemp()
@@ -779,24 +796,59 @@
         ] == 'glusterfs':
             self.storageType = self.GLUSTERFS_DOMAIN
         self._getExistingDomain()
-        if self.environment[
+
+        interactive = self.environment[
             ohostedcons.StorageEnv.STORAGE_DOMAIN_NAME
-        ] is None:
+        ] is None
+        while not self._validName(
+            self.environment[
+                ohostedcons.StorageEnv.STORAGE_DOMAIN_NAME
+            ]
+        ):
             self.environment[
                 ohostedcons.StorageEnv.STORAGE_DOMAIN_NAME
             ] = self.dialog.queryString(
                 name='OVEHOSTED_STORAGE_DOMAIN_NAME',
                 note=_(
-                    'Please provide storage domain name [@DEFAULT@]: '
+                    'Please provide storage domain name. '
+                    '[@DEFAULT@]: '
                 ),
                 prompt=True,
                 caseSensitive=True,
                 default=ohostedcons.Defaults.DEFAULT_STORAGE_DOMAIN_NAME,
             )
+            if not self._validName(
+                self.environment[
+                    ohostedcons.StorageEnv.STORAGE_DOMAIN_NAME
+                ]
+            ):
+                if interactive:
+                    self.dialog.note(
+                        text=_(
+                            'Storage domain name cannot be empty. '
+                            '{notvalid}'
+                        ).format(
+                            notvalid=self._NOT_VALID_NAME_MSG
+                        )
+                    )
+                else:
+                    raise RuntimeError(
+                        _(
+                            'Storage domain name cannot be empty. '
+                            '{notvalid}'
+                        ).format(
+                            notvalid=self._NOT_VALID_NAME_MSG
+                        )
+                    )
 
-        if self.environment[
+        interactive = self.environment[
             ohostedcons.StorageEnv.STORAGE_DATACENTER_NAME
-        ] is None:
+        ] is None
+        while not self._validName(
+            self.environment[
+                ohostedcons.StorageEnv.STORAGE_DATACENTER_NAME
+            ]
+        ):
             self.environment[
                 ohostedcons.StorageEnv.STORAGE_DATACENTER_NAME
             ] = self.dialog.queryString(
@@ -806,6 +858,29 @@
                 caseSensitive=True,
                 default=ohostedcons.Defaults.DEFAULT_STORAGE_DATACENTER_NAME,
             )
+            if not self._validName(
+                self.environment[
+                    ohostedcons.StorageEnv.STORAGE_DATACENTER_NAME
+                ]
+            ):
+                if interactive:
+                    self.dialog.note(
+                        text=_(
+                            'Data center name cannot be empty. '
+                            '{notvalid}'
+                        ).format(
+                            notvalid=self._NOT_VALID_NAME_MSG
+                        )
+                    )
+                else:
+                    raise RuntimeError(
+                        _(
+                            'Data center name cannot be empty. '
+                            '{notvalid}'
+                        ).format(
+                            notvalid=self._NOT_VALID_NAME_MSG
+                        )
+                    )
 
     @plugin.event(
         stage=plugin.Stages.STAGE_MISC,


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1e133b874a1c9ce6bafb2a9f2d18c0400de06e05
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-hosted-engine-setup
Gerrit-Branch: master
Gerrit-Owner: Alex Lourie <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to