Projet

Général

Profil

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

root / spec / classes / nftables_spec.rb @ b5874974

Historique | Voir | Annoter | Télécharger (7,68 ko)

1
# frozen_string_literal: true
2

    
3
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

    
14
      it { is_expected.to contain_package('nftables') }
15

    
16
      it {
17
        expect(subject).to contain_file('/etc/nftables/puppet.nft').with(
18
          ensure: 'file',
19
          owner: 'root',
20
          group: 'root',
21
          mode: '0640',
22
          content: %r{flush ruleset}
23
        )
24
      }
25

    
26
      it {
27
        expect(subject).to contain_file('/etc/nftables/puppet').with(
28
          ensure: 'directory',
29
          owner: 'root',
30
          group: 'root',
31
          mode: '0750',
32
          purge: true,
33
          force: true,
34
          recurse: true
35
        )
36
      }
37

    
38
      it {
39
        expect(subject).to contain_file('/etc/nftables/puppet-preflight.nft').with(
40
          ensure: 'file',
41
          owner: 'root',
42
          group: 'root',
43
          mode: '0640',
44
          content: %r{flush ruleset}
45
        )
46
      }
47

    
48
      it {
49
        expect(subject).to contain_file('/etc/nftables/puppet-preflight').with(
50
          ensure: 'directory',
51
          owner: 'root',
52
          group: 'root',
53
          mode: '0750',
54
          purge: true,
55
          force: true,
56
          recurse: true
57
        )
58
      }
59

    
60
      it {
61
        expect(subject).to contain_exec('nft validate').with(
62
          refreshonly: true,
63
          command: %r{^/usr/sbin/nft -I /etc/nftables/puppet-preflight -c -f /etc/nftables/puppet-preflight.nft.*}
64
        )
65
      }
66

    
67
      it {
68
        expect(subject).to contain_service('nftables').with(
69
          ensure: 'running',
70
          enable: true,
71
          hasrestart: true,
72
          restart: %r{/usr/bin/systemctl reload nft.*}
73
        )
74
      }
75

    
76
      it {
77
        expect(subject).to contain_systemd__dropin_file('puppet_nft.conf').with(
78
          content: %r{^ExecReload=/sbin/nft -I /etc/nftables/puppet -f /etc/sysconfig/nftables.conf$}
79
        )
80
      }
81

    
82
      it {
83
        expect(subject).to contain_service('firewalld').with(
84
          ensure: 'stopped',
85
          enable: 'mask'
86
        )
87
      }
88

    
89
      it { is_expected.to contain_class('nftables::inet_filter') }
90
      it { is_expected.to contain_class('nftables::ip_nat') }
91
      it { is_expected.to contain_class('nftables::rules::out::http') }
92
      it { is_expected.to contain_class('nftables::rules::out::https') }
93
      it { is_expected.to contain_class('nftables::rules::out::dns') }
94
      it { is_expected.to contain_class('nftables::rules::out::chrony') }
95
      it { is_expected.not_to contain_class('nftables::rules::out::all') }
96
      it { is_expected.not_to contain_nftables__rule('default_out-all') }
97

    
98
      context 'with out_all set true' do
99
        let(:params) do
100
          {
101
            out_all: true,
102
          }
103
        end
104

    
105
        it { is_expected.to contain_class('nftables::rules::out::all') }
106
        it { is_expected.not_to contain_class('nftables::rules::out::http') }
107
        it { is_expected.not_to contain_class('nftables::rules::out::https') }
108
        it { is_expected.not_to contain_class('nftables::rules::out::dns') }
109
        it { is_expected.not_to contain_class('nftables::rules::out::chrony') }
110
        it { is_expected.to contain_nftables__rule('default_out-all').with_content('accept') }
111
        it { is_expected.to contain_nftables__rule('default_out-all').with_order('90') }
112
      end
113

    
114
      context 'with custom rules' do
115
        let(:params) do
116
          {
117
            rules: {
118
              'INPUT-web_accept' => {
119
                order: '50',
120
                content: 'iifname eth0 tcp dport { 80, 443 } accept',
121
              },
122
            },
123
          }
124
        end
