Skip to content

Commit 1b63a9b

Browse files
authored
Merge pull request #8 from magicalyak/selinux
Selinux Upgrades
2 parents 4ffc614 + b00b2ae commit 1b63a9b

File tree

10 files changed

+157
-16
lines changed

10 files changed

+157
-16
lines changed

defaults/main.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ app_protect_state: present
1515
# Enable enforcing selinux (you may need to open ports on your own)
1616
app_protect_selinux: false
1717

18+
# Enable enforcing mode if true. Permissive if false (audit only, no enforcing) globally (only works with app_protect_selinux: true)
19+
app_protect_selinux_enforcing: true
20+
1821
# The installation of NGINX App Protect includes a base signature set, which may be out of date.
1922
# This option installs the latest NGINX App Protect signatures.
2023
app_protect_install_signatures: true
@@ -29,6 +32,12 @@ app_protect_delete_license: true
2932
# Default is true.
3033
nginx_start: true
3134

35+
# Increase NGINX service timeout to accomdate ruleset loading from default 90s
36+
nginx_timeout: 180
37+
38+
# App Protect Temporary Directory to use (Default: /tmp)
39+
app_protect_tempdir: /tmp
40+
3241
# Choose where to fetch the NGINX App Protect signing key from.
3342
# Default is the official NGINX App Protect signing key host.
3443
# app_protect_signing_key: https://cs.nginx.com/static/keys/app-protect.key

handlers/main.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
---
22
# handlers file for ansible-role-nginx-app-protect
3+
- name: "(Handler: All OSs) Check NGINX"
4+
command: "nginx -t"
5+
changed_when: false
6+
37
- name: "(Handler: All OSs) Run NGINX"
48
block:
59

@@ -8,11 +12,13 @@
812
name: nginx
913
state: started
1014
enabled: true
15+
notify: "(Handler: All OSs) Check NGINX"
1116

1217
- name: "(Handler: All OSs) Restart NGINX"
1318
service:
1419
name: nginx
1520
state: restarted
21+
changed_when: false
1622

1723
when:
1824
- nginx_start | bool

molecule/default/converge.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
log_policy_syslog_target: 10.1.10.105:5144
1414
nginx_demo_workload_protocol: http://
1515
nginx_demo_workload_host: 10.1.10.105:8080
16-
nginx_license:
16+
nginx_license:
1717
certificate: "./license/nginx-repo.crt"
1818
key: "./license/nginx-repo.key"
1919

tasks/configure-app-protect.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
dest: "{{ nginx_conf_template.out_file_location }}{{ nginx_conf_template.out_file_name }}.orig"
1212
remote_src: true
1313
when: nginx_conf_template_enable
14+
changed_when: false
1415

1516
- name: "Dynamically Generate NGINX App Protect security policy file"
1617
template:
@@ -56,4 +57,4 @@
5657
debug:
5758
msg: "trigger nginx reload if needed"
5859
notify: "(Handler: All OSs) Restart NGINX"
59-
changed_when: app_protect_security_policy_template_enable or app_protect_log_policy_template_enable or nginx_conf_template_enable
60+
when: app_protect_security_policy_template_enable or app_protect_log_policy_template_enable or nginx_conf_template_enable

tasks/prerequisites/setup-selinux.yml renamed to tasks/configure-selinux.yml

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@
44
name: policycoreutils-python, setools
55
state: present
66

7+
- name: "(Install: SELinux) Check for SELinux enabled"
8+
debug:
9+
msg: "You need to enable selinux, if it was disabled you need to reboot"
10+
when: ansible_selinux is undefined
11+
712
- name: "(Install: SELinux) Permissive SELinux"
813
selinux:
914
state: permissive
1015
policy: targeted
11-
when: app_protect_selinux
16+
changed_when: false
17+
when: ansible_selinux.mode == "enforcing"
1218

