Projet

Général

Profil

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

root / spec / classes / nftables_spec.rb @ bd5145ab

Historique | Voir | Annoter | Télécharger (6,35 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
        is_expected.to contain_file('/etc/nftables/puppet.nft').with(
16
          ensure: 'file',
17
          owner:  'root',
18
          group:  'root',
19
          mode:   '0640',
20 82d10659 Nacho Barrientos
          content: %r{flush ruleset},
21 01d8a819 tr
        )
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 30462da1 Steve Traylen
        is_expected.to contain_file('/etc/nftables/puppet-preflight.nft').with(
38
          ensure: 'file',
39
          owner:  'root',
40
          group:  'root',
41
          mode:   '0640',
42 82d10659 Nacho Barrientos
          content: %r{flush ruleset},
43 30462da1 Steve Traylen
        )
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 01d8a819 tr
        is_expected.to contain_service('nftables').with(
67
          ensure: 'running',
68
          enable: true,
69 30462da1 Steve Traylen
          hasrestart: true,
70
          restart: %r{/usr/bin/systemctl reload nft.*},
71 01d8a819 tr
        )
72
      }
73
74
      it {
75
        is_expected.to contain_service('firewalld').with(
76
          ensure: 'stopped',
77
          enable: 'mask',
78
        )
79
      }
80 e17693e3 Steve Traylen
      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 b171ac7f mh
        let(:params) do
89
          {
90
            out_all: true,
91
          }
92 e17693e3 Steve Traylen
        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 b3a7a6dd tr
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 61f03b47 Steve Traylen
            order:   '50-nftables-inet-filter-chain-INPUT-rule-web_accept-b',
120 b3a7a6dd tr
          )
121
        }
122
      end
123 ae9872e2 Nacho Barrientos
124 802d80d1 Nacho Barrientos
      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 ae9872e2 Nacho Barrientos
      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 03d9e7da Steve Traylen
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 64134e4e tr
    end
216
  end
217
end