190130
From 854defb4ff5e0d53f51545d20796aff662f9850f Mon Sep 17 00:00:00 2001
190130
From: Saravanakumar Arumugam <sarumuga@redhat.com>
190130
Date: Thu, 9 Jul 2015 15:56:28 +0530
190130
Subject: [PATCH 411/449] tools/glusterfind : validate session name
190130
190130
Validate a session name(during create) for the following:
190130
1. minimum 2 character length.
190130
2. Maximum 256 characters.
190130
3. No special characters apart from underscore, hyphen allowed.
190130
190130
Also, validate volume(expect, while using glusterfind list).
190130
190130
>Change-Id: I1b1e64e218f93d0a531d3cf69fc2ce7e2ed11d01
190130
>BUG: 1241494
190130
>Signed-off-by: Saravanakumar Arumugam <sarumuga@redhat.com>
190130
>Signed-off-by: Shwetha K Acharya <sacharya@redhat.com>
190130
190130
backport of https://review.gluster.org/#/c/glusterfs/+/11602/
190130
190130
BUG: 1234220
190130
Change-Id: I1b1e64e218f93d0a531d3cf69fc2ce7e2ed11d01
190130
Signed-off-by: Shwetha K Acharya <sacharya@redhat.com>
190130
Reviewed-on: https://code.engineering.redhat.com/gerrit/202469
190130
Tested-by: RHGS Build Bot <nigelb@redhat.com>
190130
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
190130
---
190130
 tools/glusterfind/src/main.py | 50 ++++++++++++++++++++++++++++++++++++-------
190130
 1 file changed, 42 insertions(+), 8 deletions(-)
190130
190130
diff --git a/tools/glusterfind/src/main.py b/tools/glusterfind/src/main.py
190130
index 5ca1fec..4b5466d 100644
190130
--- a/tools/glusterfind/src/main.py
190130
+++ b/tools/glusterfind/src/main.py
190130
@@ -23,6 +23,7 @@ import tempfile
190130
 import signal
190130
 from datetime import datetime
190130
 import codecs
190130
+import re
190130
 
190130
 from utils import execute, is_host_local, mkdirp, fail
190130
 from utils import setup_logger, human_time, handle_rm_error
190130
@@ -520,11 +521,8 @@ def write_output(outfile, outfilemerger, field_separator):
190130
                 else:
190130
                     gfind_write(f, row[0], field_separator, p_rep)
190130
 
190130
-def mode_create(session_dir, args):
190130
-    logger.debug("Init is called - Session: %s, Volume: %s"
190130
-                 % (args.session, args.volume))
190130
-
190130
-    cmd = ["gluster", 'volume', 'info', args.volume, "--xml"]
190130
+def validate_volume(volume):
190130
+    cmd = ["gluster", 'volume', 'info', volume, "--xml"]
190130
     _, data, _ = execute(cmd,
190130
                          exit_msg="Failed to Run Gluster Volume Info",
190130
                          logger=logger)
190130
@@ -532,11 +530,42 @@ def mode_create(session_dir, args):
190130
         tree = etree.fromstring(data)
190130
         statusStr = tree.find('volInfo/volumes/volume/statusStr').text
190130
     except (ParseError, AttributeError) as e:
190130
-        fail("Invalid Volume: %s" % e, logger=logger)
190130
-
190130
+        fail("Invalid Volume: Check the Volume name! %s" % e)
190130
     if statusStr != "Started":
190130
-        fail("Volume %s is not online" % args.volume, logger=logger)
190130
+        fail("Volume %s is not online" % volume)
190130
+
190130
+# The rules for a valid session name.
190130
+SESSION_NAME_RULES = {
190130
+    'min_length': 2,
190130
+    'max_length': 256,  # same as maximum volume length
190130
+    # Specifies all alphanumeric characters, underscore, hyphen.
190130
+    'valid_chars': r'0-9a-zA-Z_-',
190130
+}
190130
+
190130
+
190130
+# checks valid session name, fail otherwise
190130
+def validate_session_name(session):
190130
+    # Check for minimum length
190130
+    if len(session) < SESSION_NAME_RULES['min_length']:
190130
+        fail('session_name must be at least ' +
190130
+                 str(SESSION_NAME_RULES['min_length']) + ' characters long.')
190130
+    # Check for maximum length
190130
+    if len(session) > SESSION_NAME_RULES['max_length']:
190130
+        fail('session_name must not exceed ' +
190130
+                 str(SESSION_NAME_RULES['max_length']) + ' characters length.')
190130
+
190130
+    # Matches strings composed entirely of characters specified within
190130
+    if not re.match(r'^[' + SESSION_NAME_RULES['valid_chars'] +
190130
+                        ']+$', session):
190130
+        fail('Session name can only contain these characters: ' +
190130
+                         SESSION_NAME_RULES['valid_chars'])
190130
+
190130
+
190130
+def mode_create(session_dir, args):
190130
+    validate_session_name(args.session)
190130
 
190130
+    logger.debug("Init is called - Session: %s, Volume: %s"
190130
+                 % (args.session, args.volume))
190130
     mkdirp(session_dir, exit_on_err=True, logger=logger)
190130
     mkdirp(os.path.join(session_dir, args.volume), exit_on_err=True,
190130
            logger=logger)
190130
@@ -850,6 +879,11 @@ def main():
190130
                 args.mode not in ["create", "list", "query"]:
190130
             fail("Invalid session %s" % args.session)
190130
 
190130
+        # volume involved, validate the volume first
190130
+        if args.mode not in ["list"]:
190130
+            validate_volume(args.volume)
190130
+
190130
+
190130
         # "default" is a system defined session name
190130
         if args.mode in ["create", "post", "pre", "delete"] and \
190130
                 args.session == "default":
190130
-- 
190130
1.8.3.1
190130