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