Bug 1049608

Summary: Script server execution arguments do not interpret quoted arguments including spaces as a single parameter
Product: [Other] RHQ Project Reporter: Lukas Krejci <lkrejci>
Component: PluginsAssignee: Lukas Krejci <lkrejci>
Status: CLOSED CURRENTRELEASE QA Contact: Mike Foley <mfoley>
Severity: high Docs Contact:
Priority: unspecified    
Version: 4.10CC: hrupp, jkremser, lkrejci, loleary, mshirley
Target Milestone: ---   
Target Release: RHQ 4.10   
Hardware: All   
OS: All   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: 1020066 Environment:
Last Closed: 2014-04-23 12:31:19 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On:    
Bug Blocks: 1020066    

Description Lukas Krejci 2014-01-07 20:40:11 UTC
+++ This bug was initially created as a clone of Bug #1020066 +++

Description of problem:
Executing a script server resource with an argument string including a quoted argument that contains spaces results in the quoted argument being broken out into multiple arguments.

For example, "a b" is treated as two arguments.  '"a' is the first argument, and 'b"' is the second.

Version-Release number of selected component (if applicable):
JBoss ON 3.1.2

How reproducible:
very

Steps to Reproduce:
1. Import script server resource (script example below)
2. Perform "Execute" operation on script server resource
3. Input arguments "a b"
4. Verify output

testScript.sh contents:
#!/bin/sh

echo $# number of arguments > output.txt
echo $* >> output.txt
echo $@ >> output.txt


Actual results:
Script reports that 2 arguments have been passed into it.

Expected results:
Script should report that 1 argument was passed in.

Additional info:

--- Additional comment from Larry O'Leary on 2013-10-22 18:06:45 EDT ---

This issue is a direct result of how the operation's arguments property value is passed along to executeExecutable and then split into multiple arguments using the space:

    https://git.fedorahosted.org/cgit/rhq/rhq.git/tree/modules/plugins/script/src/main/java/org/rhq/plugins/script/ScriptServerComponent.java#n405

The end result is that:
    Arg1 "Arg 2" Arg3

Becomes:
    Arg1
    "Arg
    2"
    Arg3

And:
    Arg1 'Arg 2' Arg3

Becomes:
    Arg1
    'Arg
    2'
    Arg3

And:
    Arg1 Arg\ 2 Arg3

Becomes:
    Arg1
    Arg\
    2
    Arg3


To fix this, a more suitable regular expression and matcher should be applied to handle quoted (double or single) parameters and/or escapes. Possibly a simple command-line parser?

The most basic form is to support:

Arg1 "Arg 2" Arg3
Arg1 'Arg 2' Arg3

However, from a command-line perspective, the quote may need to be escaped:

Arg1 'Arg\'s 2 Value' Arg3

Additionally, the following format seems to be common within the Linux realm:

Arg1 Arg\ 2 Arg3

Comment 1 Lukas Krejci 2014-01-07 20:45:05 UTC
commit 545009710de643a8e21bfad57f4d09915009017a
Author: Lukas Krejci <lkrejci>
Date:   Tue Jan 7 21:43:42 2014 +0100

    [BZ 1049608] Support quotes and escapes in script args in script plugin

Comment 2 Lukas Krejci 2014-01-24 23:11:28 UTC
commit d8e1e71efc8eb3ffcf2729fc4518b37e5cd304e5
Author: Lukas Krejci <lkrejci>
Date:   Sat Jan 25 00:08:01 2014 +0100

    [BZ 1049608] - Changes to arg parsing in script server plugin
    
    * we by default keep the broken old behavior (blind split by space)
    * a boolean plugin prop to switch quoting and escapes on/off
    * configurable escape char set to \ for all platforms
    * escaping (when switched on) is POSIX-like:
      1) In unquoted text: the escape character preserves the value of any
         following character
      2) In double-quoted text: the escape character escapes " and itself
         otherwise is both the escape character and the following character
         are left intact
      3) In single-quoted text: no escaping can occur

Comment 3 Jirka Kremser 2014-01-31 11:05:25 UTC
commit in the release branch: 8058e6ba2

Comment 4 Heiko W. Rupp 2014-04-23 12:31:19 UTC
Bulk closing of 4.10 issues.

If an issue is not solved for you, please open a new BZ (or clone the existing one) with a version designator of 4.10.