Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firewall #17

Merged
merged 3 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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