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