Skip to content

Commit 4f5000b

Browse files
authored
Add support for enabling SELinux on RHEL based systems (#180)
1 parent fb9cffb commit 4f5000b

File tree

12 files changed

+169
-10
lines changed

12 files changed

+169
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ BREAKING CHANGES:
1111

1212
FEATURES:
1313

14-
Rename all modules to use the fully qualified collection name (FQCN) per Ansible guidelines.
14+
* Add support for enabling SELinux on RHEL based systems, and tweak it by default when installing NGINX App Protect DoS to avoid SELinux misconfiguration issues.
15+
* Rename all modules to use the fully qualified collection name (FQCN) per Ansible guidelines.
1516

1617
ENHANCEMENTS:
1718

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ If you wish to install NGINX App Protect WAF or NGINX App Protect DoS using this
2727
---
2828
collections:
2929
- name: community.general
30-
version: 4.4.0
30+
version: 4.6.0
3131
- name: ansible.posix
3232
version: 1.3.0
3333
- name: community.docker # Only required if you plan to use Molecule (see below)
34-
version: 2.1.1
34+
version: 2.2.1
3535
```
3636
3737
**Note:** You can alternatively install the Ansible community distribution (what is known as the "old" Ansible) if you don't want to manage individual collections.

defaults/main.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,32 @@ nginx_app_protect_log_policy_file_enable: false
110110
nginx_app_protect_log_policy_file:
111111
- src: files/config/log-policy.json
112112
dest: /etc/app_protect/conf/log-policy.json
113+
114+
# Set SELinux enforcing for NGINX (CentOS/Red Hat only) - you may need to open ports on your own
115+
nginx_app_protect_selinux: false
116+
117+
# Enable enforcing mode if true. Permissive if false (audit only, no enforcing) globally (only works with nginx_selinux: true)
118+
nginx_app_protect_selinux_enforcing: true
119+
120+
# List of TCP ports to add to http_port_t type (80 and 443 have this type already)
121+
# nginx_app_protect_selinux_tcp_ports:
122+
# - 80
123+
# - 443
124+
125+
# List of TCP ports to add to syslog_port_t type (80 and 443 have this type already)
126+
# nginx_app_protect_selinux_syslog_tcp_ports:
127+
# - 1514
128+
# - 5144
129+
130+
# List of UDP ports to add to http_port_t type
131+
# nginx_app_protect_selinux_udp_ports:
132+
# - 80
133+
# - 443
134+
135+
# List of UDP ports to add to syslog_port_t type
136+
# nginx_app_protect_selinux_syslog_udp_ports:
137+
# - 1514
138+
# - 5144
139+
140+
# Temporary directory to hold selinux modules
141+
nginx_app_protect_selinux_tempdir: /tmp

molecule/advanced/converge.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
certificate: license/nginx-repo.crt
1111
key: license/nginx-repo.key
1212
nginx_app_protect_remove_license: false
13-
nginx_app_protect_install_signatures: true
14-
nginx_app_protect_install_threat_campaigns: true
13+
nginx_app_protect_waf_install_signatures: true
14+
nginx_app_protect_waf_install_threat_campaigns: true
1515
nginx_app_protect_timeout: 180

molecule/default/converge.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
key: license/nginx-repo.key
2929
nginx_app_protect_use_rhel_subscription_repos: "{{ rhel_subscription }}"
3030
nginx_app_protect_remove_license: false
31-
nginx_app_protect_install_signatures: true
32-
nginx_app_protect_install_threat_campaigns: true
31+
nginx_app_protect_waf_install_signatures: true
32+
nginx_app_protect_waf_install_threat_campaigns: true
3333
nginx_app_protect_configure: true
3434
nginx_app_protect_security_policy_file_enable: true
3535
nginx_app_protect_security_policy_file:

tasks/common/install/service-modification.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
- name: Increase timeout for NGINX Plus service
1111
ansible.builtin.template:
12-
src: nginx.service.override.conf.j2
12+
src: services/nginx.service.override.conf.j2
1313
dest: /etc/systemd/system/nginx.service.d/override.conf
1414
owner: root
1515
group: root

tasks/common/prerequisites/install-dependencies.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
- name: (Amazon Linux/CentOS/RHEL) Import EPEL GPG key
1818
ansible.builtin.rpm_key:
1919
state: present
20-
key: https://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-{{ ((ansible_distribution == 'Amazon') | ternary('7', ansible_distribution_major_version)) }}
20+
key: https://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-{{ (ansible_distribution == 'Amazon') | ternary('7', ansible_distribution_major_version) }}
2121

2222
- name: (Amazon Linux/CentOS/RHEL) Install package dependencies
2323
ansible.builtin.yum:
@@ -55,6 +55,11 @@
5555
- not nginx_app_protect_use_rhel_subscription_repos | bool
5656
- nginx_app_protect_dos_enable | bool
5757

58+
- name: (RHEL) Enable RHEL subscription manager repos management
59+
ansible.builtin.command: "subscription-manager config --rhsm.manage_repos=1"
60+
changed_when: false
61+
when: nginx_app_protect_use_rhel_subscription_repos | bool
62+
5863
- name: (RHEL 7) Set up RHEL dependencies from RHEL official repositories
5964
community.general.rhsm_repository:
6065
name:
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
- name: Install dependencies
3+
ansible.builtin.include_tasks: "{{ role_path }}/tasks/common/prerequisites/install-dependencies.yml"
4+
5+
- name: Set up SELinux
6+
block:
7+
- name: Check if SELinux is enabled
8+
ansible.builtin.debug:
9+
msg: You need to enable SELinux, if it was disabled you need to reboot
10+
when: ansible_facts['selinux'] is undefined
11+
12+
- name: Configure SELinux
13+
ansible.builtin.include_tasks: "{{ role_path }}/tasks/common/prerequisites/setup-selinux.yml"
14+
when: ansible_facts['selinux']['mode'] is defined
15+
when:
16+
- nginx_app_protect_selinux | bool or (nginx_app_protect_dos_enable | bool and nginx_app_protect_dos_setup == "install")
17+
- "'selinux' in ansible_facts"
18+
- ansible_facts['os_family'] in ['RedHat', 'Suse']
19+
- ansible_facts['distribution'] not in ['Amazon', 'OracleLinux']
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
- name: (CentOS/RHEL) Install dependencies
3+
ansible.builtin.yum:
4+
name:
5+
- libselinux-utils
6+
- policycoreutils
7+
- selinux-policy-targeted
8+
when: ansible_facts['os_family'] == "RedHat"
9+
10+
- name: Set SELinux mode to permissive
11+
ansible.posix.selinux:
12+
state: permissive
13+
policy: targeted
14+
15+
- name: Allow SELinux HTTP network connections
16+
ansible.posix.seboolean:
17+
name: "{{ item }}"
18+
state: true
19+
persistent: true
20+
loop:
21+
- httpd_can_network_connect
22+
- httpd_can_network_relay
23+
24+
- name: Allow SELinux TCP HTTP connections on specific ports
25+
community.general.seport:
26+
ports: "{{ nginx_app_protect_selinux_tcp_ports }}"
27+
proto: tcp
28+
setype: http_port_t
29+
state: present
30+
when: nginx_app_protect_selinux_tcp_ports is defined
31+
32+
- name: Allow SELinux TCP Syslog connections on specific ports
33+
community.general.seport:
34+
ports: "{{ nginx_app_protect_selinux_syslog_tcp_ports }}"
35+
proto: tcp
36+
setype: syslogd_port_t
37+
state: present
38+
when: nginx_app_protect_selinux_syslog_tcp_ports is defined
39+
40+
- name: Allow SELinux UDP HTTP connections on specific ports
41+
community.general.seport:
42+
ports: "{{ nginx_app_protect_selinux_udp_ports }}"
43+
proto: udp
44+
setype: http_port_t
45+
state: present
46+
when: nginx_app_protect_selinux_udp_ports is defined
47+
48+
- name: Allow SELinux TCP Syslog connections on specific ports
49+
community.general.seport:
50+
ports: "{{ nginx_app_protect_selinux_syslog_tcp_ports }}"
51+
proto: udp
52+
setype: syslogd_port_t
53+
state: present
54+
when: nginx_app_protect_selinux_syslog_udp_ports is defined
55+
56+
- name: Create SELinux NGINX App Protect module
57+
ansible.builtin.template:
58+
src: "{{ role_path }}/templates/selinux/nginx-app-protect-module.te.j2"
59+
dest: "{{ nginx_app_protect_selinux_tempdir }}/nginx-app-protect-module.te"
60+
mode: 0644
61+
register: nginx_app_protect_selinux_module
62+
63+
- name: Check SELinux NGINX App Protect module
64+
ansible.builtin.command: "checkmodule -M -m -o {{ nginx_app_protect_selinux_tempdir }}/nginx-app-protect-module.mod {{ nginx_app_protect_selinux_tempdir }}/nginx-app-protect-module.te"
65+
args:
66+
creates: "{{ nginx_app_protect_selinux_tempdir }}/nginx-app-protect-module.mod"
67+
changed_when: false
68+
69+
- name: Compile SELinux NGINX App Protect module
70+
ansible.builtin.command: "semodule_package -o {{ nginx_app_protect_selinux_tempdir }}/nginx-app-protect-module.pp -m {{ nginx_app_protect_selinux_tempdir }}/nginx-app-protect-module.mod"
71+
args:
72+
creates: "{{ nginx_app_protect_selinux_tempdir }}/nginx-app-protect-module.pp"
73+
changed_when: false
74+
75+
- name: Import SELinux NGINX App Protect module
76+
ansible.builtin.command: "semodule -i {{ nginx_app_protect_selinux_tempdir }}/nginx-app-protect-module.pp" # noqa no-handler
77+
changed_when: false
78+
when: nginx_app_protect_selinux_module.changed | bool
79+
80+
- name: Set SELinux mode to enforcing
81+
ansible.posix.selinux:
82+
state: enforcing
83+
policy: targeted
84+
when: nginx_app_protect_selinux_enforcing | bool

tasks/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
when: nginx_app_protect_log_policy_file_enable | bool
2626

2727
- name: Install prerequisites
28-
ansible.builtin.include_tasks: "{{ role_path }}/tasks/common/prerequisites/install-dependencies.yml"
28+
ansible.builtin.include_tasks: "{{ role_path }}/tasks/common/prerequisites/prerequisites.yml"
2929
when: nginx_app_protect_waf_enable | bool
3030
or nginx_app_protect_dos_enable | bool
3131
tags: nginx_app_protect_prerequisites

0 commit comments

Comments
 (0)