Projet

Général

Profil

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

root / manifests / simplerule.pp @ 5944b9cb

Historique | Voir | Annoter | Télécharger (2,85 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 counter
51
#   Enable traffic counters for the matched traffic.
52

    
53
define nftables::simplerule (
54
  Enum['present','absent'] $ensure = 'present',
55
  Pattern[/^[-a-zA-Z0-9_]+$/] $rulename = $title,
56
  Pattern[/^\d\d$/] $order = '50',
57
  String $chain  = 'default_in',
58
  String $table = 'inet-filter',
59
  Enum['accept', 'continue', 'drop', 'queue', 'return'] $action = 'accept',
60
  Optional[String] $comment = undef,
61
  Optional[Variant[Array[Stdlib::Port, 1], Stdlib::Port, Pattern[/\d+-\d+/]]] $dport = undef,
62
  Optional[Enum['tcp', 'tcp4', 'tcp6', 'udp', 'udp4', 'udp6']] $proto = undef,
63
  Optional[Variant[Stdlib::IP::Address::V6, Stdlib::IP::Address::V4, Pattern[/^@[-a-zA-Z0-9_]+$/]]] $daddr = undef,
64
  Enum['ip', 'ip6'] $set_type = 'ip6',
65
  Optional[Variant[Array[Stdlib::Port, 1], Stdlib::Port, Pattern[/\d+-\d+/]]] $sport = undef,
66
  Boolean $counter = false,
67
) {
68
  if $dport and !$proto {
69
    fail('Specifying a transport protocol via $proto is mandatory when passing a $dport')
70
  }
71

    
72
  if $sport and !$proto {
73
    fail('Specifying a transport protocol via $proto is mandatory when passing a $sport')
74
  }
75

    
76
  if $ensure == 'present' {
77
    nftables::rule { "${chain}-${rulename}":
78
      content => epp('nftables/simplerule.epp',
79
        {
80
          'action'   => $action,
81
          'comment'  => $comment,
82
          'counter'  => $counter,
83
          'dport'    => $dport,
84
          'proto'    => $proto,
85
          'daddr'    => $daddr,
86
          'set_type' => $set_type,
87
          'sport'    => $sport,
88
        }
89
      ),
90
      order   => $order,
91
      table   => $table,
92
    }
93
  }
94
}