Skip to content

Commit ae52691

Browse files
(MODULES-10788) - fix for password prompt when creating mysql_login_path resource (#1334)
* (MODULES-10788) - fix for password prompt when creating mysql_login_path resource
1 parent 5c2572f commit ae52691

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

lib/puppet/provider/mysql_login_path/mysql_login_path.rb

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
require 'puppet/util/execution'
77
require 'puppet/util/suidmanager'
88
require 'open3'
9+
require 'pty'
10+
require 'expect'
11+
require 'fileutils'
12+
require 'English'
913

1014
# Implementation for the mysql_login_path type using the Resource API.
1115
class Puppet::Provider::MysqlLoginPath::MysqlLoginPath < Puppet::ResourceApi::SimpleProvider
@@ -17,6 +21,7 @@ def get_homedir(_context, uid)
1721
def mysql_config_editor_set_cmd(context, uid, password = nil, *args)
1822
args.unshift('/usr/bin/mysql_config_editor')
1923
homedir = get_homedir(context, uid)
24+
login_file_path = "#{homedir}/.mylogin.cnf"
2025

2126
if args.is_a?(Array)
2227
command = args.flatten.map(&:to_s)
@@ -25,28 +30,27 @@ def mysql_config_editor_set_cmd(context, uid, password = nil, *args)
2530
command_str = command
2631
end
2732

28-
Puppet::Util::SUIDManager.asuser(uid) do
29-
@exit_status = Open3.popen3({ 'HOME' => homedir }, command_str) do |stdin, stdout, stderr, wait_thr|
33+
begin
34+
Puppet::Util::SUIDManager.asuser(uid) do
35+
FileUtils.touch login_file_path
36+
FileUtils.chmod 0o600, login_file_path
37+
end
38+
39+
PTY.spawn({ 'HOME' => homedir }, command_str) do |input, output, _pid|
3040
if password
31-
stdin.puts(password + "\r\n")
32-
stdin.close
41+
input.expect(%r{Enter password:})
42+
output.puts password
3343
end
34-
@captured_stdout = stdout.read
35-
@captured_stderr = stderr.read
36-
wait_thr.value
3744
end
38-
end
39-
40-
if @exit_status.success? == false
45+
rescue => e
4146
raise Puppet::ExecutionFailure, _(
4247
"Execution of '%{str}' returned %{exit_status}: %{output}",
4348
) % {
4449
str: command_str,
45-
exit_status: @exit_status,
46-
output: @captured_stderr.strip,
50+
exit_status: $CHILD_STATUS.exitstatus,
51+
output: e.message,
4752
}
4853
end
49-
@captured_stdout
5054
end
5155

5256
def mysql_config_editor_cmd(context, uid, *args)

spec/unit/puppet/provider/mysql_login_path/mysql_login_path_spec.rb

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,17 @@
2626
Puppet::Util::Execution.stubs(:execute).with(['/usr/bin/my_print_defaults', '-s', 'local_socket'], failonfail: true, uid: 'root', custom_environment: { 'HOME' => '/root' })
2727
.returns("--user=root\n--password=more_secure\n--host=localhost\n--socket=/var/run/mysql.sock")
2828

29-
wait_thr_value.stubs(:success?).returns(true)
30-
wait_thr.stubs(:value).returns(wait_thr_value)
31-
Open3.stubs(:popen3)
32-
.with({ 'HOME' => '/root' },
33-
'/usr/bin/mysql_config_editor set --skip-warn -G local_socket -h localhost -u root ' \
34-
'-S /var/run/mysql/mysql.sock -p')
35-
.returns(wait_thr_value)
29+
Puppet::Util::SUIDManager.stubs(:asuser).with('root').returns(`(exit 0)`)
30+
PTY.stubs(:spawn)
31+
.with({ 'HOME' => '/root' },
32+
'/usr/bin/mysql_config_editor set --skip-warn -G local_socket -h localhost -u root ' \
33+
'-S /var/run/mysql/mysql.sock -p')
34+
.returns(`(exit 0)`)
3635

37-
Open3.stubs(:popen3)
38-
.with({ 'HOME' => '/root' },
39-
'/usr/bin/mysql_config_editor set --skip-warn -G local_socket -h 127.0.0.1 -u root -P 3306 -p')
40-
.returns(wait_thr_value)
36+
PTY.stubs(:spawn)
37+
.with({ 'HOME' => '/root' },
38+
'/usr/bin/mysql_config_editor set --skip-warn -G local_socket -h 127.0.0.1 -u root -P 3306 -p')
39+
.returns(`(exit 0)`)
4140
end
4241

4342
describe '#get' do

0 commit comments

Comments
 (0)