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

simplerule: Add support for incoming interface filtering #221

Merged
merged 1 commit into from
Dec 19, 2023
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
9 changes: 9 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2264,6 +2264,7 @@ The following parameters are available in the `nftables::simplerule` defined typ
* [`sport`](#-nftables--simplerule--sport)
* [`saddr`](#-nftables--simplerule--saddr)
* [`counter`](#-nftables--simplerule--counter)
* [`iifname`](#-nftables--simplerule--iifname)

##### <a name="-nftables--simplerule--ensure"></a>`ensure`

Expand Down Expand Up @@ -2378,6 +2379,14 @@ Enable traffic counters for the matched traffic.

Default value: `false`

##### <a name="-nftables--simplerule--iifname"></a>`iifname`

Data type: `Optional[String[1]]`

Optional filter for the incoming interface

Default value: `undef`

## Data types

### <a name="Nftables--Addr"></a>`Nftables::Addr`
Expand Down
5 changes: 5 additions & 0 deletions manifests/simplerule.pp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
#
# @param counter
# Enable traffic counters for the matched traffic.
#
# @param iifname
# Optional filter for the incoming interface
define nftables::simplerule (
Enum['present','absent'] $ensure = 'present',
Nftables::SimpleRuleName $rulename = $title,
Expand All @@ -67,6 +70,7 @@
Optional[Nftables::Port] $sport = undef,
Optional[Nftables::Addr] $saddr = undef,
Boolean $counter = false,
Optional[String[1]] $iifname = undef,
) {
if $dport and !$proto {
fail('Specifying a transport protocol via $proto is mandatory when passing a $dport')
Expand All @@ -89,6 +93,7 @@
'saddr' => $saddr,
'set_type' => $set_type,
'sport' => $sport,
'iifname' => $iifname,
}
),
order => $order,
Expand Down
58 changes: 58 additions & 0 deletions spec/acceptance/simple_rule_iifname_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

require 'spec_helper_acceptance'

describe 'nftables class' do
context 'configure a simple rule with input interface' do
it_behaves_like 'an idempotent resource' do
let(:manifest) do
<<-EOS
# default mask of firewalld service fails if service is not installed.
# https://tickets.puppetlabs.com/browse/PUP-10814
# Disable all default rules and include below explicitly
class { 'nftables':
firewalld_enable => false,
out_ntp => false,
out_http => false,
out_https => false,
out_icmp => false,
in_ssh => false,
in_icmp => false,
}
nftables::simplerule { 'dummyrule_in':
action => 'accept',
iifname => $facts['networking']['primary'],
comment => 'allow some multicast stuff',
daddr => 'ff02::fb',
}
include nftables::rules::ssh
include nftables::rules::out::dns
include nftables::rules::out::ssh
$config_path = $facts['os']['family'] ? {
'Archlinux' => '/etc/nftables.conf',
'Debian' => '/etc/nftables.conf',
default => '/etc/sysconfig/nftables.conf',
}
$nft_path = $facts['os']['family'] ? {
'Archlinux' => '/usr/bin/nft',
default => '/usr/sbin/nft',
}
# nftables cannot be started in docker so replace service with a validation only.
systemd::dropin_file{"zzz_docker_nft.conf":
ensure => present,
unit => "nftables.service",
content => [
"[Service]",
"ExecStart=",
"ExecStart=${nft_path} -c -I /etc/nftables/puppet -f ${config_path}",
"ExecReload=",
"ExecReload=${nft_path} -c -I /etc/nftables/puppet -f ${config_path}",
"",
].join("\n"),
notify => Service["nftables"],
}
EOS
end
end
end
end
8 changes: 7 additions & 1 deletion templates/simplerule.epp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Optional[Nftables::Addr] $saddr,
String $set_type,
Optional[Nftables::Port] $sport,
Optional[String[1]] $iifname,
| -%>
<%- if $proto {
$_proto = $proto ? {
Expand Down Expand Up @@ -69,4 +70,9 @@
} else {
$_counter = undef
} -%>
<%= regsubst(strip([$_ip_version_filter, $_src_port, $_dst_port, $_src_hosts, $_dst_hosts, $_counter, $action, $_comment].join(' ')), '\s+', ' ', 'G') -%>
<%- if $iifname {
$_iifname = "iifname \"${iifname}\""
} else {
$_iifname = undef
} -%>
<%= regsubst(strip([$_ip_version_filter, $_iifname, $_src_port, $_dst_port, $_src_hosts, $_dst_hosts, $_counter, $action, $_comment].join(' ')), '\s+', ' ', 'G') -%>
Loading