diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f7ce913..6025f3c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ appear at the top. ## [Unreleased][] + * [#425](https://github.com/capistrano/sshkit/pull/425): Command#group incorrectly escapes double quotes, resulting in a a syntax error when specifying the group execution using `as`. This issue manifested when user command quotes changed from double quotes to single quotes. This fix removes the double quote escaping - [@pblesi](https://github.com/pblesi). * Your contribution here! ## [1.16.0][] (2018-02-03) diff --git a/lib/sshkit/command.rb b/lib/sshkit/command.rb index edcb9852..03043639 100644 --- a/lib/sshkit/command.rb +++ b/lib/sshkit/command.rb @@ -182,7 +182,7 @@ def umask(&_block) def group(&_block) return yield unless options[:group] - "sg #{options[:group]} -c \\\"%s\\\"" % %Q{#{yield}} + %Q(sg #{options[:group]} -c "#{yield}") # We could also use the so-called heredoc format perhaps: #"newgrp #{options[:group]} < /dev/null; then echo \"You cannot switch to user 'root' using sudo, please check the sudoers file\" 1>&2; false; fi\n", + "Command: sudo -u root -- sh -c 'sg admin -c \"/usr/bin/env touch restart.txt\"'\n" + ], command_lines + end + def test_capture captured_command_result = nil Netssh.new(a_host) do |_host| diff --git a/test/unit/test_command.rb b/test/unit/test_command.rb index 6ced6f8b..d2c02e2d 100644 --- a/test/unit/test_command.rb +++ b/test/unit/test_command.rb @@ -102,12 +102,12 @@ def test_working_as_a_given_user def test_working_as_a_given_group c = Command.new(:whoami, group: :devvers) - assert_equal "sg devvers -c \\\"/usr/bin/env whoami\\\"", c.to_command + assert_equal 'sg devvers -c "/usr/bin/env whoami"', c.to_command end def test_working_as_a_given_user_and_group c = Command.new(:whoami, user: :anotheruser, group: :devvers) - assert_equal "sudo -u anotheruser -- sh -c 'sg devvers -c \\\"/usr/bin/env whoami\\\"'", c.to_command + assert_equal %Q(sudo -u anotheruser -- sh -c 'sg devvers -c "/usr/bin/env whoami"'), c.to_command end def test_umask