root / spec / acceptance / default_spec.rb @ 5dedf86c
Historique | Voir | Annoter | Télécharger (8,67 ko)
1 |
# frozen_string_literal: true
|
---|---|
2 |
|
3 |
require 'spec_helper_acceptance'
|
4 |
|
5 |
describe 'nftables class' do |
6 |
context 'configure default nftables service' do |
7 |
it 'works idempotently with no errors' do |
8 |
pp = <<-EOS |
9 |
|
10 |
# Default ArchLinux rules contain "destroy" that requires kernel >= 6.3
|
11 |
# https://gitlab.archlinux.org/archlinux/packaging/packages/nftables/-/commit/f26a7145b2885d298925819782a5302905332dbe
|
12 |
# When running on docker this may not be the case.
|
13 |
if $facts['os']['family'] == 'Archlinux' and versioncmp($facts['kernelrelease'],'6.3') < 0 {
|
14 |
$_clobber_default_config = true
|
15 |
} else {
|
16 |
$_clobber_default_config = undef
|
17 |
}
|
18 |
|
19 |
# default mask of firewalld service fails if service is not installed.
|
20 |
# https://tickets.puppetlabs.com/browse/PUP-10814
|
21 |
class { 'nftables':
|
22 |
firewalld_enable => false,
|
23 |
clobber_default_config => $_clobber_default_config,
|
24 |
}
|
25 |
$config_path = $facts['os']['family'] ? {
|
26 |
'Archlinux' => '/etc/nftables.conf',
|
27 |
'Debian' => '/etc/nftables.conf',
|
28 |
default => '/etc/sysconfig/nftables.conf',
|
29 |
}
|
30 |
$nft_path = $facts['os']['family'] ? {
|
31 |
'Archlinux' => '/usr/bin/nft',
|
32 |
default => '/usr/sbin/nft',
|
33 |
}
|
34 |
# nftables cannot be started in docker so replace service with a validation only.
|
35 |
systemd::dropin_file{"zzz_docker_nft.conf":
|
36 |
ensure => present,
|
37 |
unit => "nftables.service",
|
38 |
content => [
|
39 |
"[Service]",
|
40 |
"ExecStart=",
|
41 |
"ExecStart=${nft_path} -c -I /etc/nftables/puppet -f ${config_path}",
|
42 |
"ExecReload=",
|
43 |
"ExecReload=${nft_path} -c -I /etc/nftables/puppet -f ${config_path}",
|
44 |
"",
|
45 |
].join("\n"),
|
46 |
notify => Service["nftables"],
|
47 |
}
|
48 |
EOS
|
49 |
# Run it twice and test for idempotency
|
50 |
apply_manifest(pp, catch_failures: true) |
51 |
apply_manifest(pp, catch_changes: true) |
52 |
end
|
53 |
|
54 |
describe package('nftables') do |
55 |
it { is_expected.to be_installed } |
56 |
end
|
57 |
|
58 |
describe service('nftables') do |
59 |
it { is_expected.to be_running } |
60 |
it { is_expected.to be_enabled } |
61 |
end
|
62 |
|
63 |
describe file('/etc/nftables/puppet.nft', '/etc/systemd/system/nftables.service.d/puppet_nft.conf') do |
64 |
it { is_expected.to be_file } |
65 |
end
|
66 |
|
67 |
describe file('/etc/nftables/puppet') do |
68 |
it { is_expected.to be_directory } |
69 |
end
|
70 |
end
|
71 |
|
72 |
context 'with bad invalid nft rules' do |
73 |
it 'puppet fails but should leave nft service running' do |
74 |
pp = <<-EOS |
75 |
if $facts['os']['family'] == 'Archlinux' and versioncmp($facts['kernelrelease'],'6.3') < 0 {
|
76 |
$_clobber_default_config = true
|
77 |
} else {
|
78 |
$_clobber_default_config = undef
|
79 |
}
|
80 |
class{'nftables':
|
81 |
firewalld_enable => false,
|
82 |
clobber_default_config => $_clobber_default_config,
|
83 |
}
|
84 |
nftables::rule{'default_out-junk':
|
85 |
content => 'A load of junk',
|
86 |
}
|
87 |
$config_path = $facts['os']['family'] ? {
|
88 |
'Archlinux' => '/etc/nftables.conf',
|
89 |
'Debian' => '/etc/nftables.conf',
|
90 |
default => '/etc/sysconfig/nftables.conf',
|
91 |
}
|
92 |
$nft_path = $facts['os']['family'] ? {
|
93 |
'Archlinux' => '/usr/bin/nft',
|
94 |
default => '/usr/sbin/nft',
|
95 |
}
|
96 |
# nftables cannot be started in docker so replace service with a validation only.
|
97 |
systemd::dropin_file{"zzz_docker_nft.conf":
|
98 |
ensure => present,
|
99 |
unit => "nftables.service",
|
100 |
content => [
|
101 |
"[Service]",
|
102 |
"ExecStart=",
|
103 |
"ExecStart=${nft_path} -c -I /etc/nftables/puppet -f $config_path",
|
104 |
"ExecReload=",
|
105 |
"ExecReload=${nft_path} -c -I /etc/nftables/puppet -f $config_path",
|
106 |
"",
|
107 |
].join("\n"),
|
108 |
notify => Service["nftables"],
|
109 |
}
|
110 |
EOS
|
111 |
apply_manifest(pp, expect_failures: true) |
112 |
end
|
113 |
|
114 |
describe service('nftables') do |
115 |
it { is_expected.to be_running } |
116 |
it { is_expected.to be_enabled } |
117 |
end
|
118 |
end
|
119 |
|
120 |
context 'with totally empty firewall' do |
121 |
it 'no rules validate okay' do |
122 |
pp = <<-EOS |
123 |
if $facts['os']['family'] == 'Archlinux' and versioncmp($facts['kernelrelease'],'6.3') < 0 {
|
124 |
$_clobber_default_config = true
|
125 |
} else {
|
126 |
$_clobber_default_config = undef
|
127 |
}
|
128 |
class{'nftables':
|
129 |
firewalld_enable => false,
|
130 |
inet_filter => false,
|
131 |
nat => false,
|
132 |
clobber_default_config => $_clobber_default_config,
|
133 |
}
|
134 |
$config_path = $facts['os']['family'] ? {
|
135 |
'Archlinux' => '/etc/nftables.conf',
|
136 |
'Debian' => '/etc/nftables.conf',
|
137 |
default => '/etc/sysconfig/nftables.conf',
|
138 |
}
|
139 |
$nft_path = $facts['os']['family'] ? {
|
140 |
'Archlinux' => '/usr/bin/nft',
|
141 |
default => '/usr/sbin/nft',
|
142 |
}
|
143 |
# nftables cannot be started in docker so replace service with a validation only.
|
144 |
systemd::dropin_file{"zzz_docker_nft.conf":
|
145 |
ensure => present,
|
146 |
unit => "nftables.service",
|
147 |
content => [
|
148 |
"[Service]",
|
149 |
"ExecStart=",
|
150 |
"ExecStart=${nft_path} -c -I /etc/nftables/puppet -f $config_path",
|
151 |
"ExecReload=",
|
152 |
"ExecReload=${nft_path} -c -I /etc/nftables/puppet -f $config_path",
|
153 |
"",
|
154 |
].join("\n"),
|
155 |
notify => Service["nftables"],
|
156 |
}
|
157 |
EOS
|
158 |
apply_manifest(pp, catch_failures: true) |
159 |
end
|
160 |
|
161 |
describe service('nftables') do |
162 |
it { is_expected.to be_running } |
163 |
it { is_expected.to be_enabled } |
164 |
end
|
165 |
end
|
166 |
|
167 |
context 'with custom nat_table_name' do |
168 |
it 'no rules validate okay' do |
169 |
pp = <<-EOS |
170 |
if $facts['os']['family'] == 'Archlinux' and versioncmp($facts['kernelrelease'],'6.3') < 0 {
|
171 |
$_clobber_default_config = true
|
172 |
} else {
|
173 |
$_clobber_default_config = undef
|
174 |
}
|
175 |
class{'nftables':
|
176 |
firewalld_enable => false,
|
177 |
nat => true,
|
178 |
nat_table_name => 'mycustomtablename',
|
179 |
clobber_default_config => $_clobber_default_config,
|
180 |
}
|
181 |
$config_path = $facts['os']['family'] ? {
|
182 |
'Archlinux' => '/etc/nftables.conf',
|
183 |
'Debian' => '/etc/nftables.conf',
|
184 |
default => '/etc/sysconfig/nftables.conf',
|
185 |
}
|
186 |
$nft_path = $facts['os']['family'] ? {
|
187 |
'Archlinux' => '/usr/bin/nft',
|
188 |
default => '/usr/sbin/nft',
|
189 |
}
|
190 |
# nftables cannot be started in docker so replace service with a validation only.
|
191 |
systemd::dropin_file{"zzz_docker_nft.conf":
|
192 |
ensure => present,
|
193 |
unit => "nftables.service",
|
194 |
content => [
|
195 |
"[Service]",
|
196 |
"ExecStart=",
|
197 |
"ExecStart=${nft_path} -c -I /etc/nftables/puppet -f $config_path",
|
198 |
"ExecReload=",
|
199 |
"ExecReload=${nft_path} -c -I /etc/nftables/puppet -f $config_path",
|
200 |
"",
|
201 |
].join("\n"),
|
202 |
notify => Service["nftables"],
|
203 |
}
|
204 |
EOS
|
205 |
apply_manifest(pp, catch_failures: true) |
206 |
end
|
207 |
|
208 |
describe service('nftables') do |
209 |
it { is_expected.to be_running } |
210 |
it { is_expected.to be_enabled } |
211 |
end
|
212 |
end
|
213 |
|
214 |
context 'with only an empty netdev table' do |
215 |
it 'rules validate okay' do |
216 |
pp = <<-EOS |
217 |
if $facts['os']['family'] == 'Archlinux' and versioncmp($facts['kernelrelease'],'6.3') < 0 {
|
218 |
$_clobber_default_config = true
|
219 |
} else {
|
220 |
$_clobber_default_config = undef
|
221 |
}
|
222 |
class{'nftables':
|
223 |
firewalld_enable => false,
|
224 |
inet_filter => false,
|
225 |
nat => false,
|
226 |
clobber_default_config => $_clobber_default_config,
|
227 |
}
|
228 |
nftables::config {
|
229 |
'netdev-filter':
|
230 |
prefix => '',
|
231 |
}
|
232 |
nftables::chain {
|
233 |
[
|
234 |
'INPUT',
|
235 |
'OUTPUT',
|
236 |
'FORWARD',
|
237 |
]:
|
238 |
table => 'netdev-filter';
|
239 |
}
|
240 |
$config_path = $facts['os']['family'] ? {
|
241 |
'Archlinux' => '/etc/nftables.conf',
|
242 |
'Debian' => '/etc/nftables.conf',
|
243 |
default => '/etc/sysconfig/nftables.conf',
|
244 |
}
|
245 |
$nft_path = $facts['os']['family'] ? {
|
246 |
'Archlinux' => '/usr/bin/nft',
|
247 |
default => '/usr/sbin/nft',
|
248 |
}
|
249 |
# nftables cannot be started in docker so replace service with a validation only.
|
250 |
systemd::dropin_file{"zzz_docker_nft.conf":
|
251 |
ensure => present,
|
252 |
unit => "nftables.service",
|
253 |
content => [
|
254 |
"[Service]",
|
255 |
"ExecStart=",
|
256 |
"ExecStart=${nft_path} -c -I /etc/nftables/puppet -f $config_path",
|
257 |
"ExecReload=",
|
258 |
"ExecReload=${nft_path} -c -I /etc/nftables/puppet -f $config_path",
|
259 |
"",
|
260 |
].join("\n"),
|
261 |
notify => Service["nftables"],
|
262 |
}
|
263 |
EOS
|
264 |
apply_manifest(pp, catch_failures: true) |
265 |
end
|
266 |
|
267 |
describe service('nftables') do |
268 |
it { is_expected.to be_running } |
269 |
it { is_expected.to be_enabled } |
270 |
end
|
271 |
end
|
272 |
end
|