aboutsummaryrefslogtreecommitdiff
path: root/spec/acceptance/ferm_spec.rb
blob: eee01fa1dca734e25c91561c571a8076390212ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
require 'spec_helper_acceptance'

os_name = fact('os.name')
os_release = fact('os.release.major')

sut_os = "#{os_name}-#{os_release}"

manage_initfile = case sut_os
                  when 'CentOS-6'
                    true
                  else
                    false
                  end

iptables_output = case sut_os
                  when 'Debian-10'
                    [
                      '-A INPUT -p tcp -m tcp --dport 22 -m comment --comment allow_acceptance_tests -j ACCEPT',
                      '-A INPUT -p tcp -m tcp --dport 80 -m comment --comment jump_http -j HTTP',
                      '-A HTTP -s 127.0.0.1/32 -p tcp -m tcp --dport 80 -m comment --comment allow_http_localhost -j ACCEPT'
                    ]
                  else
                    [
                      '-A INPUT -p tcp -m comment --comment ["]*allow_acceptance_tests["]* -m tcp --dport 22 -j ACCEPT',
                      '-A INPUT -p tcp -m comment --comment ["]*jump_http["]* -m tcp --dport 80 -j HTTP',
                      '-A HTTP -s 127.0.0.1/32 -p tcp -m comment --comment ["]*allow_http_localhost["]* -m tcp --dport 80 -j ACCEPT'
                    ]
                  end

iptables_output_custom = ['-A FORWARD -s 10.8.0.0/24 -p udp -m comment --comment "OpenVPN - FORWORD all udp traffic from network 10.8.0.0/24 to subchain OPENVPN_FORWORD_RULES" -j OPENVPN_FORWORD_RULES',
                          '-A OPENVPN_FORWORD_RULES -s 10.8.0.0/24 -i tun0 -o enp4s0 -p udp -m conntrack --ctstate NEW -j ACCEPT']

# When `install_method` is `vcsrepo` with `vcstag` >= `v2.5` ferm call "legacy"
# xtables tools because nft based tools are incompatible.
iptables_save_cmd = case sut_os
                    when 'Debian-10'
                      'iptables-legacy-save'
                    else
                      'iptables-save'
                    end

