root / manifests / simplerule.pp @ 09b07e56
Historique | Voir | Annoter | Télécharger (2,95 ko)
1 |
# @summary Provides a simplified interface to nftables::rule for basic use cases. |
---|---|
2 |
# It's recommended to use nftables::rule directly if you feel comfortable with |
3 |
# nft's syntax. |
4 |
# |
5 |
# @example allow incoming traffic from port 541 on port 543 TCP to a given IP range and count packets |
6 |
# nftables::simplerule{'my_service_in': |
7 |
# action => 'accept', |
8 |
# comment => 'allow traffic to port 543', |
9 |
# counter => true, |
10 |
# proto => 'tcp', |
11 |
# dport => 543, |
12 |
# daddr => '2001:1458::/32', |
13 |
# sport => 541, |
14 |
# } |
15 |
# |
16 |
# @param rulename |
17 |
# The symbolic name for the rule to add. Defaults to the resource's title. |
18 |
# |
19 |
# @param order |
20 |
# A number representing the order of the rule. |
21 |
# |
22 |
# @param chain |
23 |
# The name of the chain to add this rule to. |
24 |
# |
25 |
# @param table |
26 |
# The name of the table to add this rule to. |
27 |
# |
28 |
# @param action |
29 |
# The verdict for the matched traffic. |
30 |
# |
31 |
# @param comment |
32 |
# A typically human-readable comment for the rule. |
33 |
# |
34 |
# @param dport |
35 |
# The destination port, ports or port range. |
36 |
# |
37 |
# @param proto |
38 |
# The transport-layer protocol to match. |
39 |
# |
40 |
# @param daddr |
41 |
# The destination address, CIDR or set to match. |
42 |
# |
43 |
# @param set_type |
44 |
# When using sets as saddr or daddr, the type of the set. |
45 |
# Use `ip` for sets of type `ipv4_addr`. |
46 |
# |
47 |
# @param sport |
48 |
# The source port, ports or port range. |
49 |
# |
50 |
# @param saddr |
51 |
# The source address, CIDR or set to match. |
52 |
# |
53 |
# @param counter |
54 |
# Enable traffic counters for the matched traffic. |
55 |
|
56 |
define nftables::simplerule ( |
57 |
Enum['present','absent'] $ensure = 'present', |
58 |
Pattern[/^[-a-zA-Z0-9_]+$/] $rulename = $title, |
59 |
Pattern[/^\d\d$/] $order = '50', |
60 |
String $chain = 'default_in', |
61 |
String $table = 'inet-filter', |
62 |
Enum['accept', 'continue', 'drop', 'queue', 'return'] $action = 'accept', |
63 |
Optional[String] $comment = undef, |
64 |
Optional[Nftables::Port] $dport = undef, |
65 |
Optional[Enum['tcp', 'tcp4', 'tcp6', 'udp', 'udp4', 'udp6']] $proto = undef, |
66 |
Optional[Variant[Stdlib::IP::Address::V6, Stdlib::IP::Address::V4, Pattern[/^@[-a-zA-Z0-9_]+$/]]] $daddr = undef, |
67 |
Enum['ip', 'ip6'] $set_type = 'ip6', |
68 |
Optional[Nftables::Port] $sport = undef, |
69 |
Optional[Variant[Stdlib::IP::Address::V6, Stdlib::IP::Address::V4, Pattern[/^@[-a-zA-Z0-9_]+$/]]] $saddr = undef, |
70 |
Boolean $counter = false, |
71 |
) { |
72 |
if $dport and !$proto { |
73 |
fail('Specifying a transport protocol via $proto is mandatory when passing a $dport') |
74 |
} |
75 |
|
76 |
if $sport and !$proto { |
77 |
fail('Specifying a transport protocol via $proto is mandatory when passing a $sport') |
78 |
} |
79 |
|
80 |
if $ensure == 'present' { |
81 |
nftables::rule { "${chain}-${rulename}": |
82 |
content => epp('nftables/simplerule.epp', |
83 |
{ |
84 |
'action' => $action, |
85 |
'comment' => $comment, |
86 |
'counter' => $counter, |
87 |
'daddr' => $daddr, |
88 |
'dport' => $dport, |
89 |
'proto' => $proto, |
90 |
'saddr' => $saddr, |
91 |
'set_type' => $set_type, |
92 |
'sport' => $sport, |
93 |
} |
94 |
), |
95 |
order => $order, |
96 |
table => $table, |
97 |
} |
98 |
} |
99 |
} |