Projet

Général

Profil

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

root / manifests / simplerule.pp @ 77abc10b

Historique | Voir | Annoter | Télécharger (1,87 ko)

1 4ec94616 Nacho Barrientos
# @summary Provides a simplified interface to nftables::rule for basic use cases
2
#
3 77abc10b Nacho Barrientos
# @example allow incoming traffic from port 541 on port 543 TCP to a given IP range and count packets
4 4ec94616 Nacho Barrientos
#   nftables::simplerule{'my_service_in':
5
#     action  => 'accept',
6
#     comment => 'allow traffic to port 543',
7
#     counter => true,
8
#     proto   => 'tcp',
9
#     dport   => 543,
10
#     daddr   => '2001:1458::/32',
11 77abc10b Nacho Barrientos
#     sport   => 541,
12 4ec94616 Nacho Barrientos
#   }
13
14 467ea4e2 Nacho Barrientos
define nftables::simplerule (
15
  Enum['present','absent'] $ensure = 'present',
16
  Pattern[/^[-a-zA-Z0-9_]+$/] $rulename = $title,
17
  Pattern[/^\d\d$/] $order = '50',
18
  String $chain  = 'default_in',
19
  String $table = 'inet-filter',
20
  Enum['accept', 'drop'] $action = 'accept',
21
  Optional[String] $comment = undef,
22 fb58f7b3 Nacho Barrientos
  Optional[Variant[Array[Stdlib::Port, 1], Stdlib::Port, Pattern[/\d+-\d+/]]] $dport = undef,
23
  Optional[Enum['tcp', 'tcp4', 'tcp6', 'udp', 'udp4', 'udp6']] $proto = undef,
24 467ea4e2 Nacho Barrientos
  Optional[Variant[Stdlib::IP::Address::V6, Stdlib::IP::Address::V4, Pattern[/^@[-a-zA-Z0-9_]+$/]]] $daddr = undef,
25
  Enum['ip', 'ip6'] $set_type = 'ip6',
26 77abc10b Nacho Barrientos
  Optional[Variant[Array[Stdlib::Port, 1], Stdlib::Port, Pattern[/\d+-\d+/]]] $sport = undef,
27 467ea4e2 Nacho Barrientos
  Boolean $counter = false,
28
) {
29 3a52fb41 Nacho Barrientos
  if $dport and !$proto {
30 2489f932 Nacho Barrientos
    fail('Specifying a transport protocol via $proto is mandatory when passing a $dport')
31 3a52fb41 Nacho Barrientos
  }
32
33 77abc10b Nacho Barrientos
  if $sport and !$proto {
34
    fail('Specifying a transport protocol via $proto is mandatory when passing a $sport')
35
  }
36
37 83382bb5 Nacho Barrientos
  if $ensure == 'present' {
38 467ea4e2 Nacho Barrientos
    nftables::rule { "${chain}-${rulename}":
39 83382bb5 Nacho Barrientos
      content => epp('nftables/simplerule.epp',
40
        {
41 aaa37172 Nacho Barrientos
          'action'   => $action,
42
          'comment'  => $comment,
43 d43ced4d Nacho Barrientos
          'counter'  => $counter,
44 aaa37172 Nacho Barrientos
          'dport'    => $dport,
45
          'proto'    => $proto,
46
          'daddr'    => $daddr,
47
          'set_type' => $set_type,
48 77abc10b Nacho Barrientos
          'sport'    => $sport,
49 83382bb5 Nacho Barrientos
        }
50
      ),
51
      order   => $order,
52
      table   => $table,
53
    }
54
  }
55
}