Skip to content

Remove default install root password if set #682

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 1 commit into from
Aug 6, 2015
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 manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
$purge_conf_dir = false
$restart = false
$root_password = 'UNSET'
$install_secret_file = '/.mysql_secret'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this supposed to be /, rather than /root/ ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When Puppet installs the MySQL-server package this file seems to end up at /.mysql_secret

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on which platform? which mysql flavour?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CentOS, Oracle MySQL

If you install the package manually the file ends up at /root/.mysql_secret, but when puppet installs it the file ends up at /.mysql_secret. At least in my initial tests. I'll ensure I write a test to cover this.

$server_package_ensure = 'present'
$server_package_manage = true
$server_service_manage = true
Expand Down
1 change: 1 addition & 0 deletions manifests/server.pp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
$config_file = $mysql::params::config_file,
$includedir = $mysql::params::includedir,
$install_options = undef,
$install_secret_file = $mysql::params::install_secret_file,
$manage_config_file = $mysql::params::manage_config_file,
$old_root_password = $mysql::params::old_root_password,
$override_options = {},
Expand Down
16 changes: 16 additions & 0 deletions manifests/server/root_password.pp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,28 @@
class mysql::server::root_password {

$options = $mysql::server::options
$secret_file = $mysql::server::install_secret_file

# New installations of MySQL will configure a default random password for the root user
# with an expiration. No actions can be performed until this password is changed. The
# below exec will remove this default password. If the user has supplied a root
# password it will be set further down with the mysql_user resource.
$rm_pass_cmd = join([
"mysqladmin -u root --password=\$(grep -o '[^ ]\\+\$' ${secret_file}) password ''",
"rm -f ${secret_file}"
], ' && ')
exec { 'remove install pass':
command => $rm_pass_cmd,
onlyif => "test -f ${secret_file}",
path => '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin'
}

# manage root password if it is set
if $mysql::server::create_root_user == true and $mysql::server::root_password != 'UNSET' {
mysql_user { 'root@localhost':
ensure => present,
password_hash => mysql_password($mysql::server::root_password),
require => Exec['remove install pass']
}
}

Expand Down
16 changes: 16 additions & 0 deletions spec/classes/mysql_server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@

context 'mysql::server::root_password' do
describe 'when defaults' do
it {
is_expected.to contain_exec('remove install pass').with(
:command => 'mysqladmin -u root --password=$(grep -o \'[^ ]\\+$\' /.mysql_secret) password \'\' && rm -f /.mysql_secret',
:onlyif => 'test -f /.mysql_secret',
:path => '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin'
)
}
it { is_expected.not_to contain_mysql_user('root@localhost') }
it { is_expected.not_to contain_file('/root/.my.cnf') }
end
Expand All @@ -84,6 +91,15 @@
it { is_expected.not_to contain_mysql_user('root@localhost') }
it { is_expected.not_to contain_file('/root/.my.cnf') }
end
describe 'when install_secret_file set to /root/.mysql_secret' do
let(:params) {{ :install_secret_file => '/root/.mysql_secret' }}
it {
is_expected.to contain_exec('remove install pass').with(
:command => 'mysqladmin -u root --password=$(grep -o \'[^ ]\\+$\' /root/.mysql_secret) password \'\' && rm -f /root/.mysql_secret',
:onlyif => 'test -f /root/.mysql_secret'
)
}
end
end

context 'mysql::server::providers' do
Expand Down