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
|
# frozen_string_literal: true
require 'spec_helper'
describe 'ferm::rule', type: :define do
on_supported_os.each do |os, facts|
context "on #{os}" do
let :facts do
facts
end
let :pre_condition do
'include ferm'
end
context 'without action' do
let(:title) { 'filter-ssh' }
let :params do
{
chain: 'INPUT',
proto: 'tcp',
dport: 22,
saddr: '127.0.0.1'
}
end
it { is_expected.not_to compile }
end
context 'without a specific interface' do
let(:title) { 'filter-ssh' }
let :params do
{
chain: 'INPUT',
action: 'ACCEPT',
proto: 'tcp',
dport: 22,
saddr: '127.0.0.1'
}
end
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_concat__fragment('INPUT-filter-ssh').with_content("mod comment comment 'filter-ssh' proto tcp dport 22 saddr @ipfilter((127.0.0.1)) ACCEPT;\n") }
it { is_expected.to contain_concat__fragment('filter-INPUT-config-include') }
it { is_expected.to contain_concat__fragment('filter-FORWARD-config-include') }
it { is_expected.to contain_concat__fragment('filter-OUTPUT-config-include') }
end
context 'with a specific interface' do
let(:title) { 'filter-ssh' }
let :params do
{
chain: 'INPUT',
action: 'ACCEPT',
proto: 'tcp',
dport: 22,
saddr: '127.0.0.1',
interface: 'eth0'
}
end
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_concat__fragment('INPUT-eth0-filter-ssh').with_content(" mod comment comment 'filter-ssh' proto tcp dport 22 saddr @ipfilter((127.0.0.1)) ACCEPT;\n") }
it { is_expected.to contain_concat__fragment('INPUT-eth0-aaa').with_content("interface eth0 {\n") }
it { is_expected.to contain_concat__fragment('INPUT-eth0-zzz').with_content("}\n") }
end
context 'with a specific interface using array for daddr' do
let(:title) { 'filter-ssh' }
let :params do
{
chain: 'INPUT',
action: 'ACCEPT',
proto: 'tcp',
dport: 22,
daddr: ['127.0.0.1', '123.123.123.123', ['10.0.0.1', '10.0.0.2']],
interface: 'eth0'
}
end
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_concat__fragment('INPUT-eth0-filter-ssh').with_content(" mod comment comment 'filter-ssh' proto tcp dport 22 daddr @ipfilter((127.0.0.1 123.123.123.123 10.0.0.1 10.0.0.2)) ACCEPT;\n") }
it { is_expected.to contain_concat__fragment('INPUT-eth0-aaa').with_content("interface eth0 {\n") }
it { is_expected.to contain_concat__fragment('INPUT-eth0-zzz').with_content("}\n") }
end
context 'without a specific interface using array for proto' do
let(:title) { 'filter-consul' }
let :params do
{
chain: 'INPUT',
action: 'ACCEPT',
proto: %w[tcp udp],
dport: [8301, 8302],
saddr: '127.0.0.1'
}
end
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_concat__fragment('INPUT-filter-consul').with_content("mod comment comment 'filter-consul' proto (tcp udp) mod multiport destination-ports (8301 8302) saddr @ipfilter((127.0.0.1)) ACCEPT;\n") }
it { is_expected.to contain_concat__fragment('filter-INPUT-config-include') }
it { is_expected.to contain_concat__fragment('filter-FORWARD-config-include') }
it { is_expected.to contain_concat__fragment('filter-OUTPUT-config-include') }
end
context 'with a valid destination-port range' do
let(:title) { 'filter-portrange' }
let :params do
{
chain: 'INPUT',
action: 'ACCEPT',
proto: 'tcp',
dport: '20000:25000',
saddr: '127.0.0.1'
}
end
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_concat__fragment('INPUT-filter-portrange').with_content("mod comment comment 'filter-portrange' proto tcp dport 20000:25000 saddr @ipfilter((127.0.0.1)) ACCEPT;\n") }
it { is_expected.to contain_concat__fragment('filter-INPUT-config-include') }
it { is_expected.to contain_concat__fragment('filter-FORWARD-config-include') }
it { is_expected.to contain_concat__fragment('filter-OUTPUT-config-include') }
end
context 'with a malformed source-port range' do
let(:title) { 'filter-malformed-portrange' }
let :params do
{
chain: 'INPUT',
action: 'ACCEPT',
proto: 'tcp',
sport: '25000:20000',
saddr: '127.0.0.1'
}
end
it { is_expected.to compile.and_raise_error(%r{Lower port number of the port range is larger than upper. 25000:20000}) }
end
context 'with an invalid destination-port range' do
let(:title) { 'filter-invalid-portrange' }
let :params do
{
chain: 'INPUT',
action: 'ACCEPT',
proto: 'tcp',
dport: '50000:65538',
saddr: '127.0.0.1'
}
end
it { is_expected.to compile.and_raise_error(%r{The data type should be 'Tuple\[Stdlib::Port, Stdlib::Port\]', not 'Tuple\[Integer\[50000, 50000\], Integer\[65538, 65538\]\]'. The data is \[50000, 65538\]}) }
end
context 'with an invalid destination-port string' do
let(:title) { 'filter-invalid-portnumber' }
let :params do
{
chain: 'INPUT',
action: 'ACCEPT',
proto: 'tcp',
dport: '65538',
saddr: '127.0.0.1'
}
end
it { is_expected.to compile.and_raise_error(%r{parameter 'dport' expects a Ferm::Port .* value, got String}) }
end
context 'with an invalid source-port number' do
let(:title) { 'filter-invalid-portnumber' }
let :params do
{
chain: 'INPUT',
action: 'ACCEPT',
proto: 'tcp',
sport: 65_538,
saddr: '127.0.0.1'
}
end
it { is_expected.to compile.and_raise_error(%r{parameter 'sport' expects a Ferm::Port .* value, got Integer}) }
end
context 'with jumping to custom chains' do
# create custom chain
let(:pre_condition) do
'include ferm ;
ferm::chain{"check-ssh":
chain => "SSH",
disable_conntrack => true,
log_dropped_packets => false,
}'
end
let(:title) { 'filter-ssh' }
let :params do
{
chain: 'INPUT',
action: 'SSH',
proto: 'tcp',
dport: 22
}
end
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_concat__fragment('filter-SSH-policy') }
it do
expect(subject).to contain_concat__fragment('INPUT-filter-ssh').\
with_content("mod comment comment 'filter-ssh' proto tcp dport 22 jump SSH;\n"). \
that_requires('Ferm::Chain[check-ssh]')
end
it { is_expected.to contain_concat__fragment('filter-INPUT-config-include') }
if facts[:os]['name'] == 'Debian'
it { is_expected.to contain_concat('/etc/ferm/ferm.d/chains/filter-SSH.conf') }
else
it { is_expected.to contain_concat('/etc/ferm.d/chains/filter-SSH.conf') }
end
end
context 'definining rules in custom chains' do
# create custom chain
let(:pre_condition) do
'include ferm ;
ferm::chain{"check-ssh":
chain => "SSH",
disable_conntrack => true,
log_dropped_packets => false,
}'
end
let(:title) { 'allow-ssh-localhost' }
let :params do
{
chain: 'SSH',
action: 'ACCEPT',
proto: 'tcp',
dport: 22,
saddr: '127.0.0.1'
}
end
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_concat__fragment('SSH-allow-ssh-localhost').with_content("mod comment comment 'allow-ssh-localhost' proto tcp dport 22 saddr @ipfilter((127.0.0.1)) ACCEPT;\n") }
it { is_expected.to contain_concat__fragment('filter-INPUT-config-include') }
it { is_expected.to contain_concat__fragment('filter-SSH-config-include') }
end
end
end
end
|