Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / spec / classes / nftables_spec.rb @ cc9fc807

Historique | Voir | Annoter | Télécharger (8,53 ko)

1 c82b960a Steve Traylen
# frozen_string_literal: true
2
3 64134e4e tr
require 'spec_helper'
4
5
describe 'nftables' do
6
  let(:pre_condition) { 'Exec{path => "/bin"}' }
7
8
  on_supported_os.each do |os, os_facts|
9
    context "on #{os}" do
10
      let(:facts) { os_facts }
11
12 8842a597 Tim Meusel
      nft_path = case os_facts[:os]['family']
13
                 when 'Archlinux'
14
                   '/usr/bin/nft'
15
                 else
16
                   '/usr/sbin/nft'
17
                 end
18
19
      it { is_expected.to compile.with_all_deps }
20 5acb554a tr
21
      it { is_expected.to contain_package('nftables') }
22
23 01d8a819 tr
      it {
24 0c9bc308 hashworks
        is_expected.to contain_file('/etc/nftables').with(
25
          ensure: 'directory',
26
          owner: 'root',
27
          group: 'root',
28
          mode: '0750'
29
        )
30
      }
31
32
      it {
33 c82b960a Steve Traylen
        expect(subject).to contain_file('/etc/nftables/puppet.nft').with(
34 01d8a819 tr
          ensure: 'file',
35 c82b960a Steve Traylen
          owner: 'root',
36
          group: 'root',
37
          mode: '0640',
38 fa92e118 Romain Tartière
          content: %r{flush ruleset}
39 01d8a819 tr
        )
40
      }
41
42
      it {
43 c82b960a Steve Traylen
        expect(subject).to contain_file('/etc/nftables/puppet').with(
44
          ensure: 'directory',
45
          owner: 'root',
46
          group: 'root',
47
          mode: '0750',
48
          purge: true,
49
          force: true,
50 fa92e118 Romain Tartière
          recurse: true
51 01d8a819 tr
        )
52
      }
53
54
      it {
55 c82b960a Steve Traylen
        expect(subject).to contain_file('/etc/nftables/puppet-preflight.nft').with(
56 30462da1 Steve Traylen
          ensure: 'file',
57 c82b960a Steve Traylen
          owner: 'root',
58
          group: 'root',
59
          mode: '0640',
60 fa92e118 Romain Tartière
          content: %r{flush ruleset}
61 30462da1 Steve Traylen
        )
62
      }
63
64
      it {
65 c82b960a Steve Traylen
        expect(subject).to contain_file('/etc/nftables/puppet-preflight').with(
66
          ensure: 'directory',
67
          owner: 'root',
68
          group: 'root',
69
          mode: '0750',
70
          purge: true,
71
          force: true,
72 fa92e118 Romain Tartière
          recurse: true
73 30462da1 Steve Traylen
        )
74
      }
