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