1319
- name: "(Install: SELinux: Booleans) Allow HTTP network connection"
1420
seboolean:
@@ -84,21 +90,46 @@
8490

8591
- name: "(Install: SELinux: Contexts) Apply contexts to opt"
8692
command: restorecon -iRv /opt/app_protect
87-
93+
changed_when: false
94+
95+
- name: "(Install: SELinux: Contexts) Apply permissions to opt/config"
96+
file:
97+
path: /opt/app_protect/config
98+
owner: nginx
99+
group: nginx
100+
mode: u=rwx,go=rx,g+s
101+
state: directory
102+
88103
- name: "(Install: SELinux: Contexts) Apply contexts to log"
89104
command: restorecon -iRv /var/log/app_protect
105+
changed_when: false
106+
107+
- name: "(Install: SELinux: Module) Create NGINX Plus App Protect Module"
108+
template:
109+
src: nginx-plus-module-appprotect.te.j2
110+
dest: "{{ app_protect_tempdir }}/nginx-plus-module-appprotect.te"
111+
register: app_protect_module
112+
113+
- name: "(Install: SELinux: Module) Check NGINX Plus App Protect Module"
114+
command: "checkmodule -M -m -o {{ app_protect_tempdir }}/nginx-plus-module-appprotect.mod {{ app_protect_tempdir }}/nginx-plus-module-appprotect.te"
115+
args:
116+
creates: "{{ app_protect_tempdir }}/nginx-plus-module-appprotect.mod"
117+
changed_when: false
90118

91-
- name: "(Install: SELinux: Custom) Generate policy"
92-
shell:
93-
cmd: cat /var/log/audit/audit.log | audit2allow -M local
94-
chdir: /tmp/
119+
- name: "(Install: SELinux: Module) Compile NGINX Plus App Protect Module"
120+
command: "semodule_package -o {{ app_protect_tempdir }}/nginx-plus-module-appprotect.pp -m {{ app_protect_tempdir }}/nginx-plus-module-appprotect.mod"
95121
args:
96-
executable: /bin/bash
122+
creates: "{{ app_protect_tempdir }}/nginx-plus-module-appprotect.pp"
123+
changed_when: false
97124

98-
- name: "(Install: SELinux: Custom) Apply local policy"
99-
command: semodule -i /tmp/local.pp
125+
- name: "(Install: SELinux: Module) Import NGINX Plus App Protect Module" # noqa 503
126+
command: "semodule -i {{ app_protect_tempdir }}/nginx-plus-module-appprotect.pp"
127+
changed_when: false
128+
when: app_protect_module.changed
100129

101130
- name: "(Install: SELinux) Enforce SELinux"
102131
selinux:
103132
state: enforcing
104133
policy: targeted
134+
changed_when: false
135+
when: app_protect_selinux_enforcing and ansible_selinux.mode == "permissive"

tasks/install-app-protect.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,21 @@
2121
success_msg: "'nginx_plus_version' is {{ (nginx_plus_version is defined) | ternary(nginx_plus_version, 'NONE') }}"
2222
quiet: true
2323

24+
- name: "(Install: Linux) Create override for NGINX Plus service"
25+
file:
26+
path: /etc/systemd/system/nginx.service.d
27+
state: directory
28+
mode: '0755'
29+
when: nginx_timeout is defined
30+
31+
- name: "(Install: Linux) Increase timeout for NGINX Plus Service"
32+
template:
33+
src: nginx.service.override.conf.j2
34+
dest: /etc/systemd/system/nginx.service.d/override.conf
35+
owner: root
36+
group: root
37+
mode: '0644'
38+
when: nginx_timeout is defined
39+
2440
- name: "(Install: Linux) Install NGINX Plus"
2541
import_tasks: install-app-protect-linux.yml

tasks/main.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
- name: "Install NGINX App Protect"
3535
block:
3636

37-
- include_tasks: prerequisites/install-prerequisites.yml
37+
- import_tasks: "prerequisites/install-prerequisites.yml"
3838
tags: nginx_prerequisites
3939

