Skip to content

Commit

Permalink
Merge pull request #17 from ehh-why-its-so-hard/firewall
Browse files Browse the repository at this point in the history
Firewall
  • Loading branch information
daniel1302 authored Oct 27, 2024
2 parents bd5df72 + 674062f commit 0b14cb1
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions .ansible-lint
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ mock_modules:
- community.postgresql.postgresql_user
- community.postgresql.postgresql_owner
- community.docker.docker_image
- community.general.iptables_state
3 changes: 3 additions & 0 deletions roles/firewall/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
firewall_open_tcp: []
firewall_open_udp: []
36 changes: 36 additions & 0 deletions roles/firewall/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
# How it works?
# OUTPUT: allowed everywhere
# INPUT: blocked all except 22 and specific ports

- name: Uninstall unsupported firewalls
ansible.builtin.apt:
pkg:
- ufw
- firewalld
state: absent

- name: Install iptables
ansible.builtin.apt:
pkg:
- iptables
- iptables-persistent
state: present

- name: Template restore file
ansible.builtin.template:
src: "etc/iptables-restore.apply"
dest: "/etc/iptables-restore.apply"
owner: "root"
group: "root"
mode: "0644"
register: iptables_restore_file

- name: Restore firewall state from a file
community.general.iptables_state:
state: restored
path: /etc/iptables-restore.apply
noflush: false
async: "{{ ansible_timeout }}"
poll: 0
when: iptables_restore_file.changed # noqa: no-handler
21 changes: 21 additions & 0 deletions roles/firewall/templates/etc/iptables-restore.apply
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
*filter
:INPUT DROP [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:VEGATCP - [0:0]
:VEGAUDP - [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -p udp -m udp --dport 53 -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -j VEGATCP
-A INPUT -p tcp -j VEGAUDP

{% for port in firewall_open_tcp %}
-A VEGATCP -p tcp -m tcp --dport {{ port|int }} -j ACCEPT
{% endfor %}

{% for port in firewall_open_udp %}
-A VEGAUDP -p udp -m udp --dport {{ port|int }} -j ACCEPT
{% endfor %}
COMMIT
File renamed without changes.

0 comments on commit 0b14cb1

Please sign in to comment.