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: Add missing sport for IPv6 with missing destination_addresses #105

Merged
merged 3 commits into from
Jan 1, 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
9 changes: 9 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ The following parameters are available in the `wireguard::interface` defined typ
* [`postup_cmds`](#-wireguard--interface--postup_cmds)
* [`predown_cmds`](#-wireguard--interface--predown_cmds)
* [`postdown_cmds`](#-wireguard--interface--postdown_cmds)
* [`endpoint_port`](#-wireguard--interface--endpoint_port)

##### <a name="-wireguard--interface--interface"></a>`interface`

Expand Down Expand Up @@ -399,6 +400,14 @@ is an array of commands which should run as preup command (only supported by wgq

Default value: `[]`

##### <a name="-wireguard--interface--endpoint_port"></a>`endpoint_port`

Data type: `Optional[Stdlib::Port]`

optional outgoing port from the other endpoint. Will be used for firewalling. If not set, we will try to parse $endpoint

Default value: `undef`

## Data types

### <a name="Wireguard--Peers"></a>`Wireguard::Peers`
Expand Down
29 changes: 17 additions & 12 deletions manifests/interface.pp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
# @param postup_cmds is an array of commands which should run as preup command (only supported by wgquick)
# @param predown_cmds is an array of commands which should run as preup command (only supported by wgquick)
# @param postdown_cmds is an array of commands which should run as preup command (only supported by wgquick)
# @param endpoint_port optional outgoing port from the other endpoint. Will be used for firewalling. If not set, we will try to parse $endpoint
#
# @author Tim Meusel <[email protected]>
# @author Sebastian Rakel <[email protected]>
Expand Down Expand Up @@ -116,28 +117,30 @@
Array[String[1]] $postup_cmds = [],
Array[String[1]] $predown_cmds = [],
Array[String[1]] $postdown_cmds = [],
Optional[Stdlib::Port] $endpoint_port = undef,
) {
include wireguard

if empty($peers) and !$public_key {
warning('peers or public_key have to been set')
}

$_endpoint_port = if $endpoint_port {
$endpoint_port
} elsif ($endpoint and $endpoint =~ /:(\d+)$/) {
Integer($1)
} else {
undef
}
if $manage_firewall {
# ToDo: It would be nice if this would be a parameter
if $endpoint =~ /:(\d+)$/ {
$endpoint_port = Integer($1)
} else {
$endpoint_port = undef
}
$source_addresses.each |$index1, $saddr| {
if $saddr =~ Stdlib::IP::Address::V4 {
if empty($destination_addresses) {
nftables::simplerule { "allow_in_wg_${interface}-${index1}":
action => 'accept',
comment => "Allow traffic from interface ${input_interface} from IP ${saddr} for wireguard tunnel ${interface}",
dport => $dport,
sport => $endpoint_port,
sport => $_endpoint_port,
proto => 'udp',
saddr => $saddr,
iifname => $input_interface,
Expand All @@ -146,7 +149,7 @@
nftables::simplerule { "allow_out_wg_${interface}-${index1}":
action => 'accept',
comment => "Allow traffic out interface ${input_interface} to IP ${saddr} for wireguard tunnel ${interface}",
dport => $endpoint_port,
dport => $_endpoint_port,
sport => $dport,
proto => 'udp',
daddr => $saddr,
Expand All @@ -161,7 +164,7 @@
action => 'accept',
comment => "Allow traffic from interface ${input_interface} from IP ${saddr} for wireguard tunnel ${interface}",
dport => $dport,
sport => $endpoint_port,
sport => $_endpoint_port,
proto => 'udp',
daddr => $_daddr,
saddr => $saddr,
Expand All @@ -171,7 +174,7 @@
nftables::simplerule { "allow_out_wg_${interface}-${index1}${index2}":
action => 'accept',
comment => "Allow traffic out interface ${input_interface} to IP ${saddr} for wireguard tunnel ${interface}",
dport => $endpoint_port,
dport => $_endpoint_port,
sport => $dport,
proto => 'udp',
daddr => $saddr,
Expand All @@ -189,6 +192,7 @@
action => 'accept',
comment => "Allow traffic from interface ${input_interface} from IP ${saddr} for wireguard tunnel ${interface}",
dport => $dport,
sport => $_endpoint_port,
proto => 'udp',
saddr => $saddr,
iifname => $input_interface,
Expand All @@ -197,7 +201,7 @@
nftables::simplerule { "allow_out_wg_${interface}-${index1}":
action => 'accept',
comment => "Allow traffic out interface ${input_interface} to IP ${saddr} for wireguard tunnel ${interface}",
dport => $endpoint_port,
dport => $_endpoint_port,
sport => $dport,
proto => 'udp',
daddr => $saddr,
Expand All @@ -212,6 +216,7 @@
action => 'accept',
comment => "Allow traffic from interface ${input_interface} from IP ${saddr} for wireguard tunnel ${interface}",
dport => $dport,
sport => $_endpoint_port,
proto => 'udp',
daddr => $_daddr,
saddr => $saddr,
Expand All @@ -221,7 +226,7 @@
nftables::simplerule { "allow_out_wg_${interface}-${index1}${index2}":
action => 'accept',
comment => "Allow traffic out interface ${input_interface} to IP ${saddr} for wireguard tunnel ${interface}",
dport => $endpoint_port,
dport => $_endpoint_port,
sport => $dport,
proto => 'udp',
daddr => $saddr,
Expand Down
113 changes: 103 additions & 10 deletions spec/defines/interface_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@
os_facts
end

context 'with only default values and manage_firewall=true it wont work' do
let :params do
{
manage_firewall: true
}
end

it { is_expected.not_to compile }
end

context 'with only default values and manage_firewall=false it wont work' do
let :params do
{
Expand Down Expand Up @@ -546,6 +536,109 @@
it { is_expected.to contain_nftables__simplerule('allow_out_wg_as1234-0') }
it { is_expected.to contain_nftables__simplerule('allow_out_wg_as1234-1') }
end

# Usually we parse the src port for incoming packets / the dst port for outgoing packets from the $endpoint param
# The param is optional, in case you want to create a passive endpoint for clients with dynamic ip addresses
# In those cases we still need to create firewall rules, but without src port for incoming packets / the dst port
# To make this all a bit easier, we also added a new parameter, $endpoint_port, which takes precedence over parsing $endpoint
# dport for incoming / sport for outgoing packets is parsed from $title.
context 'verifies src port for incoming packets / the dst port for outgoing packets' do
# check that catalog compiles and we create rules, but without the port
context 'without endpoint_port without endpoint' do
let :pre_condition do
'class {"systemd":
manage_networkd => true
}'
end
let :params do
{
public_key: 'blabla==',
manage_firewall: true,
destination_addresses: [],
addresses: [{ 'Address' => '192.0.2.1/24' }],
source_addresses: ['fe80::1', '127.0.0.1'],
}
end

it { is_expected.to compile }
it { is_expected.to contain_nftables__simplerule('allow_in_wg_as1234-0').without_sport.with_dport(1234) }
it { is_expected.to contain_nftables__simplerule('allow_in_wg_as1234-1').without_sport.with_dport(1234) }
it { is_expected.to contain_nftables__simplerule('allow_out_wg_as1234-0').without_dport.with_sport(1234) }
it { is_expected.to contain_nftables__simplerule('allow_out_wg_as1234-1').without_dport.with_sport(1234) }
end

context 'with endpoint_port' do
let :pre_condition do
'class {"systemd":
manage_networkd => true
}'
end
let :params do
{
public_key: 'blabla==',
manage_firewall: true,
destination_addresses: [],
addresses: [{ 'Address' => '192.0.2.1/24' }],
source_addresses: ['fe80::1', '127.0.0.1'],
endpoint_port: 5678,
}
end

it { is_expected.to compile }
it { is_expected.to contain_nftables__simplerule('allow_in_wg_as1234-0').with_sport(5678).with_dport(1234) }
it { is_expected.to contain_nftables__simplerule('allow_in_wg_as1234-1').with_sport(5678).with_dport(1234) }
it { is_expected.to contain_nftables__simplerule('allow_out_wg_as1234-0').with_dport(5678).with_sport(1234) }
it { is_expected.to contain_nftables__simplerule('allow_out_wg_as1234-1').with_dport(5678).with_sport(1234) }
end

context 'with endpoint' do
let :pre_condition do
'class {"systemd":
manage_networkd => true
}'
end
let :params do
{
public_key: 'blabla==',
manage_firewall: true,
destination_addresses: [],
addresses: [{ 'Address' => '192.0.2.1/24' }],
source_addresses: ['fe80::1', '127.0.0.1'],
endpoint: 'foo.example.com:5678',
}
end

it { is_expected.to compile }
it { is_expected.to contain_nftables__simplerule('allow_in_wg_as1234-0').with_sport(5678).with_dport(1234) }
it { is_expected.to contain_nftables__simplerule('allow_in_wg_as1234-1').with_sport(5678).with_dport(1234) }
it { is_expected.to contain_nftables__simplerule('allow_out_wg_as1234-0').with_dport(5678).with_sport(1234) }
it { is_expected.to contain_nftables__simplerule('allow_out_wg_as1234-1').with_dport(5678).with_sport(1234) }
end

context 'with endpoint and destination_addresses' do
let :pre_condition do
'class {"systemd":
manage_networkd => true
}'
end
let :params do
{
public_key: 'blabla==',
manage_firewall: true,
destination_addresses: ['fe80::2', '1.1.1.1'],
addresses: [{ 'Address' => '192.0.2.1/24' }],
source_addresses: ['fe80::1', '127.0.0.1'],
endpoint: 'foo.example.com:5678',
}
end

it { is_expected.to compile }
it { is_expected.to contain_nftables__simplerule('allow_in_wg_as1234-00').with_sport(5678).with_dport(1234).with_saddr('fe80::1').with_daddr('fe80::2') }
it { is_expected.to contain_nftables__simplerule('allow_in_wg_as1234-11').with_sport(5678).with_dport(1234).with_saddr('127.0.0.1').with_daddr('1.1.1.1') }
it { is_expected.to contain_nftables__simplerule('allow_out_wg_as1234-00').with_dport(5678).with_sport(1234).with_daddr('fe80::1').with_saddr('fe80::2') }
it { is_expected.to contain_nftables__simplerule('allow_out_wg_as1234-11').with_dport(5678).with_sport(1234).with_daddr('127.0.0.1').with_saddr('1.1.1.1') }
end
end
end
end
end