Skip to content

Commit d915e37

Browse files
committed
Remove default install root password if set
1 parent 60c0bfb commit d915e37

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

manifests/params.pp

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
$purge_conf_dir = false
77
$restart = false
88
$root_password = 'UNSET'
9+
$install_secret_file = '/.mysql_secret'
910
$server_package_ensure = 'present'
1011
$server_package_manage = true
1112
$server_service_manage = true

manifests/server.pp

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
$config_file = $mysql::params::config_file,
44
$includedir = $mysql::params::includedir,
55
$install_options = undef,
6+
$install_secret_file = $mysql::params::install_secret_file,
67
$manage_config_file = $mysql::params::manage_config_file,
78
$old_root_password = $mysql::params::old_root_password,
89
$override_options = {},

manifests/server/root_password.pp

+17
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,29 @@
22
class mysql::server::root_password {
33

44
$options = $mysql::server::options
5+
$secret_file = $mysql::server::install_secret_file
6+
7+
# New installations of MySQL will configure a default random password for the root user
8+
# with an expiration. No actions can be performed until this password is changed. The
9+
# below exec will remove this default password. If the user has supplied a root
10+
# password it will be set further down with the mysql_user resource.
11+
$rm_pass_cmd = join([
12+
"mysqladmin -u root --password=\$(grep -o '[^ ]\\+\$' ${secret_file}) password ''",
13+
"echo \$(grep -o '[^ ]\\+\$' ${secret_file})",
14+
"rm -f ${secret_file}"
15+
], ' && ')
16+
exec { "remove install password":
17+
command => $rm_pass_cmd,
18+
onlyif => "test -f ${secret_file}",
19+
path => "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
20+
}
521

622
# manage root password if it is set
723
if $mysql::server::create_root_user == true and $mysql::server::root_password != 'UNSET' {
824
mysql_user { 'root@localhost':
925
ensure => present,
1026
password_hash => mysql_password($mysql::server::root_password),
27+
require => Exec['remove install password']
1128
}
1229
}
1330

spec/classes/mysql_server_spec.rb

+16
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@
6161

6262
context 'mysql::server::root_password' do
6363
describe 'when defaults' do
64+
it {
65+
is_expected.to contain_exec('remove install pass').with {
66+
:command => 'mysqladmin -u root --password=$(grep -o \'[^ ]\\+$\' /.mysql_secret) password \'\' && rm -f /.mysql_secret',
67+
:test => 'test -f /root/.mysql_secret',
68+
:path => '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin'
69+
}
70+
}
6471
it { is_expected.not_to contain_mysql_user('root@localhost') }
6572
it { is_expected.not_to contain_file('/root/.my.cnf') }
6673
end
@@ -84,6 +91,15 @@
8491
it { is_expected.not_to contain_mysql_user('root@localhost') }
8592
it { is_expected.not_to contain_file('/root/.my.cnf') }
8693
end
94+
describe 'when install_secret_file set to /root/.mysql_secret' do
95+
let(:params) {{ :install_secret_file => '/root/.mysql_secret' }}
96+
it {
97+
is_expected.to contain_exec('remove install pass').with {
98+
:command => 'mysqladmin -u root --password=$(grep -o \'[^ ]\\+$\' /root/.mysql_secret) password \'\' && rm -f /root/.mysql_secret',
99+
:test => 'test -f /root/.mysql_secret'
100+
}
101+
}
102+
end
87103
end
88104

89105
context 'mysql::server::providers' do

0 commit comments

Comments
 (0)