Skip to content

Commit e6d5ab3

Browse files
arcsectorjewnix
andauthored
Simple Firewall Configuration (splunk#157)
* Firewall changes: - Firewalld Services instead of ephemeral commands - New port format to specify protocol for modularity - Defaults to UFW and if RHEL uses firewalld - Added default firewall ports per group var - Checks to see if firewall package is installed and service is running and enabled * Handlers for firewalls and merge into single yml * Firewall change requests - port proto combos predefined and referenced - super user privileges for handlers and package interactions * Firewall changes after tests - SSH Allow in UFW - Make Firewalld aware of service - Removing unnecessary reload handle of UFW - Adding RHEL 8 firewall_service * replace with firewalld in name for task running only for firewalld * Adding comments and desc to port dictionary Co-authored-by: David Twersky <jewunix@gmail.com>
1 parent 67114f3 commit e6d5ab3

File tree

12 files changed

+117
-0
lines changed

12 files changed

+117
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
splunk_firewall_ports:
3+
- "{{ splunkweb_port }}"
4+
- "{{ splunkapi_port }}"
5+
- "{{ splunktcpin_port }}"
6+
- "{{ splunkhec_port }}"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
splunk_firewall_ports:
3+
- "{{ splunkweb_port }}"
4+
- "{{ splunkapi_port }}"
5+
- "{{ splunktcpin_port }}"
6+
- "{{ splunkhec_port }}"
7+
- "{{ splunkidxcrep_port }}"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
splunk_firewall_ports:
3+
- "{{ splunkweb_port }}"
4+
- "{{ splunkapi_port }}"
5+
- "{{ splunkshcrep_port }}"

roles/splunk/defaults/main.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,16 @@ add_pstack_script: false # Set to true to install a pstack generation script for
7575
configure_dmesg: false
7676
install_utilities: false # Set to true to install the list of packages defined in the linux_packages var after installing splunk
7777
use_tuned_thp: false
78+
# Firewall configs
79+
configure_firewall: false # Whether or not to configure the firewall service on your machine, if set to true, opens firewall ports using UFW (default) or Firewalld depending on OS
80+
splunk_firewall_service: splunk # The name of the Splunk firewall service to install for firewalld
81+
# Firewall port presets - reference these in group_vars to assign them to splunk
82+
splunkweb_port: {desc: "Splunk Web", protocol: "tcp", number: 8000}
83+
splunkhec_port: {desc: "Splunk HEC", protocol: "tcp", number: 8088}
84+
splunktcpin_port: {desc: "Splunk TCPIN", protocol: "tcp", number: 9997}
85+
splunkapi_port: {desc: "Splunk API", protocol: "tcp", number: "{{ splunkd_port }}"}
86+
splunkidxcrep_port: {desc: "Splunk Indexer Clustering Replication", protocol: "tcp", number: "{{ splunk_idxc_rep_port }}"}
87+
splunkshcrep_port: {desc: "Splunk Search Head Clustering Replication", protocol: "tcp", number: "{{ splunk_shc_rep_port }}"}
88+
splunk_firewall_ports: # List of ports to allow through local firewall in dict form
89+
- "{{ splunkweb_port }}"
90+
- "{{ splunkapi_port }}"

roles/splunk/handlers/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,7 @@
8989
port: "{{ splunkd_port }}"
9090
state: started
9191
delay: 5
92+
93+
- name: reload firewalld
94+
command: firewall-cmd --reload
95+
become: true
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
- name: Ensure {{ firewall_service }} package is installed
3+
ansible.builtin.package:
4+
name: "{{ firewall_service }}"
5+
state: present
6+
become: true
7+
8+
- name: Configure firewalld for Splunk
9+
block:
10+
- name: Ensure firewalld is Started and Enabled
11+
ansible.builtin.systemd:
12+
name: "{{ firewall_service }}"
13+
state: started
14+
enabled: true
15+
become: true
16+
17+
- name: Add splunk firewalld service
18+
ansible.builtin.template:
19+
src: firewalld_service.xml.j2
20+
dest: /etc/firewalld/services/{{ splunk_firewall_service }}.xml
21+
backup: true
22+
mode: 0644
23+
owner: root
24+
group: root
25+
become: true
26+
register: firewalld
27+
28+
- name: reload firewalld
29+
command: firewall-cmd --reload
30+
become: true
31+
when: firewalld.changed
32+
33+
- name: Activate splunk firewalld service
34+
ansible.posix.firewalld:
35+
service: "{{ splunk_firewall_service }}"
36+
permanent: true
37+
state: enabled
38+
immediate: true
39+
notify: reload firewalld
40+
become: true
41+
when: firewall_service == "firewalld"
42+
43+
- name: Configure UFW for Splunk
44+
block:
45+
- name: Ensure SSH is enabled
46+
community.general.ufw:
47+
port: 22
48+
proto: tcp
49+
rule: allow
50+
state: enabled
51+
become: true
52+
53+
- name: Add splunk port to UFW
54+
community.general.ufw:
55+
port: "{{ item.number }}"
56+
proto: "{{ item.protocol }}"
57+
rule: allow
58+
state: reloaded
59+
comment: "{{ item.desc | default('') }}"
60+
become: true
61+
loop: "{{ splunk_firewall_ports }}"
62+
when: firewall_service == "ufw"

roles/splunk/tasks/configure_os.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,10 @@
2020
- name: Enable read for dmesg
2121
include_tasks: configure_dmesg.yml
2222
when: configure_dmesg
23+
24+
- name: Configure firewall service
25+
include_tasks: "configure_firewall.yml"
26+
when:
27+
- firewall_service != 'undefined'
28+
- configure_firewall != false
29+
- "'full' in group_names"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<service>
3+
<short>splunk</short>
4+
<description>Ports to be configured for splunk</description>
5+
{% for port in splunk_firewall_ports %}
6+
<!-- {{ port.desc | default('') }} -->
7+
<port protocol="{{ port.protocol }}" port="{{ port.number }}"/>
8+
{% endfor %}
9+
</service>

roles/splunk/vars/Debian.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ linux_packages:
1414
- nethogs
1515
- gdb
1616
- dnsutils
17+
firewall_service: ufw

roles/splunk/vars/RedHat.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ linux_packages:
1717
- nethogs
1818
- gdb
1919
- bind-utils
20+
firewall_service: firewalld

0 commit comments

Comments
 (0)