Skip to content

Commit 620519f

Browse files
authored
dynamically define mysql datadir and log_error (#54)
* dynamically define mysql datadir and log_error Signed-off-by: Sebastian Gumprich <[email protected]> * simplify sql command checks Signed-off-by: Sebastian Gumprich <[email protected]> * fix linting issues Signed-off-by: Sebastian Gumprich <[email protected]> * check for param before assigning it Signed-off-by: Sebastian Gumprich <[email protected]>
1 parent e74bd4f commit 620519f

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

controls/mysql_conf.rb

+18-9
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,33 @@
1818

1919
mysql_hardening_file = '/etc/mysql/conf.d/hardening.cnf'
2020

21+
user = attribute('User', description: 'MySQL database user', value: 'root', required: true)
22+
pass = attribute('Password', description: 'MySQL database password', value: 'iloverandompasswordsbutthiswilldo', required: true)
23+
24+
# get datadir and logfile-path from settings in the configuration if it is defined or from mysql itself
25+
26+
mysql_data_path = if mysql_conf.params.mysqld && mysql_conf.params.mysqld.datadir
27+
mysql_conf.params.mysqld.datadir
28+
else
29+
command("mysql -u#{user} -p#{pass} -sN -e \"select @@GLOBAL.datadir\";").stdout.strip
30+
end
31+
32+
mysql_log_file = if mysql_conf.params.mysqld && mysql_conf.params.mysqld.log_error
33+
mysql_conf.params.mysqld.log_error
34+
else
35+
command("mysql -u#{user} -p#{pass} -sN -e \"select @@GLOBAL.log_error\";").stdout.strip
36+
end
37+
2138
# set OS-dependent filenames and paths
2239
case os[:family]
2340
when 'ubuntu', 'debian'
2441
mysql_config_path = '/etc/mysql/'
2542
mysql_config_file = mysql_config_path + 'my.cnf'
26-
mysql_data_path = '/var/lib/mysql/'
27-
mysql_log_path = '/var/log/'
28-
mysql_log_file = 'mysql.log'
2943
mysql_log_group = 'adm'
3044
service_name = 'mysql'
3145
when 'redhat', 'fedora'
3246
mysql_config_path = '/etc/'
3347
mysql_config_file = mysql_config_path + 'my.cnf'
34-
mysql_data_path = '/var/lib/mysql/'
35-
mysql_log_path = '/var/log/'
36-
mysql_log_path = '/var/log/mariadb/' if os[:release] >= '7'
37-
mysql_log_file = 'mysqld.log'
38-
mysql_log_file = 'mariadb.log' if os[:release] >= '7'
3948
mysql_log_group = 'mysql'
4049
service_name = 'mysqld'
4150
service_name = 'mariadb' if os[:release] >= '7'
@@ -103,7 +112,7 @@
103112
control 'mysql-conf-06' do
104113
impact 0.5
105114
title 'ensure log file is owned by mysql user'
106-
describe file("#{mysql_log_path}/#{mysql_log_file}") do
115+
describe file(mysql_log_file) do
107116
it { should be_owned_by 'mysql' }
108117
it { should be_grouped_into mysql_log_group }
109118
it { should_not be_readable.by('others') }

controls/mysql_db.rb

+8-8
Original file line numberDiff line numberDiff line change
@@ -22,63 +22,63 @@
2222
control 'mysql-db-01' do
2323
impact 0.3
2424
title 'use supported mysql version in production'
25-
describe command("mysql -u#{user} -p#{pass} mysql -s -e 'select version();' | tail -1") do
25+
describe command("mysql -u#{user} -p#{pass} -sN -e 'select version();'") do
2626
its(:stdout) { should_not match(/Community/) }
2727
end
2828
end
2929

3030
control 'mysql-db-02' do
3131
impact 0.5
3232
title 'use mysql version 5 or higher'
33-
describe command("mysql -u#{user} -p#{pass} mysql -s -e 'select substring_index(version(),\".\",1);'") do
33+
describe command("mysql -u#{user} -p#{pass} -sN -e 'select substring_index(version(),\".\",1);'") do
3434
its(:stdout) { should cmp >= 5 }
3535
end
3636
end
3737

3838
control 'mysql-db-03' do
3939
impact 1.0
4040
title 'test database must be deleted'
41-
describe command("mysql -u#{user} -p#{pass} -s -e 'show databases like \"test\";'") do
41+
describe command("mysql -u#{user} -p#{pass} -sN -e 'show databases like \"test\";'") do
4242
its(:stdout) { should_not match(/test/) }
4343
end
4444
end
4545

4646
control 'mysql-db-04' do
4747
impact 1.0
4848
title 'deactivate annonymous user names'
49-
describe command("mysql -u#{user} -p#{pass} mysql -s -e 'select count(*) from mysql.user where user=\"\";' | tail -1") do
49+
describe command("mysql -u#{user} -p#{pass} -sN -e 'select count(*) from mysql.user where user=\"\";'") do
5050
its(:stdout) { should match(/^0/) }
5151
end
5252
end
5353

5454
control 'mysql-db-05' do
5555
impact 1.0
5656
title 'default passwords must be changed'
57-
describe command("mysql -u#{user} -p#{pass} mysql -s -e 'select count(*) from mysql.user where length(password)=0 or password=\"\";' | tail -1") do
57+
describe command("mysql -u#{user} -p#{pass} -sN -e 'select count(*) from mysql.user where length(password)=0 or password=\"\";'") do
5858
its(:stdout) { should match(/^0/) }
5959
end
6060
end
6161

6262
control 'mysql-db-06' do
6363
impact 0.5
6464
title 'the grant option must not be used'
65-
describe command("mysql -u#{user} -p#{pass} mysql -s -e 'select count(*) from mysql.user where grant_priv=\"y\" and User!=\"root\" and User!=\"debian-sys-maint\";' | tail -1") do
65+
describe command("mysql -u#{user} -p#{pass} -sN -e 'select count(*) from mysql.user where grant_priv=\"y\" and User!=\"root\" and User!=\"debian-sys-maint\";'") do
6666
its(:stdout) { should match(/^0/) }
6767
end
6868
end
6969

7070
control 'mysql-db-07' do
7171
impact 0.5
7272
title 'ensure no wildcards are used for hostnames'
73-
describe command("mysql -u#{user} -p#{pass} mysql -s -e 'select count(*) from mysql.user where host=\"%\"' | tail -1") do
73+
describe command("mysql -u#{user} -p#{pass} -sN -e 'select count(*) from mysql.user where host=\"%\"'") do
7474
its(:stdout) { should match(/^0/) }
7575
end
7676
end
7777

7878
control 'mysql-db-08' do
7979
impact 0.5
8080
title 'it must be ensured that superuser can login via localhost only'
81-
describe command("mysql -u#{user} -p#{pass} mysql -s -e 'select count(*) from mysql.user where user=\"root\" and host not in (\"localhost\",\"127.0.0.1\",\"::1\")' | tail -1") do
81+
describe command("mysql -u#{user} -p#{pass} -sN -e 'select count(*) from mysql.user where user=\"root\" and host not in (\"localhost\",\"127.0.0.1\",\"::1\")'") do
8282
its(:stdout) { should match(/^0/) }
8383
end
8484
end

0 commit comments

Comments
 (0)