Skip to content

Commit

Permalink
INF-176: Load iptables rules through a file.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablo committed Apr 2, 2020
1 parent 52b9683 commit 9b213f5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 46 deletions.
58 changes: 12 additions & 46 deletions tasks/nat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,50 +22,16 @@
net.ipv6.conf.all.disable_ipv6: 1
net.ipv6.conf.default.disable_ipv6: 1

# FIXME idempotence test fails obviously cause of these rules.
# FIXME flush or not to flush?
- name: Flush input rules
iptables:
chain: INPUT
flush: yes
notify: persist iptables
- name: Upload firewall rules
template:
src: iptables-rules.j2
dest: /tmp/iptables-rules
mode: '0444'

- name: Flush forward rules
iptables:
chain: FORWARD
flush: yes
notify: persist iptables

- name: Flush nat rules
iptables:
table: nat
chain: POSTROUTING
flush: yes
notify: persist iptables

- name: Adds forward rules
iptables:
action: append
chain: FORWARD
source: "{{ item }}"
jump: ACCEPT
notify: persist iptables
loop: "{{ vpn_gateway_configs[0].local.networks }} + {{ vpn_gateway_configs[0].remote.networks }}"

- name: Reject the rest
iptables:
action: append
chain: FORWARD
jump: REJECT
notify: persist iptables

- name: Add NAT rules
iptables:
table: nat
action: append
chain: POSTROUTING
source: "{{ item }}"
#out_interface: "{{ wan_interface }}"
jump: MASQUERADE
notify: persist iptables
loop: "{{ vpn_gateway_configs[0].local.networks }} + {{ vpn_gateway_configs[0].remote.networks }}"
- name: Load firewall rules
shell: |
set -o pipefail
cat /tmp/iptables-rules | iptables-restore
args:
executable: /bin/bash
changed_when: False
28 changes: 28 additions & 0 deletions templates/iptables-rules.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by iptables-save v1.6.1 on Tue Mar 24 10:02:12 2020
*nat
:PREROUTING ACCEPT [4:160]
:INPUT ACCEPT [4:160]
:OUTPUT ACCEPT [1:76]
:POSTROUTING ACCEPT [1:76]
{% for network in vpn_gateway_configs[0].local.networks %}
-A POSTROUTING -s "{{ network }}" -j MASQUERADE
{% endfor %}
{% for network in vpn_gateway_configs[0].remote.networks %}
-A POSTROUTING -s "{{ network }}" -j MASQUERADE
{% endfor %}
COMMIT
# Completed on Tue Mar 24 10:02:12 2020
# Generated by iptables-save v1.6.1 on Tue Mar 24 10:02:12 2020
*filter
:INPUT ACCEPT [106:6884]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [83:14956]
{% for network in vpn_gateway_configs[0].local.networks %}
-A FORWARD -s "{{ network }}" -j ACCEPT
{% endfor %}
{% for network in vpn_gateway_configs[0].remote.networks %}
-A FORWARD -s "{{ network }}" -j ACCEPT
{% endfor %}
-A FORWARD -j REJECT --reject-with icmp-port-unreachable
COMMIT
# Completed on Tue Mar 24 10:02:12 2020

0 comments on commit 9b213f5

Please sign in to comment.