4040
- import_tasks: keys/apt-key.yml
@@ -72,6 +72,12 @@
7272

7373
when: app_protect_state != "absent"
7474

75+
- name: "(Install: CentOS) Setup SELinux"
76+
include_tasks: "{{ role_path }}/tasks/configure-selinux.yml"
77+
when:
78+
- app_protect_selinux
79+
- ansible_os_family == "RedHat"
80+
7581
- name: "Remove NGINX App Protect"
7682
block:
7783

tasks/prerequisites/setup-centos.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,3 @@
33
package:
44
name: ca-certificates, epel-release
55
state: present
6-
7-
- name: "(Install: CentOS) Setup SELinux"
8-
import_tasks: setup-selinux.yml
9-
when: app_protect_selinux
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
module nginx-plus-module-appprotect 1.0;
2+
3+
require {
4+
type faillog_t;
5+
type httpd_t;
6+
type httpd_initrc_exec_t;
7+
type httpd_log_t;
8+
type http_cache_port_t;
9+
type httpd_config_t;
10+
type httpd_var_run_t;
11+
type lastlog_t;
12+
type initrc_t;
13+
type usr_t;
14+
type security_t;
15+
type shadow_t;
16+
type systemd_logind_t;
17+
type systemd_logind_sessions_t;
18+
type unreserved_port_t;
19+
type var_log_t;
20+
type var_run_t;
21+
22+
class capability { audit_write net_admin };
23+
class dbus send_msg;
24+
class dir { add_name create remove_name write };
25+
class fifo_file { getattr ioctl open read write };
26+
class file { create execute getattr read rename open setattr unlink write};
27+
class netlink_selinux_socket { create bind };
28+
class netlink_audit_socket { create nlmsg_relay read write };
29+
class passwd passwd;
30+
class security compute_av;
31+
class sock_file write;
32+
class tcp_socket name_connect;
33+
class unix_stream_socket connectto;
34+
}
35+
36+
#============= httpd_t ==============
37+
allow httpd_t httpd_log_t:file write;
38+
allow httpd_t httpd_config_t:file write;
39+
allow httpd_t http_cache_port_t:tcp_socket name_connect;
40+
allow httpd_t httpd_var_run_t:file execute;
41+
42+
allow httpd_t httpd_initrc_exec_t:fifo_file { getattr ioctl open read write };
43+
44+
allow httpd_t lastlog_t:file { open read write };
45+
46+
allow httpd_t faillog_t:file { write read open };
47+
48+
allow httpd_t initrc_t:unix_stream_socket connectto;
49+
50+
allow httpd_t unreserved_port_t:tcp_socket name_connect;
51+
52+
allow httpd_t usr_t:dir { add_name create remove_name write };
53+
allow httpd_t usr_t:file { create rename setattr unlink write };
54+
allow httpd_t usr_t:sock_file write;
55+
56+
57+
allow httpd_t security_t:security compute_av;
58+
59+
allow httpd_t self:netlink_selinux_socket {create bind };
60+
allow httpd_t self:netlink_audit_socket { create nlmsg_relay read write };
61+
allow httpd_t self:passwd passwd;
62+
allow httpd_t self:capability { audit_write net_admin };
63+
64+
allow httpd_t shadow_t:file { read write open getattr };
65+
66+
allow httpd_t systemd_logind_sessions_t:fifo_file write;
67+
allow httpd_t systemd_logind_t:dbus send_msg;
68+
69+
allow httpd_t var_log_t:file { open read };
70+
allow httpd_t var_run_t:file { read write };
71+
72+
#============= systemd_logind_t ==============
73+
allow systemd_logind_t httpd_t:dbus send_msg;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[Service]
2+
# Override default 90 second timeout
3+
TimeoutStopSec={{ nginx_timeout }}

0 commit comments

Comments
 (0)