root / spec / classes / nftables_spec.rb @ b61ccb4a
Historique | Voir | Annoter | Télécharger (6,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::rules::out::http') }
|
87 |
it { is_expected.to contain_class('nftables::rules::out::https') }
|
88 |
it { is_expected.to contain_class('nftables::rules::out::dns') }
|
89 |
it { is_expected.to contain_class('nftables::rules::out::chrony') }
|
90 |
it { is_expected.not_to contain_class('nftables::rules::out::all') }
|
91 |
it { is_expected.not_to contain_nftables__rule('default_out-all') }
|
92 |
|
93 |
context 'with out_all set true' do |
94 |
let(:params) do |
95 |
{ |
96 |
out_all: true, |
97 |
} |
98 |
end
|
99 |
|
100 |
it { is_expected.to contain_class('nftables::rules::out::all') }
|
101 |
it { is_expected.not_to contain_class('nftables::rules::out::http') }
|
102 |
it { is_expected.not_to contain_class('nftables::rules::out::https') }
|
103 |
it { is_expected.not_to contain_class('nftables::rules::out::dns') }
|
104 |
it { is_expected.not_to contain_class('nftables::rules::out::chrony') }
|
105 |
it { is_expected.to contain_nftables__rule('default_out-all').with_content('accept') } |
106 |
it { is_expected.to contain_nftables__rule('default_out-all').with_order('90') } |
107 |
end
|
108 |
|
109 |
context 'with custom rules' do |
110 |
let(:params) do |
111 |
{ |
112 |
rules: {
|
113 |
'INPUT-web_accept' => {
|
114 |
order: '50', |
115 |
content: 'iifname eth0 tcp dport { 80, 443 } accept', |
116 |
}, |
117 |
}, |
118 |
} |
119 |
end
|
120 |
|
121 |
it { |
122 |
is_expected.to contain_concat__fragment('nftables-inet-filter-chain-INPUT-rule-web_accept').with(
|
123 |
target: 'nftables-inet-filter-chain-INPUT', |
124 |
content: %r{^ iifname eth0 tcp dport \{ 80, 443 \} accept$}, |
125 |
order: '50-nftables-inet-filter-chain-INPUT-rule-web_accept-b', |
126 |
) |
127 |
} |
128 |
end
|
129 |
|
130 |
context 'with custom sets' do |
131 |
let(:params) do |
132 |
{ |
133 |
sets: {
|
134 |
'testset1' => {
|
135 |
type: 'ipv4_addr', |
136 |
gc_interval: 2, |
137 |
}, |
138 |
'testset2' => {
|
139 |
type: 'ipv6_addr', |
140 |
elements: ['2a02:62:c601::dead:beef'], |
141 |
}, |
142 |
}, |
143 |
} |
144 |
end
|
145 |
|
146 |
it { |
147 |
is_expected.to contain_nftables__set('testset1').with(
|
148 |
type: 'ipv4_addr', |
149 |
gc_interval: 2, |
150 |
table: 'inet-filter', |
151 |
) |
152 |
} |
153 |
it { |
154 |
is_expected.to contain_nftables__set('testset2').with(
|
155 |
type: 'ipv6_addr', |
156 |
elements: ['2a02:62:c601::dead:beef'], |
157 |
table: 'inet-filter', |
158 |
) |
159 |
} |
160 |
end
|
161 |
|
162 |
context 'without masking firewalld' do |
163 |
let(:params) do |
164 |
{ |
165 |
'firewalld_enable' => false, |
166 |
} |
167 |
end
|
168 |
|
169 |
it { |
170 |
is_expected.to contain_service('firewalld').with(
|
171 |
ensure: 'stopped', |
172 |
enable: false, |
173 |
) |
174 |
} |
175 |
end
|
176 |
|
177 |
context 'with with noflush_tables parameter' do |
178 |
let(:params) do |
179 |
{ |
180 |
noflush_tables: ['inet-f2b-table'], |
181 |
} |
182 |
end
|
183 |
|
184 |
context 'with no nftables fact' do |
185 |
it { is_expected.to contain_file('/etc/nftables/puppet-preflight.nft').with_content(%r{^flush ruleset$}) } |
186 |
end
|
187 |
|
188 |
context 'with nftables fact matching' do |
189 |
let(:facts) do |
190 |
super().merge(nftables: { tables: ['inet-abc', 'inet-f2b-table'] }) |
191 |
end
|
192 |
|
193 |
it { |
194 |
is_expected.to contain_file('/etc/nftables/puppet-preflight.nft').
|
195 |
with_content(%r{^table inet abc \{\}$})
|
196 |
} |
197 |
it { |
198 |
is_expected.to contain_file('/etc/nftables/puppet-preflight.nft').
|
199 |
with_content(%r{^flush table inet abc$})
|
200 |
} |
201 |
end
|
202 |
context 'with nftables fact not matching' do |
203 |
let(:facts) do |
204 |
super().merge(nftables: { tables: ['inet-abc', 'inet-ijk'] }) |
205 |
end
|
206 |
|
207 |
it { |
208 |
is_expected.to contain_file('/etc/nftables/puppet-preflight.nft').
|
209 |
with_content(%r{^table inet abc \{\}$})
|
210 |
} |
211 |
it { |
212 |
is_expected.to contain_file('/etc/nftables/puppet-preflight.nft').
|
213 |
with_content(%r{^flush table inet abc$})
|
214 |
} |
215 |
it { |
216 |
is_expected.to contain_file('/etc/nftables/puppet-preflight.nft').
|
217 |
with_content(%r{^table inet ijk \{\}$})
|
218 |
} |
219 |
it { |
220 |
is_expected.to contain_file('/etc/nftables/puppet-preflight.nft').
|
221 |
with_content(%r{^flush table inet ijk$})
|
222 |
} |
223 |
end
|
224 |
end
|
225 |
end
|
226 |
end
|
227 |
end
|