Révision 2a3b45ec
Add a define for masquerading
manifests/rules/masquerade.pp | ||
---|---|---|
1 |
# masquerade all outgoing traffic |
|
2 |
define nftables::rules::masquerade( |
|
3 |
Pattern[/^[a-zA-Z0-9_]+$/] |
|
4 |
$rulename = $title, |
|
5 |
Pattern[/^\d\d$/] |
|
6 |
$order = '70', |
|
7 |
String[1] |
|
8 |
$chain = 'POSTROUTING', |
|
9 |
Optional[String[1]] |
|
10 |
$oif = undef, |
|
11 |
Optional[String[1]] |
|
12 |
$saddr = undef, |
|
13 |
Optional[String[1]] |
|
14 |
$daddr = undef, |
|
15 |
Optional[Pattern[/^(tcp|udp)$/]] |
|
16 |
$proto = undef, |
|
17 |
Optional[Variant[String,Integer[1,65535]]] |
|
18 |
$dport = undef, |
|
19 |
Enum['present','absent'] |
|
20 |
$ensure = 'present', |
|
21 |
) { |
|
22 |
|
|
23 |
$oifname = $oif ? { |
|
24 |
undef => '', |
|
25 |
default => "oifname ${oif} ", |
|
26 |
} |
|
27 |
$src = $saddr ? { |
|
28 |
undef => '', |
|
29 |
default => "ip saddr ${saddr} ", |
|
30 |
} |
|
31 |
$dst = $daddr ? { |
|
32 |
undef => '', |
|
33 |
default => "ip daddr ${daddr} ", |
|
34 |
} |
|
35 |
|
|
36 |
if $proto and $dport { |
|
37 |
$protocol = '' |
|
38 |
$port = "${proto} dport ${dport} " |
|
39 |
} elsif $proto { |
|
40 |
$protocol = "${proto} " |
|
41 |
$port = '' |
|
42 |
} elsif $dport { |
|
43 |
$protocol = '' |
|
44 |
$port = "tcp dport ${dport} " |
|
45 |
} else { |
|
46 |
$protocol = '' |
|
47 |
$port = '' |
|
48 |
} |
|
49 |
|
|
50 |
nftables::rule{ |
|
51 |
"${chain}-${rulename}": |
|
52 |
ensure => $ensure, |
|
53 |
table => 'ip-nat', |
|
54 |
order => $order, |
|
55 |
content => "${oifname}${src}${dst}${protocol}${port}masquerade"; |
|
56 |
} |
|
57 |
} |
spec/classes/masquerade_spec.rb | ||
---|---|---|
1 |
require 'spec_helper' |
|
2 |
|
|
3 |
describe 'nftables' do |
|
4 |
let(:pre_condition) { 'Exec{path => "/bin"}' } |
|
5 |
|
|
6 |
on_supported_os.each do |os, os_facts| |
|
7 |
context "on #{os}" do |
|
8 |
let(:facts) { os_facts } |
|
9 |
|
|
10 |
context 'with masquerade' do |
|
11 |
let(:pre_condition) do |
|
12 |
""" |
|
13 |
nftables::rules::masquerade{ |
|
14 |
'masquerade_eth0': |
|
15 |
oif => 'eth0'; |
|
16 |
'masquerade_eth1_vpn': |
|
17 |
oif => 'eth1', |
|
18 |
saddr => '192.0.2.0/24'; |
|
19 |
'masquerade_ssh_gitlab': |
|
20 |
saddr => '192.0.2.0/24', |
|
21 |
daddr => '198.51.100.2', |
|
22 |
proto => 'tcp', |
|
23 |
dport => '22'; |
|
24 |
'masquerade_wireguard': |
|
25 |
proto => 'udp', |
|
26 |
dport => '51820'; |
|
27 |
} |
|
28 |
""" |
|
29 |
end |
|
30 |
|
|
31 |
it { is_expected.to compile } |
|
32 |
|
|
33 |
it { is_expected.to contain_concat('nftables-ip-nat-chain-POSTROUTING').with( |
|
34 |
:path => '/etc/nftables/puppet/ip-nat-chain-POSTROUTING.nft', |
|
35 |
:owner => 'root', |
|
36 |
:group => 'root', |
|
37 |
:mode => '0640', |
|
38 |
:ensure_newline => true, |
|
39 |
)} |
|
40 |
it { is_expected.to contain_concat__fragment('nftables-ip-nat-chain-POSTROUTING-header').with( |
|
41 |
:target => 'nftables-ip-nat-chain-POSTROUTING', |
|
42 |
:content => /^chain POSTROUTING {$/, |
|
43 |
:order => '00', |
|
44 |
)} |
|
45 |
it { is_expected.to contain_concat__fragment('nftables-ip-nat-chain-POSTROUTING-rule-type').with( |
|
46 |
:target => 'nftables-ip-nat-chain-POSTROUTING', |
|
47 |
:content => /^ type nat hook postrouting priority 100$/, |
|
48 |
:order => '01', |
|
49 |
)} |
|
50 |
it { is_expected.to contain_concat__fragment('nftables-ip-nat-chain-POSTROUTING-rule-policy').with( |
|
51 |
:target => 'nftables-ip-nat-chain-POSTROUTING', |
|
52 |
:content => /^ policy accept$/, |
|
53 |
:order => '02', |
|
54 |
)} |
|
55 |
it { is_expected.to contain_concat__fragment('nftables-ip-nat-chain-POSTROUTING-rule-masquerade_eth0').with( |
|
56 |
:target => 'nftables-ip-nat-chain-POSTROUTING', |
|
57 |
:content => /^ oifname eth0 masquerade$/, |
|
58 |
:order => '70', |
|
59 |
)} |
|
60 |
it { is_expected.to contain_concat__fragment('nftables-ip-nat-chain-POSTROUTING-rule-masquerade_eth1_vpn').with( |
|
61 |
:target => 'nftables-ip-nat-chain-POSTROUTING', |
|
62 |
:content => /^ oifname eth1 ip saddr 192\.0\.2\.0\/24 masquerade$/, |
|
63 |
:order => '70', |
|
64 |
)} |
|
65 |
it { is_expected.to contain_concat__fragment('nftables-ip-nat-chain-POSTROUTING-rule-masquerade_ssh_gitlab').with( |
|
66 |
:target => 'nftables-ip-nat-chain-POSTROUTING', |
|
67 |
:content => /^ ip saddr 192\.0\.2\.0\/24 ip daddr 198.51.100.2 tcp dport 22 masquerade$/, |
|
68 |
:order => '70', |
|
69 |
)} |
|
70 |
it { is_expected.to contain_concat__fragment('nftables-ip-nat-chain-POSTROUTING-rule-masquerade_wireguard').with( |
|
71 |
:target => 'nftables-ip-nat-chain-POSTROUTING', |
|
72 |
:content => /^ udp dport 51820 masquerade$/, |
|
73 |
:order => '70', |
|
74 |
)} |
|
75 |
it { is_expected.to contain_concat__fragment('nftables-ip-nat-chain-POSTROUTING-footer').with( |
|
76 |
:target => 'nftables-ip-nat-chain-POSTROUTING', |
|
77 |
:content => /^}$/, |
|
78 |
:order => '99', |
|
79 |
)} |
|
80 |
end |
|
81 |
end |
|
82 |
end |
|
83 |
end |
spec/classes/router_spec.rb | ||
---|---|---|
18 | 18 |
'default_fwd-drop': |
19 | 19 |
order => '90', |
20 | 20 |
content => 'iifname eth0 drop'; |
21 |
} |
|
21 | 22 |
|
22 |
'POSTROUTING-masquerade':
|
|
23 |
table => 'ip-nat',
|
|
24 |
order => '20',
|
|
25 |
content => 'oifname eth0 masquerade';
|
|
23 |
nftables::rules::masquerade{
|
|
24 |
'masquerade':
|
|
25 |
order => '20', |
|
26 |
oif => 'eth0';
|
|
26 | 27 |
} |
27 | 28 |
""" |
28 | 29 |
end |
Formats disponibles : Unified diff