Skip to content
This repository was archived by the owner on Nov 17, 2020. It is now read-only.

Commit 2932a4a

Browse files
author
Sebastian Gumprich
committed
Merge pull request #15 from fitz123/alt_version
alt version initial commit
2 parents 3d7eadc + ec90b8d commit 2932a4a

File tree

9 files changed

+111
-32
lines changed

9 files changed

+111
-32
lines changed

README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ This role focuses on security configuration of MySQL. Therefore you can add this
1111
## Requirements
1212

1313
* Ansible
14-
* Python MySQL-DB Package
15-
16-
## Usage
17-
18-
Before you use this role make sure to have a valid login-configuration in `~/.my.cnf` so Ansible is able to login into your database.
14+
* Set up `mysql_root_password` variable
1915

2016
### Example Playbook
2117

@@ -25,6 +21,7 @@ Before you use this role make sure to have a valid login-configuration in `~/.my
2521

2622
This hardening role installs the hardening but expects an existing installation of MySQL, MariaDB or Percona. Please ensure that the following variables are set accordingly:
2723

24+
- `mysql_hardening_enabled: yes` role is enabled by default and can be disabled without removing it from a playbook. You can use conditional variable, for example: `mysql_hardening_enabled: "{{ true if mysql_enabled else false }}"`
2825
- `mysql_hardening_user: 'mysql'` The user that mysql runs as.
2926
- `mysql_datadir: '/var/lib/mysql'` The MySQL data directory
3027
- `mysql_hardening_hardening_conf: '/etc/mysql/conf.d/hardening.cnf'` The path to the configuration file where the hardening will be performed

defaults/main.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1+
# switcher to enable/disable role
2+
mysql_hardening_enabled: yes
3+
14
# general configuration
25
mysql_hardening_user: 'mysql'
6+
mysql_hardening_group: 'root'
37
mysql_datadir: '/var/lib/mysql'
48
mysql_hardening_hardening_conf: '/etc/mysql/conf.d/hardening.cnf'
9+
# You have to change this to your own strong enough mysql root password
10+
mysql_root_password: '-----====>SetR00tPa$$wordH3r3!!!<====-----'
11+
# There .my.cnf with mysql root credentials will be installed
12+
mysql_user_home: "{{ ansible_env.HOME}}"
513

614
# ensure the following parameters are set properly
7-
mysql_allow_remote_root: false
15+
mysql_remove_remote_root: true
816
mysql_remove_anonymous_users: true
917
mysql_remove_test_database: true
1018

files/mysql_grants.sql

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DELETE FROM mysql.user WHERE User='';

files/mysql_remove_remote_root.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');

tasks/configure.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
3+
- name: protect my.cnf
4+
file: path='{{mysql_hardening_mysql_conf}}' mode=0600 owner=root group=root
5+
6+
- name: ensure permissions on mysql-datadir are correct
7+
file: path='{{mysql_datadir}}' state=directory owner='{{mysql_hardening_user}}' group='{{mysql_hardening_user}}'
8+
9+
- name: check mysql configuration-directory exists and has right permissions
10+
file: path='/etc/mysql/conf.d' state=directory owner='{{mysql_hardening_user}}' group='{{mysql_hardening_group}}' mode=0470
11+
12+
- name: check include-dir directive is present in my.cnf
13+
lineinfile: dest='{{mysql_hardening_mysql_conf}}' line='!includedir /etc/mysql/conf.d/' insertafter='EOF' state=present backup=yes
14+
notify: restart mysql
15+
16+
- name: apply hardening configuration
17+
template: src='hardening.cnf.j2' dest='{{mysql_hardening_hardening_conf}}' owner='{{mysql_hardening_user}}' group='{{mysql_hardening_group}}' mode=0460
18+
notify: restart mysql

tasks/main.yml

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,13 @@
44
include_vars: "{{ ansible_os_family }}.yml"
55
tags: always
66

7-
- name: protect my.cnf
8-
file: path='{{mysql_hardening_mysql_conf}}' mode=0600 owner=root group=root
9-
10-
- name: ensure permissions on mysql-datadir are correct
11-
file: path='{{mysql_datadir}}' state=directory owner='{{mysql_hardening_user}}' group='{{mysql_hardening_user}}'
12-
13-
- name: create mysql configuration-directory
14-
file: path='/etc/mysql/conf.d' state=directory owner='{{mysql_hardening_user}}' mode=0600
15-
16-
- name: add include-dir directive to my.cnf
17-
lineinfile: dest='{{mysql_hardening_mysql_conf}}' line='!includedir /etc/mysql/conf.d/' insertafter='^\[mysql\]' state=present backup=yes
18-
19-
- name: apply hardening configuration
20-
template: src='hardening.cnf.j2' dest='{{mysql_hardening_hardening_conf}}' owner='{{mysql_hardening_user}}' mode=0750
21-
notify: restart mysql
22-
23-
# Copy database dump file to remote host and restore it to database 'my_db'
24-
- name: copy the sql-script to the remote host
25-
copy: src='mysql_grants.sql' dest='/tmp/'
26-
27-
- name: run the mysql_grants.sql script
28-
mysql_db: name='mysql' state=import target='/tmp/mysql_grants.sql'
7+
- include: configure.yml
8+
when: mysql_hardening_enabled
9+
tags:
10+
- mysql_hardening
11+
12+
- include: mysql_secure_installation.yml
13+
when: mysql_hardening_enabled
14+
tags:
15+
- mysql_hardening
16+
- mysql_secure_installation
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
3+
# supported for ansible ver => 2.0
4+
#- name: Install python-mysqldb for Ansible
5+
# package: pkg=python-mysqldb state=present
6+
7+
8+
- name: Install MySQL-python for Ansible
9+
apt: name=python-mysqldb state=present
10+
when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'
11+
12+
- name: Install python-mysqldb for Ansible
13+
yum: name=MySQL-python state=present
14+
when: ansible_os_family == 'RedHat' or ansible_os_family == 'Oracle Linux'
15+
16+
- debug: msg="WARNING - you have to change default mysql_root_password"
17+
when: mysql_root_password == '-----====>SetR00tPa$$wordH3r3!!!<====-----'
18+
19+
- name: root password is present
20+
mysql_user: name=root host={{item}} password={{mysql_root_password | mandatory}} state=present
21+
with_items:
22+
- '::1'
23+
- '127.0.0.1'
24+
- 'localhost'
25+
26+
- name: install .my.cnf with credentials
27+
template: src=my.cnf.j2 dest={{mysql_user_home}}/.my.cnf
28+
mode=0400
29+
tags: my_cnf
30+
31+
- name: test database is absent
32+
mysql_db: name=test state=absent
33+
when: mysql_remove_test_database
34+
35+
# Can use only if ansible ver => 2.1
36+
#- name: anonymous users are absent
37+
# mysql_user: name='' state=absent host_all=yes
38+
# when: mysql_remove_anonymous_users
39+
40+
- name: copy mysql_remove_anonymous_users
41+
copy: src='{{item}}.sql' dest='/tmp/{{item}}.sql'
42+
with_items:
43+
- mysql_remove_anonymous_users
44+
when: mysql_remove_anonymous_users
45+
changed_when: false
46+
47+
- name: apply mysql_remove_anonymous_users
48+
mysql_db: name='mysql' state=import target='/tmp/{{item}}.sql'
49+
with_items:
50+
- mysql_remove_anonymous_users
51+
when: mysql_remove_anonymous_users
52+
changed_when: false
53+
54+
- name: copy mysql_remove_remote_root
55+
copy: src='{{item}}.sql' dest='/tmp/{{item}}.sql'
56+
with_items:
57+
- mysql_remove_remote_root
58+
when: mysql_remove_remote_root
59+
changed_when: false
60+
61+
- name: apply mysql_remove_remote_root
62+
mysql_db: name='mysql' state=import target='/tmp/{{item}}.sql'
63+
with_items:
64+
- mysql_remove_remote_root
65+
when: mysql_remove_remote_root
66+
changed_when: false

templates/my.cnf.j2

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[client]
2+
user=root
3+
password='{{ mysql_root_password | mandatory }}'
4+
#ssl

0 commit comments

Comments
 (0)