Révision 77abc10b
Implement nftables::simplerule::sport
manifests/simplerule.pp | ||
---|---|---|
1 | 1 |
# @summary Provides a simplified interface to nftables::rule for basic use cases |
2 | 2 |
# |
3 |
# @example allow incoming traffic on port 543 TCP to a given IP range and count packets |
|
3 |
# @example allow incoming traffic from port 541 on port 543 TCP to a given IP range and count packets
|
|
4 | 4 |
# nftables::simplerule{'my_service_in': |
5 | 5 |
# action => 'accept', |
6 | 6 |
# comment => 'allow traffic to port 543', |
... | ... | |
8 | 8 |
# proto => 'tcp', |
9 | 9 |
# dport => 543, |
10 | 10 |
# daddr => '2001:1458::/32', |
11 |
# sport => 541, |
|
11 | 12 |
# } |
12 | 13 |
|
13 | 14 |
define nftables::simplerule ( |
... | ... | |
22 | 23 |
Optional[Enum['tcp', 'tcp4', 'tcp6', 'udp', 'udp4', 'udp6']] $proto = undef, |
23 | 24 |
Optional[Variant[Stdlib::IP::Address::V6, Stdlib::IP::Address::V4, Pattern[/^@[-a-zA-Z0-9_]+$/]]] $daddr = undef, |
24 | 25 |
Enum['ip', 'ip6'] $set_type = 'ip6', |
26 |
Optional[Variant[Array[Stdlib::Port, 1], Stdlib::Port, Pattern[/\d+-\d+/]]] $sport = undef, |
|
25 | 27 |
Boolean $counter = false, |
26 | 28 |
) { |
27 | 29 |
if $dport and !$proto { |
28 | 30 |
fail('Specifying a transport protocol via $proto is mandatory when passing a $dport') |
29 | 31 |
} |
30 | 32 |
|
33 |
if $sport and !$proto { |
|
34 |
fail('Specifying a transport protocol via $proto is mandatory when passing a $sport') |
|
35 |
} |
|
36 |
|
|
31 | 37 |
if $ensure == 'present' { |
32 | 38 |
nftables::rule { "${chain}-${rulename}": |
33 | 39 |
content => epp('nftables/simplerule.epp', |
... | ... | |
39 | 45 |
'proto' => $proto, |
40 | 46 |
'daddr' => $daddr, |
41 | 47 |
'set_type' => $set_type, |
48 |
'sport' => $sport, |
|
42 | 49 |
} |
43 | 50 |
), |
44 | 51 |
order => $order, |
spec/defines/simplerule_spec.rb | ||
---|---|---|
19 | 19 |
} |
20 | 20 |
end |
21 | 21 |
|
22 |
describe 'port without protocol' do |
|
22 |
describe 'dport without protocol' do
|
|
23 | 23 |
let(:params) do |
24 | 24 |
{ |
25 | 25 |
dport: 333, |
... | ... | |
29 | 29 |
it { is_expected.not_to compile } |
30 | 30 |
end |
31 | 31 |
|
32 |
describe 'sport without protocol' do |
|
33 |
let(:params) do |
|
34 |
{ |
|
35 |
sport: 333, |
|
36 |
} |
|
37 |
end |
|
38 |
|
|
39 |
it { is_expected.not_to compile } |
|
40 |
end |
|
41 |
|
|
32 | 42 |
describe 'all parameters provided' do |
33 | 43 |
let(:title) { 'my_big_rule' } |
34 | 44 |
let(:params) do |
... | ... | |
37 | 47 |
comment: 'this is my rule', |
38 | 48 |
counter: true, |
39 | 49 |
dport: 333, |
50 |
sport: 444, |
|
40 | 51 |
proto: 'udp', |
41 | 52 |
chain: 'default_out', |
42 | 53 |
daddr: '2001:1458::/32', |
... | ... | |
46 | 57 |
it { is_expected.to compile } |
47 | 58 |
it { |
48 | 59 |
is_expected.to contain_nftables__rule('default_out-my_big_rule').with( |
49 |
content: 'udp dport {333} ip6 daddr 2001:1458::/32 counter accept comment "this is my rule"', |
|
60 |
content: 'udp sport {444} udp dport {333} ip6 daddr 2001:1458::/32 counter accept comment "this is my rule"',
|
|
50 | 61 |
order: '50', |
51 | 62 |
) |
52 | 63 |
} |
... | ... | |
56 | 67 |
let(:params) do |
57 | 68 |
{ |
58 | 69 |
dport: '333-334', |
70 |
sport: '1-2', |
|
59 | 71 |
proto: 'tcp', |
60 | 72 |
} |
61 | 73 |
end |
... | ... | |
63 | 75 |
it { is_expected.to compile } |
64 | 76 |
it { |
65 | 77 |
is_expected.to contain_nftables__rule('default_in-my_default_rule_name').with( |
66 |
content: 'tcp dport {333-334} accept', |
|
78 |
content: 'tcp sport {1-2} tcp dport {333-334} accept',
|
|
67 | 79 |
) |
68 | 80 |
} |
69 | 81 |
end |
... | ... | |
72 | 84 |
let(:params) do |
73 | 85 |
{ |
74 | 86 |
dport: [333, 335], |
87 |
sport: [433, 435], |
|
88 |
proto: 'tcp', |
|
89 |
} |
|
90 |
end |
|
91 |
|
|
92 |
it { is_expected.to compile } |
|
93 |
it { |
|
94 |
is_expected.to contain_nftables__rule('default_in-my_default_rule_name').with( |
|
95 |
content: 'tcp sport {433, 435} tcp dport {333, 335} accept', |
|
96 |
) |
|
97 |
} |
|
98 |
end |
|
99 |
|
|
100 |
describe 'only sport TCP traffic' do |
|
101 |
let(:params) do |
|
102 |
{ |
|
103 |
sport: 555, |
|
75 | 104 |
proto: 'tcp', |
76 | 105 |
} |
77 | 106 |
end |
... | ... | |
79 | 108 |
it { is_expected.to compile } |
80 | 109 |
it { |
81 | 110 |
is_expected.to contain_nftables__rule('default_in-my_default_rule_name').with( |
82 |
content: 'tcp dport {333, 335} accept',
|
|
111 |
content: 'tcp sport {555} accept',
|
|
83 | 112 |
) |
84 | 113 |
} |
85 | 114 |
end |
templates/simplerule.epp | ||
---|---|---|
4 | 4 |
Optional[String] $proto, |
5 | 5 |
Optional[Variant[Stdlib::IP::Address::V6, Stdlib::IP::Address::V4, Pattern[/^@[-a-zA-Z0-9_]+$/]]] $daddr, |
6 | 6 |
Enum['ip', 'ip6'] $set_type, |
7 |
Optional[Variant[Array[Stdlib::Port, 1], Stdlib::Port, String]] $sport, |
|
7 | 8 |
Boolean $counter, |
8 | 9 |
| -%> |
9 | 10 |
<%- if $proto { |
... | ... | |
43 | 44 |
} else { |
44 | 45 |
$_comment = undef |
45 | 46 |
} -%> |
47 |
<%- if $proto and $sport { |
|
48 |
$_src_port = "${_proto} sport {${Array($sport, true).join(', ')}}" |
|
49 |
} else { |
|
50 |
$_src_port = undef |
|
51 |
} -%> |
|
46 | 52 |
<%- if $counter { |
47 | 53 |
$_counter = "counter" |
48 | 54 |
} else { |
49 | 55 |
$_counter = undef |
50 | 56 |
} -%> |
51 |
<%= regsubst(strip([$_ip_version_filter, $_dst_port, $_dst_hosts, $_counter, $action, $_comment].join(' ')), '\s+', ' ', 'G') -%> |
|
57 |
<%= regsubst(strip([$_ip_version_filter, $_src_port, $_dst_port, $_dst_hosts, $_counter, $action, $_comment].join(' ')), '\s+', ' ', 'G') -%> |
Formats disponibles : Unified diff