Skip to content

Commit ee66fa0

Browse files
committed
Avoid duplicating PermitUserEnvironment
Do not add a new line if the option is already defined. This avoids adding dupulicated lines and also make sure that the existing explicit setting to disable PermitUserEnvironment is purged. Closes #1953
1 parent 0e53b92 commit ee66fa0

2 files changed

Lines changed: 2 additions & 6 deletions

File tree

lib/beaker/host/unix/exec.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,7 @@ def ssh_service_restart
301301
def ssh_permit_user_environment
302302
case self['platform']
303303
when /amazon|debian|ubuntu|archlinux|el-|centos|fedora|redhat|oracle|scientific|opensuse|sles|solaris/
304-
directory = tmpdir
305-
exec(Beaker::Command.new("echo 'PermitUserEnvironment yes' | cat - /etc/ssh/sshd_config > #{directory}/sshd_config.permit"))
306-
exec(Beaker::Command.new("mv #{directory}/sshd_config.permit /etc/ssh/sshd_config"))
304+
exec(Beaker::Command.new("sed -e 's/^PermitUserEnvironment .*/PermitUserEnvironment yes/' -e t -e '1s/^/PermitUserEnvironment yes\\n/' /etc/ssh/sshd_config > /etc/ssh/sshd_config"))
307305
exec(Beaker::Command.new("echo '' >/etc/environment")) if self['platform'].include?('ubuntu-')
308306
when /(free|open)bsd/
309307
exec(Beaker::Command.new("sudo perl -pi -e 's/^#?PermitUserEnvironment no/PermitUserEnvironment yes/' /etc/ssh/sshd_config"), { :pty => true })

spec/beaker/host/unix/exec_spec.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ def to_s
7979
describe '#ssh_permit_user_environment' do
8080
context 'When called without error' do
8181
let(:directory) { '/directory' }
82-
let(:ssh_command) { "echo 'PermitUserEnvironment yes' | cat - /etc/ssh/sshd_config > #{directory}/sshd_config.permit" }
83-
let(:ssh_move) { "mv #{directory}/sshd_config.permit /etc/ssh/sshd_config" }
82+
let(:ssh_command) { "sed -i -e 's/^PermitUserEnvironment .*/PermitUserEnvironment yes/' -e t -e '1s/^/PermitUserEnvironment yes\\n/' /etc/ssh/sshd_config" }
8483

8584
platforms = PlatformHelpers::REDHATPLATFORMS + PlatformHelpers::DEBIANPLATFORMS
8685

@@ -89,7 +88,6 @@ def to_s
8988
opts['platform'] = platform
9089
expect(instance).to receive(:exec).twice
9190
expect(instance).to receive(:tmpdir).and_return(directory)
92-
expect(Beaker::Command).to receive(:new).with(ssh_move)
9391
expect(Beaker::Command).to receive(:new).with(ssh_command)
9492
expect(instance).to receive(:ssh_service_restart)
9593
expect { instance.ssh_permit_user_environment }.not_to raise_error

0 commit comments

Comments
 (0)