Révision 3d29a6eb
Add a rule to create snat
manifests/rules/snat4.pp | ||
---|---|---|
1 |
# manage a ipv4 snat rule |
|
2 |
define nftables::rules::snat4( |
|
3 |
String[1] |
|
4 |
$snat, |
|
5 |
Pattern[/^[a-zA-Z0-9_]+$/] |
|
6 |
$rulename = $title, |
|
7 |
Pattern[/^\d\d$/] |
|
8 |
$order = '70', |
|
9 |
String[1] |
|
10 |
$chain = 'POSTROUTING', |
|
11 |
Optional[String[1]] |
|
12 |
$oif = undef, |
|
13 |
Optional[String[1]] |
|
14 |
$saddr = 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 |
|
|
32 |
if $proto and $dport { |
|
33 |
$protocol = '' |
|
34 |
$port = "${proto} dport ${dport} " |
|
35 |
} elsif $proto { |
|
36 |
$protocol = "${proto} " |
|
37 |
$port = '' |
|
38 |
} elsif $dport { |
|
39 |
$protocol = '' |
|
40 |
$port = "tcp dport ${dport} " |
|
41 |
} else { |
|
42 |
$protocol = '' |
|
43 |
$port = '' |
|
44 |
} |
|
45 |
|
|
46 |
nftables::rule{ |
|
47 |
"${chain}-${rulename}": |
|
48 |
ensure => $ensure, |
|
49 |
table => 'ip-nat', |
|
50 |
order => $order, |
|
51 |
content => "${oifname}${src}${protocol}${port}snat ${snat}"; |
|
52 |
} |
|
53 |
} |
spec/classes/snat4_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 snat4' do |
|
11 |
let(:pre_condition) do |
|
12 |
""" |
|
13 |
nftables::rules::snat4{ |
|
14 |
'static': |
|
15 |
order => '60', |
|
16 |
snat => '198.51.100.1', |
|
17 |
oif => 'eth0'; |
|
18 |
'1_1': |
|
19 |
order => '61', |
|
20 |
saddr => '192.0.2.2', |
|
21 |
snat => '198.51.100.3', |
|
22 |
oif => 'eth0'; |
|
23 |
'1_1_smtp': |
|
24 |
saddr => '192.0.2.2', |
|
25 |
snat => '198.51.100.2', |
|
26 |
dport => '25'; |
|
27 |
'1_1_wireguard': |
|
28 |
saddr => '192.0.2.2', |
|
29 |
snat => '198.51.100.2', |
|
30 |
proto => 'udp', |
|
31 |
dport => '51820'; |
|
32 |
} |
|
33 |
""" |
|
34 |
end |
|
35 |
|
|
36 |
it { is_expected.to compile } |
|
37 |
|
|
38 |
it { is_expected.to contain_concat('nftables-ip-nat-chain-POSTROUTING').with( |
|
39 |
:path => '/etc/nftables/puppet/ip-nat-chain-POSTROUTING.nft', |
|
40 |
:owner => 'root', |
|
41 |
:group => 'root', |
|
42 |
:mode => '0640', |
|
43 |
:ensure_newline => true, |
|
44 |
)} |
|
45 |
it { is_expected.to contain_concat__fragment('nftables-ip-nat-chain-POSTROUTING-header').with( |
|
46 |
:target => 'nftables-ip-nat-chain-POSTROUTING', |
|
47 |
:content => /^chain POSTROUTING {$/, |
|
48 |
:order => '00', |
|
49 |
)} |
|
50 |
it { is_expected.to contain_concat__fragment('nftables-ip-nat-chain-POSTROUTING-rule-type').with( |
|
51 |
:target => 'nftables-ip-nat-chain-POSTROUTING', |
|
52 |
:content => /^ type nat hook postrouting priority 100$/, |
|
53 |
:order => '01', |
|
54 |
)} |
|
55 |
it { is_expected.to contain_concat__fragment('nftables-ip-nat-chain-POSTROUTING-rule-policy').with( |
|
56 |
:target => 'nftables-ip-nat-chain-POSTROUTING', |
|
57 |
:content => /^ policy accept$/, |
|
58 |
:order => '02', |
|
59 |
)} |
|
60 |
it { is_expected.to contain_concat__fragment('nftables-ip-nat-chain-POSTROUTING-rule-static').with( |
|
61 |
:target => 'nftables-ip-nat-chain-POSTROUTING', |
|
62 |
:content => /^ oifname eth0 snat 198\.51\.100\.1$/, |
|
63 |
:order => '60', |
|
64 |
)} |
|
65 |
it { is_expected.to contain_concat__fragment('nftables-ip-nat-chain-POSTROUTING-rule-1_1').with( |
|
66 |
:target => 'nftables-ip-nat-chain-POSTROUTING', |
|
67 |
:content => /^ oifname eth0 ip saddr 192\.0\.2\.2 snat 198\.51\.100\.3$/, |
|
68 |
:order => '61', |
|
69 |
)} |
|
70 |
it { is_expected.to contain_concat__fragment('nftables-ip-nat-chain-POSTROUTING-rule-1_1_smtp').with( |
|
71 |
:target => 'nftables-ip-nat-chain-POSTROUTING', |
|
72 |
:content => /^ ip saddr 192\.0\.2\.2 tcp dport 25 snat 198\.51\.100\.2$/, |
|
73 |
:order => '70', |
|
74 |
)} |
|
75 |
it { is_expected.to contain_concat__fragment('nftables-ip-nat-chain-POSTROUTING-rule-1_1_wireguard').with( |
|
76 |
:target => 'nftables-ip-nat-chain-POSTROUTING', |
|
77 |
:content => /^ ip saddr 192\.0\.2\.2 udp dport 51820 snat 198\.51\.100\.2$/, |
|
78 |
:order => '70', |
|
79 |
)} |
|
80 |
it { is_expected.to contain_concat__fragment('nftables-ip-nat-chain-POSTROUTING-footer').with( |
|
81 |
:target => 'nftables-ip-nat-chain-POSTROUTING', |
|
82 |
:content => /^}$/, |
|
83 |
:order => '99', |
|
84 |
)} |
|
85 |
end |
|
86 |
end |
|
87 |
end |
|
88 |
end |
Formats disponibles : Unified diff