Projet

Général

Profil

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

root / manifests / simplerule.pp @ 6739966c

Historique | Voir | Annoter | Télécharger (3,05 ko)

1 af15de48 Nacho Barrientos
# @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 4ec94616 Nacho Barrientos
#
5 77abc10b Nacho Barrientos
# @example allow incoming traffic from port 541 on port 543 TCP to a given IP range and count packets
6 4ec94616 Nacho Barrientos
#   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 77abc10b Nacho Barrientos
#     sport   => 541,
14 4ec94616 Nacho Barrientos
#   }
15 2f28cced Nacho Barrientos
#
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 3a469f2b Nacho Barrientos
# @param saddr
51
#   The source address, CIDR or set to match.
52
#
53 2f28cced Nacho Barrientos
# @param counter
54
#   Enable traffic counters for the matched traffic.
55 4ec94616 Nacho Barrientos
56 467ea4e2 Nacho Barrientos
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 5944b9cb Nacho Barrientos
  Enum['accept', 'continue', 'drop', 'queue', 'return'] $action = 'accept',
63 467ea4e2 Nacho Barrientos
  Optional[String] $comment = undef,
64 fb58f7b3 Nacho Barrientos
  Optional[Variant[Array[Stdlib::Port, 1], Stdlib::Port, Pattern[/\d+-\d+/]]] $dport = undef,
65
  Optional[Enum['tcp', 'tcp4', 'tcp6', 'udp', 'udp4', 'udp6']] $proto = undef,
66 467ea4e2 Nacho Barrientos
  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 77abc10b Nacho Barrientos
  Optional[Variant[Array[Stdlib::Port, 1], Stdlib::Port, Pattern[/\d+-\d+/]]] $sport = undef,
69 3a469f2b Nacho Barrientos
  Optional[Variant[Stdlib::IP::Address::V6, Stdlib::IP::Address::V4, Pattern[/^@[-a-zA-Z0-9_]+$/]]] $saddr = undef,
70 467ea4e2 Nacho Barrientos
  Boolean $counter = false,
71
) {
72 3a52fb41 Nacho Barrientos
  if $dport and !$proto {
73 2489f932 Nacho Barrientos
    fail('Specifying a transport protocol via $proto is mandatory when passing a $dport')
74 3a52fb41 Nacho Barrientos
  }
75
76 77abc10b Nacho Barrientos
  if $sport and !$proto {
77
    fail('Specifying a transport protocol via $proto is mandatory when passing a $sport')
78
  }
79
80 83382bb5 Nacho Barrientos
  if $ensure == 'present' {
81 467ea4e2 Nacho Barrientos
    nftables::rule { "${chain}-${rulename}":
82 83382bb5 Nacho Barrientos
      content => epp('nftables/simplerule.epp',
83
        {
84 aaa37172 Nacho Barrientos
          'action'   => $action,
85
          'comment'  => $comment,
86 d43ced4d Nacho Barrientos
          'counter'  => $counter,
87 6739966c Nacho Barrientos
          'daddr'    => $daddr,
88 aaa37172 Nacho Barrientos
          'dport'    => $dport,
89
          'proto'    => $proto,
90 3a469f2b Nacho Barrientos
          'saddr'    => $saddr,
91 6739966c Nacho Barrientos
          'set_type' => $set_type,
92 77abc10b Nacho Barrientos
          'sport'    => $sport,
93 83382bb5 Nacho Barrientos
        }
94
      ),
95
      order   => $order,
96
      table   => $table,
97
    }
98
  }
99
}