Skip to content

Commit

Permalink
Merge pull request #71 from rundeck-plugins/issue/70
Browse files Browse the repository at this point in the history
FIX: added new functions to remove or replace the simple quotes
  • Loading branch information
ltamaster authored Jan 6, 2021
2 parents fd5ce4a + 48f72f4 commit b9b344e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
43 changes: 37 additions & 6 deletions contents/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,42 @@ def get_file(destination):

return filename

def removeSimpleQuotes(command):
result = cleanSimpleQuoteCommand(command)

def replace_single_quotes_format(command):
command = command + " "
command = re.sub(r'\s(\')',' \"', command)
command = re.sub(r'(\')\s','\" ', command)
command = re.sub(r'\'("\'")\'', "\'", command)
return result

return command
def isAPathThatRequiresDoubleQuotes(candidate):
#first check that this is not multiple paths, e.g. 'C:\windows C:\tmp...'
regexpMultipleAbsolutePath = re.compile('\'[a-zA-Z]:\\\\.*\s[a-zA-Z]:\\\\.*') #at least two absolute paths
if regexpMultipleAbsolutePath.match(candidate): return False

#verify if this is a path with no options after, windows style. e.g. 'C:\Windows /w...'
regexpPathAndOption = re.compile('\'[a-zA-Z]:\\\\.*\s/.+')
if regexpPathAndOption.match(candidate): return False

#verify if this is a path with no options after, unix style. e.g. 'C:\Windows -v'
regexpPathAndOptionUnix = re.compile('\'[a-zA-Z]:\\\\.*\s-.+')
if regexpPathAndOptionUnix.match(candidate): return False

#finally, check if this is a single path, with single quotes, and requires to be put between double quotes.e.g. 'C:\Program Files'
regexPathRequireQuotes = re.compile('\'[a-zA-Z]:\\\\.*\s')
if regexPathRequireQuotes.match(candidate):
return True
else:
return False

def cleanSimpleQuoteCommand(command):
result = re.sub(r'(\'.+?\')\s', conditionalReplace, ' '+command+' ' )
return result

def conditionalReplace( aMatch ) :
result = ''
capturedGroup = aMatch.group(1)
capturedGroup = capturedGroup.strip()

result = capturedGroup[1:(len(capturedGroup)-1)]
if isAPathThatRequiresDoubleQuotes(capturedGroup):
result = '"' + result + '"'

return result+' '
14 changes: 12 additions & 2 deletions contents/winrm-exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def filter(self, record):
operationtimeout = None
forcefail = False
exitBehaviour = "console"
cleanescapingflg = False

if "RD_CONFIG_AUTHTYPE" in os.environ:
authentication = os.getenv("RD_CONFIG_AUTHTYPE")
Expand Down Expand Up @@ -158,10 +159,18 @@ def filter(self, record):
if "RD_CONFIG_OPERATIONTIMEOUT" in os.environ:
operationtimeout = os.getenv("RD_CONFIG_OPERATIONTIMEOUT")

if "RD_CONFIG_CLEANESCAPING" in os.environ:
if os.getenv("RD_CONFIG_CLEANESCAPING") == "true":
cleanescapingflg = True
else:
cleanescapingflg = False

exec_command = os.getenv("RD_EXEC_COMMAND")
log.debug("Command will be executed: " + exec_command)

if "cmd" in shell:
exec_command = common.replace_single_quotes_format(exec_command)
if cleanescapingflg:
exec_command = common.removeSimpleQuotes(exec_command)
log.debug("Command escaped will be executed: " + exec_command)

endpoint=transport+'://'+args.hostname+':'+port

Expand Down Expand Up @@ -214,6 +223,7 @@ def filter(self, record):
log.debug("readtimeout:" + str(readtimeout))
log.debug("operationtimeout:" + str(operationtimeout))
log.debug("exit Behaviour:" + exitBehaviour)
log.debug("cleanescapingflg: " + str(cleanescapingflg))
log.debug("------------------------------------------")

if not URLLIB_INSTALLED:
Expand Down
9 changes: 9 additions & 0 deletions plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ providers:
required: false
renderingOptions:
groupName: Kerberos
- name: cleanescaping
title: Clean Escaping
description: "Cleans unnecessarily Escaped characters on commands"
type: Boolean
default: "false"
required: false
renderingOptions:
groupName: Misc
instance-scope-node-attribute: "clean-escaping"
- name: WinRMcpPython
title: WinRM Python File Copier
description: Copying files to remote Windows computer
Expand Down

0 comments on commit b9b344e

Please sign in to comment.