75
76
      it {
77 c82b960a Steve Traylen
        expect(subject).to contain_exec('nft validate').with(
78 30462da1 Steve Traylen
          refreshonly: true,
79 8842a597 Tim Meusel
          command: %r{^#{nft_path} -I /etc/nftables/puppet-preflight -c -f /etc/nftables/puppet-preflight.nft.*}
80 30462da1 Steve Traylen
        )
81
      }
82
83
      it {
84 c82b960a Steve Traylen
        expect(subject).to contain_service('nftables').with(
85 01d8a819 tr
          ensure: 'running',
86
          enable: true,
87 30462da1 Steve Traylen
          hasrestart: true,
88 cc9fc807 Tim Meusel
          restart: %r{PATH=/usr/bin:/bin systemctl reload nft.*}
89 01d8a819 tr
        )
90
      }
91
92 0c9bc308 hashworks
      if os_facts[:os]['family'] == 'Archlinux'
93
        it {
94
          expect(subject).to contain_systemd__dropin_file('puppet_nft.conf').with(
95 8842a597 Tim Meusel
            content: %r{^ExecReload=#{nft_path} -I /etc/nftables/puppet -f /etc/nftables.conf$}
96 0c9bc308 hashworks
          )
97
        }
98 ce22630b Steve Traylen
99 0c9bc308 hashworks
        it {
100
          expect(subject).to contain_service('firewalld').with(
101
            ensure: 'stopped',
102
            enable: false
103
          )
104
        }
105
      else
106
        it {
107
          expect(subject).to contain_systemd__dropin_file('puppet_nft.conf').with(
108 8842a597 Tim Meusel
            content: %r{^ExecReload=#{nft_path} -I /etc/nftables/puppet -f /etc/sysconfig/nftables.conf$}
109 0c9bc308 hashworks
          )
110
        }
111
112
        it {
113
          expect(subject).to contain_service('firewalld').with(
114
            ensure: 'stopped',
115
            enable: 'mask'
116
          )
117
        }
118
      end
119 c82b960a Steve Traylen
120 7b9d6ffc Nacho Barrientos
      it { is_expected.to contain_class('nftables::inet_filter') }
121
      it { is_expected.to contain_class('nftables::ip_nat') }
122 e17693e3 Steve Traylen
      it { is_expected.to contain_class('nftables::rules::out::http') }
123
      it { is_expected.to contain_class('nftables::rules::out::https') }
124
      it { is_expected.to contain_class('nftables::rules::out::dns') }
125
      it { is_expected.to contain_class('nftables::rules::out::chrony') }
126
      it { is_expected.not_to contain_class('nftables::rules::out::all') }
127
      it { is_expected.not_to contain_nftables__rule('default_out-all') }
128
129
      context 'with out_all set true' do
130 b171ac7f mh
        let(:params) do
131
          {
132
            out_all: true,
133
          }
134 e17693e3 Steve Traylen
        end
135
136
        it { is_expected.to contain_class('nftables::rules::out::all') }
137
        it { is_expected.not_to contain_class('nftables::rules::out::http') }
138
        it { is_expected.not_to contain_class('nftables::rules::out::https') }
139
        it { is_expected.not_to contain_class('nftables::rules::out::dns') }
140
        it { is_expected.not_to contain_class('nftables::rules::out::chrony') }
141
        it { is_expected.to contain_nftables__rule('default_out-all').with_content('accept') }
142
        it { is_expected.to contain_nftables__rule('default_out-all').with_order('90') }
143
      end
144 b3a7a6dd tr
145
      context 'with custom rules' do
146
        let(:params) do
147
          {
148
            rules: {
149
              'INPUT-web_accept' => {
150
                order: '50',
151
                content: 'iifname eth0 tcp dport { 80, 443 } accept',
152
              },
153
            },
154
          }
155
        end
156
157
        it {
158 c82b960a Steve Traylen
          expect(subject).to contain_concat__fragment('nftables-inet-filter-chain-INPUT-rule-web_accept').with(
159
            target: 'nftables-inet-filter-chain-INPUT',
160 b3a7a6dd tr
            content: %r{^  iifname eth0 tcp dport \{ 80, 443 \} accept$},
161 c82b960a Steve Traylen
            order: '50-nftables-inet-filter-chain-INPUT-rule-web_accept-b'
162 b3a7a6dd tr
          )
163
        }
164
      end
165 ae9872e2 Nacho Barrientos
166 802d80d1 Nacho Barrientos
      context 'with custom sets' do
167
        let(:params) do
168
          {
169
            sets: {
170
              'testset1' => {
171
                type: 'ipv4_addr',
172
                gc_interval: 2,
173
              },
174
              'testset2' => {
175
                type: 'ipv6_addr',
176
                elements: ['2a02:62:c601::dead:beef'],
177
              },
178
            },
179
          }
180
        end
181
182
        it {
183 c82b960a Steve Traylen
          expect(subject).to contain_nftables__set('testset1').with(
184 802d80d1 Nacho Barrientos
            type: 'ipv4_addr',
185
            gc_interval: 2,
186 fa92e118 Romain Tartière
            table: 'inet-filter'
187 802d80d1 Nacho Barrientos
          )
188
        }
189 c82b960a Steve Traylen
190 802d80d1 Nacho Barrientos
        it {
191 c82b960a Steve Traylen
          expect(subject).to contain_nftables__set('testset2').with(
192 802d80d1 Nacho Barrientos
            type: 'ipv6_addr',
193
            elements: ['2a02:62:c601::dead:beef'],
194 fa92e118 Romain Tartière
            table: 'inet-filter'
195 802d80d1 Nacho Barrientos
          )
196
        }
197
      end
198
199 ae9872e2 Nacho Barrientos
      context 'without masking firewalld' do
200
        let(:params) do
201
          {
202
            'firewalld_enable' => false,
203
          }
204
        end
205
206
        it {
207 c82b960a Steve Traylen
          expect(subject).to contain_service('firewalld').with(
208 ae9872e2 Nacho Barrientos
            ensure: 'stopped',
209 fa92e118 Romain Tartière
            enable: false
210 ae9872e2 Nacho Barrientos
          )
211
        }
212
      end
213 03d9e7da Steve Traylen
214 7b9d6ffc Nacho Barrientos
      context 'with no default filtering rules' do
215
        let(:params) do
216
          {
217
            'inet_filter' => false,
218
          }
219
        end
220
221
        it { is_expected.to contain_class('nftables::ip_nat') }
222
        it { is_expected.not_to contain_class('nftables::inet_filter') }
223
      end
224
225
      context 'with no default tables, chains or rules' do
226
        let(:params) do
227
          {
228
            'inet_filter' => false,
229
            'nat' => false,
230
          }
231
        end
232
233
        it { is_expected.not_to contain_class('nftables::ip_nat') }
234
        it { is_expected.not_to contain_class('nftables::inet_filter') }
235
        it { is_expected.to have_nftables__config_resource_count(0) }
236
        it { is_expected.to have_nftables__chain_resource_count(0) }
237
        it { is_expected.to have_nftables__rule_resource_count(0) }
238
        it { is_expected.to have_nftables__set_resource_count(0) }
239
      end
240
241 03d9e7da Steve Traylen
      context 'with with noflush_tables parameter' do
242
        let(:params) do
243
          {
244
            noflush_tables: ['inet-f2b-table'],
245
          }
246
        end
247
248
        context 'with no nftables fact' do
249
          it { is_expected.to contain_file('/etc/nftables/puppet-preflight.nft').with_content(%r{^flush ruleset$}) }
250
        end
251
252
        context 'with nftables fact matching' do
253
          let(:facts) do
254 c82b960a Steve Traylen
            super().merge(nftables: { tables: %w[inet-abc inet-f2b-table] })
255 03d9e7da Steve Traylen
          end
256
257
          it {
258 c82b960a Steve Traylen
            expect(subject).to contain_file('/etc/nftables/puppet-preflight.nft').
259 92e0fcb6 duritong
              with_content(%r{^table inet abc \{\}$})
260
          }
261 c82b960a Steve Traylen
262 92e0fcb6 duritong
          it {
263 c82b960a Steve Traylen
            expect(subject).to contain_file('/etc/nftables/puppet-preflight.nft').
264 7e5b657a Steve Traylen
              with_content(%r{^flush table inet abc$})
265 03d9e7da Steve Traylen
          }
266
        end
267 c82b960a Steve Traylen
268 03d9e7da Steve Traylen
        context 'with nftables fact not matching' do
269
          let(:facts) do
270 c82b960a Steve Traylen
            super().merge(nftables: { tables: %w[inet-abc inet-ijk] })
271 03d9e7da Steve Traylen
          end
272
273
          it {
274 c82b960a Steve Traylen
            expect(subject).to contain_file('/etc/nftables/puppet-preflight.nft').
275 92e0fcb6 duritong
              with_content(%r{^table inet abc \{\}$})
276
          }
277 c82b960a Steve Traylen
278 92e0fcb6 duritong
          it {
279 c82b960a Steve Traylen
            expect(subject).to contain_file('/etc/nftables/puppet-preflight.nft').
280 92e0fcb6 duritong
              with_content(%r{^flush table inet abc$})
281
          }
282 c82b960a Steve Traylen
283 92e0fcb6 duritong
          it {
284 c82b960a Steve Traylen
            expect(subject).to contain_file('/etc/nftables/puppet-preflight.nft').
285 92e0fcb6 duritong
              with_content(%r{^table inet ijk \{\}$})
286
          }
287 c82b960a Steve Traylen
288 92e0fcb6 duritong
          it {
289 c82b960a Steve Traylen
            expect(subject).to contain_file('/etc/nftables/puppet-preflight.nft').
290 92e0fcb6 duritong
              with_content(%r{^flush table inet ijk$})
291 03d9e7da Steve Traylen
          }
292
        end
293
      end
294 64134e4e tr
    end
295
  end
296
end