Skip to content

(MODULES-10788) - fix for password prompt when creating mysql_login_path resource #1334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 15, 2020
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
30 changes: 17 additions & 13 deletions lib/puppet/provider/mysql_login_path/mysql_login_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
require 'puppet/util/execution'
require 'puppet/util/suidmanager'
require 'open3'
require 'pty'
require 'expect'
require 'fileutils'
require 'English'

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

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

Puppet::Util::SUIDManager.asuser(uid) do
@exit_status = Open3.popen3({ 'HOME' => homedir }, command_str) do |stdin, stdout, stderr, wait_thr|
begin
Puppet::Util::SUIDManager.asuser(uid) do
FileUtils.touch login_file_path
FileUtils.chmod 0o600, login_file_path
end

PTY.spawn({ 'HOME' => homedir }, command_str) do |input, output, _pid|
if password
stdin.puts(password + "\r\n")
stdin.close
input.expect(%r{Enter password:})
output.puts password
end
@captured_stdout = stdout.read
@captured_stderr = stderr.read
wait_thr.value
end
end

if @exit_status.success? == false
rescue => e
raise Puppet::ExecutionFailure, _(
"Execution of '%{str}' returned %{exit_status}: %{output}",
) % {
str: command_str,
exit_status: @exit_status,
output: @captured_stderr.strip,
exit_status: $CHILD_STATUS.exitstatus,
output: e.message,
}
end
@captured_stdout
end

def mysql_config_editor_cmd(context, uid, *args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,17 @@
Puppet::Util::Execution.stubs(:execute).with(['/usr/bin/my_print_defaults', '-s', 'local_socket'], failonfail: true, uid: 'root', custom_environment: { 'HOME' => '/root' })
.returns("--user=root\n--password=more_secure\n--host=localhost\n--socket=/var/run/mysql.sock")

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

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

describe '#get' do
Expand Down