root / spec / classes / nftables_spec.rb @ fa92e118
Historique | Voir | Annoter | Télécharger (7,61 ko)
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 |
it { is_expected.to compile } |
11 |
|
12 |
it { is_expected.to contain_package('nftables') }
|
13 |
|
14 |
it { |
15 |
is_expected.to contain_file('/etc/nftables/puppet.nft').with(
|
16 |
ensure: 'file', |
17 |
owner: 'root', |
18 |
group: 'root', |
19 |
mode: '0640', |
20 |
content: %r{flush ruleset} |
21 |
) |
22 |
} |
23 |
|
24 |
it { |
25 |
is_expected.to contain_file('/etc/nftables/puppet').with(
|
26 |
ensure: 'directory', |
27 |
owner: 'root', |
28 |
group: 'root', |
29 |
mode: '0750', |
30 |
purge: true, |
31 |
force: true, |
32 |
recurse: true |
33 |
) |
34 |
} |
35 |
|
36 |
it { |
37 |
is_expected.to contain_file('/etc/nftables/puppet-preflight.nft').with(
|
38 |
ensure: 'file', |
39 |
owner: 'root', |
40 |
group: 'root', |
41 |
mode: '0640', |
42 |
content: %r{flush ruleset} |
43 |
) |
44 |
} |
45 |
|
46 |
it { |
47 |
is_expected.to contain_file('/etc/nftables/puppet-preflight').with(
|
48 |
ensure: 'directory', |
49 |
owner: 'root', |
50 |
group: 'root', |
51 |
mode: '0750', |
52 |
purge: true, |
53 |
force: true, |
54 |
recurse: true |
55 |
) |
56 |
} |
57 |
|
58 |
it { |
59 |
is_expected.to contain_exec('nft validate').with(
|
60 |
refreshonly: true, |
61 |
command: %r{^/usr/sbin/nft -I /etc/nftables/puppet-preflight -c -f /etc/nftables/puppet-preflight.nft.*} |
62 |
) |
63 |
} |
64 |
|
65 |
it { |
66 |
is_expected.to contain_service('nftables').with(
|
67 |
ensure: 'running', |
68 |
enable: true, |
69 |
hasrestart: true, |
70 |
restart: %r{/usr/bin/systemctl reload nft.*} |
71 |
) |
72 |
} |
73 |
|
74 |
it { |
75 |
is_expected.to contain_systemd__dropin_file('puppet_nft.conf').with(
|
76 |
content: %r{^ExecReload=/sbin/nft -I /etc/nftables/puppet -f /etc/sysconfig/nftables.conf$} |
77 |
) |
78 |
} |
79 |
|
80 |
it { |
81 |
is_expected.to contain_service('firewalld').with(
|
82 |
ensure: 'stopped', |
83 |
enable: 'mask' |
84 |
) |
85 |
} |
86 |
it { is_expected.to contain_class('nftables::inet_filter') }
|
87 |
it { is_expected.to contain_class('nftables::ip_nat') }
|
88 |
it { is_expected.to contain_class('nftables::rules::out::http') }
|
89 |
it { is_expected.to contain_class('nftables::rules::out::https') }
|
90 |
it { is_expected.to contain_class('nftables::rules::out::dns') }
|
91 |
it { is_expected.to contain_class('nftables::rules::out::chrony') }
|
92 |
it { is_expected.not_to contain_class('nftables::rules::out::all') }
|
93 |
it { is_expected.not_to contain_nftables__rule('default_out-all') }
|
94 |
|
95 |
context 'with out_all set true' do |
96 |
let(:params) do |
97 |
{ |
98 |
out_all: true, |
99 |
} |
100 |
end
|
101 |
|
102 |
it { is_expected.to contain_class('nftables::rules::out::all') }
|
103 |
it { is_expected.not_to contain_class('nftables::rules::out::http') }
|
104 |
it { is_expected.not_to contain_class('nftables::rules::out::https') }
|
105 |
it { is_expected.not_to contain_class('nftables::rules::out::dns') }
|
106 |
it { is_expected.not_to contain_class('nftables::rules::out::chrony') }
|
107 |
it { is_expected.to contain_nftables__rule('default_out-all').with_content('accept') } |
108 |
it { is_expected.to contain_nftables__rule('default_out-all').with_order('90') } |
109 |
end
|
110 |
|
111 |
context 'with custom rules' do |
112 |
let(:params) do |
113 |
{ |
114 |
rules: {
|
115 |
'INPUT-web_accept' => {
|
116 |
order: '50', |
117 |
content: 'iifname eth0 tcp dport { 80, 443 } accept', |
118 |
}, |
119 |
}, |
120 |
} |
121 |
end
|
122 |
|
123 |
it { |
124 |
is_expected.to contain_concat__fragment('nftables-inet-filter-chain-INPUT-rule-web_accept').with(
|
125 |
target: 'nftables-inet-filter-chain-INPUT', |
126 |
content: %r{^ iifname eth0 tcp dport \{ 80, 443 \} accept$}, |
127 |
order: '50-nftables-inet-filter-chain-INPUT-rule-web_accept-b' |
128 |
) |
129 |
} |
130 |
end
|
131 |
|
132 |
context 'with custom sets' do |
133 |
let(:params) do |
134 |
{ |
135 |
sets: {
|
136 |
'testset1' => {
|
137 |
type: 'ipv4_addr', |
138 |
gc_interval: 2, |
139 |
}, |
140 |
'testset2' => {
|
141 |
type: 'ipv6_addr', |
142 |
elements: ['2a02:62:c601::dead:beef'], |
143 |
}, |
144 |
}, |
145 |
} |
146 |
end
|
147 |
|
148 |
it { |
149 |
is_expected.to contain_nftables__set('testset1').with(
|
150 |
type: 'ipv4_addr', |
151 |
gc_interval: 2, |
152 |
table: 'inet-filter' |
153 |
) |
154 |
} |
155 |
it { |
156 |
is_expected.to contain_nftables__set('testset2').with(
|
157 |
type: 'ipv6_addr', |
158 |
elements: ['2a02:62:c601::dead:beef'], |
159 |
table: 'inet-filter' |
160 |
) |
161 |
} |
162 |
end
|
163 |
|
164 |
context 'without masking firewalld' do |
165 |
let(:params) do |
166 |
{ |
167 |
'firewalld_enable' => false, |
168 |
} |
169 |
end
|
170 |
|
171 |
it { |
172 |
is_expected.to contain_service('firewalld').with(
|
173 |
ensure: 'stopped', |
174 |
enable: false |
175 |
) |
176 |
} |
177 |
end
|
178 |
|
179 |
context 'with no default filtering rules' do |
180 |
let(:params) do |
181 |
{ |
182 |
'inet_filter' => false, |
183 |
} |
184 |
end
|
185 |
|
186 |
it { is_expected.to contain_class('nftables::ip_nat') }
|
187 |
it { is_expected.not_to contain_class('nftables::inet_filter') }
|
188 |
end
|
189 |
|
190 |
context 'with no default tables, chains or rules' do |
191 |
let(:params) do |
192 |
{ |
193 |
'inet_filter' => false, |
194 |
'nat' => false, |
195 |
} |
196 |
end
|
197 |
|
198 |
it { is_expected.not_to contain_class('nftables::ip_nat') }
|
199 |
it { is_expected.not_to contain_class('nftables::inet_filter') }
|
200 |
it { is_expected.to have_nftables__config_resource_count(0) }
|
201 |
it { is_expected.to have_nftables__chain_resource_count(0) }
|
202 |
it { is_expected.to have_nftables__rule_resource_count(0) }
|
203 |
it { is_expected.to have_nftables__set_resource_count(0) }
|
204 |
end
|
205 |
|
206 |
context 'with with noflush_tables parameter' do |
207 |
let(:params) do |
208 |
{ |
209 |
noflush_tables: ['inet-f2b-table'], |
210 |
} |
211 |
end
|
212 |
|
213 |
context 'with no nftables fact' do |
214 |
it { is_expected.to contain_file('/etc/nftables/puppet-preflight.nft').with_content(%r{^flush ruleset$}) } |
215 |
end
|
216 |
|
217 |
context 'with nftables fact matching' do |
218 |
let(:facts) do |
219 |
super().merge(nftables: { tables: ['inet-abc', 'inet-f2b-table'] }) |
220 |
end
|
221 |
|
222 |
it { |
223 |
is_expected.to contain_file('/etc/nftables/puppet-preflight.nft').
|
224 |
with_content(%r{^table inet abc \{\}$})
|
225 |
} |
226 |
it { |
227 |
is_expected.to contain_file('/etc/nftables/puppet-preflight.nft').
|
228 |
with_content(%r{^flush table inet abc$})
|
229 |
} |
230 |
end
|
231 |
context 'with nftables fact not matching' do |
232 |
let(:facts) do |
233 |
super().merge(nftables: { tables: ['inet-abc', 'inet-ijk'] }) |
234 |
end
|
235 |
|
236 |
it { |
237 |
is_expected.to contain_file('/etc/nftables/puppet-preflight.nft').
|
238 |
with_content(%r{^table inet abc \{\}$})
|
239 |
} |
240 |
it { |
241 |
is_expected.to contain_file('/etc/nftables/puppet-preflight.nft').
|
242 |
with_content(%r{^flush table inet abc$})
|
243 |
} |
244 |
it { |
245 |
is_expected.to contain_file('/etc/nftables/puppet-preflight.nft').
|
246 |
with_content(%r{^table inet ijk \{\}$})
|
247 |
} |
248 |
it { |
249 |
is_expected.to contain_file('/etc/nftables/puppet-preflight.nft').
|
250 |
with_content(%r{^flush table inet ijk$})
|
251 |
} |
252 |
end
|
253 |
end
|
254 |
end
|
255 |
end
|
256 |
end
|