Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/sshkit/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]} <<EOC \\\"%s\\\" EOC" % %Q{#{yield}}
end
Expand Down
14 changes: 14 additions & 0 deletions test/functional/backends/test_netssh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ def test_simple_netssh
], command_lines
end

def test_group_netssh
Netssh.new(a_host) do
as user: :root, group: :admin do
execute :touch, 'restart.txt'
end
end.run

command_lines = @output.lines.select { |line| line.start_with?('Command:') }
assert_equal [
"Command: if ! sudo -u root whoami > /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|
Expand Down
4 changes: 2 additions & 2 deletions test/unit/test_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down