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

Configure firewall rules using nftables #1136

Open
wants to merge 15 commits into
base: v2.9.x
Choose a base branch
from
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
FROM python:3.11-bookworm

RUN apt-get update && apt-get install -y wget git libxslt-dev iptables kmod swig
RUN apt-get update && apt-get install -y wget git libxslt-dev iptables kmod swig nftables python3-nftables

RUN mkdir /usr/src/admin
WORKDIR /usr/src/admin

COPY requirements.txt ./
COPY requirements-dev.txt ./

RUN pip3 install --no-cache-dir -r requirements.txt
RUN pip3 install -r requirements.txt

COPY . .

RUN update-alternatives --set iptables /usr/sbin/iptables-legacy && \
update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy

ENV PYTHONPATH="/usr/src/admin"
ENV PYTHONPATH="/usr/src/admin":/usr/lib/python3/dist-packages/

ENV COLUMNS=80
1 change: 1 addition & 0 deletions core/schains/firewall/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from .firewall_manager import SChainFirewallManager # noqa
from .iptables import IptablesController # noqa
from .nftables import NFTablesController # noqa
from .rule_controller import SChainRuleController # noqa
from .types import IRuleController # noqa
from .utils import get_default_rule_controller # noqa
14 changes: 14 additions & 0 deletions core/schains/firewall/firewall_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from typing import Iterable, Optional

from core.schains.firewall.iptables import IptablesController
from core.schains.firewall.nftables import NFTablesController
from core.schains.firewall.types import (
IFirewallManager,
IHostFirewallController,
Expand Down Expand Up @@ -70,6 +71,11 @@
rules_to_remove = actual_rules - expected_rules
self.add_rules(rules_to_add)
self.remove_rules(rules_to_remove)
self.save_rules()

def save_rules(self) -> None:
""" Saves rules into persistent storage """
self.host_controller.save_rules()

def add_rules(self, rules: Iterable[SChainRule]) -> None:
logger.debug('Adding rules %s', rules)
Expand All @@ -88,3 +94,11 @@
class IptablesSChainFirewallManager(SChainFirewallManager):
def create_host_controller(self) -> IptablesController:
return IptablesController()


class NFTSchainFirewallManager(SChainFirewallManager):
def create_host_controller(self) -> NFTablesController:
nc_controller = NFTablesController(chain=self.name)
nc_controller.create_table()
nc_controller.create_chain(self.first_port, self.last_port)
return nc_controller

Check warning on line 104 in core/schains/firewall/firewall_manager.py

View check run for this annotation

Codecov / codecov/patch

core/schains/firewall/firewall_manager.py#L101-L104

Added lines #L101 - L104 were not covered by tests
3 changes: 3 additions & 0 deletions core/schains/firewall/iptables.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,6 @@
@classmethod
def to_ip_network(cls, ip: str) -> str:
return str(ipaddress.ip_network(ip))

def save_rules(self):
raise NotImplementedError('save_rules is not implemented for iptables host controller')

Check warning on line 144 in core/schains/firewall/iptables.py

View check run for this annotation

Codecov / codecov/patch

core/schains/firewall/iptables.py#L144

Added line #L144 was not covered by tests
Loading
Loading