Skip to content

Commit 16c37f9

Browse files
committedMar 27, 2018
The flush rules are now configurable. Closes mikegleasonjr#29
1 parent 6b19d51 commit 16c37f9

File tree

5 files changed

+64
-24
lines changed

5 files changed

+64
-24
lines changed
 

‎README.md

+22-6
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ After I found out `UFW` was too limited in terms of functionalities, I tried sev
1515

1616
This role is an attempt to solve these requirements.
1717

18-
It supports **ipv4** and **ipv6*** on Debian and RedHat distributions.
19-
20-
*ipv6 support was brought up thanks to [@maloddon](https://github.com/maloddon). It is currently in early stages and knowledgable people should review the [default rules](https://github.com/mikegleasonjr/ansible-role-firewall/blob/master/defaults/main.yml). ipv6 rules are not configured by default. If you which to use them, don't forget to set `firewall_v6_configure` to `true`.
18+
It supports **ipv4** and **ipv6*** on Debian and RedHat distributions. ipv6 rules are not configured by default. If you which to use them, don't forget to set `firewall_v6_configure` to `true`.
2119

2220
Requirements
2321
------------
@@ -36,9 +34,19 @@ Role Variables
3634
`defaults/main.yml`:
3735

3836
```
37+
---
3938
firewall_v4_configure: true
4039
firewall_v6_configure: false
4140
41+
firewall_v4_flush_rules:
42+
- -F
43+
- -X
44+
- -t raw -F
45+
- -t raw -X
46+
- -t nat -F
47+
- -t nat -X
48+
- -t mangle -F
49+
- -t mangle -X
4250
firewall_v4_default_rules:
4351
001 default policies:
4452
- -P INPUT ACCEPT
@@ -58,6 +66,15 @@ firewall_v4_default_rules:
5866
firewall_v4_group_rules: {}
5967
firewall_v4_host_rules: {}
6068
69+
firewall_v6_flush_rules:
70+
- -F
71+
- -X
72+
- -t raw -F
73+
- -t raw -X
74+
- -t nat -F
75+
- -t nat -X
76+
- -t mangle -F
77+
- -t mangle -X
6178
firewall_v6_default_rules:
6279
001 default policies:
6380
- -P INPUT ACCEPT
@@ -76,18 +93,17 @@ firewall_v6_default_rules:
7693
- -P INPUT DROP
7794
firewall_v6_group_rules: {}
7895
firewall_v6_host_rules: {}
79-
8096
```
8197

82-
The keys to the `*_rules` dictionaries (`001 default policies`, `002 allow loopback`, ...) can be anything. They are only used for rules **ordering** and **overriding**. On rules generation, the keys are sorted alphabetically. That's why I chose here the 001s and 999s.
98+
The keys to the `*_rules` dictionaries, except the flush rules, can be anything. They are only used for rules **ordering** and **overriding**. On rules generation, the keys are sorted alphabetically. That's why I chose here the 001s and 999s.
8399

84100
Those defaults will generate the following script to be executed on the host (for ipv4):
85101

86102
```
87103
#!/bin/sh
88104
# Ansible managed: <redacted>
89105
90-
# flush rules & delete user-defined chains
106+
# flush rules
91107
iptables -F
92108
iptables -X
93109
iptables -t raw -F

‎defaults/main.yml

+18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
firewall_v4_configure: true
33
firewall_v6_configure: false
44

5+
firewall_v4_flush_rules:
6+
- -F
7+
- -X
8+
- -t raw -F
9+
- -t raw -X
10+
- -t nat -F
11+
- -t nat -X
12+
- -t mangle -F
13+
- -t mangle -X
514
firewall_v4_default_rules:
615
001 default policies:
716
- -P INPUT ACCEPT
@@ -21,6 +30,15 @@ firewall_v4_default_rules:
2130
firewall_v4_group_rules: {}
2231
firewall_v4_host_rules: {}
2332

33+
firewall_v6_flush_rules:
34+
- -F
35+
- -X
36+
- -t raw -F
37+
- -t raw -X
38+
- -t nat -F
39+
- -t nat -X
40+
- -t mangle -F
41+
- -t mangle -X
2442
firewall_v6_default_rules:
2543
001 default policies:
2644
- -P INPUT ACCEPT

‎templates/generated.v4.j2

+4-9
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,10 @@
44
{% set _ = merged.update(firewall_v4_group_rules) %}
55
{% set _ = merged.update(firewall_v4_host_rules) %}
66

7-
# flush rules & delete user-defined chains
8-
iptables -F
9-
iptables -X
10-
iptables -t raw -F
11-
iptables -t raw -X
12-
iptables -t nat -F
13-
iptables -t nat -X
14-
iptables -t mangle -F
15-
iptables -t mangle -X
7+
# flush rules
8+
{% for rule in firewall_v4_flush_rules %}
9+
iptables {{ rule }}
10+
{% endfor %}
1611

1712
{% for group, rules in merged|dictsort %}
1813
# {{ group }}

‎templates/generated.v6.j2

+4-9
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,10 @@
44
{% set _ = merged.update(firewall_v6_group_rules) %}
55
{% set _ = merged.update(firewall_v6_host_rules) %}
66

7-
# flush rules & delete user-defined chains
8-
ip6tables -F
9-
ip6tables -X
10-
ip6tables -t raw -F
11-
ip6tables -t raw -X
12-
ip6tables -t nat -F
13-
ip6tables -t nat -X
14-
ip6tables -t mangle -F
15-
ip6tables -t mangle -X
7+
# flush rules
8+
{% for rule in firewall_v6_flush_rules %}
9+
ip6tables {{ rule }}
10+
{% endfor %}
1611

1712
{% for group, rules in merged|dictsort %}
1813
# {{ group }}

‎tests.yml

+16
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
- role: '{{playbook_dir}}'
77
firewall_v6_configure: true
88

9+
firewall_v4_flush_rules:
10+
- -F
11+
- -X
12+
- -t raw -F
13+
- -t raw -X
14+
- -t mangle -F
15+
- -t mangle -X
16+
917
firewall_v4_group_rules:
1018
400 allow http:
1119
- -A INPUT -p tcp --dport http -j ACCEPT
@@ -14,6 +22,14 @@
1422
firewall_v4_host_rules:
1523
400 allow 7890: []
1624

25+
firewall_v6_flush_rules:
26+
- -F
27+
- -X
28+
- -t raw -F
29+
- -t raw -X
30+
- -t mangle -F
31+
- -t mangle -X
32+
1733
firewall_v6_group_rules:
1834
400 allow http:
1935
- -A INPUT -p tcp --dport http -j ACCEPT

0 commit comments

Comments
 (0)
Please sign in to comment.