125

    
126
        it {
127
          expect(subject).to contain_concat__fragment('nftables-inet-filter-chain-INPUT-rule-web_accept').with(
128
            target: 'nftables-inet-filter-chain-INPUT',
129
            content: %r{^  iifname eth0 tcp dport \{ 80, 443 \} accept$},
130
            order: '50-nftables-inet-filter-chain-INPUT-rule-web_accept-b'
131
          )
132
        }
133
      end
134

    
135
      context 'with custom sets' do
136
        let(:params) do
137
          {
138
            sets: {
139
              'testset1' => {
140
                type: 'ipv4_addr',
141
                gc_interval: 2,
142
              },
143
              'testset2' => {
144
                type: 'ipv6_addr',
145
                elements: ['2a02:62:c601::dead:beef'],
146
              },
147
            },
148
          }
149
        end
150

    
151
        it {
152
          expect(subject).to contain_nftables__set('testset1').with(
153
            type: 'ipv4_addr',
154
            gc_interval: 2,
155
            table: 'inet-filter'
156
          )
157
        }
158

    
159
        it {
160
          expect(subject).to contain_nftables__set('testset2').with(
161
            type: 'ipv6_addr',
162
            elements: ['2a02:62:c601::dead:beef'],
163
            table: 'inet-filter'
164
          )
165
        }
166
      end
167

    
168
      context 'without masking firewalld' do
169
        let(:params) do
170
          {
171
            'firewalld_enable' => false,
172
          }
173
        end
174

    
175
        it {
176
          expect(subject).to contain_service('firewalld').with(
177
            ensure: 'stopped',
178
            enable: false
179
          )
180
        }
181
      end
182

    
183
      context 'with no default filtering rules' do
184
        let(:params) do
185
          {
186
            'inet_filter' => false,
187
          }
188
        end
189

    
190
        it { is_expected.to contain_class('nftables::ip_nat') }
191
        it { is_expected.not_to contain_class('nftables::inet_filter') }
192
      end
193

    
194
      context 'with no default tables, chains or rules' do
195
        let(:params) do
196
          {
197
            'inet_filter' => false,
198
            'nat' => false,
199
          }
200
        end
201

    
202
        it { is_expected.not_to contain_class('nftables::ip_nat') }
203
        it { is_expected.not_to contain_class('nftables::inet_filter') }
204
        it { is_expected.to have_nftables__config_resource_count(0) }
205
        it { is_expected.to have_nftables__chain_resource_count(0) }
206
        it { is_expected.to have_nftables__rule_resource_count(0) }
207
        it { is_expected.to have_nftables__set_resource_count(0) }
208
      end
209

    
210
      context 'with with noflush_tables parameter' do
211
        let(:params) do
212
          {
213
            noflush_tables: ['inet-f2b-table'],
214
          }
215
        end
216

    
217
        context 'with no nftables fact' do
218
          it { is_expected.to contain_file('/etc/nftables/puppet-preflight.nft').with_content(%r{^flush ruleset$}) }
219
        end
220

    
221
        context 'with nftables fact matching' do
222
          let(:facts) do
223
            super().merge(nftables: { tables: %w[inet-abc inet-f2b-table] })
224
          end
225

    
226
          it {
227
            expect(subject).to contain_file('/etc/nftables/puppet-preflight.nft').
228
              with_content(%r{^table inet abc \{\}$})
229
          }
230

    
231
          it {
232
            expect(subject).to contain_file('/etc/nftables/puppet-preflight.nft').
233
              with_content(%r{^flush table inet abc$})
234
          }
235
        end
236

    
237
        context 'with nftables fact not matching' do
238
          let(:facts) do
239
            super().merge(nftables: { tables: %w[inet-abc inet-ijk] })
240
          end
241

    
242
          it {
243
            expect(subject).to contain_file('/etc/nftables/puppet-preflight.nft').
244
              with_content(%r{^table inet abc \{\}$})
245
          }
246

    
247
          it {
248
            expect(subject).to contain_file('/etc/nftables/puppet-preflight.nft').
249
              with_content(%r{^flush table inet abc$})
250
          }
251

    
252
          it {
253
            expect(subject).to contain_file('/etc/nftables/puppet-preflight.nft').
254
              with_content(%r{^table inet ijk \{\}$})
255
          }
256

    
257
          it {
258
            expect(subject).to contain_file('/etc/nftables/puppet-preflight.nft').
259
              with_content(%r{^flush table inet ijk$})
260
          }
261
        end
262
      end
263
    end
264
  end
265
end