Skip to content

Commit ced1a08

Browse files
committed
Merge pull request #682 from eems-leo/process-secret-file
Remove default install root password if set
2 parents 4049258 + a26b80d commit ced1a08

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

manifests/params.pp

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
$purge_conf_dir = false
66
$restart = false
77
$root_password = 'UNSET'
8+
$install_secret_file = '/.mysql_secret'
89
$server_package_ensure = 'present'
910
$server_package_manage = true
1011
$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
$override_options = {},
89
$package_ensure = $mysql::params::server_package_ensure,

manifests/server/root_password.pp

+16
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,28 @@
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+
"rm -f ${secret_file}"
14+
], ' && ')
15+
exec { 'remove install pass':
16+
command => $rm_pass_cmd,
17+
onlyif => "test -f ${secret_file}",
18+
path => '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin'
19+
}
520

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

spec/classes/mysql_server_spec.rb

+16
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@
6969

7070
context 'mysql::server::root_password' do
7171
describe 'when defaults' do
72+
it {
73+
is_expected.to contain_exec('remove install pass').with(
74+
:command => 'mysqladmin -u root --password=$(grep -o \'[^ ]\\+$\' /.mysql_secret) password \'\' && rm -f /.mysql_secret',
75+
:onlyif => 'test -f /.mysql_secret',
76+
:path => '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin'
77+
)
78+
}
7279
it { is_expected.not_to contain_mysql_user('root@localhost') }
7380
it { is_expected.not_to contain_file('/root/.my.cnf') }
7481
end
@@ -92,6 +99,15 @@
9299
it { is_expected.not_to contain_mysql_user('root@localhost') }
93100
it { is_expected.not_to contain_file('/root/.my.cnf') }
94101
end
102+
describe 'when install_secret_file set to /root/.mysql_secret' do
103+
let(:params) {{ :install_secret_file => '/root/.mysql_secret' }}
104+
it {
105+
is_expected.to contain_exec('remove install pass').with(
106+
:command => 'mysqladmin -u root --password=$(grep -o \'[^ ]\\+$\' /root/.mysql_secret) password \'\' && rm -f /root/.mysql_secret',
107+
:onlyif => 'test -f /root/.mysql_secret'
108+
)
109+
}
110+
end
95111
end
96112

97113
context 'mysql::server::providers' do

0 commit comments

Comments
 (0)