Projet

Général

Profil

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

root / spec / classes / nftables_spec.rb @ c8683bd8

Historique | Voir | Annoter | Télécharger (6,65 ko)

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