Skip to content

Commit

Permalink
Cleanup nftables chain after schain removal
Browse files Browse the repository at this point in the history
  • Loading branch information
badrogger committed Dec 26, 2024
1 parent 517a3a8 commit 0aacca3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions core/schains/firewall/firewall_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def remove_rules(self, rules: Iterable[SChainRule]) -> None:

def flush(self) -> None:
self.remove_rules(self.rules)
self.host_controller.cleanup()


class IptablesSChainFirewallManager(SChainFirewallManager):
Expand Down
5 changes: 4 additions & 1 deletion core/schains/firewall/iptables.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,8 @@ def from_ip_network(cls, ip: str) -> str:
def to_ip_network(cls, ip: str) -> str:
return str(ipaddress.ip_network(ip))

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

def cleanup(self) -> None:
raise NotImplementedError('cleanup is not implemented for iptables host controller')
22 changes: 22 additions & 0 deletions core/schains/firewall/nftables.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,25 @@ def create_chain(self, first_port: int, last_port: int) -> None:
)
self.add_schain_drop_rule(first_port, last_port)

def delete_chain(self) -> None:
if self.has_chain(self.chain):
logger.info('Removing chain %s', self.chain)
self.run_json_cmd(
self._compose_json(
[
{
'delete': {
'chain': {
'family': self.FAMILY,
'table': self.table,
'name': self.chain
}
}
}
]
)
)

@property
def chains(self) -> list[dict]:
output = self.run_cmd('list chains')
Expand Down Expand Up @@ -334,3 +353,6 @@ def save_rules(self) -> None:
nft_chain_path = os.path.join(NFT_CHAIN_BASE_PATH, f'{self.chain}.conf')
with open(nft_chain_path, 'w') as nft_chain_file:
nft_chain_file.write(chain_rules)

def cleanup(self) -> None:
self.delete_chain()
4 changes: 4 additions & 0 deletions core/schains/firewall/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ def has_rule(self, rule: SChainRule) -> bool: # pragma: no cover
def save_rules(self) -> None: # pragma: no cover
pass

@abstractmethod
def cleanup(self) -> None: # pragma: no cover
pass


class IFirewallManager(ABC):
@property
Expand Down

0 comments on commit 0aacca3

Please sign in to comment.