Skip to content

Commit 6e24797

Browse files
authored
Merge pull request #423 from dev-sec/drop_users_wo_passwords
add new tasks to delete mysql users without passwords
2 parents add303f + 8c89d78 commit 6e24797

File tree

5 files changed

+101
-12
lines changed

5 files changed

+101
-12
lines changed

molecule/mysql_hardening/converge.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
- mysql_python_package_debian is not defined
2323
- ansible_distribution != "Ubuntu"
2424
- ansible_distribution_major_version|int < 20
25-
- include_role:
26-
name: dev-sec.mysql
27-
2825
- include_role:
2926
name: mysql_hardening
3027
vars:

molecule/mysql_hardening/prepare.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,34 @@
2525
file:
2626
path: "/etc/mysql/conf.d"
2727
state: directory
28+
29+
- name: Determine required MySQL Python libraries (Ubuntu Focal Fossa ++)
30+
set_fact:
31+
mysql_python_package_debian: "python3-pymysql"
32+
when:
33+
- mysql_python_package_debian is not defined
34+
- ansible_distribution == "Ubuntu"
35+
- ansible_distribution_major_version|int > 19
36+
37+
- name: Determine required MySQL Python libraries.
38+
set_fact:
39+
mysql_python_package_debian: "{% if 'python3' in ansible_python_interpreter|default('') %}python3-mysqldb{% else %}python-mysqldb{% endif %}"
40+
when:
41+
- mysql_python_package_debian is not defined
42+
- ansible_distribution != "Ubuntu"
43+
- ansible_distribution_major_version|int < 20
44+
45+
- include_role:
46+
name: dev-sec.mysql
47+
48+
- name: create a user with an empty password
49+
community.mysql.mysql_query:
50+
query:
51+
- "CREATE USER foo@bar;"
52+
login_unix_socket: "{{ login_unix_socket | default(omit) }}"
53+
vars:
54+
overwrite_global_mycnf: false
55+
mysql_root_password: iloverandompasswordsbutthiswilldo
56+
mysql_user_password: iloverandompasswordsbutthiswilldo
57+
mysql_config_file: /etc/mysql/mariadb.cnf
58+
mysql_root_password_update: true

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ ansible
44
ansible-lint
55
docker
66
flake8
7+
jmespath

roles/mysql_hardening/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ This role provides security configurations for MySQL and its derivates. It is in
99
It configures:
1010

1111
- Permissions for the various configuration files and folders
12-
- Removes anonymous users, root-users without a password and test databases
12+
- Removes anonymous users, users without a password or authentication_string and test databases
1313
- various hardening options inside MySQL
1414

1515
## Requirements

roles/mysql_hardening/tasks/mysql_secure_installation.yml

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
msg: 'ERROR - you have to change default mysql_root_password'
55
when: mysql_root_password == '-----====>SetR00tPa$$wordH3r3!!!<====-----'
66

7-
- name: Root password is present
8-
mysql_user:
7+
- name: ensure that the root password is present
8+
community.mysql.mysql_user:
99
name: 'root'
1010
host_all: true
1111
password: '{{ mysql_root_password | mandatory }}'
@@ -19,24 +19,84 @@
1919
mode: '0400'
2020
tags: my_cnf
2121

22-
- name: Test database is absent
23-
mysql_db:
22+
- name: ensure that the test database is absent
23+
community.mysql.mysql_db:
2424
name: test
2525
state: absent
2626
login_unix_socket: "{{ login_unix_socket | default(omit) }}"
2727
when: mysql_remove_test_database
2828

29-
- name: Anonymous users are absent
30-
mysql_user:
29+
- name: ensure that anonymous users are absent
30+
community.mysql.mysql_user:
3131
name: ''
3232
state: absent
3333
host_all: true
3434
login_unix_socket: "{{ login_unix_socket | default(omit) }}"
3535
when: mysql_remove_anonymous_users
3636

37-
- name: Remove remote root
37+
- name: ensure that root can only login from localhost
3838
community.mysql.mysql_query:
3939
query:
40-
- DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1')
40+
- DELETE
41+
FROM mysql.user
42+
WHERE USER='root'
43+
AND HOST NOT IN ('localhost',
44+
'127.0.0.1',
45+
'::1')
4146
login_unix_socket: "{{ login_unix_socket | default(omit) }}"
4247
when: mysql_remove_remote_root
48+
49+
- name: get all users that have no password or authentication_string on MySQL version >= 5.7.6
50+
community.mysql.mysql_query:
51+
query:
52+
- SELECT GROUP_CONCAT(USER, '@', HOST SEPARATOR ', ') AS users
53+
FROM mysql.user
54+
WHERE (length(authentication_string)=0
55+
OR authentication_string="")
56+
AND USER NOT IN ('mysql.sys',
57+
'mysqlxsys',
58+
'mariadb.sys');
59+
login_unix_socket: "{{ login_unix_socket | default(omit) }}"
60+
register: mysql_users_wo_passwords_or_auth_string
61+
when:
62+
- mysql_version.version.full is version('5.7.6', '>=')
63+
64+
- name: get all users that have no password on MySQL version < 5.7.6
65+
community.mysql.mysql_query:
66+
query:
67+
- SELECT GROUP_CONCAT(USER, '@', HOST SEPARATOR ', ') AS users
68+
FROM mysql.user
69+
WHERE (length(password)=0
70+
OR password="")
71+
AND (length(authentication_string)=0
72+
OR authentication_string="")
73+
AND USER NOT IN ('mysql.sys',
74+
'mysqlxsys',
75+
'mariadb.sys');
76+
login_unix_socket: "{{ login_unix_socket | default(omit) }}"
77+
register: mysql_users_wo_passwords
78+
when:
79+
- mysql_version.version.full is version('5.7.6', '<')
80+
81+
- name: create a fact for users without password or authentication_string
82+
set_fact:
83+
users_wo_auth: "{{ mysql_users_wo_passwords_or_auth_string.query_result.0.0 | community.general.json_query('users') }}"
84+
when:
85+
- mysql_users_wo_passwords_or_auth_string.query_result is defined
86+
- mysql_users_wo_passwords_or_auth_string.query_result != "" # noqa empty-string-compare
87+
88+
- name: create a fact for users without password
89+
set_fact:
90+
users_wo_auth: "{{ mysql_users_wo_passwords.query_result.0.0 | community.general.json_query('users') }}"
91+
when:
92+
- mysql_users_wo_passwords.query_result is defined
93+
- mysql_users_wo_passwords.query_result != "" # noqa empty-string-compare
94+
95+
- name: ensure that there are no users without password or authentication_string
96+
community.mysql.mysql_query:
97+
query:
98+
- "DROP USER {{ users_wo_auth }}"
99+
login_unix_socket: "{{ login_unix_socket | default(omit) }}"
100+
when:
101+
- users_wo_auth is defined
102+
- users_wo_auth != "" # noqa empty-string-compare

0 commit comments

Comments
 (0)