Projet

Général

Profil

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

root / spec / classes / nftables_spec.rb @ 0c9bc308

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