Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for securing web interface with SSL #51

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ Role Variables
* `monit_webinterface_enabled`: Enable monit web interface. Defaults to `true`.
* `monit_webinterface_bind`: IP address to bind web interface. Defaults to `0.0.0.0` (listen for external requests).
* `monit_webinterface_port`: Port for web interface. Defaults to `2812`.
* `monit_webinterface_ssl_enabled`: Enable SSL for monit web interface. Defaults to `false`.
* `monit_webinterface_ssl_pemfile`: Path to the cerficate in PEM format (required when SSL is enabled for web interface). If you are using Lets Encrypt for generating the certificate, concatenate the ca chain and private key using `cat /etc/letsencrypt/live/YOUR_DOMAIN/{privkey,fullchain}.pem > /etc/monit/cert.pem`.
* `monit_webinterface_ssl_selfsigned`: Allow/reject self signed certificate. Defaults to `reject`.
* `monit_webinterface_rw_group`: Define group of users allowed to read and write on web interface. It is only applied when defined and is empty by default.
* `monit_webinterface_r_group`: Define group of users allowed to read on web interface. It is only applied when defined and is empty by default.
* `monit_webinterface_acl_rules`: List of ACL rules for the web interface, such as "localhost" or "hauk:password". It is only applied when defined and is empty by default. You should probably define at least one for the httpd service to start.
Expand Down
4 changes: 4 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ monit_mailserver_port: 25
monit_webinterface_enabled: true
monit_webinterface_bind: 0.0.0.0
monit_webinterface_port: 2812

monit_webinterface_ssl_enabled: false
monit_webinterface_ssl_pemfile: ''
monit_webinterface_ssl_selfsigned: 'reject'
20 changes: 20 additions & 0 deletions tasks/certificate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
- name: certificate - ensure directory exists for local self-signed TLS cert
file:
path: "{{ monit_webinterface_ssl_pemfile | dirname }}"
state: directory

- name: certificate - generate certificate
shell: >
openssl req -new -x509 -days 365 -nodes
-out {{ monit_webinterface_ssl_pemfile }}
-keyout {{ monit_webinterface_ssl_pemfile }}
-subj "/C=XX/ST=YY/L=ZZ/O=Acme Corporation/OU=IT Department/CN={{ ansible_fqdn }}"

- name: certificate - generate dhparams
shell: "openssl dhparam -2 2048 >> {{ monit_webinterface_ssl_pemfile }}"

- name: certificate - change file permission
file:
path: "{{ monit_webinterface_ssl_pemfile }}"
mode: '0600'
10 changes: 2 additions & 8 deletions tasks/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

- name: create lib folder
file: path="{{ monit_lib_folder }}" state=directory mode=0600

- name: config - Setup monitrc
template:
src: monitrc.j2
Expand All @@ -21,13 +21,7 @@
notify: restart monit

- name: config - Setup webinterface
template:
src: webinterface.j2
dest: "{{ monit_includes }}/webinterface"
owner: root
group: root
mode: 0644
notify: restart monit
include: web.yml

- name: config - Setup mail alerts
template:
Expand Down
10 changes: 10 additions & 0 deletions tasks/web.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
---
- name: web - check monit certificate exists
stat:
path: "{{ monit_webinterface_ssl_pemfile }}"
register: stat_result
when: monit_webinterface_ssl_enabled and monit_webinterface_ssl_pemfile != ''

- name: web - create self signed certificate
include: certificate.yml
when: stat_result is defined and stat_result.stat.exists == False

- name: web - Setup webinterface
template:
src: webinterface.j2
Expand Down
6 changes: 6 additions & 0 deletions templates/webinterface.j2
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ set httpd
allow {{ rule }}
{% endfor %}
{% endif %}
{%if monit_webinterface_ssl_enabled and monit_webinterface_ssl_pemfile != '' %}
with ssl {
pemfile: {{ monit_webinterface_ssl_pemfile }}
selfsigned: {{ monit_webinterface_ssl_selfsigned }}
}
{% endif %}
{% if monit_webinterface_rw_group is defined %}
allow @{{ monit_webinterface_rw_group }}
{% endif %}
Expand Down