basic_manifest = %(
  class { 'ferm':
    manage_service    => true,
    manage_configfile => true,
    manage_initfile   => #{manage_initfile}, # CentOS-6 does not provide init script
    forward_policy    => 'DROP',
    output_policy     => 'ACCEPT',
    input_policy      => 'DROP',
    rules             => {
      'allow_acceptance_tests' => {
        chain  => 'INPUT',
        action => 'ACCEPT',
        proto  => tcp,
        dport  => 22,
      },
    },
    ip_versions      => ['ip'], #only ipv4 available with CI
)

describe 'ferm' do
  context 'with basics settings and vcsrepo install_method' do
    pp = [basic_manifest, "install_method => 'vcsrepo',}"].join("\n")

    it 'works with no error' do
      apply_manifest(pp, catch_failures: true)
    end
    it 'works idempotently' do
      apply_manifest(pp, catch_changes: true)
    end

    describe package('ferm') do
      it { is_expected.not_to be_installed }
    end

    describe service('ferm') do
      it { is_expected.to be_running }
    end

    describe command("#{iptables_save_cmd} -t filter") do
      its(:stdout) { is_expected.to match %r{.*filter.*:INPUT DROP.*:FORWARD DROP.*:OUTPUT ACCEPT.*}m }
      its(:stdout) { is_expected.not_to match %r{state INVALID -j DROP} }
      its(:stdout) { is_expected.to match %r{allow_acceptance_tests.*-j ACCEPT}m }
    end
  end

  context 'with basics settings and default install_method' do
    pp = [basic_manifest, '}'].join("\n")

    it 'works with no error' do
      apply_manifest(pp, catch_failures: true)
    end
    it 'works idempotently' do
      apply_manifest(pp, catch_changes: true)
    end

    describe package('ferm') do
      it { is_expected.to be_installed }
    end

    describe service('ferm') do
      it { is_expected.to be_running }
    end

    describe command('iptables-save') do
      its(:stdout) { is_expected.to match %r{.*filter.*:INPUT DROP.*:FORWARD DROP.*:OUTPUT ACCEPT.*}m }
      its(:stdout) { is_expected.not_to match %r{state INVALID -j DROP} }
    end

    describe iptables do
      it do
        is_expected.to have_rule(iptables_output[0]). \
          with_table('filter'). \
          with_chain('INPUT')
      end
    end

    context 'with custom chains' do
      advanced_manifest = %(
        ferm::chain { 'check-http':
          chain               => 'HTTP',
          disable_conntrack   => true,
          log_dropped_packets => false,
        }
        ferm::rule { 'jump_http':
          chain             => 'INPUT',
          action            => 'HTTP',
          proto             => 'tcp',
          dport             => 80,
          require           => Ferm::Chain['check-http'],
        }
        ferm::rule { 'allow_http_localhost':
          chain             => 'HTTP',
          action            => 'ACCEPT',
          proto             => 'tcp',
          dport             => 80,
          saddr             => '127.0.0.1',
          require           => Ferm::Chain['check-http'],
        }
      )
      pp = [basic_manifest, '}', advanced_manifest].join("\n")

      it 'works with no error' do
        apply_manifest(pp, catch_failures: true)
      end
      it 'works idempotently' do
        apply_manifest(pp, catch_changes: true)
      end

      describe iptables do
        it do
          is_expected.to have_rule(iptables_output[1]). \
            with_table('filter'). \
            with_chain('INPUT')
        end
        it do
          is_expected.to have_rule(iptables_output[2]). \
            with_table('filter'). \
            with_chain('HTTP')
        end
      end
    end

    context 'with dropping INVALID packets' do
      pp2 = %(
        class { 'ferm':
          manage_service                            => true,
          manage_configfile                         => true,
          manage_initfile                           => #{manage_initfile}, # CentOS-6 does not provide init script
          forward_policy                            => 'DROP',
          output_policy                             => 'ACCEPT',
          input_policy                              => 'DROP',
          input_drop_invalid_packets_with_conntrack => true,
          rules             => {
            'allow_acceptance_tests' => {
              chain  => 'INPUT',
              action => 'ACCEPT',
              proto  => tcp,
              dport  => 22,
            },
          },
          ip_versions      => ['ip'], #only ipv4 available with CI
        }
      )

      it 'works with no error' do
        apply_manifest(pp2, catch_failures: true)
      end
      it 'works idempotently' do
        apply_manifest(pp2, catch_changes: true)
      end

      describe service('ferm') do
        it { is_expected.to be_running }
      end

      describe command('iptables-save') do
        its(:stdout) { is_expected.to match %r{INPUT.*state INVALID -j DROP} }
      end
    end
  end

  context 'with custom chain using ferm DSL as content' do
    advanced_manifest = %(
      $my_rules = @(EOT)
      chain OPENVPN_FORWORD_RULES {
        proto udp {
          interface tun0 {
            outerface enp4s0 {
              mod conntrack ctstate (NEW) saddr @ipfilter((10.8.0.0/24)) ACCEPT;
            }
          }
        }
      }
      | EOT

      ferm::chain{'OPENVPN_FORWORD_RULES':
        chain   => 'OPENVPN_FORWORD_RULES',
        content => $my_rules,
      }

      ferm::rule { "OpenVPN - FORWORD all udp traffic from network 10.8.0.0/24 to subchain OPENVPN_FORWORD_RULES":
        chain     => 'FORWARD',
        action    => 'OPENVPN_FORWORD_RULES',
        saddr     => '10.8.0.0/24',
        proto     => 'udp',
      }
    )

    pp = [basic_manifest, '}', advanced_manifest].join("\n")

    it 'works with no error' do
      apply_manifest(pp, catch_failures: true)
    end
    it 'works idempotently' do
      apply_manifest(pp, catch_changes: true)
    end

    describe iptables do
      it do
        is_expected.to have_rule(iptables_output_custom[0]). \
          with_table('filter'). \
          with_chain('FORWARD')
      end
      it do
        is_expected.to have_rule(iptables_output_custom[1]). \
          with_table('filter'). \
          with_chain('OPENVPN_FORWORD_RULES')
      end
    end

    describe service('ferm') do
      it { is_expected.to be_running }
    end

    describe command('iptables-save') do
      its(:stdout) { is_expected.to match %r{FORWARD.*-j OPENVPN_FORWORD_RULES} }
    end
  end
end